Changeset 373


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

Fixing minor things, adjusting termination conditions on tests

Location:
branches/alta/mystic-0.2a1
Files:
8 edited

Legend:

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

    r367 r373  
    247247        solver.Solve(wrapped_costfunc, term, StepMonitor=StepMonitor,\ 
    248248                    EvaluationMonitor=EvaluationMonitor,\ 
    249                     sigint_callback=sigint_callback, constraints=lambda x: x,\ 
    250                     constraints_method='direct', **kwds) 
     249                    sigint_callback=sigint_callback, **kwds) 
    251250        x = solver.Solution() 
    252251 
  • branches/alta/mystic-0.2a1/mystic_test_suite.py

    r369 r373  
    11#!/usr/bin/python 
    2 """a test suite for Mystic solvers and constraints.""" 
     2"""A test suite for Mystic solvers and constraints. 
     3Note: VTR termination with default tolerance shouldn't work for functions  
     4whose value at the minimum is negative! 
     5Also, the two differential evolution solvers are global, while the other solvers 
     6are local optimizers.""" 
    37# should report clock-time, # of iterations, and # of function evaluations 
    48 
     
    213217        import numpy 
    214218        from mystic.tools import random_seed 
    215        #random_seed(123) 
     219        #random_seed(123) 
    216220        esow = Sow() 
    217221        ssow = Sow()  
     
    401405        import numpy 
    402406        from mystic.tools import random_seed 
    403        #random_seed(123) 
     407        #random_seed(123) 
    404408        esow = Sow() 
    405409        ssow = Sow()  
     
    584588        self.maxiter = 10000 
    585589        self.nplaces = 0 # Precision of answer  
    586                          #XXX Perhaps need something more lenient, as there are many 
    587                          # failures here. 
    588590 
    589591    def _run_solver(self, **kwds): 
     
    591593        import numpy 
    592594        from mystic.tools import random_seed 
    593        #random_seed(123) 
     595        random_seed(123) 
    594596        esow = Sow() 
    595597        ssow = Sow()  
     
    30103012 
    30113013class TestPaviani(unittest.TestCase): 
    3012     """Paviani's function, or TP110 of Schittkowski's test problems.""" 
     3014    """Paviani's function, or TP110 of Schittkowski's test problems. 
     3015F(min) is negative, so VTR default can fail.""" 
    30133016 
    30143017    def setUp(self): 
     
    32173220    # Comment out suites in the list below to test specific test cost functions only 
    32183221    # (Testing all the problems will take some time) 
    3219     allsuites = unittest.TestSuite([suite1, suite2, suite3, suite4, suite5, \ 
    3220                                     suite6, suite7, suite8, suite9, suite10, \ 
    3221                                     suite11, suite12, suite13, suite14,\ 
    3222                                     suite15, suite16, suite17]) 
    3223     #allsuites = unittest.TestSuite([suite1]) 
     3222    #allsuites = unittest.TestSuite([suite1, suite2, suite3, suite4, suite5, \ 
     3223    #                                suite6, suite7, suite8, suite9, suite10, \ 
     3224    #                                suite11, suite12, suite13, suite14,\ 
     3225    #                                suite15, suite16, suite17]) 
     3226    allsuites = unittest.TestSuite([suite4]) 
    32243227    unittest.TextTestRunner(verbosity=2).run(allsuites) 
  • branches/alta/mystic-0.2a1/test_constraints_cvxopt_qp_allsolvers.py

    r361 r373  
    232232# Additional parameters 
    233233ndim = 2        # Number of variables in the x vector. 
    234 x0 = [0.5, 0.5]   # Infeasible initial point is ok! 
    235 #x0 = [0.8, 0.3] 
    236 #x0 = [1., 1.] 
     234#x0 = [0.5, 0.5]   # Feasible 
     235#x0 = [0.8, 0.3] # Infeasible 
     236x0 = [1., 1.]  # Infeasible 
    237237npop = 25       # Required for differential evolution solver 
    238238 
     
    589589    from scipy_bfgs import BFGSSolver 
    590590    from mystic.termination import VTR 
     591    from mystic.termination import GradientTermination 
     592    from mystic.termination import ChangeOverGeneration as COG 
    591593    solver = BFGSSolver(ndim) 
    592594       
    593595    solver.SetInitialPoints(x0) 
    594596    solver.enable_signal_handler() 
    595     term = VTR() 
     597    term = VTR() # works well 
     598    #term = GradientTermination() # works well 
     599    #term= COG() # works well 
    596600    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    597601                constraints_method='auglag') 
     
    619623    from scipy_ncg import NCGSolver 
    620624    from mystic.termination import VTR 
     625    from mystic.termination import SolutionImprovement 
     626    from mystic.termination import ChangeOverGeneration as COG 
    621627    solver = NCGSolver(ndim) 
    622628       
    623629    solver.SetInitialPoints(x0) 
    624630    solver.enable_signal_handler() 
    625     term = VTR() 
    626     solver.Solve(costfunc, term, constraints=constraints_string, \ 
    627                 constraints_method='auglag') 
     631    term = VTR() # very slow with [0.8, 0.3] 
     632    #term = SolutionImprovement() # very slow with [0.8, 0.3] 
     633    #term = COG() # very slow with [0.8, 0.3] 
     634    solver.Solve(costfunc, term, constraints=constraints_string, \ 
     635                constraints_method='auglag', disp=True) 
    628636    soln = solver.Solution() 
    629637 
     
    11351143    test_powelldirectional_auglag() 
    11361144    test_bfgs_auglag()  
    1137     test_ncg_auglag() # little off. gets stuck with x0=[0.8, 0.3] 
     1145    test_ncg_auglag() # very slow, esp. in the first SUMT iteration. probably 
     1146                       # relies on the hack to not get completely stuck. 
    11381147    test_cg_auglag()  
    11391148 
  • 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) 
  • branches/alta/mystic-0.2a1/test_constraints_tp24.py

    r371 r373  
    1111x0 = [1., 0.5] # feasible 
    1212expected solution = [3., 1.7320508075688772] where f=-1. 
     13Solution is at corner of feasible region. 
    1314""" 
    1415 
     
    5455 
    5556    from differential_evolution import DifferentialEvolutionSolver 
    56     from mystic.termination import VTR 
    5757    from mystic.termination import CandidateRelativeTolerance as CRT 
    5858    from mystic.termination import ChangeOverGeneration as COG 
     
    6161    solver.SetInitialPoints(x0) 
    6262    solver.enable_signal_handler() 
    63     #term = VTR() # not very good 
    64     #term = CRT() # doesn't do much 
    65     term = COG() # this works well 
     63    term = CRT() # works well 
     64    #term = COG() # this works well 
    6665    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    6766                constraints_method='penalty') 
     
    8887 
    8988    from differential_evolution import DifferentialEvolutionSolver2 
    90     from mystic.termination import VTR 
    91     from mystic.termination import ChangeOverGeneration as COG 
     89    from mystic.termination import ChangeOverGeneration as COG 
     90    from mystic.termination import CandidateRelativeTolerance as CRT 
    9291    solver = DifferentialEvolutionSolver2(ndim, npop) 
    9392       
    9493    solver.SetInitialPoints(x0) 
    9594    solver.enable_signal_handler() 
    96     #term = VTR() # does not work well 
    97     term = COG() # does work well 
     95    term = CRT() # works well 
     96    #term = COG() # does work well 
    9897    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    9998                constraints_method='penalty') 
     
    120119 
    121120    from scipy_optimize import NelderMeadSimplexSolver 
    122     from mystic.termination import VTR 
    123     from mystic.termination import ChangeOverGeneration as COG 
     121    from mystic.termination import ChangeOverGeneration as COG 
     122    from mystic.termination import CandidateRelativeTolerance as CRT 
    124123    solver = NelderMeadSimplexSolver(ndim) 
    125124       
    126125    solver.SetInitialPoints(x0) 
    127126    solver.enable_signal_handler() 
    128     #term = VTR() # doesn't work well 
    129127    term = COG() # does work well 
     128    #term = CRT() # litte bit off 
    130129    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    131130                constraints_method='penalty') 
     
    152151 
    153152    from scipy_optimize import PowellDirectionalSolver 
    154     from mystic.termination import VTR 
     153    #from mystic.termination import VTR 
     154    from mystic.termination import ChangeOverGeneration as COG 
    155155    solver = PowellDirectionalSolver(ndim) 
    156156       
    157157    solver.SetInitialPoints(x0) 
    158158    solver.enable_signal_handler() 
    159     term = VTR() 
     159    #term = VTR() # strangely, seems to work ok 
     160    term = COG() # works well 
    160161    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    161162                constraints_method='penalty') 
     
    182183 
    183184    from scipy_bfgs import BFGSSolver 
    184     from mystic.termination import VTR 
    185185    from mystic.termination import GradientTermination 
     186    from mystic.termination import ChangeOverGeneration as COG 
    186187    solver = BFGSSolver(ndim) 
    187188       
    188189    solver.SetInitialPoints(x0) 
    189190    solver.enable_signal_handler() 
    190     #term = VTR() # does not work well 
    191     term = GradientTermination() # does work well 
     191    #term = GradientTermination() # does work well 
     192    term = COG() # works well 
    192193    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    193194                constraints_method='penalty') 
     
    214215 
    215216    from scipy_ncg import NCGSolver 
    216     from mystic.termination import VTR 
    217217    from termination import SolutionImprovement 
    218218    from termination import ChangeOverGeneration as COG 
     
    222222    solver.SetInitialPoints(x0) 
    223223    solver.enable_signal_handler() 
    224     term = VTR() # does not work well 
    225     #term = SolutionImprovement() # does not work well 
     224    term = SolutionImprovement() # does not work well 
    226225    #term = COG() # does not work well 
    227226    #term = NCOG() # does not work well 
     
    250249 
    251250    from scipy_cg import CGSolver 
    252     from mystic.termination import VTR 
    253251    from termination import ChangeOverGeneration as COG 
    254252    from termination import NormalizedChangeOverGeneration as NCOG 
     
    258256    solver.SetInitialPoints(x0) 
    259257    solver.enable_signal_handler() 
    260     #term = VTR() # does not work well 
    261258    term = COG() # works well 
    262259    solver.Solve(costfunc, term, constraints=constraints_string, \ 
     
    318315 
    319316    from differential_evolution import DifferentialEvolutionSolver2 
    320     from mystic.termination import VTR 
    321     from mystic.termination import CandidateRelativeTolerance as CRT 
     317    from mystic.termination import CandidateRelativeTolerance as CRT 
     318    from mystic.termination import ChangeOverGeneration as COG 
    322319    solver = DifferentialEvolutionSolver2(ndim, npop) 
    323320       
    324321    solver.SetInitialPoints(x0) 
    325322    solver.enable_signal_handler() 
    326     term = VTR() # does not work well 
    327     #term = CRT() # works well 
     323    term = CRT() # works well 
     324    #term = COG() # works well 
    328325    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    329326                constraints_method='auglag') 
     
    351348 
    352349    from scipy_optimize import NelderMeadSimplexSolver 
    353     from mystic.termination import VTR 
    354     from termination import ChangeOverGeneration as COG 
     350    from mystic.termination import ChangeOverGeneration as COG 
     351    from mystic.termination import CandidateRelativeTolerance as CRT 
    355352    solver = NelderMeadSimplexSolver(ndim) 
    356353       
    357354    solver.SetInitialPoints(x0) 
    358355    solver.enable_signal_handler() 
    359     #term = VTR() # does not work well 
    360     term = COG() # works well 
     356    #term = COG() # works well 
     357    term = CRT() # works well 
    361358    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    362359                constraints_method='auglag') 
     
    384381    from scipy_optimize import PowellDirectionalSolver 
    385382    from mystic.termination import VTR 
     383    from mystic.termination import ChangeOverGeneration as COG 
    386384    solver = PowellDirectionalSolver(ndim) 
    387385       
    388386    solver.SetInitialPoints(x0) 
    389387    solver.enable_signal_handler() 
    390     term = VTR() 
     388    #term = VTR() 
     389    term = COG() # works well 
    391390    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    392391                constraints_method='auglag') 
     
    412411 
    413412    from scipy_bfgs import BFGSSolver 
    414     from mystic.termination import VTR 
    415413    from termination import GradientTermination 
     414    from mystic.termination import ChangeOverGeneration as COG 
    416415    solver = BFGSSolver(ndim) 
    417416       
    418417    solver.SetInitialPoints(x0) 
    419418    solver.enable_signal_handler() 
    420     #term = VTR() # does not work well 
    421     term = GradientTermination() # works well 
     419    #term = GradientTermination() # works well 
     420    term = COG() # works well 
    422421    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    423422                constraints_method='auglag') 
     
    444443 
    445444    from scipy_ncg import NCGSolver 
    446     from mystic.termination import VTR 
    447445    from termination import SolutionImprovement 
     446    from mystic.termination import ChangeOverGeneration as COG 
    448447    solver = NCGSolver(ndim) 
    449448       
    450449    solver.SetInitialPoints(x0) 
    451450    solver.enable_signal_handler() 
    452     term = VTR() # does not work well 
    453     term = SolutionImprovement() # works well 
     451    #term = SolutionImprovement() # works well 
     452    term = COG() # works well 
    454453    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    455454                constraints_method='auglag') 
     
    475474 
    476475    from scipy_cg import CGSolver 
    477     from mystic.termination import VTR 
    478476    from termination import ChangeOverGeneration as COG 
    479477    solver = CGSolver(ndim) 
     
    481479    solver.SetInitialPoints(x0) 
    482480    solver.enable_signal_handler() 
    483     term = VTR() # does not work well 
    484481    term = COG() # works well 
    485482    solver.Solve(costfunc, term, constraints=constraints_string, \ 
     
    509506 
    510507    from differential_evolution import DifferentialEvolutionSolver 
    511     from mystic.termination import VTR 
    512508    from mystic.termination import ChangeOverGeneration as COG 
    513509    from mystic.termination import NormalizedChangeOverGeneration as NCOG 
     
    517513    solver.SetInitialPoints(x0) 
    518514    solver.enable_signal_handler() 
    519     #term = VTR() # does not work 
    520515    #term = COG() # a bit off 
    521516    #term = NCOG() # also a bit off 
     
    545540 
    546541    from differential_evolution import DifferentialEvolutionSolver2 
    547     from mystic.termination import VTR 
    548     from mystic.termination import CandidateRelativeTolerance as CRT 
     542    from mystic.termination import CandidateRelativeTolerance as CRT 
     543    from mystic.termination import ChangeOverGeneration as COG 
    549544    solver = DifferentialEvolutionSolver2(ndim, npop) 
    550545       
    551546    solver.SetInitialPoints(x0) 
    552547    solver.enable_signal_handler() 
    553     #term = VTR() # does not work well 
    554     term = CRT() # works well 
     548    #term = CRT() # works well 
     549    term = COG() # works ok 
    555550    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    556551                constraints_method='filter') 
     
    577572 
    578573    from scipy_optimize import NelderMeadSimplexSolver 
    579     from mystic.termination import VTR 
    580574    from mystic.termination import ChangeOverGeneration as COG 
    581575    from mystic.termination import NormalizedChangeOverGeneration as NCOG 
     
    585579    solver.SetInitialPoints(x0) 
    586580    solver.enable_signal_handler() 
    587     term = VTR() # doesn't work  
    588581    #term = COG() # quite a bit off 
    589     #term = CRT() # also off 
     582    term = CRT() # also off 
    590583    #term = NCOG() # also off 
    591584    solver.Solve(costfunc, term, constraints=constraints_string, \ 
     
    645638 
    646639    from scipy_bfgs import BFGSSolver 
    647     from mystic.termination import VTR 
    648640    from termination import GradientTermination 
    649641    from mystic.termination import ChangeOverGeneration as COG 
     
    652644    solver.SetInitialPoints(x0) 
    653645    solver.enable_signal_handler() 
    654     term = VTR() # does not work well 
    655     #term = GradientTermination() # also doesn't work 
     646    term = GradientTermination() # also doesn't work 
    656647    #term = COG() # doesn't work well either 
    657648    solver.Solve(costfunc, term, constraints=constraints_string, \ 
     
    679670 
    680671    from scipy_ncg import NCGSolver 
    681     from mystic.termination import VTR 
    682672    from termination import SolutionImprovement 
    683673    from mystic.termination import ChangeOverGeneration as COG 
     
    686676    solver.SetInitialPoints(x0) 
    687677    solver.enable_signal_handler() 
    688     term = VTR() # does not work well 
    689     #term = SolutionImprovement() # doesn't work either 
     678    term = SolutionImprovement() # doesn't work either 
    690679    #term = COG() # doesn't work either 
    691680    solver.Solve(costfunc, term, constraints=constraints_string, \ 
     
    713702 
    714703    from scipy_cg import CGSolver 
    715     from mystic.termination import VTR 
    716704    from termination import GradientTermination  
    717705    from mystic.termination import ChangeOverGeneration as COG 
     
    720708    solver.SetInitialPoints(x0) 
    721709    solver.enable_signal_handler() 
    722     term = VTR() # doesn't work well 
    723     #term = GradientTermination() # doesn't work well 
     710    term = GradientTermination() # doesn't work well 
    724711    #term = COG() # doesn't work well 
    725712    solver.Solve(costfunc, term, constraints=constraints_string, \ 
     
    739726 
    740727if __name__ == '__main__': 
    741     # Penalty method doesn't work well with defaults/VTR -- possibly because  
    742     # solvers think they are already at a minimum, since function values are 
    743     # relatively flat near x0, according to the contour plot. 
    744     test_diffev_penalty() # needs COG 
    745     test_diffev2_penalty() # needs COG 
    746     test_neldermead_penalty() # needs COG 
     728    # Penalty method doesn't work well with defaults/VTR, since the final  
     729    # function value is negative. 
     730    test_diffev_penalty()  
     731    test_diffev2_penalty()  
     732    test_neldermead_penalty()  
    747733    test_powelldirectional_penalty() 
    748     test_bfgs_penalty() # needs GradientTermination 
    749     test_ncg_penalty() # won't work despite trying 
    750     test_cg_penalty() # needs COG 
     734    test_bfgs_penalty()  
     735    test_ncg_penalty() # bad answer 
     736    test_cg_penalty()  
    751737 
    752738    test_diffev_auglag()  
    753     test_diffev2_auglag() # needs CRT 
    754     test_neldermead_auglag() # needs COG 
     739    test_diffev2_auglag()  
     740    test_neldermead_auglag()  
    755741    test_powelldirectional_auglag() 
    756     test_bfgs_auglag() # needs GradientTermination 
    757     test_ncg_auglag() # needs SolutionImprovement termination 
    758     test_cg_auglag() # needs COG 
    759  
    760     test_diffev_filter() # needs CRT 
    761     test_diffev2_filter() # needs CRT 
     742    test_bfgs_auglag()  
     743    test_ncg_auglag()  
     744    test_cg_auglag()  
     745 
     746    test_diffev_filter()  
     747    test_diffev2_filter() 
    762748    test_neldermead_filter() # bad answer 
    763749    test_powelldirectional_filter() # improves with COG 
  • branches/alta/mystic-0.2a1/test_constraints_tp6.py

    r364 r373  
    88Mystic Results: 
    99--------------- 
     10Results below are after testing different termination conditions and using 
     11ones that worked, if any worked. 
     12 
    1013Differential Evolution, penalty 
    11 soln= [ 0.93185794  0.86835217] 
    12 f value = 0.00464334043612 
    13 Time elapsed= 3.22497200966 
    14 -7.04931582951e-05  ?=  0.0 
     14soln= [ 0.99999626  0.99999252] 
     15f value = 1.39955330985e-11 
     16Time elapsed= 1.82334899902 
     172.74909317532e-08  ?=  0.0 
    1518True 
    1619 
     
    1821soln= [ 0.77856495  0.6061442 ] 
    1922f value = 0.0490334793345 
    20 Time elapsed= 5.53334498405 
     23Time elapsed= 2.31935405731 
    2124-0.000191837517554  ?=  0.0 
    2225True 
     
    2528soln= [-0.79924969  0.63882551] 
    2629f value = 3.23729944276 
    27 Time elapsed= 0.173187971115 
     30Time elapsed= 0.0765020847321 
    28310.000254426113928  ?=  0.0 
    2932True 
     
    3235soln= [-0.54298813  0.29474638] 
    3336f value = 2.3808123589 
    34 Time elapsed= 0.693130016327 
     37Time elapsed= 0.30738401413 
    3538-0.000897233040719  ?=  0.0 
    3639True 
     
    3942soln= [-1.2  1. ] 
    4043f value = 4.84 
    41 Time elapsed= 0.347265005112 
     44Time elapsed= 0.158123016357 
    4245-4.4  ?=  0.0 
    4346False 
    4447 
    4548NCG, penalty 
    46 soln= [-1.03440639  1.06999695] 
    47 f value = 4.13880936014 
    48 Time elapsed= 0.591888189316 
    49 3.72762859691e-06  ?=  0.0 
     49soln= [-1.03447871  1.07014734] 
     50f value = 4.13910362412 
     51Time elapsed= 0.00758099555969 
     521.13919376488e-05  ?=  0.0 
    5053True 
    5154 
     
    5356soln= [ 0.2151731   0.04624554] 
    5457f value = 0.615953267272 
    55 Time elapsed= 0.507454156876 
     58Time elapsed= 0.318397045135 
    5659-0.000539243908523  ?=  0.0 
    5760True 
    5861 
    5962DifferentialEvolution, auglag 
    60 soln= [ 1.0265255   1.05539154] 
    61 f value = 0.000703602183114 
    62 Time elapsed= 0.444896936417 
    63 0.0163693695118  ?=  0.0 
     63soln= [ 1.00001488  1.00002945] 
     64f value = 2.21482471957e-10 
     65Time elapsed= 0.617501974106 
     66-3.15108766635e-06  ?=  0.0 
     67True 
     68 
     69DifferentialEvolution2, auglag 
     70soln= [ 1.00000246  1.00000481] 
     71f value = 6.04295189197e-12 
     72Time elapsed= 0.828866958618 
     73-1.08042532698e-06  ?=  0.0 
     74True 
     75 
     76NelderMead, auglag 
     77soln= [ 0.99999706  0.99999401] 
     78f value = 8.62311013723e-12 
     79Time elapsed= 0.106360912323 
     80-1.15163547698e-06  ?=  0.0 
     81True 
     82 
     83PowellDirectional, auglag 
     84soln= [ 1.  1.] 
     85f value = 0.0 
     86Time elapsed= 0.686278104782 
     870.0  ?=  0.0 
     88True 
     89 
     90BFGS, auglag 
     91soln= [ 0.99999077  0.99998178] 
     92f value = 8.51914601677e-11 
     93Time elapsed= 0.0958249568939 
     942.39940897706e-06  ?=  0.0 
     95True 
     96 
     97NCG, auglag 
     98soln= [-0.23107736  0.05350618] 
     99f value = 1.51555147832 
     100Time elapsed= 32.7819998264 
     1010.00109432112118  ?=  0.0 
    64102False 
    65103 
    66 DifferentialEvolution2, auglag 
    67 soln= [ 0.95122535  0.90491171] 
    68 f value = 0.00237896631011 
    69 Time elapsed= 0.696881055832 
    70 0.000820387523576  ?=  0.0 
    71 True 
    72  
    73 NelderMead, auglag 
    74 soln= [ 0.9665114   0.93380755] 
    75 f value = 0.00112148653455 
    76 Time elapsed= 0.149505138397 
    77 -0.00336728044141  ?=  0.0 
    78 False 
    79  
    80 PowellDirectional, auglag 
    81 soln= [ 0.97953372  0.9594863 ] 
    82 f value = 0.000418868698238 
    83 Time elapsed= 0.570738077164 
    84 -1.11022302463e-15  ?=  0.0 
    85 True 
    86  
    87 BFGS, auglag 
    88 soln= [ 0.95481343  0.91217455] 
    89 f value = 0.00204182583475 
    90 Time elapsed= 0.164708137512 
    91 0.00505859260701  ?=  0.0 
    92 False 
    93  
    94 NCG gets stuck 
    95  
    96104CG, auglag 
    97 soln= [ 0.95841557  0.91844345] 
    98 f value = 0.00172926461434 
    99 Time elapsed= 0.449685096741 
    100 -0.0011695707853  ?=  0.0 
    101 False 
     105soln= [ 0.99997764  0.99995528] 
     106f value = 4.99909986311e-10 
     107Time elapsed= 0.253920793533 
     108-6.79715139729e-08  ?=  0.0 
     109True 
    102110 
    103111 
     
    157165    from mystic.termination import VTR 
    158166    from mystic.termination import CandidateRelativeTolerance as CRT 
     167    from mystic.termination import ChangeOverGeneration as COG 
    159168    solver = DifferentialEvolutionSolver(ndim, npop) 
    160169     
    161170    solver.SetInitialPoints(x0) 
    162171    solver.enable_signal_handler() 
    163     term = VTR() 
    164     #term = CRT() 
     172    #term = VTR() # must specify tol=0 
     173    #term = CRT() # works well 
     174    term = COG() # works well 
    165175    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    166176                constraints_method='penalty') 
     
    190200    from mystic.termination import CandidateRelativeTolerance as CRT 
    191201    from mystic.termination import ChangeOverGeneration as COG 
     202    from mystic.termination import NormalizedChangeOverGeneration as NCOG 
     203    from mystic.strategy import Rand1Bin 
     204 
     205    npop = 70 # Won't work unless npop is large 
    192206    solver = DifferentialEvolutionSolver2(ndim, npop) 
    193207     
    194208    solver.SetInitialPoints(x0) 
    195209    solver.enable_signal_handler() 
    196     term = VTR() 
    197     #term = CRT() 
    198     #term = COG() 
     210    #term = VTR() # must specify tol=0 
     211    #term = CRT() # bad 
     212    term = COG() # bad 
     213    #term = NCOG() # bad 
    199214    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    200215                constraints_method='penalty') 
     
    224239    from mystic.termination import ChangeOverGeneration as COG 
    225240    from mystic.termination import NormalizedChangeOverGeneration as NCOG 
     241    from mystic.termination import CandidateRelativeTolerance as CRT 
    226242    solver = NelderMeadSimplexSolver(ndim) 
    227243     
    228244    solver.SetInitialPoints(x0) 
    229245    solver.enable_signal_handler() 
    230     term = VTR() # does not work well 
     246    #term = VTR(0) # must specify tol=0 
    231247    #term = COG() # does not work well 
    232248    #term = NCOG() # does not work well 
     249    term = CRT() # bad 
    233250    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    234251                constraints_method='penalty') 
     
    257274    from mystic.termination import VTR 
    258275    from mystic.termination import ChangeOverGeneration as COG 
     276    from mystic.termination import NormalizedChangeOverGeneration as NCOG 
    259277    solver = PowellDirectionalSolver(ndim) 
    260278     
    261279    solver.SetInitialPoints(x0) 
    262280    solver.enable_signal_handler() 
    263     term = VTR() 
    264     #term = COG() 
     281    #term = VTR(0) # must specify tol=0 
     282    #term = COG() # bad 
     283    term = NCOG() # bad 
    265284    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    266285                constraints_method='penalty') 
     
    289308    from mystic.termination import VTR 
    290309    from mystic.termination import GradientTermination 
     310    from mystic.termination import ChangeOverGeneration as COG 
    291311    solver = BFGSSolver(ndim) 
    292312     
    293313    solver.SetInitialPoints(x0) 
    294314    solver.enable_signal_handler() 
    295     term = VTR() 
    296     #term = GradientTermination() 
     315    #term = VTR(0) # must specify tol=0 
     316    term = GradientTermination() # doesn't iterate 
     317    #term = COG() # doesn't iterate 
    297318    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    298319                constraints_method='penalty')#, penalty=1e3) 
     
    320341    from scipy_ncg import NCGSolver 
    321342    from mystic.termination import VTR 
     343    from mystic.termination import ChangeOverGeneration as COG 
     344    from mystic.termination import SolutionImprovement 
    322345    solver = NCGSolver(ndim) 
    323346     
    324347    solver.SetInitialPoints(x0) 
    325348    solver.enable_signal_handler() 
    326     term = VTR() 
     349    #term = VTR(0) # must specify tol=0 
     350    #term = COG() # bad 
     351    term = SolutionImprovement() # local min 
    327352    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    328353                constraints_method='penalty') 
     
    352377    from mystic.termination import GradientTermination 
    353378    from mystic.termination import ChangeOverGeneration as COG 
     379    from mystic.termination import NormalizedChangeOverGeneration as NCOG 
    354380    solver = CGSolver(ndim) 
    355381     
    356382    solver.SetInitialPoints(x0) 
    357383    solver.enable_signal_handler() 
    358     term = VTR() # doesn't work 
    359     #term = GradientTermination() # doesn't help 
     384    #term = VTR(0) # must specify tol=0 
     385    term = GradientTermination() # doesn't help 
    360386    #term = COG() # doesn't work either 
    361     solver.Solve(costfunc, term, constraints=constraints_string, \ 
    362                 constraints_method='penalty')#, penalty=1e3) 
     387    #term = NCOG() # bad 
     388    solver.Solve(costfunc, term, constraints=constraints_string, \ 
     389                constraints_method='penalty')#, penalty=1e7) 
    363390    soln = solver.Solution() 
    364391 
     
    391418    solver.SetInitialPoints(x0) 
    392419    solver.enable_signal_handler() 
    393     term = VTR() 
    394     #term = CRT() 
     420    #term = VTR(0) # must specify tol=0 
     421    term = CRT() # works well 
    395422    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    396423                constraints_method='auglag') 
     
    423450    solver.SetInitialPoints(x0) 
    424451    solver.enable_signal_handler() 
    425     term = VTR() 
    426     #term = CRT() 
     452    #term = VTR(0) # must specify tol=0 
     453    term = CRT() # works well 
    427454    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    428455                constraints_method='auglag') 
     
    456483    solver.SetInitialPoints(x0) 
    457484    solver.enable_signal_handler() 
    458     term = VTR() # gives answer that is a bit off 
    459     #term = COG() # works well 
     485    #term = VTR(0)  # must specify tol=0 
     486    term = COG() # works well 
    460487    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    461488                constraints_method='auglag') 
     
    488515    solver.SetInitialPoints(x0) 
    489516    solver.enable_signal_handler() 
    490     term = VTR() # gives answer that is a bit off 
    491     #term = COG()  # works well 
     517    #term = VTR(0) # must specify tol=0 
     518    term = COG()  # works well 
    492519    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    493520                constraints_method='auglag') 
     
    519546    solver.SetInitialPoints(x0) 
    520547    solver.enable_signal_handler() 
    521     term = VTR() # gives answer that is quite off 
    522     #term = GradientTermination() # works well 
     548    #term = VTR(0) # must specify tol=0 
     549    term = GradientTermination() # works well 
    523550    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    524551                constraints_method='auglag') 
     
    547574    from mystic.termination import VTR 
    548575    from mystic.termination import SolutionImprovement 
     576    from mystic.termination import ChangeOverGeneration as COG 
    549577    solver = NCGSolver(ndim) 
    550578     
    551579    solver.SetInitialPoints(x0) 
    552580    solver.enable_signal_handler() 
    553     term = VTR() # doesn't work 
     581    #term = VTR(0) # must specify tol=0 
    554582    #term = SolutionImprovement() # doesn't help 
     583    term = COG() # works well 
    555584    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    556585                constraints_method='auglag') 
     
    582611    solver.SetInitialPoints(x0) 
    583612    solver.enable_signal_handler() 
    584     term = VTR() # gives answer that is a bit off   
    585     #term = COG() # much better 
     613    #term = VTR(0)  # must specify tol=0 
     614    term = COG() # much better 
    586615    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    587616                constraints_method='auglag') 
     
    598627 
    599628if __name__ == '__main__': 
    600     # All tests do poorly with defaults.  
    601     test_diffev_penalty() # good with CRT 
    602     test_diffev2_penalty() # really won't work 
    603     test_neldermead_penalty() # off 
    604     test_powelldirectional_penalty() # off 
    605     test_bfgs_penalty() # needs penalty=1e3 and GradientTermination 
    606     test_ncg_penalty() # off 
    607     test_cg_penalty() # off 
    608  
    609     test_diffev_auglag() # good with CRT 
    610     test_diffev2_auglag() # good with CRT 
    611     test_neldermead_auglag() # good with COG 
    612     test_powelldirectional_auglag() # good with COG 
    613     test_bfgs_auglag() # good with GradientTermination 
    614     test_ncg_auglag() # stuck 
    615     test_cg_auglag() # good with COG 
     629    # All tests do poorly with defaults because VTR has a default tol of 0.005, 
     630    # and f(minimum) here is 0.  
     631    test_diffev_penalty()  
     632    test_diffev2_penalty() # need to increase npop 
     633    test_neldermead_penalty() # bad answer; higher penalty -> local min 
     634    test_powelldirectional_penalty() # bad answer; higher penalty -> local min 
     635    test_bfgs_penalty() # needs lower penalty 
     636    test_ncg_penalty() # local min 
     637    test_cg_penalty() # quite bad; higher penalty -> local min 
     638 
     639    test_diffev_auglag()  
     640    test_diffev2_auglag()  
     641    test_neldermead_auglag()  
     642    test_powelldirectional_auglag()  
     643    test_bfgs_auglag() 
     644    test_ncg_auglag() # takes a very long time, and bad answer 
     645    test_cg_auglag()  
    616646    print "expected: [1., 1.]" 
    617647 
  • branches/alta/mystic-0.2a1/test_constraints_zimmermann.py

    r369 r373  
    1616Mystic Results: 
    1717--------------- 
     18Note that more precise results could be obtained by adjusting termination, 
     19since VTR's default is tol=0.005, which is pretty rough. 
     20 
    1821Differential Evolution, penalty 
    1922soln= [6.9934815592274999, 2.0016578755677443] 
     
    207210    from differential_evolution import DifferentialEvolutionSolver 
    208211    from mystic.termination import VTR 
     212    from mystic.termination import CandidateRelativeTolerance as CRT 
    209213    from mystic.strategy import Rand1Bin 
    210214    solver = DifferentialEvolutionSolver(ndim, npop) 
    211215    solver.SetRandomInitialPoints(lb, ub) 
    212216    solver.enable_signal_handler() 
    213     term = VTR() 
     217    #term = VTR() 
     218    term = CRT() # more precise 
    214219    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    215220                constraints_method='penalty', strategy=Rand1Bin) 
     
    484489    from scipy_optimize import NelderMeadSimplexSolver 
    485490    from mystic.termination import VTR 
     491    from mystic.termination import CandidateRelativeTolerance as CRT 
    486492    solver = NelderMeadSimplexSolver(ndim) 
    487493     
    488494    solver.SetRandomInitialPoints(lb, ub) 
    489495    solver.enable_signal_handler() 
    490     term = VTR() 
     496    #term = VTR() 
     497    term = CRT() 
    491498    solver.Solve(costfunc, term, constraints=constraints_string, \ 
    492499                constraints_method='auglag') 
  • branches/alta/mystic-0.2a1/tutorial_example_constrained.py

    r361 r373  
    2727 
    2828# Mystic can handle constraints in the form of matrices as well. 
    29 A = [[-1.0,0.0],[0.0,-1.0]] 
    30 b = [0.0,0.0] 
    31 G = [1.0, 1.0] 
    32 h = [1.0] 
    33 from constraint_tools import matrix_to_symbolic 
     29G = [[-1.0,0.0],[0.0,-1.0]] 
     30h = [0.0,0.0] 
     31A = [1.0, 1.0] 
     32b = [1.0] 
     33from mystic.constraint_tools import matrix_to_symbolic 
    3434constraints_string = matrix_to_symbolic(A=A, b=b, G=G, h=h) 
    3535 
     
    5252 
    5353# Let's verify that the constraints actually are satisfied. 
    54 from constraint_tools import verify_constraints_satisfied 
     54from mystic.constraint_tools import verify_constraints_satisfied 
    5555print "Are constraints satisfied?" 
    5656print verify_constraints_satisfied(constraints_string, soln, ndim, tol=1e-7) 
Note: See TracChangeset for help on using the changeset viewer.