1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | # Author: Patrick Hung (patrickh @caltech) |
---|
4 | # Copyright (c) 1997-2016 California Institute of Technology. |
---|
5 | # License: 3-clause BSD. The full license text is available at: |
---|
6 | # - http://mmckerns.github.io/project/mystic/browser/mystic/LICENSE |
---|
7 | """ |
---|
8 | See test_zimmermann.py. |
---|
9 | |
---|
10 | This one uses Scipy's CG (Polak-Ribiere) plus matlab viz. |
---|
11 | """ |
---|
12 | |
---|
13 | import sam |
---|
14 | from test_zimmermann import * |
---|
15 | from scipy.optimize import fmin_cg |
---|
16 | import numpy |
---|
17 | from mystic.tools import getch |
---|
18 | |
---|
19 | def draw_contour(): |
---|
20 | import numpy |
---|
21 | |
---|
22 | x, y = numpy.mgrid[-.51:7.5:0.05,-.51:7.5:0.05] |
---|
23 | c = 0*x |
---|
24 | s,t = x.shape |
---|
25 | for i in range(s): |
---|
26 | for j in range(t): |
---|
27 | xx,yy = x[i,j], y[i,j] |
---|
28 | c[i,j] = CostFunction([xx,yy]) |
---|
29 | |
---|
30 | |
---|
31 | sam.putarray('X',x) |
---|
32 | sam.putarray('Y',y) |
---|
33 | sam.putarray('C',c) |
---|
34 | |
---|
35 | sam.verbose() |
---|
36 | sam.eval("[c,h]=contourf(X,Y,log(C*20+1)+2,100);set(h,'EdgeColor','none')") |
---|
37 | sam.eval("title('Zimmermann''s Corner. Min at 7,2')") |
---|
38 | sam.eval('hold on') |
---|
39 | |
---|
40 | |
---|
41 | def run_once(x0,x1): |
---|
42 | sol = fmin_cg(CostFunction, [x0, x1], retall = True, full_output=1) |
---|
43 | xy = numpy.asarray(sol[-1]) |
---|
44 | sam.putarray('xy',xy) |
---|
45 | sam.eval("plot(xy(:,1),xy(:,2),'w-','LineWidth',2)") |
---|
46 | sam.eval("plot(xy(:,1),xy(:,2),'wo','MarkerSize',6)") |
---|
47 | return sol |
---|
48 | |
---|
49 | if __name__ == '__main__': |
---|
50 | draw_contour() |
---|
51 | run_once(1,3) |
---|
52 | run_once(4,2) |
---|
53 | run_once(7,0.1) |
---|
54 | run_once(6,4) |
---|
55 | run_once(0,7) |
---|
56 | |
---|
57 | getch("Press any key to quit") |
---|
58 | |
---|
59 | # end of file |
---|