Changeset 473


Ignore:
Timestamp:
07/07/11 08:44:38 (5 years ago)
Author:
mmckerns
Message:

converted 'constraints' keyword to SetConstraints? method;
enabled use of constraints in DifferentialEvolutionSolver?;
added new constraints example ('test_rosenbrock3b')

Location:
mystic
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • mystic/examples/Make.mm

    r235 r473  
    7979    test_rosenbrock2.py \ 
    8080    test_rosenbrock3.py \ 
     81    test_rosenbrock3b.py \ 
    8182##  test_twistedgaussian.py \ 
    8283##  test_twistedgaussian2.py \ 
  • mystic/examples/test_rosenbrock3.py

    r471 r473  
    4141   #solver.SetEvaluationLimits(maxiter=13) 
    4242    solver.SetGenerationMonitor(stepmon) 
     43    solver.SetConstraints(constrain) 
    4344    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) 
    4546    print solver.bestSolution 
    4647   #print "Current function value: %s" % solver.bestEnergy 
  • mystic/mystic/abstract_solver.py

    r470 r473  
    134134        self._evalmon         = Null 
    135135 
     136        self._constraints     = lambda x: x 
     137       #self._penalty         = lambda x: x 
     138 
    136139        import mystic.termination 
    137140        self._EARLYEXIT       = mystic.termination.EARLYEXIT 
     
    145148        """get the number of function calls""" 
    146149        return self._fcalls[0] 
     150 
     151    def SetConstraints(self, constraints): 
     152        """apply a constraints function to the optimization 
     153 
     154input:: 
     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 
    147166 
    148167    def SetGenerationMonitor(self, monitor): 
  • mystic/mystic/differential_evolution.py

    r470 r473  
    241241        if kwds.has_key('StepMonitor'): \ 
    242242           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 
    243246        #------------------------------------------------------------- 
    244247 
     
    266269            # generate trialSolution (within valid range) 
    267270            self.trialSolution[:] = self.population[candidate][:] 
     271            # -apply constraints- 
     272            self.trialSolution[:] = self._constraints(self.trialSolution[:]) 
     273            # -end- 
    268274            trialEnergy = costfunction(self.trialSolution) 
    269275 
     
    296302                # generate trialSolution (within valid range) 
    297303                strategy(self, candidate) 
     304                # -apply constraints- 
     305                self.trialSolution[:] = self._constraints(self.trialSolution[:]) 
     306                # -end- 
    298307                trialEnergy = costfunction(self.trialSolution) 
    299308 
     
    406415        iteration.  It is called as callback(xk), where xk is 
    407416        the current parameter vector.  [default = None] 
    408     constraints -- an optional user-supplied function to call just 
    409         prior to each function evaluation.  It is called as 
    410         xk' = constraints(xk), where xk is the current parameter vector. 
    411         Ideally, this function is used to ensure each function evaulation 
    412         encounters a parameter set that satisfies user-supplied constraints. 
    413417    disp -- non-zero to print convergence messages. 
    414418        """ 
     
    426430        if kwds.has_key('callback'): callback = kwds['callback'] 
    427431        if kwds.has_key('disp'): disp = kwds['disp'] 
    428         #XXX:[HACK] accept constraints 
    429         constraints = lambda x: x 
    430         if kwds.has_key('constraints'): constraints = kwds['constraints'] 
    431         if not constraints: constraints = lambda x: x 
    432         #XXX:[HACK] -end- 
    433432        # backward compatibility 
    434433        if kwds.has_key('EvaluationMonitor'): \ 
     
    436435        if kwds.has_key('StepMonitor'): \ 
    437436           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 
    438440        #------------------------------------------------------------- 
    439441 
     
    467469            # generate trialSolution (within valid range) 
    468470            self.trialSolution[:] = self.population[candidate][:] 
    469             #XXX:[HACK] apply constraints 
    470             self.trialSolution[:] = constraints(self.trialSolution[:]) 
    471             #XXX:[HACK] -end- 
     471            # -apply constraints- 
     472            self.trialSolution[:] = self._constraints(self.trialSolution[:]) 
     473            # -end- 
    472474            trialPop[candidate][:] = self.trialSolution[:] 
    473475 
     
    508510                # generate trialSolution (within valid range) 
    509511                strategy(self, candidate) 
    510                 #XXX:[HACK] apply constraints 
    511                 self.trialSolution[:] = constraints(self.trialSolution[:]) 
    512                 #XXX:[HACK] -end- 
     512                # -apply constraints- 
     513                self.trialSolution[:] = self._constraints(self.trialSolution[:]) 
     514                # -end- 
    513515                trialPop[candidate][:] = self.trialSolution[:] 
    514516 
  • mystic/mystic/nested.py

    r470 r473  
    8989        if kwds.has_key('StepMonitor'): \ 
    9090           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 
    9194        #------------------------------------------------------------- 
    9295 
     
    152155    solver.SetStrictRanges(min=%s, max=%s) 
    153156""" % (str(lower), str(upper)) 
     157#        local_opt += """\n 
     158#    solver.SetConstraints(%s) 
     159#""" % (self._solver._constraints)  #FIXME: needs to take a string 
    154160        local_opt += """\n 
    155161    solver.SetEvaluationLimits(%s, %s) 
     
    273279        if kwds.has_key('StepMonitor'): \ 
    274280           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 
    275284        #------------------------------------------------------------- 
    276285 
     
    329338    solver.SetStrictRanges(min=%s, max=%s) 
    330339""" % (str(lower), str(upper)) 
     340#        local_opt += """\n 
     341#    solver.SetConstraints(%s) 
     342#""" % (self._solver._constraints)  #FIXME: needs to take a string 
    331343        local_opt += """\n 
    332344    solver.SetEvaluationLimits(%s, %s) 
  • mystic/mystic/scipy_optimize.py

    r470 r473  
    162162        callback=None  #user-supplied function, called after each step 
    163163        radius=0.05    #percentage change for initial simplex values 
    164         constraints = lambda x: x # Constraints function to be imposed 
    165164        if kwds.has_key('callback'): callback = kwds['callback'] 
    166165        if kwds.has_key('disp'): disp = kwds['disp'] 
    167166        if kwds.has_key('radius'): radius = kwds['radius'] 
    168         if kwds.has_key('constraints'): constraints = kwds['constraints'] 
    169         if not constraints: constraints = lambda x: x 
    170167        # backward compatibility 
    171168        if kwds.has_key('EvaluationMonitor'): \ 
     
    173170        if kwds.has_key('StepMonitor'): \ 
    174171           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 
    175175        #------------------------------------------------------------- 
    176176 
     
    189189 
    190190        # wrap constraints function 
    191         func = wrap_nested(func, constraints) 
     191        func = wrap_nested(func, self._constraints) 
    192192 
    193193        id = self.id 
    194194        x0 = asfarray(x0).flatten() 
    195         x0 = asfarray(constraints(x0)) 
     195        x0 = asfarray(self._constraints(x0)) 
    196196        N = len(x0) #XXX: this should be equal to self.nDim 
    197197        rank = len(x0.shape) 
     
    245245 
    246246            # apply constraints  #XXX: is this the only appropriate place??? 
    247             sim[0] = asfarray(constraints(sim[0])) 
     247            sim[0] = asfarray(self._constraints(sim[0])) 
    248248 
    249249            xbar = numpy.add.reduce(sim[:-1],0) / N 
     
    501501        callback=None  #user-supplied function, called after each step 
    502502        xtol=1e-4      #line-search error tolerance 
    503         constraints = lambda x: x # Constraints function to be imposed 
    504503        if kwds.has_key('callback'): callback = kwds['callback'] 
    505504        if kwds.has_key('direc'): direc = kwds['direc']  #XXX: best interface? 
    506505        if kwds.has_key('xtol'): xtol = kwds['xtol'] 
    507506        if kwds.has_key('disp'): disp = kwds['disp'] 
    508         if kwds.has_key('constraints'): constraints = kwds['constraints'] 
    509         if not constraints: constraints = lambda x: x 
    510507        # backward compatibility 
    511508        if kwds.has_key('EvaluationMonitor'): \ 
     
    513510        if kwds.has_key('StepMonitor'): \ 
    514511           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 
    515515        #------------------------------------------------------------- 
    516516 
     
    529529 
    530530        # wrap constraints function 
    531         func = wrap_nested(func, constraints) 
     531        func = wrap_nested(func, self._constraints) 
    532532 
    533533        id = self.id 
    534534        x = asfarray(x0).flatten() 
    535         x = asfarray(constraints(x)) 
     535        x = asfarray(self._constraints(x)) 
    536536        if retall: 
    537537            allvecs = [x] 
     
    577577 
    578578                # apply constraints 
    579                 x = asfarray(constraints(x)) 
     579                x = asfarray(self._constraints(x)) 
    580580 
    581581            iter += 1 
     
    607607                        direc[-1] = direc1 
    608608  
    609                #        x = asfarray(constraints(x)) 
     609               #        x = asfarray(self._constraints(x)) 
    610610 
    611611                self.energy_history[-1] = fval #...update to 'best' energy 
  • mystic/tests/solver_test_suite.py

    r470 r473  
    11#!/usr/bin/python 
    2 """A test suite for Mystic solvers and constraints. 
     2"""A test suite for Mystic solvers. 
    33Note: VTR termination with default tolerance shouldn't work for functions  
    44whose value at the minimum is negative! 
Note: See TracChangeset for help on using the changeset viewer.