Changeset 373 for branches/alta/mystic-0.2a1/test_constraints_range.py
- Timestamp:
- 08/04/10 16:07:50 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/alta/mystic-0.2a1/test_constraints_range.py
r360 r373 9 9 10 10 Mathematica: x -> -0.61479, y -> 0.38521 11 12 OpenOpt Code and Results: 13 ------------------------- 14 from openopt import NLP, GLP 15 from math import * 16 from mystic.models import rosen as f 17 def c(x): 18 return -(max(x) - min(x) - 1.) 19 #x0 = [0., 1.75] 20 x0 = [0., 0.] 21 22 problem = GLP(f, x0, c=c, lb=[-1., -1.], ub=[3., 3.], population=40, 23 baseVectorStrategy='random', searchDirectionStrategy='random') 24 soln = problem.solve('de') 25 26 #problem = NLP(f, x0, c=c) 27 #soln = problem.solve('ralg') #[-0.61778501 0.38221429] 28 #soln = problem.solve('scipy_slsqp') #[-0.61478988 0.38521012] also, very fast! 29 print soln.xf 30 31 slsqp is great with a feasible x0, but fails with infeasible. ralg also does 32 poorly with infeasible x0. de does ok with any x0, as long as lb and ub is 33 narrow enough. 11 34 """ 12 35 … … 36 59 #x0 = [0., 0.] # Another infeasible starting point. 37 60 #x0 = [0., -0.5] # Difficult. Considerably closer to local min than global min 61 #x0 = [1.5, -0.5] # Even more difficult. Feasible, but in a disjoint feasible 62 # region from the global min. 38 63 39 64 constraints_string = """ … … 57 82 from mystic.termination import VTR 58 83 from mystic.termination import CandidateRelativeTolerance as CRT 84 from mystic.termination import ChangeOverGeneration as COG 59 85 from mystic.strategy import Rand1Bin 60 86 solver = DifferentialEvolutionSolver(ndim, npop) … … 64 90 term = VTR() 65 91 #term = CRT() 66 solver.Solve(costfunc, term, constraints=constraints_string, \ 67 constraints_method='penalty') 92 #term = COG() 93 solver.Solve(costfunc, term, constraints=constraints_string, \ 94 constraints_method='penalty', penalty=1e6, strategy=Rand1Bin) 68 95 soln = solver.Solution() 69 96 … … 282 309 time_before = time.time() 283 310 284 from differential_evolution import DifferentialEvolutionSolver311 from mystic.differential_evolution import DifferentialEvolutionSolver 285 312 from mystic.termination import VTR 286 313 from mystic.termination import CandidateRelativeTolerance as CRT … … 293 320 #term = CRT() 294 321 solver.Solve(costfunc, term, constraints=constraints_string, \ 295 constraints_method='auglag', rh=1e3, rg=1e3)322 constraints_method='auglag', strategy=Rand1Bin, rh=1e3, rg=1e3) 296 323 soln = solver.Solution() 297 324 … … 315 342 time_before = time.time() 316 343 317 from differential_evolution import DifferentialEvolutionSolver2 318 from mystic.termination import VTR 344 from mystic.differential_evolution import DifferentialEvolutionSolver2 345 from mystic.termination import VTR 346 from mystic.termination import CandidateRelativeTolerance as CRT 319 347 from mystic.strategy import Rand1Bin 320 348 solver = DifferentialEvolutionSolver2(ndim, npop) 321 322 solver. SetInitialPoints(x0)323 solver.enable_signal_handler()324 term = VTR()325 solver.Solve(costfunc, term, constraints=constraints_string, \ 326 constraints_method='auglag', rg=1e3, rh=1e3)#, strategy=Rand1Bin)327 soln = solver.Solution() 328 329 time_elapsed = time.time() - time_before 330 331 print "soln=", soln 332 print "f value =", costfunc(soln) 333 print "Time elapsed=", time_elapsed 334 335 from constraint_tools import verify_constraints_satisfied 336 print verify_constraints_satisfied(constraints_string, soln , disp=False)349 solver.SetInitialPoints(x0) 350 solver.enable_signal_handler() 351 term = VTR() 352 #term = CRT() 353 solver.Solve(costfunc, term, constraints=constraints_string, \ 354 constraints_method='auglag', strategy=Rand1Bin, rg=1e3, rh=1e3) 355 soln = solver.Solution() 356 357 time_elapsed = time.time() - time_before 358 359 print "soln=", soln 360 print "f value =", costfunc(soln) 361 print "Time elapsed=", time_elapsed 362 363 from constraint_tools import verify_constraints_satisfied 364 print verify_constraints_satisfied(constraints_string, soln)#, disp=False) 337 365 338 366 def test_neldermead_auglag(): … … 714 742 # with x0=[0., 0.], BFGS, NCG give non-minimum answers with penalty. 715 743 # with x0=[-0.5, -0.5] and penalty method, local optimizers can only find 716 # local minima. 744 # local minima. With [0., -0.5], all penalty method are stuck in local min. 717 745 test_diffev_penalty() 718 746 test_diffev2_penalty() … … 726 754 # infeasible region and end up at a local minimum. See 727 755 # http://ugcs.caltech.edu/~altafang/constrained_rosenbrock.html 756 # With [0., -0.5], all but diffev, diffev2, and bfgs get stuck in local min. 757 # With [1.5, -0.5], diffev works, and diffev2 works only with high 758 # CrossProbability and ScalingFactor. 728 759 test_diffev_auglag() 729 test_diffev2_auglag() # strange bug: with x0=[-0.5, -0.5] ,760 test_diffev2_auglag() # strange bug: with x0=[-0.5, -0.5] and [0., -0.5], 730 761 # get different answer depending on 731 762 # whether previous line is commented out or not. 763 # The effect also exists if you switch their orders. 732 764 test_neldermead_auglag() 733 765 test_powelldirectional_auglag() # needs rg and rh = 1e4 (for feasible x0)
Note: See TracChangeset
for help on using the changeset viewer.