-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobjectives
177 lines (157 loc) · 7 KB
/
objectives
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import logging
from six import StringIO
from six.moves import range
import pyutilib.th as unittest
from pyomo.common.log import LoggingIntercept
from pyomo.contrib.multistart.high_conf_stop import should_stop
from pyomo.environ import (
ConcreteModel, Constraint, NonNegativeReals, Objective, SolverFactory, Var,
maximize, sin, value
)
@unittest.skipIf(not SolverFactory('ipopt').available(), "IPOPT not available")
class MultistartTests(unittest.TestCase):
"""
Due to stochastic nature of the random restarts, these tests just
demonstrate, that for a small sample, the test will not do worse than the
standard solver. this is non-exhaustive due to the randomness. Hence all
asserts are inequalities.
"""
# test standard random restarts
def test_as_good_with_iteration_rand(self):
# initialize model with data
m = build_model()
# create ipopt solver
optsolver = SolverFactory('ipopt')
optsolver.solve(m)
for i in range(10):
m2 = build_model()
SolverFactory('multistart').solve(m2, iterations=10)
m_objectives = m.component_data_objects(Objective, active=True)
m_obj = next(m_objectives, None)
m2_objectives = m2.component_data_objects(Objective, active=True)
m2_obj = next(m2_objectives,None)
self.assertTrue((value(m2_obj.expr)) >= (value(m_obj.expr) - .001))
del m2
def test_as_good_with_iteration_other_strategies(self):
"""Test that other strategies do no worse"""
# initialize model with data
m = build_model()
# create ipopt solver
SolverFactory('ipopt').solve(m)
for i in range(10):
m2 = build_model()
SolverFactory('multistart').solve(
m2, iterations=10, strategy='rand_distributed')
m_objectives = m.component_data_objects(Objective, active=True)
m_obj = next(m_objectives, None)
m2_objectives = m2.component_data_objects(Objective, active=True)
m2_obj = next(m2_objectives,None)
#Assert that multistart solver does no worse than standard solver
self.assertTrue((value(m2_obj.expr)) >= (value(m_obj.expr) - .001))
del m2
for i in range(10):
m2 = build_model()
SolverFactory('multistart').solve(
m2, iterations=10, strategy='midpoint_guess_and_bound')
m_objectives = m.component_data_objects(Objective, active=True)
m_obj = next(m_objectives, None)
m2_objectives = m2.component_data_objects(Objective, active=True)
m2_obj = next(m2_objectives,None)
#Assert that multistart solver does no worse than standard solver
self.assertTrue((value(m2_obj.expr)) >= (value(m_obj.expr) - .001))
del m2
for i in range(10):
m2 = build_model()
SolverFactory('multistart').solve(
m2, iterations=10, strategy='rand_guess_and_bound')
m_objectives = m.component_data_objects(Objective, active=True)
m_obj = next(m_objectives, None)
m2_objectives = m2.component_data_objects(Objective, active=True)
m2_obj = next(m2_objectives,None)
#Assert that multistart solver does no worse than standard solver
self.assertTrue((value(m2_obj.expr)) >= (value(m_obj.expr) - .001))
del m2
def test_as_good_with_HCS_rule(self):
"""test that the high confidence stopping rule with very lenient
parameters does no worse.
"""
# initialize model with data
m = build_model()
# create ipopt solver
SolverFactory('ipopt').solve(m)
for i in range(5):
m2 = build_model()
SolverFactory('multistart').solve(
m2, iterations=-1, stopping_mass=0.99, stopping_delta=0.99)
m_objectives = m.component_data_objects(Objective, active=True)
m_obj = next(m_objectives, None)
m2_objectives = m2.component_data_objects(Objective, active=True)
m2_obj = next(m2_objectives,None)
#Assert that multistart solver does no worse than standard solver
self.assertTrue((value(m2_obj.expr)) >= (value(m_obj.expr) - .001))
del m2
def test_missing_bounds(self):
m = ConcreteModel()
m.x = Var(domain=NonNegativeReals)
m.obj = Objective(expr=m.x)
output = StringIO()
with LoggingIntercept(output, 'pyomo.contrib.multistart', logging.WARNING):
SolverFactory('multistart').solve(m)
self.assertIn("Unable to reinitialize value of unbounded "
"variable x with bounds (0, None).",
output.getvalue().strip())
def test_var_value_None(self):
m = ConcreteModel()
m.x = Var(bounds=(0, 1))
m.obj = Objective(expr=m.x)
SolverFactory('multistart').solve(m)
def test_model_infeasible(self):
m = ConcreteModel()
m.x = Var(bounds=(0, 1))
m.c = Constraint(expr=m.x >= 2)
m.o = Objective(expr=m.x)
SolverFactory('multistart').solve(m, iterations=2)
output = StringIO()
with LoggingIntercept(output, 'pyomo.contrib.multistart', logging.WARNING):
SolverFactory('multistart').solve(
m, iterations=-1, HCS_max_iterations=3)
self.assertIn("High confidence stopping rule was unable to "
"complete after 3 iterations.",
output.getvalue().strip())
def test_should_stop(self):
soln = [0] * 149
self.assertFalse(should_stop(soln, 0.5, 0.5, 0.001))
soln += [0.001]
self.assertTrue(should_stop(soln, 0.5, 0.5, 0.001))
soln = [0] * 149 + [0.01]
self.assertFalse(should_stop(soln, 0.5, 0.5, 0.001))
soln = [0] * 149 + [-0.001]
self.assertTrue(should_stop(soln, 0.5, 0.5, 0.001))
def test_multiple_obj(self):
m = ConcreteModel()
m.x = Var()
m.o = Objective(expr=m.x)
m.o2 = Objective(expr=m.x)
with self.assertRaisesRegexp(RuntimeError, "multiple active objectives"):
SolverFactory('multistart').solve(m)
def test_no_obj(self):
m = ConcreteModel()
m.x = Var()
with self.assertRaisesRegexp(RuntimeError, "no active objective"):
SolverFactory('multistart').solve(m)
def test_const_obj(self):
m = ConcreteModel()
m.x = Var()
m.o = Objective(expr = 5)
with self.assertRaisesRegexp(RuntimeError, "constant objective"):
SolverFactory('multistart').solve(m)
def build_model():
"""Simple non-convex model with many local minima"""
model = ConcreteModel()
model.x1 = Var(initialize=1, bounds=(0, 100))
model.x2 = Var(initialize=5, bounds=(5, 6))
model.x2.fix(5)
model.objtv = Objective(expr=model.x1 * sin(model.x1), sense=maximize)
return model
if __name__ == '__main__':
unittest.main()