CONOPT
Loading...
Searching...
No Matches
leastsq10.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
14public class leastsq10 {
15 public static void main(String argv[]) {
16 System.loadLibrary("conoptjni4");
17
18 String name = "leastsq10";
19
20 Conopt conopt = new Conopt(name);
21 LeastSq10ModelData model = new LeastSq10ModelData(700, 500);
23
24 model.buildModel();
25
26 conopt.loadModel(model);
27 conopt.setMessageHandler(msghdlr);
28
29 // try to set the license using the environment variables
30 try {
31 int license_int_1 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_1"));
32 int license_int_2 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_2"));
33 int license_int_3 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_3"));
34 String license_text = System.getenv("CONOPT_LICENSE_TEXT");
35
38 } catch (Exception e) {
39 System.out.println("Unable to set license: " + e.getMessage());
40 }
41
42 conopt.solve();
43
44 conopt.printStatus();
45
46 std s = new std();
47 int retcode = s.checkSolve(name, conopt.modelStatus(), conopt.solutionStatus(),
48 conopt.objectiveValue(), 19.4443, 0.001);
49
50 msghdlr.close();
51
52 System.exit(retcode);
53 }
54}
55
56class LeastSq10ModelData extends ModelData {
57 private int seed = 12359;
58
59 // Model Parameters
60 public double[] A; // length = nobs * dimx
61 public double[] B; // length = nobs * dimx
62 public double[] C; // length = nobs * dimx
63 public double[] Obs; // length = nobs
64 public int nobs;
65 public int dimx;
66
67 // Declaring the variable and constraint indices
68 public int[] varx; // length = dimx
69 public int[] varres; // length = nobs
70 public int consobj;
71 public int[] consresidual; // length = nobs
72
73
74 public LeastSq10ModelData(int numobs, int dimensionx) {
75 super();
76
77 this.nobs = numobs;
78 this.dimx = dimensionx;
79
80 this.A = new double[nobs * dimx];
81 this.B = new double[nobs * dimx];
82 this.C = new double[nobs * dimx];
83 this.Obs = new double[nobs];
84
85 defineData();
86 }
87
88 // @brief Defines a pseudo random number between 0 and 1
89 private double rndx() {
90 seed = seed * 1027 + 25;
91 int times = seed / 1_048_576;
92 seed = seed - 1_048_576 * times;
93 return (double) seed / 1_048_576.0;
94 }
95
96 // @brief Defines the data for the problem
97 private void defineData() {
98 final double Xtarg = -1.0;
99 final double Noise = 1.0;
100
101 int k = 0;
102 for (int i = 0; i < nobs; ++i) {
103 double O = 0.0;
104 for (int j = 0; j < dimx; ++j) {
105 A[k] = rndx();
106 B[k] = rndx();
107 O += A[k] * Xtarg + B[k] * (Xtarg * Xtarg);
108 ++k;
109 }
110 Obs[i] = O + Noise * rndx();
111 }
112 }
113
118 public void buildModel() {
119 // adding the variables to the model
120 List<Integer> varxList = new ArrayList<>(dimx);
121 for (int i = 0; i < dimx; ++i) {
122 int varidx = addVariable(-Conopt.Infinity, Conopt.Infinity, -0.8);
123 varxList.add(varidx);
124 }
125
126 List<Integer> varresList = new ArrayList<>(nobs);
127 for (int i = 0; i < nobs; ++i) {
128 int varidx = addVariable(-Conopt.Infinity, Conopt.Infinity, 0.0);
129 varresList.add(varidx);
130 }
131
132 this.varx = varxList.stream().mapToInt(Integer::intValue).toArray();
133 this.varres = varresList.stream().mapToInt(Integer::intValue).toArray();
134
135
136
137 // adding the constraints to the model
138 this.consresidual = new int[nobs];
139 for (int i = 0; i < nobs; ++i) {
140
141 int m = dimx + 1; // +1 for the residual var
142 int[] varidx = new int[m];
143 double[] coeffs = new double[m];
144 int[] nlf = new int[m];
145
146 for (int j = 0; j < dimx; ++j) {
147 varidx[j] = varx[j];
148 coeffs[j] = 0.0;
149 nlf[j] = 1;
150 }
151
152 // Add residual var linearly with coefficient -1
153 varidx[dimx] = varres[i];
154 coeffs[dimx] = -1.0;
155 nlf[dimx] = 0;
156
157 int considx = addConstraint(ConstraintType.Eq, Obs[i], varidx, coeffs, nlf);
158 consresidual[i] = considx;
159 }
160
161 // Objective function
162 double[] objCoeffs = new double[nobs];
163 int[] objNlf = new int[nobs];
164 for (int i = 0; i < nobs; ++i) {
165 objCoeffs[i] = 1.0;
166 objNlf[i] = 1; // residuals appear nonlinearly in the objective (r_i^2)
167 }
168
169 consobj = addConstraint(ConstraintType.Free, 0.0, varres, objCoeffs, objNlf);
170
171 // setting the objective constraint
173
174 // setting the optimisation direction
176
177 // setting the second derivative evaluation type
179 }
180
185 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
186 double g = 0;
187 if (rowno == consobj) {
188 double sum = 0.0;
189 for (int i = 0; i < nobs; i++) {
190 sum += Math.pow(x[varres[i]], 2);
191 }
192 g = sum;
193 }
194 else {
195 int k = rowno * dimx;
196 double sum = 0.0;
197 for (int i = 0; i < dimx; i++) {
198 sum += A[k] * x[varx[i]] + B[k] * Math.pow(x[varx[i]], 2);
199 k++;
200 }
201 g = sum;
202 }
203
204 return g;
205 }
206
211 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
212 assert x.length == jac.length;
213
214 if (rowno == consobj) {
215 for (int i = 0; i < nobs; i++) {
216 jac[varres[i]] = 2 * x[varres[i]];
217 }
218 }
219 else {
220 int k = rowno * dimx;
221 for (int i = 0; i < dimx; i++) {
222 jac[varx[i]] = A[k] + 2 * B[k] * x[varx[i]];
223 k++;
224 }
225 }
226 }
227
232 public void initDirectionalSDEval(double[] x, double[] dx, int[] rowlist, int numthread, boolean newpoint) {
233 // Constraint row with b(i,j)*x(j)**2 as only nonlinear term.
234 // Compute the directional term 2*b(i,j)*dx(j) and keep for later
235
236 int k = 0;
237 for (int i = 0; i < nobs; i++) {
238 for (int j = 0; j < dimx; j++) {
239 C[k] = 2.0 * B[k] * dx[varx[j]];
240 k++;
241 }
242 }
243 }
244
245
250 public void evaluateDirectionalSD(double[] x, double[] dx, double[] d2g, int rowno, int[] jacnum, int thread) {
251 assert x.length == d2g.length;
252 assert dx.length == d2g.length;
253
254 if (rowno == nobs) {
255 /* Objective row: sum(i, res(i)**2 ) */
256
257 for (int i = 0; i < dimx; i++)
258 d2g[varx[i]] = 0;
259 for (int i = 0; i < nobs; i++)
260 d2g[varres[i]] = 2.0 * dx[varres[i]];
261 }
262 else {
263 /* Constraint row with b(i,j)*x(j)**2 as only nonlinear term */
264
265 int k = rowno * dimx;
266 for (int i = 0; i < dimx; i++) {
267 d2g[varx[i]] = C[k];
268 k++;
269 }
270 for (int i = 0; i < nobs; i++)
271 d2g[varres[i]] = 0;
272 }
273 }
274}
The Conopt class.
Definition conopt.py:1380
static final ConstraintType Eq
static final ConstraintType Free
A class that can be extended to build and solve a model using Conopt.
Definition conopt.py:2407
static final ObjectiveElement Constraint
static final SDEvaluationType Constraint
static final Sense Minimize
Definition Sense.java:25
static void main(String argv[])
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:20
addConstraint(self, *args)
Overload 1: adds a constraint to the problem.
Definition conopt.py:2621
setObjectiveElement(self, elem, elemindex)
sets the index for the objective variable or constraint
Definition conopt.py:2766
addVariable(self, *args)
Overload 1: adds a variable to the model.
Definition conopt.py:2677
setOptimizationSense(self, sense)
sets the optimisation direction.
Definition conopt.py:2775
setSDEvaluationType(self, sdevaltype)
informs CONOPT of the method for evaluating the second derivative
Definition conopt.py:2880
void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
void buildModel()
adds variables and constraints to the model
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row
void evaluateDirectionalSD(double[] x, double[] dx, double[] d2g, int rowno, int[] jacnum, int thread)
computes the directional second derivative for a single constraint
void initDirectionalSDEval(double[] x, double[] dx, int[] rowlist, int numthread, boolean newpoint)
a callback for the initialisation of the second derivative evaluation.
Definition std.py:1
real *8, parameter k
Definition tutoriali.f90:30