1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) |
---|
4 | # Copyright (c) 1997-2016 California Institute of Technology. |
---|
5 | # License: 3-clause BSD. The full license text is available at: |
---|
6 | # - http://mmckerns.github.io/project/mystic/browser/mystic/LICENSE |
---|
7 | |
---|
8 | from mystic.models import rosen |
---|
9 | from mystic.solvers import * |
---|
10 | from mystic.termination import VTRChangeOverGeneration |
---|
11 | from mystic.monitors import VerboseMonitor, Monitor |
---|
12 | from mystic.tools import random_seed |
---|
13 | random_seed(123) |
---|
14 | lb, ub = [-100.]*3, [100]*3 |
---|
15 | interval = None |
---|
16 | |
---|
17 | if interval: |
---|
18 | _stepmon = VerboseMonitor(interval) |
---|
19 | else: |
---|
20 | _stepmon = Monitor() |
---|
21 | _term = VTRChangeOverGeneration(generations=200) |
---|
22 | _solver = DifferentialEvolutionSolver(3, 20)#40) |
---|
23 | _solver.SetRandomInitialPoints(lb,ub) |
---|
24 | _solver.SetStrictRanges(lb,ub) |
---|
25 | _solver.SetTermination(_term) |
---|
26 | _solver.SetGenerationMonitor(_stepmon) |
---|
27 | _solver.SetEvaluationLimits(100, 1000) |
---|
28 | _solver.Solve(rosen) |
---|
29 | |
---|
30 | _energy = _solver.bestEnergy |
---|
31 | _solution = _solver.bestSolution |
---|
32 | _population = _solver.population |
---|
33 | |
---|
34 | _solver.SetEvaluationLimits(10000, 100000) |
---|
35 | _solver.Solve() |
---|
36 | |
---|
37 | _energy = _solver.bestEnergy |
---|
38 | _solution = _solver.bestSolution |
---|
39 | _population = _solver.population |
---|
40 | |
---|
41 | # again, with same method order |
---|
42 | random_seed(123) |
---|
43 | if interval: |
---|
44 | stepmon = VerboseMonitor(interval) |
---|
45 | else: |
---|
46 | stepmon = Monitor() |
---|
47 | term = VTRChangeOverGeneration(generations=200) |
---|
48 | solver = DifferentialEvolutionSolver(3, 20)#40) |
---|
49 | solver.SetRandomInitialPoints(lb,ub) |
---|
50 | solver.SetStrictRanges(lb,ub) |
---|
51 | solver.SetTermination(term) |
---|
52 | solver.SetGenerationMonitor(stepmon) |
---|
53 | solver.SetEvaluationLimits(100, 1000) |
---|
54 | solver.Solve(rosen) |
---|
55 | |
---|
56 | energy = solver.bestEnergy |
---|
57 | solution = solver.bestSolution |
---|
58 | population = solver.population |
---|
59 | |
---|
60 | solver.SetEvaluationLimits(10000, 100000) |
---|
61 | solver.Solve() |
---|
62 | |
---|
63 | energy = solver.bestEnergy |
---|
64 | solution = solver.bestSolution |
---|
65 | population = solver.population |
---|
66 | |
---|
67 | # compare results |
---|
68 | assert energy == _energy |
---|
69 | assert all(solution == _solution) |
---|
70 | |
---|
71 | # again, with different method order |
---|
72 | random_seed(123) |
---|
73 | if interval: |
---|
74 | stepmon = VerboseMonitor(interval) |
---|
75 | else: |
---|
76 | stepmon = Monitor() |
---|
77 | term = VTRChangeOverGeneration(generations=200) |
---|
78 | solver = DifferentialEvolutionSolver(3, 20)#40) |
---|
79 | solver.SetRandomInitialPoints(lb,ub) |
---|
80 | solver.SetGenerationMonitor(stepmon) |
---|
81 | solver.SetEvaluationLimits(100, 1000) |
---|
82 | solver.SetTermination(term) |
---|
83 | solver.SetStrictRanges(lb,ub) |
---|
84 | solver.Solve(rosen) |
---|
85 | |
---|
86 | energy = solver.bestEnergy |
---|
87 | solution = solver.bestSolution |
---|
88 | population = solver.population |
---|
89 | |
---|
90 | solver.SetEvaluationLimits(10000, 100000) |
---|
91 | solver.Solve() |
---|
92 | |
---|
93 | energy = solver.bestEnergy |
---|
94 | solution = solver.bestSolution |
---|
95 | population = solver.population |
---|
96 | |
---|
97 | # compare results |
---|
98 | assert energy == _energy |
---|
99 | assert all(solution == _solution) |
---|
100 | |
---|
101 | # again, but focused on method order for population |
---|
102 | random_seed(123) |
---|
103 | if interval: |
---|
104 | stepmon = VerboseMonitor(interval) |
---|
105 | else: |
---|
106 | stepmon = Monitor() |
---|
107 | term = VTRChangeOverGeneration(generations=200) |
---|
108 | solver = DifferentialEvolutionSolver(3, 20)#40) |
---|
109 | solver.SetGenerationMonitor(stepmon) |
---|
110 | solver.SetEvaluationLimits(100, 1000) |
---|
111 | solver.SetTermination(term) |
---|
112 | solver.SetStrictRanges(lb,ub) |
---|
113 | solver.SetRandomInitialPoints(lb,ub) |
---|
114 | solver.Solve(rosen) |
---|
115 | |
---|
116 | energy = solver.bestEnergy |
---|
117 | solution = solver.bestSolution |
---|
118 | population = solver.population |
---|
119 | |
---|
120 | solver.SetEvaluationLimits(10000, 100000) |
---|
121 | solver.Solve() |
---|
122 | |
---|
123 | energy = solver.bestEnergy |
---|
124 | solution = solver.bestSolution |
---|
125 | population = solver.population |
---|
126 | |
---|
127 | # compare results |
---|
128 | assert energy == _energy |
---|
129 | assert all(solution == _solution) |
---|
130 | |
---|
131 | # start over... this time focus on methods called on the restart |
---|
132 | interval = None |
---|
133 | random_seed(213) |
---|
134 | if interval: |
---|
135 | _stepmon2 = VerboseMonitor(interval) |
---|
136 | else: |
---|
137 | _stepmon2 = Monitor() |
---|
138 | _term2 = VTRChangeOverGeneration(generations=2000) |
---|
139 | _solver2 = DifferentialEvolutionSolver(3, 20)#40) |
---|
140 | _solver2.SetEvaluationLimits(100, 1000) |
---|
141 | _solver2.SetTermination(_term2) |
---|
142 | _solver2.SetRandomInitialPoints(lb,ub) |
---|
143 | _solver2.Solve(rosen) |
---|
144 | |
---|
145 | _energy2 = _solver2.bestEnergy |
---|
146 | _solution2 = _solver2.bestSolution |
---|
147 | _population2 = _solver2.population |
---|
148 | |
---|
149 | _term2 = VTRChangeOverGeneration(generations=200) |
---|
150 | _solver2.SetStrictRanges(lb,ub) |
---|
151 | _solver2.SetEvaluationLimits(new=True) |
---|
152 | _solver2.SetGenerationMonitor(_stepmon2) |
---|
153 | _solver2.SetTermination(_term2) |
---|
154 | _solver2.Solve() |
---|
155 | |
---|
156 | _energy2 = _solver2.bestEnergy |
---|
157 | _solution2 = _solver2.bestSolution |
---|
158 | _population2 = _solver2.population |
---|
159 | |
---|
160 | # again, but swap method order for restart |
---|
161 | random_seed(213) |
---|
162 | if interval: |
---|
163 | stepmon2 = VerboseMonitor(interval) |
---|
164 | else: |
---|
165 | stepmon2 = Monitor() |
---|
166 | term2 = VTRChangeOverGeneration(generations=2000) |
---|
167 | solver2 = DifferentialEvolutionSolver(3, 20)#40) |
---|
168 | solver2.SetEvaluationLimits(100, 1000) |
---|
169 | solver2.SetTermination(term2) |
---|
170 | solver2.SetRandomInitialPoints(lb,ub) |
---|
171 | solver2.Solve(rosen) |
---|
172 | |
---|
173 | energy2 = solver2.bestEnergy |
---|
174 | solution2 = solver2.bestSolution |
---|
175 | population2 = solver2.population |
---|
176 | |
---|
177 | term2 = VTRChangeOverGeneration(generations=200) |
---|
178 | solver2.SetTermination(term2) |
---|
179 | solver2.SetGenerationMonitor(stepmon2) |
---|
180 | solver2.SetEvaluationLimits(new=True) |
---|
181 | solver2.SetStrictRanges(lb,ub) |
---|
182 | solver2.Solve() |
---|
183 | |
---|
184 | energy2 = solver2.bestEnergy |
---|
185 | solution2 = solver2.bestSolution |
---|
186 | population2 = solver2.population |
---|
187 | |
---|
188 | # compare results |
---|
189 | assert energy2 == _energy2 |
---|
190 | assert all(solution2 == _solution2) |
---|
191 | |
---|
192 | # start over... and change so initialize population in the restart |
---|
193 | interval = None |
---|
194 | random_seed(123) |
---|
195 | if interval: |
---|
196 | _stepmon3 = VerboseMonitor(interval) |
---|
197 | else: |
---|
198 | _stepmon3 = Monitor() |
---|
199 | _term3 = VTRChangeOverGeneration(generations=2000) |
---|
200 | _solver3 = DifferentialEvolutionSolver(3, 20)#40) |
---|
201 | _solver3.SetEvaluationLimits(100, 1000) |
---|
202 | _solver3.SetTermination(_term3) |
---|
203 | _solver3.SetGenerationMonitor(_stepmon3) |
---|
204 | _solver3.SetRandomInitialPoints(lb,ub) |
---|
205 | _solver3.Solve(rosen) |
---|
206 | |
---|
207 | _energy3 = _solver3.bestEnergy |
---|
208 | _solution3 = _solver3.bestSolution |
---|
209 | _population3 = _solver3.population |
---|
210 | |
---|
211 | _lb, _ub = [-10.]*3, [10]*3 |
---|
212 | _term3 = VTRChangeOverGeneration(generations=200) |
---|
213 | _solver3.SetRandomInitialPoints(_lb,_ub) #FIXME: pretty much causes a flatline |
---|
214 | _solver3.SetStrictRanges(lb,ub) # regardless of _lb,_ub value |
---|
215 | _solver3.SetEvaluationLimits(200, 2000, new=True)# check if pop is uniform |
---|
216 | _solver3.SetTermination(_term3) |
---|
217 | _solver3.Solve() |
---|
218 | |
---|
219 | _energy3 = _solver3.bestEnergy |
---|
220 | _solution3 = _solver3.bestSolution |
---|
221 | _population3 = _solver3.population |
---|
222 | # FIXME: when population becomes uniform, the solver will get stuck |
---|
223 | # would be good to be able to inject some randomness into a restart |
---|
224 | |
---|
225 | # again, but swap method order for restart |
---|
226 | random_seed(123) |
---|
227 | if interval: |
---|
228 | stepmon3 = VerboseMonitor(interval) |
---|
229 | else: |
---|
230 | stepmon3 = Monitor() |
---|
231 | term3 = VTRChangeOverGeneration(generations=2000) |
---|
232 | solver3 = DifferentialEvolutionSolver(3, 20)#40) |
---|
233 | solver3.SetEvaluationLimits(100, 1000) |
---|
234 | solver3.SetTermination(term3) |
---|
235 | solver3.SetGenerationMonitor(stepmon3) |
---|
236 | solver3.SetRandomInitialPoints(lb,ub) |
---|
237 | solver3.Solve(rosen) |
---|
238 | |
---|
239 | energy3 = solver3.bestEnergy |
---|
240 | solution3 = solver3.bestSolution |
---|
241 | population3 = solver3.population |
---|
242 | |
---|
243 | term3 = VTRChangeOverGeneration(generations=200) |
---|
244 | solver3.SetTermination(term3) |
---|
245 | solver3.SetEvaluationLimits(200, 2000, new=True) |
---|
246 | solver3.SetStrictRanges(lb,ub) |
---|
247 | solver3.SetRandomInitialPoints(_lb,_ub) |
---|
248 | solver3.Solve() |
---|
249 | |
---|
250 | energy3 = solver3.bestEnergy |
---|
251 | solution3 = solver3.bestSolution |
---|
252 | population3 = solver3.population |
---|
253 | |
---|
254 | # compare results |
---|
255 | assert energy3 == _energy3 |
---|
256 | assert all(solution3 == _solution3) |
---|
257 | |
---|
258 | # TODO: start over... but work with Step instead of Solve |
---|
259 | |
---|
260 | # EOF |
---|