1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#!/usr/bin/env python
#
# Author: Patrick Hung (patrickh @caltech)
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
# Copyright (c) 1997-2016 California Institute of Technology.
# Copyright (c) 2016-2024 The Uncertainty Quantification Foundation.
# License: 3-clause BSD. The full license text is available at:
# - https://github.com/uqfoundation/mystic/blob/master/LICENSE
"""
Runs differential evolution as a pyre application
Based on DifferentialEvolutionSolver
"""
from pyre.applications.Script import Script
from mystic.helputil import paginate
from mystic.solvers import DifferentialEvolutionSolver
import logging
class DerunApp(Script):
"""Differential Evolution Solver wrapped into a Pyre Application."""
class Inventory(Script.Inventory):
import pyre.inventory
# the defaults
scale = pyre.inventory.float('scale', default = 0.7)
scale.meta['tip'] = 'Differential Evolution scale factor.'
probability = pyre.inventory.float('probability', default = 0.5)
probability.meta['tip'] = 'Differential Evolution crossover probability.'
costfunc = pyre.inventory.str('costfunc', default = 'dummy')
costfunc.meta['tip'] = 'The python module containing the cost-function and other data.'
strategy = pyre.inventory.str('strategy', default = 'Best1Exp')
strategy.meta['tip'] = 'The differential evolution strategy.'
strategy.meta['known_alternatives'] = ['Best1Bin', 'Rand1Exp']
verbose = pyre.inventory.bool('verbose', default = False)
verbose.meta['tip'] = 'Turns on logging.'
def main(self, *args, **kwds):
solver = DifferentialEvolutionSolver(self.mod.ND, self.mod.NP)
costfunction = self.mod.cost
termination = self.mod.termination
MAX_GENERATIONS = self.mod.MAX_GENERATIONS
solver.SetRandomInitialPoints(min = self.mod.min, max = self.mod.max)
solver.SetEvaluationLimits(generations=MAX_GENERATIONS)
solver.Solve(costfunction, termination, strategy=self.strategy,\
CrossProbability=self.probability, \
ScalingFactor=self.scale)
self.solution = solver.Solution()
return
def __init__(self):
Script.__init__(self, 'derunapp')
self.scale = ''
self.probability = ''
self.mod = ''
self.strategy = ''
self.solution = None
return
def _defaults(self):
Script._defaults(self)
return
def _configure(self):
from mystic import strategy as detools
Script._configure(self)
mod = __import__(self.inventory.costfunc)
self.mod = mod
self.scale = self.inventory.scale
self.probability = self.inventory.probability
try:
self.probability = self.mod.probability
except:
pass
try:
self.scale = self.mod.scale
except:
pass
self.strategy = getattr(detools,self.inventory.strategy)
if self.inventory.verbose:
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S')
def _init(self):
Script._init(self)
return
def help(self):
doc = """
# this will solve the default "example" problem
%(name)s
""" % {'name' : __file__}
paginate(doc)
return
# main
if __name__ == '__main__':
app = DerunApp()
app.run()
# Chebyshev8 polynomial (used with "dummy.py")
from mystic.models.poly import chebyshev8coeffs as target_coeffs
from mystic.math import poly1d
print("target:\n%s" % poly1d(target_coeffs))
print("\nDE Solution:\n%s" % poly1d(app.solution))
# End of file
|