Skip to content

Commit 66ea9d0

Browse files
Adding NLDF example
images, source code (.java), and Readme
1 parent f625b3f commit 66ea9d0

File tree

15 files changed

+549
-0
lines changed

15 files changed

+549
-0
lines changed

NLDF/GenDFEx.java

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

0 commit comments

Comments
 (0)