source: mystic/examples/example10.py @ 855

Revision 855, 2.9 KB checked in by mmckerns, 5 months ago (diff)

updated copyright to 2016

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2#
3# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
4# Copyright (c) 1997-2016 California Institute of Technology.
5# License: 3-clause BSD.  The full license text is available at:
6#  - http://trac.mystic.cacr.caltech.edu/project/mystic/browser/mystic/LICENSE
7"""
8Example:
9    - Solve 8th-order Chebyshev polynomial coefficients with DE.
10    - Plot (x2) of convergence to Chebyshev polynomial.
11    - Monitor (x2) Chi-Squared for Chebyshev polynomial.
12
13Demonstrates:
14    - standard models
15    - expanded solver interface
16    - built-in random initial guess
17    - customized monitors and termination conditions
18    - customized DE mutation strategies
19    - use of solver members to retrieve results information
20"""
21
22# Differential Evolution solver
23from mystic.solvers import DifferentialEvolutionSolver2
24
25# Chebyshev polynomial and cost function
26from mystic.models.poly import chebyshev8, chebyshev8cost
27from mystic.models.poly import chebyshev8coeffs
28
29# tools
30from mystic.termination import VTR
31from mystic.strategy import Best1Exp
32from mystic.monitors import VerboseMonitor, Monitor
33from mystic.tools import getch, random_seed
34from mystic.math import poly1d
35import pylab
36pylab.ion()
37
38# draw the plot
39def plot_frame(label=None):
40    pylab.close()
41    pylab.title("8th-order Chebyshev coefficient convergence")
42    pylab.xlabel("Differential Evolution %s" % label)
43    pylab.ylabel("Chi-Squared")
44    pylab.draw()
45    return
46 
47# plot the polynomial trajectories
48def plot_params(monitor):
49    x = range(len(monitor))
50    y = monitor.y
51    pylab.plot(x,y,'b-')
52    pylab.axis([1,0.5*x[-1],0,y[1]],'k-')
53    pylab.draw()
54    return
55
56
57if __name__ == '__main__':
58
59    print "Differential Evolution"
60    print "======================"
61
62    # set range for random initial guess
63    ndim = 9
64    x0 = [(-100,100)]*ndim
65    random_seed(123)
66
67    # configure monitors
68    stepmon = VerboseMonitor(50)
69    evalmon = Monitor()
70
71    # use DE to solve 8th-order Chebyshev coefficients
72    npop = 10*ndim
73    solver = DifferentialEvolutionSolver2(ndim,npop)
74    solver.SetRandomInitialPoints(min=[-100]*ndim, max=[100]*ndim)
75    solver.SetEvaluationLimits(generations=999)
76    solver.SetEvaluationMonitor(evalmon)
77    solver.SetGenerationMonitor(stepmon)
78    solver.enable_signal_handler()
79    solver.Solve(chebyshev8cost, termination=VTR(0.01), strategy=Best1Exp, \
80                 CrossProbability=1.0, ScalingFactor=0.9)
81    solution = solver.bestSolution
82
83    # get solved coefficients and Chi-Squared (from solver members)
84    iterations = solver.generations
85    cost = solver.bestEnergy
86    print "Generation %d has best Chi-Squared: %f" % (iterations, cost)
87    print "Solved Coefficients:\n %s\n" % poly1d(solver.bestSolution)
88
89    # plot convergence of coefficients per iteration
90    plot_frame('iterations')
91    plot_params(stepmon)
92    getch()
93
94    # plot convergence of coefficients per function call
95    plot_frame('function calls')
96    plot_params(evalmon)
97    getch()
98
99# end of file
Note: See TracBrowser for help on using the repository browser.