source: mystic/tests/test_method_order.py @ 855

Revision 855, 7.0 KB checked in by mmckerns, 5 months ago (diff)

updated copyright to 2016

Line 
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
8from mystic.models import rosen
9from mystic.solvers import *
10from mystic.termination import VTRChangeOverGeneration
11from mystic.monitors import VerboseMonitor, Monitor
12from mystic.tools import random_seed
13random_seed(123)
14lb, ub = [-100.]*3, [100]*3
15interval = None
16
17if interval:
18  _stepmon = VerboseMonitor(interval)
19else:
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
42random_seed(123)
43if interval:
44  stepmon = VerboseMonitor(interval)
45else:
46  stepmon = Monitor()
47term = VTRChangeOverGeneration(generations=200)
48solver = DifferentialEvolutionSolver(3, 20)#40)
49solver.SetRandomInitialPoints(lb,ub)
50solver.SetStrictRanges(lb,ub)
51solver.SetTermination(term)
52solver.SetGenerationMonitor(stepmon)
53solver.SetEvaluationLimits(100, 1000)
54solver.Solve(rosen)
55
56energy = solver.bestEnergy
57solution =  solver.bestSolution
58population = solver.population
59
60solver.SetEvaluationLimits(10000, 100000)
61solver.Solve()
62
63energy = solver.bestEnergy
64solution = solver.bestSolution
65population = solver.population
66
67# compare results
68assert energy == _energy
69assert all(solution == _solution)
70
71# again, with different method order
72random_seed(123)
73if interval:
74  stepmon = VerboseMonitor(interval)
75else:
76  stepmon = Monitor()
77term = VTRChangeOverGeneration(generations=200)
78solver = DifferentialEvolutionSolver(3, 20)#40)
79solver.SetRandomInitialPoints(lb,ub)
80solver.SetGenerationMonitor(stepmon)
81solver.SetEvaluationLimits(100, 1000)
82solver.SetTermination(term)
83solver.SetStrictRanges(lb,ub)
84solver.Solve(rosen)
85
86energy = solver.bestEnergy
87solution =  solver.bestSolution
88population = solver.population
89
90solver.SetEvaluationLimits(10000, 100000)
91solver.Solve()
92
93energy = solver.bestEnergy
94solution = solver.bestSolution
95population = solver.population
96
97# compare results
98assert energy == _energy
99assert all(solution == _solution)
100
101# again, but focused on method order for population
102random_seed(123)
103if interval:
104  stepmon = VerboseMonitor(interval)
105else:
106  stepmon = Monitor()
107term = VTRChangeOverGeneration(generations=200)
108solver = DifferentialEvolutionSolver(3, 20)#40)
109solver.SetGenerationMonitor(stepmon)
110solver.SetEvaluationLimits(100, 1000)
111solver.SetTermination(term)
112solver.SetStrictRanges(lb,ub)
113solver.SetRandomInitialPoints(lb,ub)
114solver.Solve(rosen)
115
116energy = solver.bestEnergy
117solution =  solver.bestSolution
118population = solver.population
119
120solver.SetEvaluationLimits(10000, 100000)
121solver.Solve()
122
123energy = solver.bestEnergy
124solution = solver.bestSolution
125population = solver.population
126
127# compare results
128assert energy == _energy
129assert all(solution == _solution)
130
131# start over... this time focus on methods called on the restart
132interval = None
133random_seed(213)
134if interval:
135  _stepmon2 = VerboseMonitor(interval)
136else:
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
161random_seed(213)
162if interval:
163  stepmon2 = VerboseMonitor(interval)
164else:
165  stepmon2 = Monitor()
166term2 = VTRChangeOverGeneration(generations=2000)
167solver2 = DifferentialEvolutionSolver(3, 20)#40)
168solver2.SetEvaluationLimits(100, 1000)
169solver2.SetTermination(term2)
170solver2.SetRandomInitialPoints(lb,ub)
171solver2.Solve(rosen)
172
173energy2 = solver2.bestEnergy
174solution2 =  solver2.bestSolution
175population2 = solver2.population
176
177term2 = VTRChangeOverGeneration(generations=200)
178solver2.SetTermination(term2)
179solver2.SetGenerationMonitor(stepmon2)
180solver2.SetEvaluationLimits(new=True)
181solver2.SetStrictRanges(lb,ub)
182solver2.Solve()
183
184energy2 = solver2.bestEnergy
185solution2 = solver2.bestSolution
186population2 = solver2.population
187
188# compare results
189assert energy2 == _energy2
190assert all(solution2 == _solution2)
191
192# start over... and change so initialize population in the restart
193interval = None
194random_seed(123)
195if interval:
196  _stepmon3 = VerboseMonitor(interval)
197else:
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
226random_seed(123)
227if interval:
228  stepmon3 = VerboseMonitor(interval)
229else:
230  stepmon3 = Monitor()
231term3 = VTRChangeOverGeneration(generations=2000)
232solver3 = DifferentialEvolutionSolver(3, 20)#40)
233solver3.SetEvaluationLimits(100, 1000)
234solver3.SetTermination(term3)
235solver3.SetGenerationMonitor(stepmon3)
236solver3.SetRandomInitialPoints(lb,ub)
237solver3.Solve(rosen)
238
239energy3 = solver3.bestEnergy
240solution3 =  solver3.bestSolution
241population3 = solver3.population
242
243term3 = VTRChangeOverGeneration(generations=200)
244solver3.SetTermination(term3)
245solver3.SetEvaluationLimits(200, 2000, new=True)
246solver3.SetStrictRanges(lb,ub)
247solver3.SetRandomInitialPoints(_lb,_ub)
248solver3.Solve()
249
250energy3 = solver3.bestEnergy
251solution3 = solver3.bestSolution
252population3 = solver3.population
253
254# compare results
255assert energy3 == _energy3
256assert all(solution3 == _solution3)
257
258# TODO: start over... but work with Step instead of Solve
259
260# EOF
Note: See TracBrowser for help on using the repository browser.