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 | """ |
---|

8 | Example: |
---|

9 | - Minimize Rosenbrock's Function with Nelder-Mead. |
---|

10 | - Plot of Rosenbrock's function minimum. |
---|

11 | |
---|

12 | Demonstrates: |
---|

13 | - standard models |
---|

14 | - minimal solver interface |
---|

15 | """ |
---|

16 | |
---|

17 | # Nelder-Mead solver |
---|

18 | from mystic.solvers import fmin |
---|

19 | |
---|

20 | # Rosenbrock function |
---|

21 | from mystic.models import rosen |
---|

22 | |
---|

23 | # tools |
---|

24 | import pylab |
---|

25 | |
---|

26 | |
---|

27 | if __name__ == '__main__': |
---|

28 | |
---|

29 | print "Nelder-Mead Simplex" |
---|

30 | print "===================" |
---|

31 | |
---|

32 | # initial guess |
---|

33 | x0 = [0.8,1.2,0.7] |
---|

34 | |
---|

35 | # use Nelder-Mead to minimize the Rosenbrock function |
---|

36 | solution = fmin(rosen,x0) |
---|

37 | print solution |
---|

38 | |
---|

39 | # plot the Rosenbrock function (one plot per axis) |
---|

40 | x = [0.01*i for i in range(200)] |
---|

41 | pylab.plot(x,[rosen([i,1.,1.]) for i in x]) |
---|

42 | pylab.plot(x,[rosen([1.,i,1.]) for i in x]) |
---|

43 | pylab.plot(x,[rosen([1.,1.,i]) for i in x]) |
---|

44 | |
---|

45 | # plot the solved minimum (for x) |
---|

46 | pylab.plot([solution[0]],[rosen(solution)],'bo') |
---|

47 | |
---|

48 | # draw the plot |
---|

49 | pylab.title("minimium of Rosenbrock's function") |
---|

50 | pylab.xlabel("x, y, z") |
---|

51 | pylab.ylabel("f(i) = Rosenbrock's function") |
---|

52 | pylab.legend(["f(x,1,1)","f(1,y,1)","f(1,1,z)"]) |
---|

53 | pylab.show() |
---|

54 | |
---|

55 | # end of file |
---|