1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2// Copyright (C) 2008-2009 - INRIA - Michael Baudin
3// Copyright (C) 2011 - DIGITEO - Michael Baudin
4//
5// Copyright (C) 2012 - 2016 - Scilab Enterprises
6//
7// This file is hereby licensed under the terms of the GNU GPL v2.0,
8// pursuant to article 5.3.4 of the CeCILL v.2.1.
9// This file was originally licensed under the terms of the CeCILL v2.1,
10// and continues to be available under such terms.
11// For more information, see the COPYING file which you should have received
12// along with this program.
13
14// <-- CLI SHELL MODE -->
15// <-- ENGLISH IMPOSED -->
16
17
18
19//
20// The _MYDATA_ variable name is chosen so that
21// no name conflict can possibly occur.
22//
23function [ y , index ] = rosenbrock ( x , index )
24  a = _MYDATA_.a
25  y = 100*(x(2)-x(1)^2)^2 + ( a - x(1))^2;
26  _MYDATA_.nb = _MYDATA_.nb + 1
27endfunction
28
29x0 = [11.0 140.0]';
30
31//
32// Test with an additional argument
33//
34_MYDATA_ = tlist(["T_MYSTUFF","a","nb"]);
35_MYDATA_.a = 12.0;
36_MYDATA_.nb = 0;
37
38nm = neldermead_new ();
39nm = neldermead_configure(nm,"-numberofvariables",2);
40nm = neldermead_configure(nm,"-function",rosenbrock);
41nm = neldermead_configure(nm,"-x0",x0);
42nm = neldermead_configure(nm,"-maxfunevals",%inf);
43nm = neldermead_configure(nm,"-maxiter",10);
44nm = neldermead_search(nm, "off");
45iter = neldermead_get(nm,"-iterations");
46assert_checkequal ( iter , 10 );
47// Check _MYDATA_.nb
48// The variable is just read, not written and the nb field
49// is not updated.
50assert_checkequal ( _MYDATA_.nb , 0 );
51// Cleanup
52nm = neldermead_destroy(nm);
53
54//
55// In this case, the mydata variable is passed
56// explicitely by the neldermead class.
57// So the actual name "mydata" does not matter
58// and whatever variable name can be used.
59//
60function [ y , index ] = rosenbrock2 ( x , index , mydata )
61  a = mydata.a
62  y = 100*(x(2)-x(1)^2)^2 + ( a - x(1))^2;
63endfunction
64
65//
66// Test with an additional argument
67//
68mystuff = tlist(["T_MYSTUFF","a"]);
69mystuff.a = 12.0;
70
71nm = neldermead_new ();
72nm = neldermead_configure(nm,"-numberofvariables",2);
73nm = neldermead_configure(nm,"-function",list(rosenbrock2,mystuff));
74nm = neldermead_configure(nm,"-x0",x0);
75nm = neldermead_configure(nm,"-maxfunevals",%inf);
76nm = neldermead_configure(nm,"-maxiter",10);
77nm = neldermead_search(nm, "off");
78iter = neldermead_get(nm,"-iterations");
79assert_checkequal ( iter , 10 );
80nm = neldermead_destroy(nm);
81
82//
83// Use a global variable.
84//
85function [ y , index ] = rosenbrock3 ( x , index )
86  global _MYDATA_
87  a = _MYDATA_.a
88  y = 100*(x(2)-x(1)^2)^2 + ( a - x(1))^2;
89  _MYDATA_.nb = _MYDATA_.nb + 1
90endfunction
91
92//
93// Test with an additional argument
94//
95global _MYDATA_
96_MYDATA_ = tlist(["T_MYSTUFF","a","nb"]);
97_MYDATA_.a = 12.0;
98_MYDATA_.nb = 0;
99
100nm = neldermead_new ();
101nm = neldermead_configure(nm,"-numberofvariables",2);
102nm = neldermead_configure(nm,"-function",rosenbrock3);
103nm = neldermead_configure(nm,"-x0",x0);
104nm = neldermead_configure(nm,"-maxfunevals",%inf);
105nm = neldermead_configure(nm,"-maxiter",10);
106nm = neldermead_search(nm, "off");
107iter = neldermead_get(nm,"-iterations");
108assert_checkequal ( iter , 10 );
109// Check _MYDATA_.nb
110// The variable is not just read, it is also written and the nb field
111// IS updated.
112assert_checkequal ( _MYDATA_.nb > 10 , %T );
113nm = neldermead_destroy(nm);
114
115
116
117
118