Changeset 1994
 Timestamp:
 Dec 13, 2009 1:01:57 PM (10 years ago)
 Location:
 coopr.pyomo/trunk/coopr/pyomo
 Files:

 5 added
 5 edited
Legend:
 Unmodified
 Added
 Removed

coopr.pyomo/trunk/coopr/pyomo/base/PyomoModel.py
r1992 r1994 30 30 from rangeset import RangeSet 31 31 from var import Var 32 from constraint import Objective, Constraint 32 from constraint import Objective, Constraint, ConstraintData 33 33 from param import Param 34 34 … … 55 55 # 56 56 self._declarations=[] 57 # the _name_varmap is assigned when the problem definition 57 # the _name_varmap and _name_conmap dictionarys are assigned when 58 # the problem definition 58 59 # file is written, i.e., via the write() command. intent 59 60 # is to map names that will be coming back from a solver 60 61 # into Pyomo variables. Values are of type _VarValue. 61 62 self._name_varmap={} 63 self._name_conmap={} 62 64 self.name="unknown" 63 65 self.tmpctr=0 64 66 self._varctr=0 65 #self._varmap={}66 67 self._presolvers = ["simple_presolver"] 67 68 # … … 80 81 raise KeyError, "Unknown component type: %s" % str(ctype) 81 82 82 83 83 84 84 def active_components(self, _ctype=None): … … 98 98 if not _ctype is None: 99 99 return tmp[_ctype] 100 print "ACTIVE",tmp 100 101 return tmp 101 102 … … 312 313 Load a solution. 313 314 """ 315 # 316 # Load variable data 317 # 314 318 for name in soln.variable: 315 319 # … … 331 335 raise TypeError, "Variable '"+name+"' in model '"+self.name+"' is currently fixed  new value is not expected in solution" 332 336 333 # the value is always present in any legal solution. 334 self._name_varmap[name].value = soln.variable[name].value 335 336 # the reducedcost might be present, depending on what the solver is reporting. 337 if hasattr(soln.variable[name],"rc"): 338 self._name_varmap[name].rc = soln.variable[name].rc 337 #print "HERE",soln.variable[name].keys() 338 for _key in soln.variable[name].keys(): 339 key = _key[0].lower() + _key[1:] 340 if key == 'value': 341 self._name_varmap[name].value = soln.variable[name].value 342 elif not key == 'id': 343 setattr(self._name_varmap[name], key, getattr(soln.variable[name], _key)) 344 # 345 # Load constraint data 346 # 347 for name in soln.constraint: 348 # 349 # This is a hack 350 # 351 if name.endswith('ONE_VAR_CONSTANT'): 352 continue 353 if symbol_map is not None: 354 name = symbol_map[name] 355 # 356 # This is a hack. Is there a standard convention for constraint 357 # names that we can apply to all data formats? 358 # 359 if name[0:2] == 'c_': 360 _name = name[4:1] 361 #print "HERE",_name 362 if _name not in self._name_conmap: 363 names="" 364 raise KeyError, "Constraint name '"+name+"' is not in model "+self.name+". Valid constraint names: "+str(self._name_conmap.keys()) 365 if not isinstance(self._name_conmap[_name],ConstraintData): 366 raise TypeError, "Constraint '"+name+"' in model '"+self.name+"' is type "+str(type(self._name_conmap[_name])) 367 368 #print "HERE",soln.constraint[name].keys() 369 for _key in soln.constraint[name].keys(): 370 key = _key[0].lower() + _key[1:] 371 if key == 'value': 372 self._name_conmap[name].value = soln.constraint[name].value 373 elif not key == 'id': 374 setattr(self._name_conmap[_name], key, getattr(soln.constraint[name], _key)) 339 375 340 376 def write(self,filename=None,format=ProblemFormat.cpxlp): … … 399 435 400 436 401 def display(self, filename=None, ostream=None):437 def display(self, filename=None, ostream=None): 402 438 """ 403 439 Print the Pyomo model in a verbose format. … … 413 449 print >>ostream, "" 414 450 print >>ostream, " Variables:" 415 if len(self._component[Var]) == 0: 416 print >>ostream, " None" 451 VAR = self.active_components(Var) 452 if len(VAR) == 0: 453 print >>ostream, " None" 417 454 else: 418 for ndx in self._component[Var]:419 self._component[Var][ndx].display(prefix=" ",ostream=ostream)455 for ndx in VAR: 456 VAR[ndx].display(prefix=" ",ostream=ostream) 420 457 print >>ostream, "" 421 458 print >>ostream, " Objectives:" 422 if len(self._component[Objective]) == 0: 423 print >>ostream, " None" 459 OBJ = self.active_components(Objective) 460 if len(OBJ) == 0: 461 print >>ostream, " None" 424 462 else: 425 for ndx in self._component[Objective]:426 self._component[Objective][ndx].display(prefix=" ",ostream=ostream)463 for ndx in OBJ: 464 OBJ[ndx].display(prefix=" ",ostream=ostream) 427 465 print >>ostream, "" 466 CON = self.active_components(Constraint) 428 467 print >>ostream, " Constraints:" 429 if len( self._component[Constraint]) == 0:430 print >>ostream, " None"468 if len(CON) == 0: 469 print >>ostream, " None" 431 470 else: 432 for ndx in self._component[Constraint]:433 self._component[Constraint][ndx].display(prefix=" ",ostream=ostream)434 471 for ndx in CON: 472 CON[ndx].display(prefix=" ",ostream=ostream) 473 
coopr.pyomo/trunk/coopr/pyomo/base/component.py
r1992 r1994 7 7 8 8 def activate(self): 9 self. _active=True9 self.active=True 10 10 11 11 def deactivate(self): 12 self. _active=False12 self.active=False 13 13 14 14 def type(self): 
coopr.pyomo/trunk/coopr/pyomo/base/constraint.py
r1993 r1994 178 178 else: 179 179 for key in self._data: 180 if not self._data[key].active: 181 continue 180 182 val = self._data[key].expr(exception=False) 181 183 print >>ostream, prefix+" "+str(key)+" : "+str(val) … … 548 550 print >>ostream, prefix+" Value="+pyutilib.misc.format_io(self._data[None].body()) 549 551 else: 550 print >>ostream, prefix+" \tLower\tBody\t\tUpper"552 flag=True 551 553 for key in self._data: 554 if not self._data[key].active: 555 continue 556 if flag: 557 print >>ostream, prefix+" \tLower\tBody\t\tUpper" 558 flag=False 552 559 if self._data[key].lower is not None: 553 560 lval = str(self._data[key].lower()) … … 560 567 uval = "Infinity" 561 568 print >>ostream, prefix+" "+str(key)+" :\t"+lval+"\t"+val+"\t"+uval 569 if flag: 570 print >>ostream, prefix+" None active" 562 571 563 572 
coopr.pyomo/trunk/coopr/pyomo/presolve/generic_varlabels.py
r1992 r1994 9 9 # _________________________________________________________________________ 10 10 11 from coopr.pyomo.base import IPyomoPresolver, IPyomoPresolveAction, Var 11 from coopr.pyomo.base import IPyomoPresolver, IPyomoPresolveAction, Var, Constraint 12 12 import pyutilib.plugin.core 13 13 … … 60 60 var._varval[V].label = self._name_fix( var._varval[V].name ) 61 61 model._name_varmap[ var._varval[V].label ] = var._varval[V] 62 for id in model._con: 63 model._con[id].label = self._name_fix( model._con[id].name ) 62 Cons = model.active_components(Constraint) 63 for con in Cons.values(): 64 for C in con._data: 65 con._data[C].label = self._name_fix( con._data[C].name ) 66 model._name_conmap[ con._data[C].label ] = con._data[C] 64 67 return model 65 68 
coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_model.py
r1992 r1994 2 2 # Unit Tests for Elements of a Model 3 3 # 4 # TestSimpleVar Class for testing single variables 5 # TestArrayVar Class for testing array of variables 6 # TestSimpleObj Class for testing single objective 7 # TestArrayObj Class for testing array of objective 8 # TestSimpleCon Class for testing single constraint 9 # TestArrayCon Class for testing array of constraint 10 # TestModel Class to test the Model class 4 # Test Class to test the Model class 11 5 # 12 6 … … 34 28 35 29 36 class Test SimpleVar(PyomoModel):30 class Test(PyomoModel): 37 31 38 32 def setUp(self): … … 43 37 44 38 def tearDown(self): 45 pass46 47 def test_fixed_attr(self):48 """Test fixed attribute"""49 self.model.x = Var()50 self.instance = self.model.create()51 self.instance.x.fixed = True52 self.failUnlessEqual(self.instance.x.fixed, True)53 54 def test_value_attr(self):55 """Test value attribute"""56 self.model.x = Var()57 self.instance = self.model.create()58 self.instance.x.value = 3.559 self.failUnlessEqual(self.instance.x.value, 3.5)60 61 def test_initial_attr(self):62 """Test initial attribute"""63 self.model.x = Var()64 self.instance = self.model.create()65 self.instance.x.initial = 3.566 self.failUnlessEqual(self.instance.x.initial, 3.5)67 68 def test_domain_attr(self):69 """Test domain attribute"""70 self.model.x = Var()71 self.instance = self.model.create()72 self.instance.x.domain = Integers73 self.failUnlessEqual(type(self.instance.x.domain), IntegerSet)74 75 def test_name_attr(self):76 """Test name attribute"""77 #78 # A user would never need to do this, but this79 # attribute is needed within Pyomo80 #81 self.model.x = Var()82 self.model.x.name = "foo"83 self.failUnlessEqual(self.model.x.name, "foo")84 85 def test_lb_attr1(self):86 """Test lb attribute"""87 self.model.x = Var()88 self.instance = self.model.create()89 self.instance.x.setlb(1.0)90 self.failUnlessEqual(self.instance.x.lb(), 1.0)91 92 def test_lb_attr2(self):93 """Test lb attribute"""94 self.model.x = Var(within=NonNegativeReals, bounds=(1,2))95 self.instance = self.model.create()96 self.failUnlessEqual(self.instance.x.lb(), 0.0)97 self.failUnlessEqual(self.instance.x.ub(), 2.0)98 99 def test_ub_attr1(self):100 """Test ub attribute"""101 self.model.x = Var()102 self.instance = self.model.create()103 self.instance.x.setub(1.0)104 self.failUnlessEqual(self.instance.x.ub(), 1.0)105 106 def test_ub_attr2(self):107 """Test ub attribute"""108 self.model.x = Var(within=NonPositiveReals, bounds=(2,1))109 self.instance = self.model.create()110 self.failUnlessEqual(self.instance.x.lb(), 2.0)111 self.failUnlessEqual(self.instance.x.ub(), 0.0)112 113 def test_within_option(self):114 """Test within option"""115 self.model.x = Var(within=Integers)116 self.failUnlessEqual(type(self.model.x.domain), IntegerSet)117 118 def test_initialize_option(self):119 """Test initialize option"""120 self.model.x = Var(initialize=1.3)121 self.instance = self.model.create()122 self.failUnlessEqual(self.instance.x.initial, 1.3)123 124 def test_bounds_option1(self):125 """Test bounds option"""126 def x_bounds(model):127 return (1.0,1.0)128 self.model.x = Var(bounds=x_bounds)129 self.instance = self.model.create()130 self.failUnlessEqual(self.instance.x.lb(), 1.0)131 self.failUnlessEqual(self.instance.x.ub(), 1.0)132 133 def test_bounds_option2(self):134 """Test bounds option"""135 self.model.x = Var(bounds=(1.0,1.0))136 self.instance = self.model.create()137 self.failUnlessEqual(self.instance.x.lb(), 1.0)138 self.failUnlessEqual(self.instance.x.ub(), 1.0)139 140 def test_rule_option(self):141 """Test rule option"""142 def x_init(model):143 return 1.3144 self.model.x = Var(initialize=x_init)145 self.instance = self.model.create()146 self.failUnlessEqual(self.instance.x.value, None)147 self.failUnlessEqual(self.instance.x.initial, 1.3)148 149 def test_reset(self):150 """Test reset method"""151 self.model.x = Var(initialize=3)152 self.instance = self.model.create()153 self.failUnlessEqual(self.instance.x.initial,3)154 self.failUnlessEqual(self.instance.x.value,None)155 self.instance.x.reset()156 self.failUnlessEqual(self.instance.x.initial,3)157 self.failUnlessEqual(self.instance.x.initial,3)158 159 def test_dim(self):160 """Test dim method"""161 self.model.x = Var()162 self.instance = self.model.create()163 self.failUnlessEqual(self.instance.x.dim(),0)164 165 def test_keys(self):166 """Test keys method"""167 self.model.x = Var()168 self.instance = self.model.create()169 self.failUnlessEqual(self.instance.x.keys(),[None])170 171 def test_len(self):172 """Test len method"""173 self.model.x = Var()174 self.instance = self.model.create()175 self.failUnlessEqual(len(self.instance.x),1)176 177 def test_simplify(self):178 """Test simplify method"""179 self.model.x = Var()180 self.failUnlessEqual(type(self.model.x.simplify(self.model)),_VarElement)181 182 def test_value(self):183 """Check the value of the variable"""184 self.model.x = Var(initialize=3.3)185 self.instance = self.model.create()186 tmp = value(self.instance.x.initial)187 self.failUnlessEqual( type(tmp), float)188 self.failUnlessEqual( tmp, 3.3 )189 tmp = float(self.instance.x.initial)190 self.failUnlessEqual( type(tmp), float)191 self.failUnlessEqual( tmp, 3.3 )192 tmp = int(self.instance.x.initial)193 self.failUnlessEqual( type(tmp), int)194 self.failUnlessEqual( tmp, 3 )195 196 197 class TestArrayVar(TestSimpleVar):198 199 def setUp(self):200 #201 # Create Model202 #203 PyomoModel.setUp(self)204 self.model.A = Set(initialize=[1,2])205 206 def test_fixed_attr(self):207 """Test fixed attribute"""208 self.model.x = Var(self.model.A)209 self.model.y = Var(self.model.A)210 self.instance = self.model.create()211 self.instance.x.fixed = True212 #try:213 #self.instance.x.fixed214 #except AttributeError:215 #pass216 #else:217 #self.fail("test_fixed_attr")218 self.failUnlessEqual(self.instance.x[1].fixed, False)219 self.instance.y[1].fixed=True220 self.failUnlessEqual(self.instance.y[1].fixed, True)221 222 def test_value_attr(self):223 """Test value attribute"""224 self.model.x = Var(self.model.A)225 self.model.y = Var(self.model.A)226 self.instance = self.model.create()227 try:228 self.instance.x = 3.5229 self.fail("Expected ValueError")230 except ValueError:231 pass232 self.instance.y[1] = 3.5233 self.failUnlessEqual(self.instance.y[1], 3.5)234 235 #def test_initial_attr(self):236 #"""Test initial attribute"""237 #self.model.x = Var(self.model.A)238 #self.instance = self.model.create()239 #self.instance.x.initial = 3.5240 #self.failUnlessEqual(self.instance.x[1].initial, 3.5)241 242 #def test_lb_attr(self):243 #"""Test lb attribute"""244 #self.model.x = Var(self.model.A)245 #self.instance = self.model.create()246 #self.instance.x.setlb(1.0)247 #self.failUnlessEqual(self.instance.x[1].lb(), 1.0)248 249 #def test_ub_attr(self):250 #"""Test ub attribute"""251 #self.model.x = Var(self.model.A)252 #self.instance = self.model.create()253 #self.instance.x.setub(1.0)254 #self.failUnlessEqual(self.instance.x[1].ub(), 1.0)255 256 def test_initialize_option(self):257 """Test initialize option"""258 self.model.x = Var(self.model.A,initialize={1:1.3,2:2.3})259 self.instance = self.model.create()260 self.failUnlessEqual(self.instance.x[1], 1.3)261 self.failUnlessEqual(self.instance.x[2], 2.3)262 263 def test_bounds_option1(self):264 """Test bounds option"""265 def x_bounds(i,model):266 return (1.0,1.0)267 self.model.x = Var(self.model.A, bounds=x_bounds)268 self.instance = self.model.create()269 self.failUnlessEqual(self.instance.x[1].lb(), 1.0)270 self.failUnlessEqual(self.instance.x[1].ub(), 1.0)271 272 def test_bounds_option2(self):273 """Test bounds option"""274 self.model.x = Var(self.model.A, bounds=(1.0,1.0))275 self.instance = self.model.create()276 self.failUnlessEqual(self.instance.x[1].lb(), 1.0)277 self.failUnlessEqual(self.instance.x[1].ub(), 1.0)278 279 def test_rule_option(self):280 """Test rule option"""281 def x_init(i,model):282 return 1.3283 self.model.x = Var(self.model.A, initialize=x_init)284 self.instance = self.model.create()285 self.failUnlessEqual(self.instance.x[1].initial, 1.3)286 287 def test_reset(self):288 """Test reset method"""289 self.model.x = Var(self.model.A,initialize=3)290 self.instance = self.model.create()291 self.failUnlessEqual(self.instance.x[1].initial,3)292 self.failUnlessEqual(self.instance.x[1].value,None)293 self.instance.x.reset()294 self.failUnlessEqual(self.instance.x[1].initial,3)295 self.failUnlessEqual(self.instance.x[1].initial,3)296 297 def test_dim(self):298 """Test dim method"""299 self.model.x = Var(self.model.A)300 self.instance = self.model.create()301 self.failUnlessEqual(self.instance.x.dim(),1)302 303 def test_keys(self):304 """Test keys method"""305 self.model.x = Var(self.model.A)306 self.instance = self.model.create()307 self.failUnlessEqual(self.instance.x.keys(),[1,2])308 309 def test_len(self):310 """Test len method"""311 self.model.x = Var(self.model.A)312 self.instance = self.model.create()313 self.failUnlessEqual(len(self.instance.x),2)314 315 def test_value(self):316 """Check the value of the variable"""317 self.model.x = Var(self.model.A,initialize=3.3)318 self.instance = self.model.create()319 tmp = value(self.instance.x[1].initial)320 self.failUnlessEqual( type(tmp), float)321 self.failUnlessEqual( tmp, 3.3 )322 tmp = float(self.instance.x[1].initial)323 self.failUnlessEqual( type(tmp), float)324 self.failUnlessEqual( tmp, 3.3 )325 tmp = int(self.instance.x[1].initial)326 self.failUnlessEqual( type(tmp), int)327 self.failUnlessEqual( tmp, 3 )328 329 330 class Test2DArrayVar(TestSimpleVar):331 332 def setUp(self):333 #334 # Create Model335 #336 PyomoModel.setUp(self)337 self.model.A = Set(initialize=[1,2])338 339 def test_fixed_attr(self):340 """Test fixed attribute"""341 self.model.x = Var(self.model.A,self.model.A)342 self.model.y = Var(self.model.A,self.model.A)343 self.instance = self.model.create()344 self.instance.x.fixed = True345 #try:346 #self.instance.x.fixed347 #except AttributeError:348 #pass349 #else:350 #self.fail("test_fixed_attr")351 self.failUnlessEqual(self.instance.x[1,2].fixed, False)352 self.instance.y[1,2].fixed=True353 self.failUnlessEqual(self.instance.y[1,2].fixed, True)354 355 def test_value_attr(self):356 """Test value attribute"""357 self.model.x = Var(self.model.A,self.model.A)358 self.model.y = Var(self.model.A,self.model.A)359 self.instance = self.model.create()360 try:361 self.instance.x = 3.5362 self.fail("Expected ValueError")363 except ValueError:364 pass365 self.instance.y[1,2] = 3.5366 self.failUnlessEqual(self.instance.y[1,2], 3.5)367 368 #def test_initial_attr(self):369 #"""Test initial attribute"""370 #self.model.x = Var(self.model.A,self.model.A)371 #self.instance = self.model.create()372 #self.instance.x.initial = 3.5373 #self.failUnlessEqual(self.instance.x[1,1].initial, 3.5)374 375 #def test_lb_attr(self):376 #"""Test lb attribute"""377 #self.model.x = Var(self.model.A,self.model.A)378 #self.instance = self.model.create()379 #self.instance.x.setlb(1.0)380 #self.failUnlessEqual(self.instance.x[2,1].lb(), 1.0)381 382 #def test_ub_attr(self):383 #"""Test ub attribute"""384 #self.model.x = Var(self.model.A,self.model.A)385 #self.instance = self.model.create()386 #self.instance.x.setub(1.0)387 #self.failUnlessEqual(self.instance.x[2,1].ub(), 1.0)388 389 def test_initialize_option(self):390 """Test initialize option"""391 self.model.x = Var(self.model.A,self.model.A,initialize={(1,1):1.3,(2,2):2.3})392 self.instance = self.model.create()393 self.failUnlessEqual(self.instance.x[1,1], 1.3)394 self.failUnlessEqual(self.instance.x[2,2], 2.3)395 try:396 value(self.instance.x[1,2])397 self.fail("Expected KeyError")398 except KeyError:399 pass400 401 def test_bounds_option1(self):402 """Test bounds option"""403 def x_bounds(i,j,model):404 return (1.0*(i+j),1.0*(i+j))405 self.model.x = Var(self.model.A, self.model.A, bounds=x_bounds)406 self.instance = self.model.create()407 self.failUnlessEqual(self.instance.x[1,1].lb(), 2.0)408 self.failUnlessEqual(self.instance.x[1,2].ub(), 3.0)409 410 def test_bounds_option2(self):411 """Test bounds option"""412 self.model.x = Var(self.model.A, self.model.A, bounds=(1.0,1.0))413 self.instance = self.model.create()414 self.failUnlessEqual(self.instance.x[1,1].lb(), 1.0)415 self.failUnlessEqual(self.instance.x[1,1].ub(), 1.0)416 417 def test_rule_option(self):418 """Test rule option"""419 def x_init(i,j,model):420 return 1.3421 self.model.x = Var(self.model.A, self.model.A, initialize=x_init)422 self.instance = self.model.create()423 self.failUnlessEqual(self.instance.x[1,2].initial, 1.3)424 425 def test_reset(self):426 """Test reset method"""427 self.model.x = Var(self.model.A,self.model.A, initialize=3)428 self.instance = self.model.create()429 self.failUnlessEqual(self.instance.x[1,1].initial,3)430 self.failUnlessEqual(self.instance.x[1,1].value,None)431 self.instance.x.reset()432 self.failUnlessEqual(self.instance.x[1,1].initial,3)433 self.failUnlessEqual(self.instance.x[1,1].initial,3)434 435 def test_dim(self):436 """Test dim method"""437 self.model.x = Var(self.model.A,self.model.A)438 self.instance = self.model.create()439 self.failUnlessEqual(self.instance.x.dim(),2)440 441 def test_keys(self):442 """Test keys method"""443 self.model.x = Var(self.model.A,self.model.A)444 self.instance = self.model.create()445 ans = [(1,1),(1,2),(2,1),(2,2)]446 self.failUnlessEqual(self.instance.x.keys().sort(),ans.sort())447 448 def test_len(self):449 """Test len method"""450 self.model.x = Var(self.model.A,self.model.A)451 self.instance = self.model.create()452 self.failUnlessEqual(len(self.instance.x),4)453 454 def test_value(self):455 """Check the value of the variable"""456 self.model.x = Var(self.model.A,self.model.A,initialize=3.3)457 self.instance = self.model.create()458 tmp = value(self.instance.x[1,1].initial)459 self.failUnlessEqual( type(tmp), float)460 self.failUnlessEqual( tmp, 3.3 )461 tmp = float(self.instance.x[1,1].initial)462 self.failUnlessEqual( type(tmp), float)463 self.failUnlessEqual( tmp, 3.3 )464 tmp = int(self.instance.x[1,1].initial)465 self.failUnlessEqual( type(tmp), int)466 self.failUnlessEqual( tmp, 3 )467 468 469 class TestVarComplexArray(PyomoModel):470 471 def test_index1(self):472 self.model.A = Set(initialize=range(0,4))473 def B_index(model):474 for i in model.A:475 if i%2 == 0:476 yield i477 def B_init(i,j,model):478 if j:479 return 2+i480 return (2+i)481 self.model.B = Var(B_index, [True,False], initialize=B_init)482 self.instance = self.model.create()483 #self.instance.pprint()484 self.failUnlessEqual(set(self.instance.B.keys()),set([(0,True),(2,True),(0, False),(2,False)]))485 self.failUnlessEqual(self.instance.B[0,True],2)486 self.failUnlessEqual(self.instance.B[0,False],2)487 self.failUnlessEqual(self.instance.B[2,True],4)488 self.failUnlessEqual(self.instance.B[2,False],4)489 490 491 class TestSimpleObj(PyomoModel):492 493 def setUp(self):494 #495 # Create Model496 #497 PyomoModel.setUp(self)498 499 def tearDown(self):500 pass501 502 def test_rule_option(self):503 """Test rule option"""504 def f(model):505 ans=0506 for i in model.x.keys():507 ans = ans + model.x[i]508 return ans509 self.model.x = Var(RangeSet(1,4),initialize=2)510 self.model.obj = Objective(rule=f)511 self.instance = self.model.create()512 try:513 self.failUnlessEqual(self.instance.obj(), 8)514 except ValueError:515 pass516 else:517 self.fail("test_rule_option")518 self.instance.x.reset()519 self.failUnlessEqual(self.instance.obj(), 8)520 self.failUnlessEqual(value(self.instance.obj), 8)521 522 def test_sense_option(self):523 """Test sense option"""524 def rule(model):525 return 1.0526 self.model.obj = Objective(sense=maximize, rule=rule)527 self.instance = self.model.create()528 self.failUnlessEqual(self.instance.obj.sense, maximize)529 530 def test_dim(self):531 """Test dim method"""532 def rule(model):533 return 1534 self.model.obj = Objective(rule=rule)535 self.instance = self.model.create()536 self.failUnlessEqual(self.instance.obj.dim(),0)537 538 def test_keys(self):539 """Test keys method"""540 def rule(model):541 return 1542 self.model.obj = Objective(rule=rule)543 self.instance = self.model.create()544 self.failUnlessEqual(self.instance.obj.keys(),[None])545 546 def test_len(self):547 """Test len method"""548 def rule(model):549 return 1.0550 self.model.obj = Objective(rule=rule)551 self.instance = self.model.create()552 self.failUnlessEqual(len(self.instance.obj),1)553 """Test rule option"""554 def f(model):555 ans=0556 for i in model.x.keys():557 ans = ans + model.x[i]558 return ans559 self.model.x = Var(RangeSet(1,4),initialize=2)560 self.model.obj = Objective(rule=f)561 self.instance = self.model.create()562 self.failUnlessEqual(len(self.instance.obj),1)563 564 565 class TestArrayObj(PyomoModel):566 567 def setUp(self):568 #569 # Create Model570 #571 PyomoModel.setUp(self)572 self.model.A = Set(initialize=[1,2])573 574 def tearDown(self):575 pass576 577 def test_rule_option(self):578 """Test rule option"""579 def f(i,model):580 ans=0581 for j in model.x.keys():582 ans = ans + model.x[j]583 ans *= i584 return ans585 self.model.x = Var(RangeSet(1,4),initialize=2)586 self.model.obj = Objective(self.model.A,rule=f)587 self.instance = self.model.create()588 try:589 self.instance.obj[1]()590 self.fail("Excepted ValueError due to uninitialized variables")591 except ValueError:592 pass593 self.instance.x.reset()594 self.failUnlessEqual(self.instance.obj[1](), 8)595 self.failUnlessEqual(self.instance.obj[2](), 16)596 self.failUnlessEqual(value(self.instance.obj[1]), 8)597 self.failUnlessEqual(value(self.instance.obj[2]), 16)598 599 def test_sense_option(self):600 """Test sense option"""601 self.model.obj = Objective(self.model.A,sense=maximize)602 self.instance = self.model.create()603 self.failUnlessEqual(self.instance.obj.sense, maximize)604 605 def test_dim(self):606 """Test dim method"""607 self.model.obj = Objective(self.model.A)608 self.instance = self.model.create()609 self.failUnlessEqual(self.instance.obj.dim(),1)610 611 def test_keys(self):612 """Test keys method"""613 def A_rule(i, model):614 return model.x615 self.model.x = Var()616 self.model.obj = Objective(self.model.A, rule=A_rule)617 self.instance = self.model.create()618 self.failUnlessEqual(len(self.instance.obj.keys()),2)619 620 def test_len(self):621 """Test len method"""622 self.model.obj = Objective(self.model.A)623 self.instance = self.model.create()624 self.failUnlessEqual(len(self.instance.obj),0)625 """Test rule option"""626 def f(model):627 ans=0628 for i in model.x.keys():629 ans = ans + model.x[i]630 return ans631 self.model.x = Var(RangeSet(1,4),initialize=2)632 self.model.obj = Objective(rule=f)633 self.instance = self.model.create()634 self.failUnlessEqual(len(self.instance.obj),1)635 636 637 class Test2DArrayObj(PyomoModel):638 639 def setUp(self):640 #641 # Create Model642 #643 PyomoModel.setUp(self)644 self.model.A = Set(initialize=[1,2])645 646 def tearDown(self):647 pass648 649 def test_rule_option(self):650 """Test rule option"""651 def f(i,k,model):652 ans=0653 for j in model.x.keys():654 ans = ans + model.x[j]655 ans *= i656 return ans657 self.model.x = Var(RangeSet(1,4),initialize=2)658 self.model.obj = Objective(self.model.A,self.model.A, rule=f)659 self.instance = self.model.create()660 try:661 self.failUnlessEqual(self.instance.obj(),None)662 self.fail("Expected ValueError")663 except ValueError:664 pass665 self.instance.x.reset()666 self.failUnlessEqual(self.instance.obj[1,1](), 8)667 self.failUnlessEqual(self.instance.obj[2,1](), 16)668 self.failUnlessEqual(value(self.instance.obj[1,1]), 8)669 self.failUnlessEqual(value(self.instance.obj[2,1]), 16)670 671 def test_sense_option(self):672 """Test sense option"""673 self.model.obj = Objective(self.model.A,self.model.A,sense=maximize)674 self.instance = self.model.create()675 self.failUnlessEqual(self.instance.obj.sense, maximize)676 677 def test_dim(self):678 """Test dim method"""679 self.model.obj = Objective(self.model.A,self.model.A)680 self.instance = self.model.create()681 self.failUnlessEqual(self.instance.obj.dim(),2)682 683 def test_keys(self):684 """Test keys method"""685 def A_rule(i,j,model):686 return model.x687 self.model.x = Var()688 self.model.obj = Objective(self.model.A,self.model.A, rule=A_rule)689 self.instance = self.model.create()690 self.failUnlessEqual(len(self.instance.obj.keys()),4)691 692 def test_len(self):693 """Test len method"""694 self.model.obj = Objective(self.model.A,self.model.A)695 self.instance = self.model.create()696 self.failUnlessEqual(len(self.instance.obj),0)697 """Test rule option"""698 def f(model):699 ans=0700 for i in model.x.keys():701 ans = ans + model.x[i]702 return ans703 self.model.x = Var(RangeSet(1,4),initialize=2)704 self.model.obj = Objective(rule=f)705 self.instance = self.model.create()706 self.failUnlessEqual(len(self.instance.obj),1)707 708 709 class TestSimpleCon(PyomoModel):710 711 def setUp(self):712 #713 # Create Model714 #715 PyomoModel.setUp(self)716 717 def tearDown(self):718 pass719 720 def test_rule1(self):721 """Test rule option"""722 def f(model):723 ans=0724 for i in model.x.keys():725 ans = ans + model.x[i]726 ans = ans >= 0727 ans = ans <= 1728 return ans729 self.model.x = Var(RangeSet(1,4),initialize=2)730 self.model.c = Constraint(rule=f)731 self.instance = self.model.create()732 try:733 self.failUnlessEqual(self.instance.c(), 8)734 except ValueError:735 pass736 else:737 self.fail("test_rule_option")738 self.instance.x.reset()739 self.failUnlessEqual(self.instance.c(), 8)740 self.failUnlessEqual(value(self.instance.c), 8)741 742 def test_rule2(self):743 """Test rule option"""744 def f(model):745 ans=0746 for i in model.x.keys():747 ans = ans + model.x[i]748 return (0,ans,1)749 self.model.x = Var(RangeSet(1,4),initialize=2)750 self.model.c = Constraint(rule=f)751 self.instance = self.model.create()752 try:753 self.failUnlessEqual(self.instance.c(), 8)754 except ValueError:755 pass756 else:757 self.fail("test_rule_option")758 self.instance.x.reset()759 self.failUnlessEqual(self.instance.c(), 8)760 self.failUnlessEqual(value(self.instance.c), 8)761 762 def test_rule3(self):763 """Test rule option"""764 def f(model):765 ans=0766 for i in model.x.keys():767 ans = ans + model.x[i]768 return (0,ans,None)769 self.model.x = Var(RangeSet(1,4),initialize=2)770 self.model.c = Constraint(rule=f)771 self.instance = self.model.create()772 try:773 self.failUnlessEqual(self.instance.c(), 8)774 except ValueError:775 pass776 else:777 self.fail("test_rule_option")778 self.instance.x.reset()779 self.failUnlessEqual(self.instance.c(), 8)780 self.failUnlessEqual(value(self.instance.c), 8)781 782 def test_rule4(self):783 """Test rule option"""784 def f(model):785 ans=0786 for i in model.x.keys():787 ans = ans + model.x[i]788 return (None,ans,1)789 self.model.x = Var(RangeSet(1,4),initialize=2)790 self.model.c = Constraint(rule=f)791 self.instance = self.model.create()792 try:793 self.failUnlessEqual(self.instance.c(), 8)794 except ValueError:795 pass796 else:797 self.fail("test_rule_option")798 self.instance.x.reset()799 self.failUnlessEqual(self.instance.c(), 8)800 self.failUnlessEqual(value(self.instance.c), 8)801 802 def test_rule5(self):803 """Test rule option"""804 def f(model):805 ans=0806 for i in model.x.keys():807 ans = ans + model.x[i]808 return (ans,1)809 self.model.x = Var(RangeSet(1,4),initialize=2)810 self.model.c = Constraint(rule=f)811 self.instance = self.model.create()812 try:813 self.failUnlessEqual(self.instance.c(), 8)814 except ValueError:815 pass816 else:817 self.fail("test_rule_option")818 self.instance.x.reset()819 self.failUnlessEqual(self.instance.c(), 8)820 self.failUnlessEqual(value(self.instance.c), 8)821 822 def test_dim(self):823 """Test dim method"""824 self.model.c = Constraint()825 self.instance = self.model.create()826 self.failUnlessEqual(self.instance.c.dim(),0)827 828 def test_keys(self):829 """Test keys method"""830 self.model.c = Constraint()831 self.instance = self.model.create()832 self.failUnlessEqual(self.instance.c.keys(),[])833 834 def test_len(self):835 """Test len method"""836 self.model.c = Constraint()837 self.instance = self.model.create()838 self.failUnlessEqual(len(self.instance.c),0)839 """Test rule option"""840 def f(model):841 ans=0842 for i in model.x.keys():843 ans = ans + model.x[i]844 ans = ans == 0845 return ans846 self.model.x = Var(RangeSet(1,4),initialize=2)847 self.model.c = Constraint(rule=f)848 self.instance = self.model.create()849 self.failUnlessEqual(len(self.instance.c),1)850 851 852 class TestArrayCon(PyomoModel):853 854 def setUp(self):855 #856 # Create Model857 #858 PyomoModel.setUp(self)859 self.model.A = Set(initialize=[1,2,3,4])860 861 def tearDown(self):862 pass863 864 def test_rule_option1(self):865 """Test rule option"""866 def f(i,model):867 ans=0868 for j in model.x.keys():869 ans = ans + model.x[j]870 ans *= i871 ans = ans < 0872 ans = ans > 0873 return ans874 self.model.x = Var(RangeSet(1,4),initialize=2)875 self.model.c = Constraint(self.model.A,rule=f)876 self.instance = self.model.create()877 try:878 self.instance.c[1]()879 self.fail("Expected ValueError")880 except ValueError:881 pass882 self.instance.x.reset()883 self.failUnlessEqual(self.instance.c[1](), 8)884 self.failUnlessEqual(self.instance.c[2](), 16)885 self.failUnlessEqual(value(self.instance.c[1]), 8)886 self.failUnlessEqual(value(self.instance.c[2]), 16)887 self.failUnlessEqual(len(self.instance.c), 4)888 889 def test_rule_option2(self):890 """Test rule option"""891 def f(i,model):892 if i%2 == 0:893 return 0894 ans=0895 for j in model.x.keys():896 ans = ans + model.x[j]897 ans *= i898 ans = ans < 0899 ans = ans > 0900 return ans901 self.model.x = Var(RangeSet(1,4),initialize=2)902 self.model.c = Constraint(self.model.A,rule=f)903 self.instance = self.model.create()904 try:905 self.instance.c[1]()906 self.fail("Expected ValueError")907 except ValueError:908 pass909 self.instance.x.reset()910 self.failUnlessEqual(self.instance.c[1](), 8)911 self.failUnlessEqual(value(self.instance.c[1]), 8)912 self.failUnlessEqual(len(self.instance.c), 2)913 914 def test_rule_option3(self):915 """Test rule option"""916 def f(i,model):917 if i%2 == 0:918 return None919 ans=0920 for j in model.x.keys():921 ans = ans + model.x[j]922 ans *= i923 ans = ans < 0924 ans = ans > 0925 return ans926 self.model.x = Var(RangeSet(1,4),initialize=2)927 self.model.c = Constraint(self.model.A,rule=f)928 self.instance = self.model.create()929 try:930 self.instance.c[1]()931 self.fail("Expected ValueError")932 except ValueError:933 pass934 self.instance.x.reset()935 self.failUnlessEqual(self.instance.c[1](), 8)936 self.failUnlessEqual(value(self.instance.c[1]), 8)937 self.failUnlessEqual(len(self.instance.c), 2)938 939 def test_rule_option4(self):940 """Test rule option"""941 def f(model):942 res={}943 for i in [0,1,2]:944 ans=0945 for j in model.x.keys():946 ans = ans + model.x[j]947 ans *= i948 ans = ans < 0949 ans = ans > 0950 res[i]=ans951 return res952 self.model.x = Var(RangeSet(1,4),initialize=2)953 self.model.c = Constraint(self.model.A,rule=f)954 try:955 self.instance = self.model.create()956 self.fail("Expected IndexError")957 except IndexError:958 pass959 960 def test_dim(self):961 """Test dim method"""962 self.model.c = Constraint(self.model.A)963 self.instance = self.model.create()964 self.failUnlessEqual(self.instance.c.dim(),1)965 966 def test_keys(self):967 """Test keys method"""968 self.model.c = Constraint(self.model.A)969 self.instance = self.model.create()970 self.failUnlessEqual(len(self.instance.c.keys()),0)971 972 def test_len(self):973 """Test len method"""974 self.model.c = Constraint(self.model.A)975 self.instance = self.model.create()976 self.failUnlessEqual(len(self.instance.c),0)977 """Test rule option"""978 def f(model):979 ans=0980 for i in model.x.keys():981 ans = ans + model.x[i]982 ans = ans==2983 return ans984 self.model.x = Var(RangeSet(1,4),initialize=2)985 self.model.c = Constraint(rule=f)986 self.instance = self.model.create()987 self.failUnlessEqual(len(self.instance.c),1)988 989 def test_rule_error1(self):990 """Verify that errors in the rule dictionary are identified"""991 def f(model):992 res={}993 for i in model.A:994 if i%2 != 0:995 ans=0996 for j in model.x.keys():997 ans = ans + model.x[j]998 ans *= i999 ans = ans < 01000 ans = ans > 01001 res[i]=ans1002 return res1003 self.model.x = Var(RangeSet(1,4),initialize=2)1004 self.model.c = Constraint(self.model.A,rule=f)1005 self.instance = self.model.create()1006 try:1007 self.instance.c[1]()1008 self.fail("Expected ValueError")1009 except ValueError:1010 pass1011 self.instance.x.reset()1012 self.failUnlessEqual(self.instance.c[1](), 8)1013 self.failUnlessEqual(value(self.instance.c[1]), 8)1014 self.failUnlessEqual(len(self.instance.c), 2)1015 1016 1017 1018 class Test2DArrayCon(PyomoModel):1019 1020 def setUp(self):1021 #1022 # Create Model1023 #1024 PyomoModel.setUp(self)1025 self.model.A = Set(initialize=[1,2])1026 1027 def tearDown(self):1028 pass1029 1030 def test_rule_option(self):1031 """Test rule option"""1032 def f(i,j,model):1033 ans=01034 for j in model.x.keys():1035 ans = ans + model.x[j]1036 ans *= i1037 ans = ans < 01038 ans = ans > 01039 return ans1040 self.model.x = Var(RangeSet(1,4),initialize=2)1041 self.model.c = Constraint(self.model.A,self.model.A,rule=f)1042 self.instance = self.model.create()1043 try:1044 self.instance.c[1,1]()1045 self.fail("Expected ValueError")1046 except ValueError:1047 pass1048 self.instance.x.reset()1049 self.failUnlessEqual(self.instance.c[1,1](), 8)1050 self.failUnlessEqual(self.instance.c[2,1](), 16)1051 self.failUnlessEqual(value(self.instance.c[1,1]), 8)1052 self.failUnlessEqual(value(self.instance.c[2,1]), 16)1053 1054 def test_dim(self):1055 """Test dim method"""1056 self.model.c = Constraint(self.model.A,self.model.A)1057 self.instance = self.model.create()1058 self.failUnlessEqual(self.instance.c.dim(),2)1059 1060 def test_keys(self):1061 """Test keys method"""1062 self.model.c = Constraint(self.model.A,self.model.A)1063 self.instance = self.model.create()1064 self.failUnlessEqual(len(self.instance.c.keys()),0)1065 1066 def test_len(self):1067 """Test len method"""1068 self.model.c = Constraint(self.model.A,self.model.A)1069 self.instance = self.model.create()1070 self.failUnlessEqual(len(self.instance.c),0)1071 """Test rule option"""1072 def f(model):1073 ans=01074 for i in model.x.keys():1075 ans = ans + model.x[i]1076 ans = ans==21077 return ans1078 self.model.x = Var(RangeSet(1,4),initialize=2)1079 self.model.c = Constraint(rule=f)1080 self.instance = self.model.create()1081 self.failUnlessEqual(len(self.instance.c),1)1082 1083 1084 class TestModel(PyomoModel):1085 1086 def setUp(self):1087 #1088 # Create Model1089 #1090 PyomoModel.setUp(self)1091 1092 def tearDown(self):1093 #pass1094 39 if os.path.exists("unknown.lp"): 1095 40 os.unlink("unknown.lp") 41 pyutilib.services.TempfileManager.clear_tempfiles() 1096 42 1097 43 def test_clear_attribute(self): … … 1118 64 self.model.x = Var(self.model.A, bounds=(1,1)) 1119 65 def obj_rule(model): 1120 expr = 0 1121 for i in model.A: 1122 expr += model.x[i] 1123 return expr 66 return summation(model.x) 1124 67 self.model.obj = Objective(rule=obj_rule) 1125 68 self.instance = self.model.create() … … 1130 73 self.model.x = Var(self.model.A, bounds=(1,1)) 1131 74 def obj_rule(model): 1132 expr = 0 1133 for i in model.A: 1134 expr += model.x[i] 1135 return expr 75 return summation(model.x) 1136 76 self.model.obj = Objective(rule=obj_rule) 1137 77 def c_rule(model): 1138 return (1, model.x[1]+model.x[2], 2)78 return (1, model.x[1]+model.x[2], 2) 1139 79 self.model.c = Constraint(rule=c_rule) 1140 80 self.instance = self.model.create() … … 1150 90 1151 91 def test_solve1(self): 1152 self.model.A = RangeSet(1,4)1153 self.model.x = Var(self.model.A, bounds=(1,1))1154 def obj_rule(model):1155 expr = 01156 for i in model.A:1157 expr += model.x[i]1158 return expr1159 self.model.obj = Objective(rule=obj_rule)1160 def c_rule(model):1161 expr = 01162 for i in model.A:1163 expr += i*model.x[i]1164 return (expr,0)1165 self.model.c = Constraint(rule=c_rule)1166 self.instance = self.model.create()1167 #self.instance.pprint()1168 92 if not pyutilib.services.registered_executable("glpsol"): 1169 93 return 1170 else: 1171 opt = SolverFactory('glpk') 94 self.model.A = RangeSet(1,4) 95 self.model.x = Var(self.model.A, bounds=(1,1)) 96 def obj_rule(model): 97 return summation(model.x) 98 self.model.obj = Objective(rule=obj_rule) 99 def c_rule(model): 100 expr = 0 101 for i in model.A: 102 expr += i*model.x[i] 103 return expr == 0 104 self.model.c = Constraint(rule=c_rule) 105 self.instance = self.model.create() 106 #self.instance.pprint() 107 opt = SolverFactory('glpk') 1172 108 solutions = opt.solve(self.instance, keepFiles=True) 1173 109 self.instance.load(solutions) 1174 110 self.instance.display(currdir+"solve1.out") 1175 111 self.failUnlessFileEqualsBaseline(currdir+"solve1.out",currdir+"solve1.txt") 112 # 113 def d_rule(model): 114 return model.x[1] > 0 115 self.model.d = Constraint(rule=d_rule) 116 self.model.d.deactivate() 117 self.instance = self.model.create() 118 solutions = opt.solve(self.instance, keepFiles=True) 119 self.instance.load(solutions) 120 self.instance.display(currdir+"solve1.out") 121 self.failUnlessFileEqualsBaseline(currdir+"solve1.out",currdir+"solve1.txt") 122 # 123 self.model.d.activate() 124 self.instance = self.model.create() 125 solutions = opt.solve(self.instance, keepFiles=True) 126 self.instance.load(solutions) 127 self.instance.display(currdir+"solve1.out") 128 self.failUnlessFileEqualsBaseline(currdir+"solve1.out",currdir+"solve1a.txt") 129 # 130 self.model.d.deactivate() 131 def e_rule(i, model): 132 return model.x[i] > 0 133 self.model.e = Constraint(self.model.A, rule=e_rule) 134 self.instance = self.model.create() 135 for i in self.instance.A: 136 self.instance.e[i].deactivate() 137 solutions = opt.solve(self.instance, keepFiles=True) 138 self.instance.load(solutions) 139 self.instance.display(currdir+"solve1.out") 140 self.failUnlessFileEqualsBaseline(currdir+"solve1.out",currdir+"solve1b.txt") 1176 141 1177 142 def Xtest_solve2(self): … … 1181 146 impacted this test... 1182 147 """ 148 if not pyutilib.services.registered_executable("glpsol"): 149 return 1183 150 self.model.A = RangeSet(1,4) 1184 151 self.model.x = Var(self.model.A, bounds=(1,1)) … … 1191 158 self.instance = self.model.create() 1192 159 #self.instance.pprint() 1193 if not pyutilib.services.registered_executable("glpsol"): 1194 return 1195 else: 1196 opt = solvers.GLPK(keepFiles=True) 160 opt = solvers.GLPK(keepFiles=True) 1197 161 solutions = opt.solve(self.instance) 1198 162 solutions.write() … … 1218 182 1219 183 def test_solve4(self): 1220 self.model.A = RangeSet(1,4) 1221 self.model.x = Var(self.model.A, bounds=(1,1)) 1222 def obj_rule(model): 1223 expr = 0 1224 for i in model.A: 1225 expr += model.x[i] 1226 return expr 184 if not pyutilib.services.registered_executable("glpsol"): 185 return 186 self.model.A = RangeSet(1,4) 187 self.model.x = Var(self.model.A, bounds=(1,1)) 188 def obj_rule(model): 189 return summation(model.x) 1227 190 self.model.obj = Objective(rule=obj_rule) 1228 191 def c_rule(model): … … 1230 193 for i in model.A: 1231 194 expr += i*model.x[i] 1232 return (expr,0)195 return expr == 0 1233 196 self.model.c = Constraint(rule=c_rule) 1234 197 self.instance = self.model.create() 1235 198 #self.instance.pprint() 1236 if not pyutilib.services.registered_executable("glpsol"): 1237 return 1238 else: 1239 opt = SolverFactory('glpk', keepFiles=True) 199 opt = SolverFactory('glpk', keepFiles=True) 1240 200 solutions = opt.solve(self.instance) 1241 201 self.instance.load(solutions.solution(0))
Note: See TracChangeset
for help on using the changeset viewer.