Changeset 5828


Ignore:
Timestamp:
May 29, 2012 4:24:11 PM (7 years ago)
Author:
jdsiiro
Message:

Fixing an error in the new Param class where constructing a Param from
multiple implicit sets led to the internal sets never being constructed.

Location:
coopr.pyomo/trunk/coopr/pyomo
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/base/block.py

    r5803 r5828  
    410410        # NB: only "old" IndexedComponent contain _index_set.
    411411        # SparseIndexedComponent does not have that attribute
    412         if '_index_set' in dir(val) and val._index_set is not None:
     412        if val._index_set is not None:
    413413            ctr=0
    414414            for tset in val._index_set:
     
    419419                    )
    420420                ctr += 1
    421             val._index = self._construct_temporary_set(
    422               val._index_set,
    423               val.name+"_index"
    424             )
    425         if isinstance(val._index,_SetContainer) and \
    426             val._index.name == "_unknown_":
     421            if val._index is None:
     422                val._index = self._construct_temporary_set(
     423                    val._index_set,
     424                    val.name+"_index" )
     425        if isinstance(val._index,_SetContainer) and val._index.name == "_unknown_":
    427426            self._construct_temporary_set(val._index,val.name+"_index")
    428427        if getattr(val,'domain',None) is not None and val.domain.name == "_unknown_":
  • coopr.pyomo/trunk/coopr/pyomo/base/sparse_indexed_component.py

    r5804 r5828  
    6464            # If no indexing sets are provided, generate a dummy index
    6565            #
     66            self._index_set = None
    6667            self._index = UnindexedComponent_set
    6768            self._data = {None: weakref.ref(self)}
    68         else:
     69        elif len(args) == 1:
     70            self._index_set = None
    6971            self._index = process_setarg(args[0])
    70             if len(args) > 1:
    71                 self._index = self._index.cross(*args[1:])
     72        else:
     73            self._index_set = [process_setarg(x) for x in args]
     74            self._index = self._index_set[0].cross(*(self._index_set[1:]))
    7275        #
    7376        # Setup indices for suffix data
  • coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_param.py

    r5803 r5828  
    331331
    332332
    333 class ArrayParam1(ParamTester, unittest.TestCase):
     333class ArrayParam_sparse_noDefault(ParamTester, unittest.TestCase):
    334334
    335335    def setUp(self, **kwds):
     
    343343
    344344
    345 class ArrayParam2(ParamTester, unittest.TestCase):
     345class ArrayParam_sparse_intDefault(ParamTester, unittest.TestCase):
    346346
    347347    def setUp(self, **kwds):
     
    355355
    356356
    357 class ArrayParam3(ParamTester, unittest.TestCase):
     357class ArrayParam_sparse_floatDefault(ParamTester, unittest.TestCase):
    358358
    359359    def setUp(self, **kwds):
     
    367367
    368368
    369 class ArrayParam4(ParamTester, unittest.TestCase):
     369class ArrayParam_dense_intDefault_scalarInit(ParamTester, unittest.TestCase):
    370370
    371371    def setUp(self, **kwds):
     
    379379
    380380
    381 class ArrayParam5(ParamTester, unittest.TestCase):
     381class ArrayParam_dense_intDefault_dictInit(ParamTester, unittest.TestCase):
    382382
    383383    def setUp(self, **kwds):
     
    393393
    394394
    395 class ArrayParam6(object):
     395class ArrayParam6(unittest.TestCase):
    396396
    397397    def setUp(self, **kwds):
     
    412412                return 2+i
    413413            return -(2+i)
    414         self.model.B = Param(B_index, [True,False], initialize=B_init, repn=self.repn)
     414        self.model.B = Param( B_index, [True,False],
     415                              initialize=B_init, repn=self.repn )
    415416        self.instance = self.model.create()
    416417        #self.instance.pprint()
    417         self.assertEqual(set(self.instance.B.keys()),set([(0,True),(2,True),(0,   False),(2,False)]))
     418        self.assertEqual(set(self.instance.B.keys()),
     419                         set([(0,True),(2,True),(0,   False),(2,False)]))
    418420        self.assertEqual(self.instance.B[0,True],2)
    419421        self.assertEqual(self.instance.B[0,False],-2)
     
    476478        model.C = Set(dimen=1, initialize=[9,8,7,6,5])
    477479        model.x = Param(model.A, model.B, model.C, initialize=-1, repn=self.repn)
    478         model.y = Param(model.B, initialize=(1,1), repn=self.repn)
     480        #model.y = Param(model.B, initialize=(1,1), repn=self.repn)
     481        model.y = Param(model.B, initialize=((1,1,7),2), repn=self.repn)
    479482        instance=model.create()
    480483        self.assertEqual( instance.components.x.dim(), 6)
     
    817820        self.assertRaises(ValueError, instance.a)
    818821
     822    def test_indexOverRange_abstract(self):
     823        model = AbstractModel()
     824        model.p = Param(range(1,3), range(2), initialize=1.0)
     825        inst = model.create()
     826        self.assertEqual( sorted(inst.p.keys()),
     827                          [(1,0), (1,1), (2,0), (2,1)] )
     828        self.assertEqual( inst.p[1,0], 1.0 )
     829        self.assertRaises( KeyError, inst.p.__getitem__, (0, 0) )
     830
     831    def test_indexOverRange_concrete(self):
     832        inst = ConcreteModel()
     833        inst.p = Param(range(1,3), range(2), initialize=1.0)
     834        self.assertEqual( sorted(inst.p.keys()),
     835                          [(1,0), (1,1), (2,0), (2,1)] )
     836        self.assertEqual( inst.p[1,0], 1.0 )
     837        self.assertRaises( KeyError, inst.p.__getitem__, (0, 0) )
     838
     839
    819840    def test_get_set(self):
    820841        model=AbstractModel()
Note: See TracChangeset for help on using the changeset viewer.