Changeset 177
- Timestamp:
- 08/10/09 13:37:47 (7 years ago)
- Location:
- branches/alta/mystic-0.1a2
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/alta/mystic-0.1a2/examples/test_snobfit.py
r168 r177 7 7 Demonstrates: 8 8 - minimal solver interface for snobfit 9 - Use of a mystic solver without just the minimal interface 10 - Use of SnobfitSolver and SnobfitSoftSolver 9 11 10 12 These tests are adapted from the snobfit.py file. … … 13 15 from mystic.snobfit_solver import snobfit 14 16 from mystic.softConstraint import SoftConstraint, SoftConstraints 17 from mystic.tools import random_seed 15 18 import numpy 16 19 … … 25 28 return f 26 29 30 def hsf18(x): 31 f = 0.01*x[0]**2 + x[1]**2 32 return f 33 27 34 iter = 0 28 35 # plot the parameter trajectories … … 33 40 iter += 1 34 41 return 35 36 def hsf18(x):37 f = 0.01*x[0]**2 + x[1]**238 return f39 42 40 43 #--------------------------------------------------------- … … 60 63 v = -u 61 64 xglob = numpy.array([1, 1]) 62 results = snobfit(ros, x0, (u, v), seed = 1, callback=print_params,\ 63 disp=1,full_output=1) 65 random_seed(1) 66 results = snobfit(ros, x0, (u, v), callback=print_params,\ 67 disp=1,full_output=1, retuct=True) 64 68 print('optimize:', results) 65 69 … … 85 89 s.add( SoftConstraint( F=hsc18_2, Flo=0, Fhi=numpy.inf, sigma=1.25) ) 86 90 91 random_seed(1) 87 92 results = snobfit(hsf18, x0, (u, v), 88 constraint=s, full_output=1, 89 retall=1, 90 disp=1, seed = 1 91 ) 93 constraint=s, 94 #retall=1, callback=print_params, 95 disp=1, full_output=1) 92 96 print results 93 97 … … 96 100 # Rosenbrock function. 97 101 98 def test_4(): 102 def test_ros2(): 103 """Minimize the Rosenbrock function.""" 104 99 105 from mystic.tools import Sow, random_seed 106 from mystic.termination import VTRChangeOverGenerations 107 from snobfit_solver import SnobfitSolver 108 109 # Create the step and evaluation monitors, which capture progress 100 110 stepmon = Sow() 101 111 evalmon = Sow() 102 from mystic.termination import SnobfitTermination103 from mystic.snobfit_solver import SnobfitSolver104 112 113 # The initial guess x0 and bounds u,v 105 114 x0 = numpy.array([2, 3]) 106 115 u = -5.12*numpy.ones(2) … … 108 117 109 118 random_seed(1) 110 solver = SnobfitSolver(len(x0)) 119 120 # Initialize the solver 121 solver = SnobfitSolver(len(x0),7) 111 122 solver.SetStrictRanges(u, v) 112 123 solver.SetInitialPoints(x0) 113 124 solver.enable_signal_handler() 114 125 115 solver.Solve(ros,termination=SnobfitTermination(),\ 126 # Call the main Solve() function 127 solver.Solve(ros,termination=VTRChangeOverGenerations(1e-6, 0, 250),\ 116 128 EvaluationMonitor=evalmon,StepMonitor=stepmon) 117 solution = solver.Solution()118 129 130 # Get solution 119 131 x = solver.bestSolution 132 133 # Get the function value at the solution 120 134 fval = solver.bestEnergy 135 136 # Get the number of function evaluations and iterations 121 137 fcalls = len(evalmon.x) 122 138 iterations = len(stepmon.x) 123 139 124 print ('optimize:', x, fval, iterations, fcalls) 140 # Output the results 141 print "Solution: ", x 142 print "Function value at solution: ", fval 143 print "Iterations: ", iterations 144 print "Function calls: ", fcalls 125 145 126 146 #------------------------------------------------------------------ … … 128 148 if __name__ == "__main__": 129 149 #test_1() 130 test_ros() 131 #test_hsf18() 132 #test_ 4()150 test_ros() # Uses callback 151 #test_hsf18() # Uses SnobfitSoftSolver 152 #test_ros2() -
branches/alta/mystic-0.1a2/mystic/snobfit_solver.py
r175 r177 83 83 """Snobfit optimization. """ 84 84 85 def __init__(self, dim, **kwds):85 def __init__(self, dim, NP=None): 86 86 """ 87 87 Required input: … … 94 94 than parameters. Default is dn = 5 and NP adjusted accordingly. 95 95 """ 96 if kwds.has_key("NP"):96 if NP: 97 97 NP = NP 98 98 else: … … 155 155 self.func = wrap_bounds(self.func, self._strictMin, self._strictMax) 156 156 157 # Fix for when bounds were set after initial points: reinitialize157 # Re-initialize population if bounds were set after initial points. 158 158 # This may give different results from if SetStrictRanges was called first 159 159 x0 = self.population[0] … … 167 167 x0 = self.population[0] 168 168 self.x = self.population 169 170 self.fglob = 0171 169 172 170 # The number of safeguarded nearest neighbors … … 270 268 return 271 269 272 # Overwrite abstract_solver's SetRandomInitialPoints()270 # Overwrite AbstractSolver's SetRandomInitialPoints() 273 271 274 # If SetStrictRange was called first, use those bounds.272 # If SetStrictRanges was called first, use those bounds. 275 273 # If SetInitialPoints was called first, temporarily use defaults, 276 # and Solve() will re-initialize with user-specified bounds274 # and Solve() will re-initialize with the bounds from SetStrictRanges 277 275 def SetRandomInitialPoints(self,min=None,max=None): 278 276 if self._useStrictRange: … … 287 285 if min is None: min = [-1e3]*self.nDim 288 286 if max is None: max = [1e3]*self.nDim 289 self.population = self._setInitRecommendedEvalPoints(min,max) 290 print self.population 291 # def SetInitialPoints(self,x0): 292 # self.population = self._setInitRecommendedEvalPoints(self._strictMin,self._strictMax) 293 # self.population[0] = x0 287 self.population = self._setInitRecommendedEvalPoints(min,max) 294 288 295 289 ############################################################################ 296 290 # Helper methods for SnobfitSolver 297 291 298 def _setInitRecommendedEvalPoints(self, min,max):292 def _setInitRecommendedEvalPoints(self, min, max): 299 293 """ 300 294 Snobfit's initialization of the population … … 2367 2361 self.func = wrap_bounds(self.func, self._strictMin, self._strictMax) 2368 2362 2369 # Fix for whenbounds were set after initial points.2363 # Re-initialize population if bounds were set after initial points. 2370 2364 x0 = self.population[0] 2371 2365 self.SetInitialPoints(x0) … … 2681 2675 1 : 'Maximum number of function evaluations.' 2682 2676 2 : 'Maximum number of iterations.' 2683 allvecs -- list - a list of solutions at each iteration 2677 allvecs -- list - Returned if retall is true. 2678 A list of solutions at each iteration. 2684 2679 uncertainty -- list - Returned if retuct is True. The uncertainty of 2685 2680 the fitting parameters. … … 2687 2682 2688 2683 from mystic.tools import Sow 2689 from mystic.termination import FglobTermination, VTRChangeOverGenerations2690 2691 2684 stepmon = Sow() 2692 2685 evalmon = Sow() 2693 2686 2687 # Use SnobfitSoftSolver if there are SoftConstraints. 2688 # Pass npop to the solver if it was specified. If it was not, 2689 # let the solver use the default. 2694 2690 if constraint is None: 2695 2691 if npop: … … 2704 2700 2705 2701 if fglob != 0: 2706 termination = FglobTermination(fglob=fglob, tolerance=rtol, generations=nstop) 2702 from mystic.termination import FglobTermination 2703 termination = FglobTermination(fglob, rtol, nstop) 2707 2704 else: 2708 termination = VTRChangeOverGenerations(costtolerance=ftol, gentolerance=0,\2709 generations=nstop)2705 from mystic.termination import VTRChangeOverGenerations 2706 termination = VTRChangeOverGenerations(ftol, 0, nstop) 2710 2707 2711 2708 # Bounds are required
Note: See TracChangeset
for help on using the changeset viewer.