Ignore:
Timestamp:
08/04/10 16:07:50 (6 years ago)
Author:
altafang
Message:

Fixing minor things, adjusting termination conditions on tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/alta/mystic-0.2a1/test_constraints_range.py

    r360 r373  
    99 
    1010Mathematica: x -> -0.61479, y -> 0.38521 
     11 
     12OpenOpt Code and Results: 
     13------------------------- 
     14from openopt import NLP, GLP 
     15from math import * 
     16from mystic.models import rosen as f 
     17def c(x): 
     18    return -(max(x) - min(x) - 1.) 
     19#x0 = [0., 1.75] 
     20x0 = [0., 0.] 
     21 
     22problem = GLP(f, x0, c=c, lb=[-1., -1.], ub=[3., 3.], population=40,  
     23baseVectorStrategy='random', searchDirectionStrategy='random') 
     24soln = 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! 
     29print soln.xf 
     30 
     31slsqp is great with a feasible x0, but fails with infeasible. ralg also does 
     32poorly with infeasible x0. de does ok with any x0, as long as lb and ub is 
     33narrow enough. 
    1134""" 
    1235 
     
    3659#x0 = [0., 0.] # Another infeasible starting point. 
    3760#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. 
    3863 
    3964constraints_string = """ 
     
    5782    from mystic.termination import VTR 
    5883    from mystic.termination import CandidateRelativeTolerance as CRT 
     84    from mystic.termination import ChangeOverGeneration as COG 
    5985    from mystic.strategy import Rand1Bin 
    6086    solver = DifferentialEvolutionSolver(ndim, npop) 
     
    6490    term = VTR() 
    6591    #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) 
    6895    soln = solver.Solution() 
    6996 
     
    282309    time_before = time.time() 
    283310 
    284     from differential_evolution import DifferentialEvolutionSolver 
     311    from mystic.differential_evolution import DifferentialEvolutionSolver 
    285312    from mystic.termination import VTR 
    286313    from mystic.termination import CandidateRelativeTolerance as CRT 
     
    293320    #term = CRT() 
    294321    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) 
    296323    soln = solver.Solution() 
    297324 
     
    315342    time_before = time.time() 
    316343 
    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 
    319347    from mystic.strategy import Rand1Bin 
    320348    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) 
    337365 
    338366def test_neldermead_auglag(): 
     
    714742    # with x0=[0., 0.], BFGS, NCG give non-minimum answers with penalty. 
    715743    # 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. 
    717745    test_diffev_penalty() 
    718746    test_diffev2_penalty() 
     
    726754    # infeasible region and end up at a local minimum. See 
    727755    # 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. 
    728759    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], 
    730761                          # get different answer depending on  
    731762                          # whether previous line is commented out or not. 
     763                         # The effect also exists if you switch their orders. 
    732764    test_neldermead_auglag() 
    733765    test_powelldirectional_auglag() # needs rg and rh = 1e4 (for feasible x0) 
Note: See TracChangeset for help on using the changeset viewer.