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
|
#!/usr/bin/env python
#
# 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
from mystic.monitors import *
a = Null()
b = Monitor()
c = VerboseMonitor(None)
a([1,],1)
a([2,],2)
b([0,],0)
b.extend(a)
assert b.x == [[0]]
assert len(b) == 1
a.extend(b)
assert a.x == Null()
#assert len(a) == None #XXX: len(Null()) throws a TypeError
c([1,],1)
c([2,],2)
c.prepend(b)
assert len(b) == 1
assert len(c) == 3
c([3,],3)
assert c.x == [[0], [1], [2], [3]]
assert len(c) == 4
c.prepend(c)
assert c.x == [[0], [1], [2], [3], [0], [1], [2], [3]]
assert len(c) == 8
from mystic.solvers import NelderMeadSimplexSolver
from mystic.tools import random_seed
random_seed(123)
lb = [-100,-100,-100]
ub = [1000,1000,1000]
ndim = len(lb)
maxiter = 10
maxfun = 1e+6
def cost(x):
ax,bx,c = x
return (ax)**2 - bx + c
monitor = Monitor()
solver = NelderMeadSimplexSolver(ndim)
solver.SetRandomInitialPoints(min=lb,max=ub)
solver.SetStrictRanges(min=lb,max=ub)
solver.SetEvaluationLimits(maxiter,maxfun)
solver.SetGenerationMonitor(monitor)
solver.Solve(cost)
solved = solver.bestSolution
monitor.info("solved: %s" % solved)
lmon = len(monitor)
assert solver.bestEnergy == monitor.y[-1]
for xs,x in zip(solved,monitor.x[-1]):
assert xs == x
solver.SetEvaluationLimits(maxiter*2,maxfun)
solver.SetGenerationMonitor(monitor)
solver.Solve(cost)
assert len(monitor) > lmon
assert solver.bestEnergy == monitor.y[-1]
for xs,x in zip(solver.bestSolution,monitor.x[-1]):
assert xs == x
solver.SetEvaluationLimits(maxiter*3,maxfun)
solver.SetGenerationMonitor(monitor, new=True)
solver.Solve(cost)
assert solver.bestEnergy == monitor.y[-1]
for xs,x in zip(solver.bestSolution,monitor.x[-1]):
assert xs == x
# EOF
|