Changeset 473
- Timestamp:
- 07/07/11 08:44:38 (5 years ago)
- Location:
- mystic
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/examples/Make.mm
r235 r473 79 79 test_rosenbrock2.py \ 80 80 test_rosenbrock3.py \ 81 test_rosenbrock3b.py \ 81 82 ## test_twistedgaussian.py \ 82 83 ## test_twistedgaussian2.py \ -
mystic/examples/test_rosenbrock3.py
r471 r473 41 41 #solver.SetEvaluationLimits(maxiter=13) 42 42 solver.SetGenerationMonitor(stepmon) 43 solver.SetConstraints(constrain) 43 44 solver.enable_signal_handler() 44 solver.Solve(rosen, NCOG(tolerance=1e-4), disp=1 , constraints=constrain)45 solver.Solve(rosen, NCOG(tolerance=1e-4), disp=1) 45 46 print solver.bestSolution 46 47 #print "Current function value: %s" % solver.bestEnergy -
mystic/mystic/abstract_solver.py
r470 r473 134 134 self._evalmon = Null 135 135 136 self._constraints = lambda x: x 137 #self._penalty = lambda x: x 138 136 139 import mystic.termination 137 140 self._EARLYEXIT = mystic.termination.EARLYEXIT … … 145 148 """get the number of function calls""" 146 149 return self._fcalls[0] 150 151 def SetConstraints(self, constraints): 152 """apply a constraints function to the optimization 153 154 input:: 155 - a constraints function of the form: xk' = constraints(xk), 156 where xk is the current parameter vector. Ideally, this function 157 is constructed so the parameter vector it passes to the cost function 158 will satisfy the desired (i.e. encoded) constraints.""" 159 if not constraints: 160 self._constraints = lambda x: x 161 elif not callable(constraints): 162 raise TypeError, "'%s' is not a callable function" % constraints 163 else: #XXX: check for format: x' = constraints(x) ? 164 self._constraints = constraints 165 return 147 166 148 167 def SetGenerationMonitor(self, monitor): -
mystic/mystic/differential_evolution.py
r470 r473 241 241 if kwds.has_key('StepMonitor'): \ 242 242 self._stepmon = kwds['StepMonitor'] 243 if kwds.has_key('constraints'): \ 244 self._constraints = kwds['constraints'] 245 if not self._constraints: self._constraints = lambda x: x 243 246 #------------------------------------------------------------- 244 247 … … 266 269 # generate trialSolution (within valid range) 267 270 self.trialSolution[:] = self.population[candidate][:] 271 # -apply constraints- 272 self.trialSolution[:] = self._constraints(self.trialSolution[:]) 273 # -end- 268 274 trialEnergy = costfunction(self.trialSolution) 269 275 … … 296 302 # generate trialSolution (within valid range) 297 303 strategy(self, candidate) 304 # -apply constraints- 305 self.trialSolution[:] = self._constraints(self.trialSolution[:]) 306 # -end- 298 307 trialEnergy = costfunction(self.trialSolution) 299 308 … … 406 415 iteration. It is called as callback(xk), where xk is 407 416 the current parameter vector. [default = None] 408 constraints -- an optional user-supplied function to call just409 prior to each function evaluation. It is called as410 xk' = constraints(xk), where xk is the current parameter vector.411 Ideally, this function is used to ensure each function evaulation412 encounters a parameter set that satisfies user-supplied constraints.413 417 disp -- non-zero to print convergence messages. 414 418 """ … … 426 430 if kwds.has_key('callback'): callback = kwds['callback'] 427 431 if kwds.has_key('disp'): disp = kwds['disp'] 428 #XXX:[HACK] accept constraints429 constraints = lambda x: x430 if kwds.has_key('constraints'): constraints = kwds['constraints']431 if not constraints: constraints = lambda x: x432 #XXX:[HACK] -end-433 432 # backward compatibility 434 433 if kwds.has_key('EvaluationMonitor'): \ … … 436 435 if kwds.has_key('StepMonitor'): \ 437 436 self._stepmon = kwds['StepMonitor'] 437 if kwds.has_key('constraints'): \ 438 self._constraints = kwds['constraints'] 439 if not self._constraints: self._constraints = lambda x: x 438 440 #------------------------------------------------------------- 439 441 … … 467 469 # generate trialSolution (within valid range) 468 470 self.trialSolution[:] = self.population[candidate][:] 469 # XXX:[HACK] apply constraints470 self.trialSolution[:] = constraints(self.trialSolution[:])471 # XXX:[HACK]-end-471 # -apply constraints- 472 self.trialSolution[:] = self._constraints(self.trialSolution[:]) 473 # -end- 472 474 trialPop[candidate][:] = self.trialSolution[:] 473 475 … … 508 510 # generate trialSolution (within valid range) 509 511 strategy(self, candidate) 510 # XXX:[HACK] apply constraints511 self.trialSolution[:] = constraints(self.trialSolution[:])512 # XXX:[HACK]-end-512 # -apply constraints- 513 self.trialSolution[:] = self._constraints(self.trialSolution[:]) 514 # -end- 513 515 trialPop[candidate][:] = self.trialSolution[:] 514 516 -
mystic/mystic/nested.py
r470 r473 89 89 if kwds.has_key('StepMonitor'): \ 90 90 self._stepmon = kwds['StepMonitor'] 91 #if kwds.has_key('constraints'): \ 92 # self._constraints = kwds['constraints'] 93 #if not self._constraints: self._constraints = lambda x: x 91 94 #------------------------------------------------------------- 92 95 … … 152 155 solver.SetStrictRanges(min=%s, max=%s) 153 156 """ % (str(lower), str(upper)) 157 # local_opt += """\n 158 # solver.SetConstraints(%s) 159 #""" % (self._solver._constraints) #FIXME: needs to take a string 154 160 local_opt += """\n 155 161 solver.SetEvaluationLimits(%s, %s) … … 273 279 if kwds.has_key('StepMonitor'): \ 274 280 self._stepmon = kwds['StepMonitor'] 281 #if kwds.has_key('constraints'): \ 282 # self._constraints = kwds['constraints'] 283 #if not self._constraints: self._constraints = lambda x: x 275 284 #------------------------------------------------------------- 276 285 … … 329 338 solver.SetStrictRanges(min=%s, max=%s) 330 339 """ % (str(lower), str(upper)) 340 # local_opt += """\n 341 # solver.SetConstraints(%s) 342 #""" % (self._solver._constraints) #FIXME: needs to take a string 331 343 local_opt += """\n 332 344 solver.SetEvaluationLimits(%s, %s) -
mystic/mystic/scipy_optimize.py
r470 r473 162 162 callback=None #user-supplied function, called after each step 163 163 radius=0.05 #percentage change for initial simplex values 164 constraints = lambda x: x # Constraints function to be imposed165 164 if kwds.has_key('callback'): callback = kwds['callback'] 166 165 if kwds.has_key('disp'): disp = kwds['disp'] 167 166 if kwds.has_key('radius'): radius = kwds['radius'] 168 if kwds.has_key('constraints'): constraints = kwds['constraints']169 if not constraints: constraints = lambda x: x170 167 # backward compatibility 171 168 if kwds.has_key('EvaluationMonitor'): \ … … 173 170 if kwds.has_key('StepMonitor'): \ 174 171 self._stepmon = kwds['StepMonitor'] 172 if kwds.has_key('constraints'): \ 173 self._constraints = kwds['constraints'] 174 if not self._constraints: self._constraints = lambda x: x 175 175 #------------------------------------------------------------- 176 176 … … 189 189 190 190 # wrap constraints function 191 func = wrap_nested(func, constraints)191 func = wrap_nested(func, self._constraints) 192 192 193 193 id = self.id 194 194 x0 = asfarray(x0).flatten() 195 x0 = asfarray( constraints(x0))195 x0 = asfarray(self._constraints(x0)) 196 196 N = len(x0) #XXX: this should be equal to self.nDim 197 197 rank = len(x0.shape) … … 245 245 246 246 # apply constraints #XXX: is this the only appropriate place??? 247 sim[0] = asfarray( constraints(sim[0]))247 sim[0] = asfarray(self._constraints(sim[0])) 248 248 249 249 xbar = numpy.add.reduce(sim[:-1],0) / N … … 501 501 callback=None #user-supplied function, called after each step 502 502 xtol=1e-4 #line-search error tolerance 503 constraints = lambda x: x # Constraints function to be imposed504 503 if kwds.has_key('callback'): callback = kwds['callback'] 505 504 if kwds.has_key('direc'): direc = kwds['direc'] #XXX: best interface? 506 505 if kwds.has_key('xtol'): xtol = kwds['xtol'] 507 506 if kwds.has_key('disp'): disp = kwds['disp'] 508 if kwds.has_key('constraints'): constraints = kwds['constraints']509 if not constraints: constraints = lambda x: x510 507 # backward compatibility 511 508 if kwds.has_key('EvaluationMonitor'): \ … … 513 510 if kwds.has_key('StepMonitor'): \ 514 511 self._stepmon = kwds['StepMonitor'] 512 if kwds.has_key('constraints'): \ 513 self._constraints = kwds['constraints'] 514 if not self._constraints: self._constraints = lambda x: x 515 515 #------------------------------------------------------------- 516 516 … … 529 529 530 530 # wrap constraints function 531 func = wrap_nested(func, constraints)531 func = wrap_nested(func, self._constraints) 532 532 533 533 id = self.id 534 534 x = asfarray(x0).flatten() 535 x = asfarray( constraints(x))535 x = asfarray(self._constraints(x)) 536 536 if retall: 537 537 allvecs = [x] … … 577 577 578 578 # apply constraints 579 x = asfarray( constraints(x))579 x = asfarray(self._constraints(x)) 580 580 581 581 iter += 1 … … 607 607 direc[-1] = direc1 608 608 609 # x = asfarray( constraints(x))609 # x = asfarray(self._constraints(x)) 610 610 611 611 self.energy_history[-1] = fval #...update to 'best' energy -
mystic/tests/solver_test_suite.py
r470 r473 1 1 #!/usr/bin/python 2 """A test suite for Mystic solvers and constraints.2 """A test suite for Mystic solvers. 3 3 Note: VTR termination with default tolerance shouldn't work for functions 4 4 whose value at the minimum is negative!
Note: See TracChangeset
for help on using the changeset viewer.