CONOPT
Loading...
Searching...
No Matches
leastsq5.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
14public class leastsq5 {
15 public static void main(String argv[]) {
16 System.loadLibrary("conoptjni4");
17
18 String name = "leastsq5";
19
20 Conopt conopt = new Conopt(name);
21 LeastSq5ModelData model = new LeastSq5ModelData(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 LeastSq5ModelData 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[] Obs; // length = nobs
63 public int nobs;
64 public int dimx;
65
66 // Declaring the variable and constraint indices
67 public int[] varx; // length = dimx
68 public int[] varres; // length = nobs
69 public int consobj;
70 public int[] consresidual; // length = nobs
71
72
73 public LeastSq5ModelData(int numobs, int dimensionx) {
74 super();
75
76 this.nobs = numobs;
77 this.dimx = dimensionx;
78
79 this.A = new double[nobs * dimx];
80 this.B = new double[nobs * dimx];
81 this.Obs = new double[nobs];
82
83 defineData();
84 }
85
86 // @brief Defines a pseudo random number between 0 and 1
87 private double rndx() {
88 seed = seed * 1027 + 25;
89 int times = seed / 1_048_576;
90 seed = seed - 1_048_576 * times;
91 return (double) seed / 1_048_576.0;
92 }
93
94 // @brief Defines the data for the problem
95 private void defineData() {
96 final double Xtarg = -1.0;
97 final double Noise = 1.0;
98
99 int k = 0;
100 for (int i = 0; i < nobs; ++i) {
101 double O = 0.0;
102 for (int j = 0; j < dimx; ++j) {
103 A[k] = rndx();
104 B[k] = rndx();
105 O += A[k] * Xtarg + B[k] * (Xtarg * Xtarg);
106 ++k;
107 }
108 Obs[i] = O + Noise * rndx();
109 }
110 }
111
116 public void buildModel() {
117 // adding the variables to the model
118 List<Integer> varxList = new ArrayList<>(dimx);
119 for (int i = 0; i < dimx; ++i) {
120 int varidx = addVariable(-Conopt.Infinity, Conopt.Infinity, -0.8);
121 varxList.add(varidx);
122 }
123
124 List<Integer> varresList = new ArrayList<>(nobs);
125 for (int i = 0; i < nobs; ++i) {
126 int varidx = addVariable(-Conopt.Infinity, Conopt.Infinity, 0.0);
127 varresList.add(varidx);
128 }
129
130 this.varx = varxList.stream().mapToInt(Integer::intValue).toArray();
131 this.varres = varresList.stream().mapToInt(Integer::intValue).toArray();
132
133
134
135 // adding the constraints to the model
136 this.consresidual = new int[nobs];
137 for (int i = 0; i < nobs; ++i) {
138
139 int m = dimx + 1; // +1 for the residual var
140 int[] varidx = new int[m];
141 double[] coeffs = new double[m];
142 int[] nlf = new int[m];
143
144 for (int j = 0; j < dimx; ++j) {
145 varidx[j] = varx[j];
146 coeffs[j] = 0.0;
147 nlf[j] = 1;
148 }
149
150 // Add residual var linearly with coefficient -1
151 varidx[dimx] = varres[i];
152 coeffs[dimx] = -1.0;
153 nlf[dimx] = 0;
154
155 int considx = addConstraint(ConstraintType.Eq, Obs[i], varidx, coeffs, nlf);
156 consresidual[i] = considx;
157 }
158
159 // Objective function
160 double[] objCoeffs = new double[nobs];
161 int[] objNlf = new int[nobs];
162 for (int i = 0; i < nobs; ++i) {
163 objCoeffs[i] = 1.0;
164 objNlf[i] = 1; // residuals appear nonlinearly in the objective (r_i^2)
165 }
166
167 consobj = addConstraint(ConstraintType.Free, 0.0, varres, objCoeffs, objNlf);
168
169 // setting the objective constraint
171
172 // setting the optimisation direction
174
175 // setting the second derivative evaluation type
177 }
178
183 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
184 double g = 0;
185 if (rowno == consobj) {
186 double sum = 0.0;
187 for (int i = 0; i < nobs; i++) {
188 sum += Math.pow(x[varres[i]], 2);
189 }
190 g = sum;
191 }
192 else {
193 int k = rowno * dimx;
194 double sum = 0.0;
195 for (int i = 0; i < dimx; i++) {
196 sum += A[k] * x[varx[i]] + B[k] * Math.pow(x[varx[i]], 2);
197 k++;
198 }
199 g = sum;
200 }
201
202 return g;
203 }
204
209 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
210 assert x.length == jac.length;
211
212 if (rowno == consobj) {
213 for (int i = 0; i < nobs; i++) {
214 jac[varres[i]] = 2 * x[varres[i]];
215 }
216 }
217 else {
218 int k = rowno * dimx;
219 for (int i = 0; i < dimx; i++) {
220 jac[varx[i]] = A[k] + 2 * B[k] * x[varx[i]];
221 k++;
222 }
223 }
224 }
225
230 public void evaluateDirectionalSD(double[] x, double[] dx, double[] d2g, int rowno, int[] jacnum, int thread) {
231 assert x.length == d2g.length;
232 assert dx.length == d2g.length;
233
234 if (rowno == consobj) {
235
236 /* Objective row: sum(i, res(i)**2 ) */
237
238 for (int i = 0; i < dimx; i++)
239 d2g[varx[i]] = 0;
240 for (int i = 0; i < nobs; i++)
241 d2g[varres[i]] = 2.0 * dx[varres[i]];
242 }
243 else {
244
245 /* Constraint row with b(i,j)*x(j)**2 as only nonlinear term */
246
247 int k = rowno * dimx;
248 for (int i = 0; i < dimx; i++) {
249 d2g[varx[i]] = 2.0 * B[k] * dx[varx[i]];
250 k++;
251 }
252 for (int i = 0; i < nobs; i++)
253 d2g[varres[i]] = 0;
254 }
255
256 }
257}
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[])
Definition leastsq5.java:15
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 evaluateDirectionalSD(double[] x, double[] dx, double[] d2g, int rowno, int[] jacnum, int thread)
computes the directional second derivative for a single constraint
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
Definition std.py:1
real *8, parameter k
Definition tutoriali.f90:30