#
source:
coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_model.py
@
1994

Revision 1994, 7.0 KB checked in by wehart, 4 years ago (diff) |
---|

Line | |
---|---|

1 | # |

2 | # Unit Tests for Elements of a Model |

3 | # |

4 | # Test Class to test the Model class |

5 | # |

6 | |

7 | import unittest |

8 | import os |

9 | import sys |

10 | from os.path import abspath, dirname |

11 | sys.path.insert(0, dirname(dirname(abspath(__file__)))+os.sep+".."+os.sep+"..") |

12 | currdir = dirname(abspath(__file__))+os.sep |

13 | |

14 | from coopr.pyomo.base import IntegerSet |

15 | from coopr.pyomo import * |

16 | from coopr.opt import * |

17 | from coopr.pyomo.base.var import _VarElement |

18 | import pyutilib.th |

19 | import pyutilib.services |

20 | |

21 | class PyomoModel(pyutilib.th.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 | |

30 | class Test(PyomoModel): |

31 | |

32 | def setUp(self): |

33 | # |

34 | # Create Model |

35 | # |

36 | PyomoModel.setUp(self) |

37 | |

38 | def tearDown(self): |

39 | if os.path.exists("unknown.lp"): |

40 | os.unlink("unknown.lp") |

41 | pyutilib.services.TempfileManager.clear_tempfiles() |

42 | |

43 | def test_clear_attribute(self): |

44 | """ Coverage of the _clear_attribute method """ |

45 | self.model.A = Set() |

46 | self.failUnlessEqual(self.model.A.name,"A") |

47 | self.model.A = Var() |

48 | self.failUnlessEqual(self.model.A.name,"A") |

49 | self.model.A = Param() |

50 | self.failUnlessEqual(self.model.A.name,"A") |

51 | self.model.A = Objective() |

52 | self.failUnlessEqual(self.model.A.name,"A") |

53 | self.model.A = Constraint() |

54 | self.failUnlessEqual(self.model.A.name,"A") |

55 | self.model.A = Set() |

56 | self.failUnlessEqual(self.model.A.name,"A") |

57 | |

58 | def test_set_attr(self): |

59 | self.model.x = Param() |

60 | self.model.x = None |

61 | |

62 | def test_write(self): |

63 | self.model.A = RangeSet(1,4) |

64 | self.model.x = Var(self.model.A, bounds=(-1,1)) |

65 | def obj_rule(model): |

66 | return summation(model.x) |

67 | self.model.obj = Objective(rule=obj_rule) |

68 | self.instance = self.model.create() |

69 | self.instance.write() |

70 | |

71 | def test_write2(self): |

72 | self.model.A = RangeSet(1,4) |

73 | self.model.x = Var(self.model.A, bounds=(-1,1)) |

74 | def obj_rule(model): |

75 | return summation(model.x) |

76 | self.model.obj = Objective(rule=obj_rule) |

77 | def c_rule(model): |

78 | return (1, model.x[1]+model.x[2], 2) |

79 | self.model.c = Constraint(rule=c_rule) |

80 | self.instance = self.model.create() |

81 | self.instance.write() |

82 | |

83 | def test_write_error(self): |

84 | self.instance = self.model.create() |

85 | try: |

86 | self.instance.write(format=ProblemFormat.ospl) |

87 | self.fail("Expected error because ospl writer is not available") |

88 | except ValueError: |

89 | pass |

90 | |

91 | def test_solve1(self): |

92 | if not pyutilib.services.registered_executable("glpsol"): |

93 | return |

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') |

108 | solutions = opt.solve(self.instance, keepFiles=True) |

109 | self.instance.load(solutions) |

110 | self.instance.display(currdir+"solve1.out") |

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") |

141 | |

142 | def Xtest_solve2(self): |

143 | """ |

144 | WEH - this is disabled because glpk appears to work fine |

145 | on this example. I'm not quite sure what has changed that has |

146 | impacted this test... |

147 | """ |

148 | if not pyutilib.services.registered_executable("glpsol"): |

149 | return |

150 | self.model.A = RangeSet(1,4) |

151 | self.model.x = Var(self.model.A, bounds=(-1,1)) |

152 | def obj_rule(model): |

153 | expr = 0 |

154 | for i in model.A: |

155 | expr += model.x[i] |

156 | return expr |

157 | self.model.obj = Objective(rule=obj_rule) |

158 | self.instance = self.model.create() |

159 | #self.instance.pprint() |

160 | opt = solvers.GLPK(keepFiles=True) |

161 | solutions = opt.solve(self.instance) |

162 | solutions.write() |

163 | sys.exit(1) |

164 | try: |

165 | self.instance.load(solutions) |

166 | self.fail("Cannot load a solution with a bad solver status") |

167 | except ValueError: |

168 | pass |

169 | |

170 | def test_solve3(self): |

171 | self.model.A = RangeSet(1,4) |

172 | self.model.x = Var(self.model.A, bounds=(-1,1)) |

173 | def obj_rule(model): |

174 | expr = 0 |

175 | for i in model.A: |

176 | expr += model.x[i] |

177 | return expr |

178 | self.model.obj = Objective(rule=obj_rule) |

179 | self.instance = self.model.create() |

180 | self.instance.display(currdir+"solve3.out") |

181 | self.failUnlessFileEqualsBaseline(currdir+"solve3.out",currdir+"solve3.txt") |

182 | |

183 | def test_solve4(self): |

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) |

190 | self.model.obj = Objective(rule=obj_rule) |

191 | def c_rule(model): |

192 | expr = 0 |

193 | for i in model.A: |

194 | expr += i*model.x[i] |

195 | return expr == 0 |

196 | self.model.c = Constraint(rule=c_rule) |

197 | self.instance = self.model.create() |

198 | #self.instance.pprint() |

199 | opt = SolverFactory('glpk', keepFiles=True) |

200 | solutions = opt.solve(self.instance) |

201 | self.instance.load(solutions.solution(0)) |

202 | self.instance.display(currdir+"solve1.out") |

203 | self.failUnlessFileEqualsBaseline(currdir+"solve1.out",currdir+"solve1.txt") |

204 | |

205 | if __name__ == "__main__": |

206 | unittest.main() |

207 |

**Note:**See TracBrowser for help on using the repository browser.