if __name__ == '__main__': print("Differential Evolution") print("======================") # set range for random initial guess ndim = 9 x0 = [(-100, 100)] * ndim random_seed(321) # draw frame and exact coefficients plot_exact() # use DE to solve 8th-order Chebyshev coefficients npop = 10 * ndim solution = diffev(chebyshev8cost, x0, npop) # use pretty print for polynomials print(poly1d(solution)) # compare solution with actual 8th-order Chebyshev coefficients print("\nActual Coefficients:\n %s\n" % poly1d(chebyshev8coeffs)) # plot solution versus exact coefficients plot_solution(solution) getch() # end of file
def cost_function(params): x = data(params)[1] - datapts return numpysum(real((conjugate(x)*x))) if __name__ == '__main__': plotview = [-60,60, 0,2500] target = [1., 2., 1.] x,datapts = data(target) #myCost = cost_function ##myCost = PolyCostFactory(target,x) F = CostFactory() F.addModel(ForwardPolyFactory,'poly',len(target)) myCost = F.getCostFunction(evalpts=x, observations=datapts) import pylab pylab.ion() print "target: ",target plot_sol(target,'r-') solution, stepmon = de_solve(myCost) print "solution: ",solution plot_sol(solution,'g-') print "" # print "at step 10: ",stepmon.x[10] getch() # End of file
#!/usr/bin/env python # # Author: Patrick Hung (patrickh @caltech) # Copyright (c) 1997-2016 California Institute of Technology. # Copyright (c) 2016-2022 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.tools import getch import Gnuplot, numpy g = Gnuplot.Gnuplot(debug = 1) g.clear() x = numpy.arange(-4, 4, 0.01) y = numpy.cos(x) y2 = numpy.cos(2* x) kwds = {'with':'line'} g.plot(Gnuplot.Data(x, y, **kwds)) getch('next: any key') g.plot(Gnuplot.Data(x, y2, **kwds)) getch('any key to quit') # end of file
if __name__ == '__main__': print("Powell's Method") print("===============") # initial guess import random from mystic.tools import random_seed random_seed(123) ndim = 9 x0 = [random.uniform(-100, 100) for i in range(ndim)] # draw frame and exact coefficients plot_exact() # use Powell's method to solve 8th-order Chebyshev coefficients solution = fmin_powell(chebyshev8cost, x0) # use pretty print for polynomials print(poly1d(solution)) # compare solution with actual 8th-order Chebyshev coefficients print("\nActual Coefficients:\n %s\n" % poly1d(chebyshev8coeffs)) # plot solution versus exact coefficients plot_solution(solution) getch() #XXX: or plt.show() ? # end of file
Testing the Corana parabola in 1D. Requires sam. """ import sam, numpy, mystic #from test_corana import * from mystic.solvers import fmin from mystic.tools import getch from mystic.models.corana import corana1d as Corana1 x = numpy.arange(-2., 2., 0.01) y = [Corana1([c]) for c in x] sam.put('x', x) sam.put('y', y) sam.eval("plot(x,y,'LineWidth',1); hold on") for xinit in numpy.arange(0.1,2,0.1): sol = fmin(Corana1, [xinit], full_output=1, retall=1) xx = mystic.flatten_array(sol[-1]) yy = [Corana1([c]) for c in xx] sam.put('xx', xx) sam.put('yy', yy) sam.eval("plot(xx,yy,'r-',xx,yy,'ko','LineWidth',2)") sam.eval("axis([0 2 0 4])") getch('press any key to exit') # end of file
sam.putarray('X', x) sam.putarray('Y', y) sam.putarray('C', c) sam.verbose() sam.eval("[c,h]=contourf(X,Y,log(C*20+1)+2,100);set(h,'EdgeColor','none')") sam.eval("title('Zimmermann''s Corner. Min at 7,2')") sam.eval('hold on') def run_once(): simplex = Monitor() solver = fmin(2) solver.SetRandomInitialPoints([0, 0], [7, 7]) solver.SetGenerationMonitor(simplex) solver.Solve(CostFunction, termination=CRT()) sol = solver.Solution() for x in simplex.x: sam.putarray('x', x) sam.eval("plot(x([1,2,3,1],1),x([1,2,3,1],2),'k-')") draw_contour() for i in range(8): run_once() getch("Press any key to quit") # end of file
sam.putarray('X',x) sam.putarray('Y',y) sam.putarray('C',c) sam.verbose() sam.eval("[c,h]=contourf(X,Y,C,100);set(h,'EdgeColor','none')") #sam.eval("[c,h]=contourf(X,Y,log(C*20+1)+2,100);set(h,'EdgeColor','none')") sam.eval("title('Corana''s Parabola in 2D. Min at 0,0')") sam.eval('hold on') def run_once(): simplex = Monitor() solver = fmin(2) solver.SetRandomInitialPoints([0,0],[2,2]) solver.SetGenerationMonitor(simplex) solver.Solve(Corana2, termination=CRT()) sol = solver.Solution() for x in simplex.x: sam.putarray('x',x) sam.eval("plot(x([1,2,3,1],1),x([1,2,3,1],2),'w-')") draw_contour() for i in range(8): run_once() getch("Press any key to quit") # end of file
if __name__ == '__main__': print("Powell's Method") print("===============") # initial guess import random from mystic.tools import random_seed random_seed(123) ndim = 9 x0 = [random.uniform(-100,100) for i in range(ndim)] # draw frame and exact coefficients plot_exact() # use Powell's method to solve 8th-order Chebyshev coefficients solution = fmin_powell(chebyshev8cost,x0) # use pretty print for polynomials print(poly1d(solution)) # compare solution with actual 8th-order Chebyshev coefficients print("\nActual Coefficients:\n %s\n" % poly1d(chebyshev8coeffs)) # plot solution versus exact coefficients plot_solution(solution) getch() #XXX: or plt.show() ? # end of file
if __name__ == '__main__': print("Powell's Method") print("===============") # initial guess import random from mystic.tools import random_seed random_seed(123) ndim = 9 x0 = [random.uniform(-100, 100) for i in range(ndim)] # draw frame and exact coefficients plot_exact() # use Powell's method to solve 8th-order Chebyshev coefficients solution = fmin_powell(chebyshev8cost, x0) # use pretty print for polynomials print(poly1d(solution)) # compare solution with actual 8th-order Chebyshev coefficients print("\nActual Coefficients:\n %s\n" % poly1d(chebyshev8coeffs)) # plot solution versus exact coefficients plot_solution(solution) getch() #XXX: or pylab.show() ? # end of file
if __name__ == '__main__': print "Powell's Method" print "===============" # initial guess import random from mystic.tools import random_seed random_seed(123) ndim = 9 x0 = [random.uniform(-100,100) for i in range(ndim)] # draw frame and exact coefficients plot_exact() # use Powell's method to solve 8th-order Chebyshev coefficients solution = fmin_powell(chebyshev8cost,x0) # use pretty print for polynomials print poly1d(solution) # compare solution with actual 8th-order Chebyshev coefficients print "\nActual Coefficients:\n %s\n" % poly1d(chebyshev8coeffs) # plot solution versus exact coefficients plot_solution(solution) getch() #XXX: or pylab.show() ? # end of file
#!/usr/bin/env python # # Author: Patrick Hung (patrickh @caltech) # Copyright (c) 1997-2014 California Institute of Technology. # License: 3-clause BSD. The full license text is available at: # - http://trac.mystic.cacr.caltech.edu/project/mystic/browser/mystic/LICENSE from mystic.tools import getch import Gnuplot, numpy g = Gnuplot.Gnuplot(debug = 1) g.clear() x = numpy.arange(-4, 4, 0.01) y = numpy.cos(x) y2 = numpy.cos(2* x) g.plot(Gnuplot.Data(x, y, with='line')) getch('next: any key') g.plot(Gnuplot.Data(x, y2, with='line')) getch('any key to quit') # end of file