source: coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_var.py @ 2433

Last change on this file since 2433 was 2433, checked in by wehart, 11 years ago

Merged revisions 2388-2432 via svnmerge from
https://software.sandia.gov/svn/public/coopr/coopr.pyomo/trunk

........

r2399 | wehart | 2010-02-24 11:36:56 -0700 (Wed, 24 Feb 2010) | 2 lines


Adding a work-around when pstats cannot be imported.

........

r2400 | wehart | 2010-02-24 14:51:08 -0700 (Wed, 24 Feb 2010) | 2 lines


Misc documentation updates.

........

r2408 | wehart | 2010-02-28 05:41:04 -0700 (Sun, 28 Feb 2010) | 3 lines


Allow an earlier version of Python. I'm still not sure this is a good idea,
but it's necessary for Jython.

........

r2409 | wehart | 2010-02-28 05:42:33 -0700 (Sun, 28 Feb 2010) | 7 lines


Rework of profiling imports.


Refinement of Pyomo command-line parsing, which is more
specific now.


Adding automatic import of pyomo to package.

........

r2415 | jwatson | 2010-03-08 20:53:13 -0700 (Mon, 08 Mar 2010) | 3 lines


Significantly improved performance of PyomoModel? _clear_attribute method, mainly by eliminate unnecessary calls to it through _setattr_exec_.

........

r2416 | jwatson | 2010-03-09 16:45:22 -0700 (Tue, 09 Mar 2010) | 5 lines


Modified the AMPL dat file parser to instantiate the lexer and yaccer objects only once, for the lifetime of the module.


They were being inadvertently created at each invocation, which pyomo users wouldn't notice. But PySP users, who can be creating 1K or more instances, do!

........

r2422 | wehart | 2010-03-11 16:01:09 -0700 (Thu, 11 Mar 2010) | 3 lines


Rework of unit tests to (a) import pyutilib.th as 'unittest' and
(b) employ test skipping.

........

r2430 | wehart | 2010-03-11 23:38:22 -0700 (Thu, 11 Mar 2010) | 2 lines


Reworking class decorators.

........

File size: 16.5 KB
Line 
1#
2# Unit Tests for Elements of a Model
3#
4# TestSimpleVar                Class for testing single variables
5# TestArrayVar                Class for testing array of variables
6#
7
8import os
9import sys
10from os.path import abspath, dirname
11sys.path.insert(0, dirname(dirname(abspath(__file__)))+os.sep+".."+os.sep+"..")
12currdir = dirname(abspath(__file__))+os.sep
13
14from coopr.pyomo.base import IntegerSet
15from coopr.pyomo import *
16from coopr.opt import *
17from coopr.pyomo.base.var import _VarElement
18import pyutilib.th as unittest
19import pyutilib.services
20
21class PyomoModel(unittest.TestCase):
22
23    def setUp(self):
24        self.model = Model()
25
26    def construct(self,filename):
27        self.instance = self.model.create(filename)
28
29
30class TestSimpleVar(PyomoModel):
31
32    def setUp(self):
33        #
34        # Create Model
35        #
36        PyomoModel.setUp(self)
37
38    def tearDown(self):
39        pass
40
41    def test_fixed_attr(self):
42        """Test fixed attribute"""
43        self.model.x = Var()
44        self.instance = self.model.create()
45        self.instance.x.fixed = True
46        self.failUnlessEqual(self.instance.x.fixed, True)
47
48    def test_value_attr(self):
49        """Test value attribute"""
50        self.model.x = Var()
51        self.instance = self.model.create()
52        self.instance.x.value = 3.5
53        self.failUnlessEqual(self.instance.x.value, 3.5)
54
55    def test_initial_attr(self):
56        """Test initial attribute"""
57        self.model.x = Var()
58        self.instance = self.model.create()
59        self.instance.x.initial = 3.5
60        self.failUnlessEqual(self.instance.x.initial, 3.5)
61
62    def test_domain_attr(self):
63        """Test domain attribute"""
64        self.model.x = Var()
65        self.instance = self.model.create()
66        self.instance.x.domain = Integers
67        self.failUnlessEqual(type(self.instance.x.domain), IntegerSet)
68
69    def test_name_attr(self):
70        """Test name attribute"""
71        #
72        # A user would never need to do this, but this
73        # attribute is needed within Pyomo
74        #
75        self.model.x = Var()
76        self.model.x.name = "foo"
77        self.failUnlessEqual(self.model.x.name, "foo")
78
79    def test_lb_attr1(self):
80        """Test lb attribute"""
81        self.model.x = Var()
82        self.instance = self.model.create()
83        self.instance.x.setlb(-1.0)
84        self.failUnlessEqual(self.instance.x.lb(), -1.0)
85
86    def test_lb_attr2(self):
87        """Test lb attribute"""
88        self.model.x = Var(within=NonNegativeReals, bounds=(-1,2))
89        self.instance = self.model.create()
90        self.failUnlessEqual(self.instance.x.lb(), 0.0)
91        self.failUnlessEqual(self.instance.x.ub(), 2.0)
92
93    def test_ub_attr1(self):
94        """Test ub attribute"""
95        self.model.x = Var()
96        self.instance = self.model.create()
97        self.instance.x.setub(1.0)
98        self.failUnlessEqual(self.instance.x.ub(), 1.0)
99
100    def test_ub_attr2(self):
101        """Test ub attribute"""
102        self.model.x = Var(within=NonPositiveReals, bounds=(-2,1))
103        self.instance = self.model.create()
104        self.failUnlessEqual(self.instance.x.lb(), -2.0)
105        self.failUnlessEqual(self.instance.x.ub(), 0.0)
106
107    def test_within_option(self):
108        """Test within option"""
109        self.model.x = Var(within=Integers)
110        self.failUnlessEqual(type(self.model.x.domain), IntegerSet)
111
112    def test_initialize_option(self):
113        """Test initialize option"""
114        self.model.x = Var(initialize=1.3)
115        self.instance = self.model.create()
116        self.failUnlessEqual(self.instance.x.initial, 1.3)
117
118    def test_bounds_option1(self):
119        """Test bounds option"""
120        def x_bounds(model):
121          return (-1.0,1.0)
122        self.model.x = Var(bounds=x_bounds)
123        self.instance = self.model.create()
124        self.failUnlessEqual(self.instance.x.lb(), -1.0)
125        self.failUnlessEqual(self.instance.x.ub(), 1.0)
126
127    def test_bounds_option2(self):
128        """Test bounds option"""
129        self.model.x = Var(bounds=(-1.0,1.0))
130        self.instance = self.model.create()
131        self.failUnlessEqual(self.instance.x.lb(), -1.0)
132        self.failUnlessEqual(self.instance.x.ub(), 1.0)
133
134    def test_rule_option(self):
135        """Test rule option"""
136        def x_init(model):
137          return 1.3
138        self.model.x = Var(initialize=x_init)
139        self.instance = self.model.create()
140        self.failUnlessEqual(self.instance.x.value, None)
141        self.failUnlessEqual(self.instance.x.initial, 1.3)
142
143    def test_reset(self):
144        """Test reset method"""
145        self.model.x = Var(initialize=3)
146        self.instance = self.model.create()
147        self.failUnlessEqual(self.instance.x.initial,3)
148        self.failUnlessEqual(self.instance.x.value,None)
149        self.instance.x.reset()
150        self.failUnlessEqual(self.instance.x.initial,3)
151        self.failUnlessEqual(self.instance.x.initial,3)
152
153    def test_dim(self):
154        """Test dim method"""
155        self.model.x = Var()
156        self.instance = self.model.create()
157        self.failUnlessEqual(self.instance.x.dim(),0)
158
159    def test_keys(self):
160        """Test keys method"""
161        self.model.x = Var()
162        self.instance = self.model.create()
163        self.failUnlessEqual(self.instance.x.keys(),[None])
164
165    def test_len(self):
166        """Test len method"""
167        self.model.x = Var()
168        self.instance = self.model.create()
169        self.failUnlessEqual(len(self.instance.x),1)
170
171    def test_simplify(self):
172        """Test simplify method"""
173        self.model.x = Var()
174        self.failUnlessEqual(type(self.model.x.simplify(self.model)),_VarElement)
175
176    def test_value(self):
177        """Check the value of the variable"""
178        self.model.x = Var(initialize=3.3)
179        self.instance = self.model.create()
180        tmp = value(self.instance.x.initial)
181        self.failUnlessEqual( type(tmp), float)
182        self.failUnlessEqual( tmp, 3.3 )
183        tmp = float(self.instance.x.initial)
184        self.failUnlessEqual( type(tmp), float)
185        self.failUnlessEqual( tmp, 3.3 )
186        tmp = int(self.instance.x.initial)
187        self.failUnlessEqual( type(tmp), int)
188        self.failUnlessEqual( tmp, 3 )
189
190
191class TestArrayVar(TestSimpleVar):
192
193    def setUp(self):
194        #
195        # Create Model
196        #
197        PyomoModel.setUp(self)
198        self.model.A = Set(initialize=[1,2])
199
200    def test_fixed_attr(self):
201        """Test fixed attribute"""
202        self.model.x = Var(self.model.A)
203        self.model.y = Var(self.model.A)
204        self.instance = self.model.create()
205        self.instance.x.fixed = True
206        #try:
207          #self.instance.x.fixed
208        #except AttributeError:
209          #pass
210        #else:
211          #self.fail("test_fixed_attr")
212        self.failUnlessEqual(self.instance.x[1].fixed, False)
213        self.instance.y[1].fixed=True
214        self.failUnlessEqual(self.instance.y[1].fixed, True)
215
216    def test_value_attr(self):
217        """Test value attribute"""
218        self.model.x = Var(self.model.A)
219        self.model.y = Var(self.model.A)
220        self.instance = self.model.create()
221        try:
222            self.instance.x = 3.5
223            self.fail("Expected ValueError")
224        except ValueError:
225            pass
226        self.instance.y[1] = 3.5
227        self.failUnlessEqual(self.instance.y[1], 3.5)
228
229    #def test_initial_attr(self):
230        #"""Test initial attribute"""
231        #self.model.x = Var(self.model.A)
232        #self.instance = self.model.create()
233        #self.instance.x.initial = 3.5
234        #self.failUnlessEqual(self.instance.x[1].initial, 3.5)
235
236    #def test_lb_attr(self):
237        #"""Test lb attribute"""
238        #self.model.x = Var(self.model.A)
239        #self.instance = self.model.create()
240        #self.instance.x.setlb(-1.0)
241        #self.failUnlessEqual(self.instance.x[1].lb(), -1.0)
242
243    #def test_ub_attr(self):
244        #"""Test ub attribute"""
245        #self.model.x = Var(self.model.A)
246        #self.instance = self.model.create()
247        #self.instance.x.setub(1.0)
248        #self.failUnlessEqual(self.instance.x[1].ub(), 1.0)
249
250    def test_initialize_option(self):
251        """Test initialize option"""
252        self.model.x = Var(self.model.A,initialize={1:1.3,2:2.3})
253        self.instance = self.model.create()
254        self.failUnlessEqual(self.instance.x[1], 1.3)
255        self.failUnlessEqual(self.instance.x[2], 2.3)
256
257    def test_bounds_option1(self):
258        """Test bounds option"""
259        def x_bounds(i,model):
260          return (-1.0,1.0)
261        self.model.x = Var(self.model.A, bounds=x_bounds)
262        self.instance = self.model.create()
263        self.failUnlessEqual(self.instance.x[1].lb(), -1.0)
264        self.failUnlessEqual(self.instance.x[1].ub(), 1.0)
265
266    def test_bounds_option2(self):
267        """Test bounds option"""
268        self.model.x = Var(self.model.A, bounds=(-1.0,1.0))
269        self.instance = self.model.create()
270        self.failUnlessEqual(self.instance.x[1].lb(), -1.0)
271        self.failUnlessEqual(self.instance.x[1].ub(), 1.0)
272
273    def test_rule_option(self):
274        """Test rule option"""
275        def x_init(i,model):
276          return 1.3
277        self.model.x = Var(self.model.A, initialize=x_init)
278        self.instance = self.model.create()
279        self.failUnlessEqual(self.instance.x[1].initial, 1.3)
280
281    def test_reset(self):
282        """Test reset method"""
283        self.model.x = Var(self.model.A,initialize=3)
284        self.instance = self.model.create()
285        self.failUnlessEqual(self.instance.x[1].initial,3)
286        self.failUnlessEqual(self.instance.x[1].value,None)
287        self.instance.x.reset()
288        self.failUnlessEqual(self.instance.x[1].initial,3)
289        self.failUnlessEqual(self.instance.x[1].initial,3)
290
291    def test_dim(self):
292        """Test dim method"""
293        self.model.x = Var(self.model.A)
294        self.instance = self.model.create()
295        self.failUnlessEqual(self.instance.x.dim(),1)
296
297    def test_keys(self):
298        """Test keys method"""
299        self.model.x = Var(self.model.A)
300        self.instance = self.model.create()
301        self.failUnlessEqual(self.instance.x.keys(),[1,2])
302
303    def test_len(self):
304        """Test len method"""
305        self.model.x = Var(self.model.A)
306        self.instance = self.model.create()
307        self.failUnlessEqual(len(self.instance.x),2)
308
309    def test_value(self):
310        """Check the value of the variable"""
311        self.model.x = Var(self.model.A,initialize=3.3)
312        self.instance = self.model.create()
313        tmp = value(self.instance.x[1].initial)
314        self.failUnlessEqual( type(tmp), float)
315        self.failUnlessEqual( tmp, 3.3 )
316        tmp = float(self.instance.x[1].initial)
317        self.failUnlessEqual( type(tmp), float)
318        self.failUnlessEqual( tmp, 3.3 )
319        tmp = int(self.instance.x[1].initial)
320        self.failUnlessEqual( type(tmp), int)
321        self.failUnlessEqual( tmp, 3 )
322
323
324class Test2DArrayVar(TestSimpleVar):
325
326    def setUp(self):
327        #
328        # Create Model
329        #
330        PyomoModel.setUp(self)
331        self.model.A = Set(initialize=[1,2])
332
333    def test_fixed_attr(self):
334        """Test fixed attribute"""
335        self.model.x = Var(self.model.A,self.model.A)
336        self.model.y = Var(self.model.A,self.model.A)
337        self.instance = self.model.create()
338        self.instance.x.fixed = True
339        #try:
340          #self.instance.x.fixed
341        #except AttributeError:
342          #pass
343        #else:
344          #self.fail("test_fixed_attr")
345        self.failUnlessEqual(self.instance.x[1,2].fixed, False)
346        self.instance.y[1,2].fixed=True
347        self.failUnlessEqual(self.instance.y[1,2].fixed, True)
348
349    def test_value_attr(self):
350        """Test value attribute"""
351        self.model.x = Var(self.model.A,self.model.A)
352        self.model.y = Var(self.model.A,self.model.A)
353        self.instance = self.model.create()
354        try:
355            self.instance.x = 3.5
356            self.fail("Expected ValueError")
357        except ValueError:
358            pass
359        self.instance.y[1,2] = 3.5
360        self.failUnlessEqual(self.instance.y[1,2], 3.5)
361
362    #def test_initial_attr(self):
363        #"""Test initial attribute"""
364        #self.model.x = Var(self.model.A,self.model.A)
365        #self.instance = self.model.create()
366        #self.instance.x.initial = 3.5
367        #self.failUnlessEqual(self.instance.x[1,1].initial, 3.5)
368
369    #def test_lb_attr(self):
370        #"""Test lb attribute"""
371        #self.model.x = Var(self.model.A,self.model.A)
372        #self.instance = self.model.create()
373        #self.instance.x.setlb(-1.0)
374        #self.failUnlessEqual(self.instance.x[2,1].lb(), -1.0)
375
376    #def test_ub_attr(self):
377        #"""Test ub attribute"""
378        #self.model.x = Var(self.model.A,self.model.A)
379        #self.instance = self.model.create()
380        #self.instance.x.setub(1.0)
381        #self.failUnlessEqual(self.instance.x[2,1].ub(), 1.0)
382
383    def test_initialize_option(self):
384        """Test initialize option"""
385        self.model.x = Var(self.model.A,self.model.A,initialize={(1,1):1.3,(2,2):2.3})
386        self.instance = self.model.create()
387        self.failUnlessEqual(self.instance.x[1,1], 1.3)
388        self.failUnlessEqual(self.instance.x[2,2], 2.3)
389        try:
390            value(self.instance.x[1,2])
391            self.fail("Expected KeyError")
392        except KeyError:
393            pass
394
395    def test_bounds_option1(self):
396        """Test bounds option"""
397        def x_bounds(i,j,model):
398          return (-1.0*(i+j),1.0*(i+j))
399        self.model.x = Var(self.model.A, self.model.A, bounds=x_bounds)
400        self.instance = self.model.create()
401        self.failUnlessEqual(self.instance.x[1,1].lb(), -2.0)
402        self.failUnlessEqual(self.instance.x[1,2].ub(), 3.0)
403
404    def test_bounds_option2(self):
405        """Test bounds option"""
406        self.model.x = Var(self.model.A, self.model.A, bounds=(-1.0,1.0))
407        self.instance = self.model.create()
408        self.failUnlessEqual(self.instance.x[1,1].lb(), -1.0)
409        self.failUnlessEqual(self.instance.x[1,1].ub(), 1.0)
410
411    def test_rule_option(self):
412        """Test rule option"""
413        def x_init(i,j,model):
414          return 1.3
415        self.model.x = Var(self.model.A, self.model.A, initialize=x_init)
416        self.instance = self.model.create()
417        self.failUnlessEqual(self.instance.x[1,2].initial, 1.3)
418
419    def test_reset(self):
420        """Test reset method"""
421        self.model.x = Var(self.model.A,self.model.A, initialize=3)
422        self.instance = self.model.create()
423        self.failUnlessEqual(self.instance.x[1,1].initial,3)
424        self.failUnlessEqual(self.instance.x[1,1].value,None)
425        self.instance.x.reset()
426        self.failUnlessEqual(self.instance.x[1,1].initial,3)
427        self.failUnlessEqual(self.instance.x[1,1].initial,3)
428
429    def test_dim(self):
430        """Test dim method"""
431        self.model.x = Var(self.model.A,self.model.A)
432        self.instance = self.model.create()
433        self.failUnlessEqual(self.instance.x.dim(),2)
434
435    def test_keys(self):
436        """Test keys method"""
437        self.model.x = Var(self.model.A,self.model.A)
438        self.instance = self.model.create()
439        ans = [(1,1),(1,2),(2,1),(2,2)]
440        self.failUnlessEqual(self.instance.x.keys().sort(),ans.sort())
441
442    def test_len(self):
443        """Test len method"""
444        self.model.x = Var(self.model.A,self.model.A)
445        self.instance = self.model.create()
446        self.failUnlessEqual(len(self.instance.x),4)
447
448    def test_value(self):
449        """Check the value of the variable"""
450        self.model.x = Var(self.model.A,self.model.A,initialize=3.3)
451        self.instance = self.model.create()
452        tmp = value(self.instance.x[1,1].initial)
453        self.failUnlessEqual( type(tmp), float)
454        self.failUnlessEqual( tmp, 3.3 )
455        tmp = float(self.instance.x[1,1].initial)
456        self.failUnlessEqual( type(tmp), float)
457        self.failUnlessEqual( tmp, 3.3 )
458        tmp = int(self.instance.x[1,1].initial)
459        self.failUnlessEqual( type(tmp), int)
460        self.failUnlessEqual( tmp, 3 )
461
462
463class TestVarComplexArray(PyomoModel):
464
465    def test_index1(self):
466        self.model.A = Set(initialize=range(0,4))
467        def B_index(model):
468            for i in model.A:
469                if i%2 == 0:
470                    yield i
471        def B_init(i,j,model):
472            if j:
473                return 2+i
474            return -(2+i)
475        self.model.B = Var(B_index, [True,False], initialize=B_init)
476        self.instance = self.model.create()
477        #self.instance.pprint()
478        self.failUnlessEqual(set(self.instance.B.keys()),set([(0,True),(2,True),(0,   False),(2,False)]))
479        self.failUnlessEqual(self.instance.B[0,True],2)
480        self.failUnlessEqual(self.instance.B[0,False],-2)
481        self.failUnlessEqual(self.instance.B[2,True],4)
482        self.failUnlessEqual(self.instance.B[2,False],-4)
483
484
485if __name__ == "__main__":
486   unittest.main()
487
Note: See TracBrowser for help on using the repository browser.