source: coopr.pyomo/stable/2.3/coopr/pyomo/tests/unit/test_obj.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: 7.5 KB
Line 
1#
2# Unit Tests for Elements of a Model
3#
4# TestSimpleObj                Class for testing single objective
5# TestArrayObj                Class for testing array of objective
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 TestSimpleObj(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_rule_option(self):
42        """Test rule option"""
43        def f(model):
44          ans=0
45          for i in model.x.keys():
46            ans = ans + model.x[i]
47          return ans
48        self.model.x = Var(RangeSet(1,4),initialize=2)
49        self.model.obj = Objective(rule=f)
50        self.instance = self.model.create()
51        try:
52          self.failUnlessEqual(self.instance.obj(), 8)
53        except ValueError:
54          pass
55        else:
56          self.fail("test_rule_option")
57        self.instance.x.reset()
58        self.failUnlessEqual(self.instance.obj(), 8)
59        self.failUnlessEqual(value(self.instance.obj), 8)
60
61    def test_sense_option(self):
62        """Test sense option"""
63        def rule(model):
64            return 1.0
65        self.model.obj = Objective(sense=maximize, rule=rule)
66        self.instance = self.model.create()
67        self.failUnlessEqual(self.instance.obj.sense, maximize)
68
69    def test_dim(self):
70        """Test dim method"""
71        def rule(model):
72            return 1
73        self.model.obj = Objective(rule=rule)
74        self.instance = self.model.create()
75        self.failUnlessEqual(self.instance.obj.dim(),0)
76
77    def test_keys(self):
78        """Test keys method"""
79        def rule(model):
80            return 1
81        self.model.obj = Objective(rule=rule)
82        self.instance = self.model.create()
83        self.failUnlessEqual(self.instance.obj.keys(),[None])
84
85    def test_len(self):
86        """Test len method"""
87        def rule(model):
88            return 1.0
89        self.model.obj = Objective(rule=rule)
90        self.instance = self.model.create()
91        self.failUnlessEqual(len(self.instance.obj),1)
92        """Test rule option"""
93        def f(model):
94          ans=0
95          for i in model.x.keys():
96            ans = ans + model.x[i]
97          return ans
98        self.model.x = Var(RangeSet(1,4),initialize=2)
99        self.model.obj = Objective(rule=f)
100        self.instance = self.model.create()
101        self.failUnlessEqual(len(self.instance.obj),1)
102
103
104class TestArrayObj(PyomoModel):
105
106    def setUp(self):
107        #
108        # Create Model
109        #
110        PyomoModel.setUp(self)
111        self.model.A = Set(initialize=[1,2])
112
113    def tearDown(self):
114        pass
115
116    def test_rule_option(self):
117        """Test rule option"""
118        def f(i,model):
119          ans=0
120          for j in model.x.keys():
121            ans = ans + model.x[j]
122          ans *= i
123          return ans
124        self.model.x = Var(RangeSet(1,4),initialize=2)
125        self.model.obj = Objective(self.model.A,rule=f)
126        self.instance = self.model.create()
127        try:
128            self.instance.obj[1]()
129            self.fail("Excepted ValueError due to uninitialized variables")
130        except ValueError:
131            pass
132        self.instance.x.reset()
133        self.failUnlessEqual(self.instance.obj[1](), 8)
134        self.failUnlessEqual(self.instance.obj[2](), 16)
135        self.failUnlessEqual(value(self.instance.obj[1]), 8)
136        self.failUnlessEqual(value(self.instance.obj[2]), 16)
137
138    def test_sense_option(self):
139        """Test sense option"""
140        self.model.obj = Objective(self.model.A,sense=maximize)
141        self.instance = self.model.create()
142        self.failUnlessEqual(self.instance.obj.sense, maximize)
143
144    def test_dim(self):
145        """Test dim method"""
146        self.model.obj = Objective(self.model.A)
147        self.instance = self.model.create()
148        self.failUnlessEqual(self.instance.obj.dim(),1)
149
150    def test_keys(self):
151        """Test keys method"""
152        def A_rule(i, model):
153            return model.x
154        self.model.x = Var()
155        self.model.obj = Objective(self.model.A, rule=A_rule)
156        self.instance = self.model.create()
157        self.failUnlessEqual(len(self.instance.obj.keys()),2)
158
159    def test_len(self):
160        """Test len method"""
161        self.model.obj = Objective(self.model.A)
162        self.instance = self.model.create()
163        self.failUnlessEqual(len(self.instance.obj),0)
164        """Test rule option"""
165        def f(model):
166          ans=0
167          for i in model.x.keys():
168            ans = ans + model.x[i]
169          return ans
170        self.model.x = Var(RangeSet(1,4),initialize=2)
171        self.model.obj = Objective(rule=f)
172        self.instance = self.model.create()
173        self.failUnlessEqual(len(self.instance.obj),1)
174
175
176class Test2DArrayObj(PyomoModel):
177
178    def setUp(self):
179        #
180        # Create Model
181        #
182        PyomoModel.setUp(self)
183        self.model.A = Set(initialize=[1,2])
184
185    def tearDown(self):
186        pass
187
188    def test_rule_option(self):
189        """Test rule option"""
190        def f(i,k,model):
191          ans=0
192          for j in model.x.keys():
193            ans = ans + model.x[j]
194          ans *= i
195          return ans
196        self.model.x = Var(RangeSet(1,4),initialize=2)
197        self.model.obj = Objective(self.model.A,self.model.A, rule=f)
198        self.instance = self.model.create()
199        try:
200            self.failUnlessEqual(self.instance.obj(),None)
201            self.fail("Expected ValueError")
202        except ValueError:
203            pass
204        self.instance.x.reset()
205        self.failUnlessEqual(self.instance.obj[1,1](), 8)
206        self.failUnlessEqual(self.instance.obj[2,1](), 16)
207        self.failUnlessEqual(value(self.instance.obj[1,1]), 8)
208        self.failUnlessEqual(value(self.instance.obj[2,1]), 16)
209
210    def test_sense_option(self):
211        """Test sense option"""
212        self.model.obj = Objective(self.model.A,self.model.A,sense=maximize)
213        self.instance = self.model.create()
214        self.failUnlessEqual(self.instance.obj.sense, maximize)
215
216    def test_dim(self):
217        """Test dim method"""
218        self.model.obj = Objective(self.model.A,self.model.A)
219        self.instance = self.model.create()
220        self.failUnlessEqual(self.instance.obj.dim(),2)
221
222    def test_keys(self):
223        """Test keys method"""
224        def A_rule(i,j,model):
225            return model.x
226        self.model.x = Var()
227        self.model.obj = Objective(self.model.A,self.model.A, rule=A_rule)
228        self.instance = self.model.create()
229        self.failUnlessEqual(len(self.instance.obj.keys()),4)
230
231    def test_len(self):
232        """Test len method"""
233        self.model.obj = Objective(self.model.A,self.model.A)
234        self.instance = self.model.create()
235        self.failUnlessEqual(len(self.instance.obj),0)
236        """Test rule option"""
237        def f(model):
238          ans=0
239          for i in model.x.keys():
240            ans = ans + model.x[i]
241          return ans
242        self.model.x = Var(RangeSet(1,4),initialize=2)
243        self.model.obj = Objective(rule=f)
244        self.instance = self.model.create()
245        self.failUnlessEqual(len(self.instance.obj),1)
246
247
248if __name__ == "__main__":
249   unittest.main()
250
Note: See TracBrowser for help on using the repository browser.