Changeset 177


Ignore:
Timestamp:
08/10/09 13:37:47 (7 years ago)
Author:
altafang
Message:

Fixing small things in snobfit and test_snobfit

Location:
branches/alta/mystic-0.1a2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/alta/mystic-0.1a2/examples/test_snobfit.py

    r168 r177  
    77Demonstrates: 
    88    - minimal solver interface for snobfit 
     9    - Use of a mystic solver without just the minimal interface 
     10    - Use of SnobfitSolver and SnobfitSoftSolver 
    911 
    1012These tests are adapted from the snobfit.py file. 
     
    1315from mystic.snobfit_solver import snobfit 
    1416from mystic.softConstraint import SoftConstraint, SoftConstraints 
     17from mystic.tools import random_seed 
    1518import numpy 
    1619 
     
    2528    return f 
    2629 
     30def hsf18(x): 
     31    f = 0.01*x[0]**2 + x[1]**2 
     32    return f 
     33 
    2734iter = 0 
    2835# plot the parameter trajectories 
     
    3340    iter += 1 
    3441    return 
    35  
    36 def hsf18(x): 
    37     f = 0.01*x[0]**2 + x[1]**2 
    38     return f 
    3942 
    4043#--------------------------------------------------------- 
     
    6063    v = -u 
    6164    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) 
    6468    print('optimize:', results) 
    6569 
     
    8589    s.add(  SoftConstraint( F=hsc18_2, Flo=0, Fhi=numpy.inf, sigma=1.25) ) 
    8690 
     91    random_seed(1) 
    8792    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) 
    9296    print results 
    9397 
     
    96100#         Rosenbrock function. 
    97101 
    98 def test_4(): 
     102def test_ros2(): 
     103    """Minimize the Rosenbrock function.""" 
     104 
    99105    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  
    100110    stepmon = Sow() 
    101111    evalmon = Sow() 
    102     from mystic.termination import SnobfitTermination 
    103     from mystic.snobfit_solver import SnobfitSolver 
    104112 
     113    # The initial guess x0 and bounds u,v 
    105114    x0 = numpy.array([2, 3]) 
    106115    u = -5.12*numpy.ones(2) 
     
    108117 
    109118    random_seed(1) 
    110     solver = SnobfitSolver(len(x0)) 
     119 
     120    # Initialize the solver 
     121    solver = SnobfitSolver(len(x0),7) 
    111122    solver.SetStrictRanges(u, v) 
    112123    solver.SetInitialPoints(x0) 
    113124    solver.enable_signal_handler() 
    114125 
    115     solver.Solve(ros,termination=SnobfitTermination(),\ 
     126    # Call the main Solve() function 
     127    solver.Solve(ros,termination=VTRChangeOverGenerations(1e-6, 0, 250),\ 
    116128                 EvaluationMonitor=evalmon,StepMonitor=stepmon) 
    117     solution = solver.Solution() 
    118129 
     130    # Get solution 
    119131    x = solver.bestSolution 
     132 
     133    # Get the function value at the solution 
    120134    fval = solver.bestEnergy 
     135 
     136    # Get the number of function evaluations and iterations 
    121137    fcalls = len(evalmon.x) 
    122138    iterations = len(stepmon.x) 
    123139 
    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 
    125145 
    126146#------------------------------------------------------------------ 
     
    128148if __name__ == "__main__": 
    129149    #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  
    8383    """Snobfit optimization. """ 
    8484 
    85     def __init__(self, dim, **kwds): 
     85    def __init__(self, dim, NP=None): 
    8686        """ 
    8787Required input:  
     
    9494                than parameters. Default is dn = 5 and NP adjusted accordingly. 
    9595        """ 
    96         if kwds.has_key("NP"): 
     96        if NP: 
    9797            NP = NP 
    9898        else: 
     
    155155            self.func = wrap_bounds(self.func, self._strictMin, self._strictMax) 
    156156 
    157             # Fix for when bounds were set after initial points: reinitialize 
     157            # Re-initialize population if bounds were set after initial points.  
    158158            # This may give different results from if SetStrictRanges was called first 
    159159            x0 = self.population[0] 
     
    167167        x0 = self.population[0] 
    168168        self.x = self.population 
    169  
    170         self.fglob = 0 
    171169 
    172170        # The number of safeguarded nearest neighbors 
     
    270268        return  
    271269 
    272     # Overwrite abstract_solver's SetRandomInitialPoints() 
     270    # Overwrite AbstractSolver's SetRandomInitialPoints() 
    273271     
    274     # If SetStrictRange was called first, use those bounds. 
     272    # If SetStrictRanges was called first, use those bounds. 
    275273    # If SetInitialPoints was called first, temporarily use defaults, 
    276     # and Solve() will re-initialize with user-specified bounds 
     274    # and Solve() will re-initialize with the bounds from SetStrictRanges 
    277275    def SetRandomInitialPoints(self,min=None,max=None): 
    278276        if self._useStrictRange: 
     
    287285            if min is None: min = [-1e3]*self.nDim   
    288286            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) 
    294288         
    295289############################################################################ 
    296290# Helper methods for SnobfitSolver 
    297291 
    298     def _setInitRecommendedEvalPoints(self,min,max): 
     292    def _setInitRecommendedEvalPoints(self, min, max): 
    299293        """ 
    300294        Snobfit's initialization of the population 
     
    23672361            self.func = wrap_bounds(self.func, self._strictMin, self._strictMax) 
    23682362 
    2369             # Fix for when bounds were set after initial points.  
     2363            # Re-initialize population if bounds were set after initial points.  
    23702364            x0 = self.population[0] 
    23712365            self.SetInitialPoints(x0) 
     
    26812675        1 : 'Maximum number of function evaluations.' 
    26822676        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. 
    26842679    uncertainty -- list - Returned if retuct is True. The uncertainty of  
    26852680                   the fitting parameters. 
     
    26872682 
    26882683    from mystic.tools import Sow 
    2689     from mystic.termination import FglobTermination, VTRChangeOverGenerations 
    2690  
    26912684    stepmon = Sow() 
    26922685    evalmon = Sow() 
    26932686 
     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. 
    26942690    if constraint is None: 
    26952691        if npop: 
     
    27042700 
    27052701    if fglob != 0: 
    2706         termination = FglobTermination(fglob=fglob, tolerance=rtol, generations=nstop) 
     2702        from mystic.termination import FglobTermination 
     2703        termination = FglobTermination(fglob, rtol, nstop) 
    27072704    else: 
    2708         termination = VTRChangeOverGenerations(costtolerance=ftol, gentolerance=0,\ 
    2709                                                generations=nstop) 
     2705        from mystic.termination import VTRChangeOverGenerations 
     2706        termination = VTRChangeOverGenerations(ftol, 0, nstop) 
    27102707 
    27112708    # Bounds are required 
Note: See TracChangeset for help on using the changeset viewer.