1+ import com .nag .routines .E04 .E04GN ; // nagf_opt_handle_solve_nldf
2+ import com .nag .routines .E04 .E04GNU ; // monit
3+ import com .nag .routines .E04 .E04GNX ; // confun dummy
4+ import com .nag .routines .E04 .E04GNY ; // congrd dummy
5+ import com .nag .routines .E04 .E04RA ; // Handle init
6+ import com .nag .routines .E04 .E04RH ; // box bounds
7+ import com .nag .routines .E04 .E04RJ ; // linear constraints
8+ import com .nag .routines .E04 .E04RM ; // add model and residual sparsity structure
9+ import com .nag .routines .E04 .E04RZ ; // destroy handle
10+ import com .nag .routines .E04 .E04ZM ; // optional parameters
11+
12+ import java .lang .Math ;
13+ import java .util .Arrays ;
14+
15+
16+ public class GenDFEx {
17+
18+ public static void main (String [] args ) {
19+
20+ E04GN e04gn = new E04GN (); // the solver
21+ E04RA e04ra = new E04RA (); // the handle initializer
22+ E04RM e04rm = new E04RM (); // for setting model and residual sparsity structure
23+ E04ZM e04zm = new E04ZM (); // for setting optional parameters
24+ E04RZ e04rz = new E04RZ (); // handle destroyer
25+
26+
27+ MONIT monit = new MONIT (); // defined below using E04GNU
28+ CONFUN confun = new CONFUN (); // defined below using E04GNX (dummy)
29+ CONGRD congrd = new CONGRD (); // defined below using E04GNY (dummy)
30+
31+
32+ // Set up data and initial handle parameters
33+ double [] t = linspace (0.5 , 2.5 , 21 );
34+ double [] ruser1 = toydata1 (t ); // For Example 1
35+ double [] ruser2 = toydata2 (t ); // For Example 2
36+
37+ double [] x = new double [2 ]; // instantiate an array for as many variable you need
38+ long handle = 0 ;
39+ int nvar = x .length ;
40+ int ifail ;
41+ int nres = t .length ;
42+
43+ // Init for sparsity structure
44+ int isparse = 0 ;
45+ int nnzrd = 0 ;
46+ int [] irowrd = new int [nnzrd ];
47+ int [] icolrd = new int [nnzrd ];
48+
49+
50+
51+ // Get handle
52+ ifail = 0 ;
53+ e04ra .eval (handle , nvar , ifail );
54+ handle = e04ra .getHANDLE ();
55+
56+ // Define the residual functions and sparsity structure
57+ ifail = 0 ;
58+ e04rm .eval (handle , nres , isparse , nnzrd , irowrd , icolrd , ifail );
59+
60+ // Set options
61+ ifail = 0 ;
62+ e04zm .eval (handle , "NLDF Loss Function Type = L2" , ifail );
63+ e04zm .eval (handle , "Print Level = 1" , ifail );
64+ e04zm .eval (handle , "Print Options = No" , ifail );
65+ e04zm .eval (handle , "Print Solution = Yes" , ifail );
66+
67+ // Initialize all the remaining parameters
68+ LSQFUN lsqfun = new LSQFUN ();
69+ LSQGRD lsqgrd = new LSQGRD ();
70+ double [] rx = new double [nres ];
71+ double [] rinfo = new double [100 ];
72+ double [] stats = new double [100 ];
73+ int [] iuser = new int [0 ];
74+ long cpuser = 0 ;
75+
76+ // Solve
77+ System .out .println ("\n ----Solving Toy Dataset #1 with L2 Loss Function----" );
78+ ifail = 0 ;
79+ x = init_x (); //give x the initial guess you want to start from
80+ // NOTE: x will be changed during solve
81+ e04gn .eval (handle , lsqfun , lsqgrd , confun , congrd , monit , nvar , x , nres , rx , rinfo ,
82+ stats , iuser , ruser1 , cpuser , ifail );
83+
84+ System .out .println ("\n ----Solving Toy Dataset #1 with L1 Loss Function----" );
85+ ifail = 0 ;
86+ x = init_x ();
87+ e04zm .eval (handle , "NLDF Loss Function Type = L1" , ifail );
88+ e04gn .eval (handle , lsqfun , lsqgrd , confun , congrd , monit , nvar , x , nres , rx , rinfo ,
89+ stats , iuser , ruser1 , cpuser , ifail );
90+
91+
92+
93+ // The trade-off of a loss function
94+ // The handle can keep getting used. We are only changing the data passed to the
95+ // solver using ruser2 (first 3 and last 3 data points different from middle)
96+ System .out .println ("\n ----Solving Toy Dataset #2 with L2 Loss Function----" );
97+ ifail = 0 ;
98+ x = init_x ();
99+ e04zm .eval (handle , "NLDF Loss Function Type = L2" , ifail );
100+ e04gn .eval (handle , lsqfun , lsqgrd , confun , congrd , monit , nvar , x , nres , rx , rinfo ,
101+ stats , iuser , ruser2 , cpuser , ifail );
102+
103+ System .out .println ("\n ----Solving Toy Dataset #2 with L1 Loss Function----" );
104+ ifail = 0 ;
105+ x = init_x ();
106+ e04zm .eval (handle , "NLDF Loss Function Type = L1" , ifail );
107+ e04gn .eval (handle , lsqfun , lsqgrd , confun , congrd , monit , nvar , x , nres , rx , rinfo ,
108+ stats , iuser , ruser2 , cpuser , ifail );
109+
110+ System .out .println ("\n ----Solving Toy Dataset #2 with ATAN Loss Function----" );
111+ ifail = 0 ;
112+ x = init_x ();
113+ e04zm .eval (handle , "NLDF Loss Function Type = ATAN" , ifail );
114+ e04gn .eval (handle , lsqfun , lsqgrd , confun , congrd , monit , nvar , x , nres , rx , rinfo ,
115+ stats , iuser , ruser2 , cpuser , ifail );
116+
117+ e04rz .eval (handle ,ifail ); // Destroy the handle
118+
119+ }
120+
121+
122+ public static class LSQFUN extends E04GN .Abstract_E04GN_LSQFUN {
123+ public void eval () {
124+ for (int i = 0 ; i < NRES ; i ++){
125+ this .RX [i ] = RUSER [NRES + i ] - X [0 ] * Math .sin (X [1 ] * RUSER [i ]);
126+ }
127+ }
128+ }
129+
130+ public static class LSQGRD extends E04GN .Abstract_E04GN_LSQGRD {
131+ public void eval () {
132+ for (int i = 0 ; i < NRES ; i ++){
133+ this .RDX [i * NVAR ] = (-1 * Math .sin (X [1 ]*RUSER [i ]));
134+ this .RDX [i * NVAR + 1 ] = (-1 * RUSER [i ] * X [0 ] * Math .cos (X [1 ] * RUSER [i ]));
135+ }
136+ }
137+ }
138+
139+ // Dummy Functions required for NLDF solver
140+ public static class CONFUN extends E04GN .Abstract_E04GN_CONFUN {
141+ public void eval (){
142+ this .eval ();
143+ }
144+ }
145+
146+ public static class CONGRD extends E04GN .Abstract_E04GN_CONGRD {
147+ public void eval (){
148+ this .eval ();
149+ }
150+ }
151+
152+ public static class MONIT extends E04GN .Abstract_E04GN_MONIT {
153+ public void eval (){
154+ this .eval ();
155+ }
156+ }
157+
158+ // Utilities for setting up data for problem
159+ public static double [] linspace (double startPoint , double endPoint , int length ) {
160+ double [] a = new double [length ];
161+ double step = (endPoint - startPoint ) / (length - 1 );
162+ a [0 ] = startPoint ;
163+ a [length - 1 ] = endPoint ;
164+ for (int i = 1 ; i < length - 1 ; i ++) {
165+ a [i ] = startPoint + i * step ;
166+ }
167+ return a ;
168+ }
169+
170+ public static double [] toydata1 (double [] t ) {
171+ double [] y = new double [t .length * 2 ];
172+ for (int i = 0 ; i < t .length * 2 ; i ++){
173+ if (i < t .length ){
174+ y [i ] = t [i ];
175+ }
176+ else {
177+ y [i ] = Math .sin (t [i -t .length ]);
178+ if (i - t .length == 10 ){
179+ y [i ] = 5 * y [i ];
180+ }
181+ }
182+ }
183+ return y ;
184+ }
185+
186+ public static double [] toydata2 (double [] t ) {
187+ double [] y = new double [t .length * 2 ];
188+ for (int i = 0 ; i < t .length * 2 ; i ++){
189+ if (i < t .length ){
190+ y [i ] = t [i ];
191+ }
192+ else {
193+ y [i ] = Math .sin (t [i -t .length ]);
194+ if ((i - t .length >= 3 ) && (i - t .length < 18 )){
195+ y [i ] = 5 * y [i ];
196+ }
197+ }
198+ }
199+ return y ;
200+ }
201+
202+ // For resetting the initial guess
203+ public static double [] init_x () {
204+ double [] x = new double [] {2.1 ,1.4 };
205+ return x ;
206+ }
207+ }
0 commit comments