CONOPT
Loading...
Searching...
No Matches
leastsq2.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
14public class leastsq2 {
15 public static void main(String argv[]) {
16 System.loadLibrary("conoptjni4");
17
18 String name = "leastsq2";
19
20 Conopt conopt = new Conopt(name);
21 LeastSq2ModelData model = new LeastSq2ModelData(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 LeastSq2ModelData 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 LeastSq2ModelData(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 structure of the hessian
176 int[] Qrow = new int[dimx + nobs];
177 int[] Qcol = new int[dimx + nobs];
178
179 for (int i = 0; i < dimx + nobs; ++i) {
180 Qrow[i] = i;
181 Qcol[i] = i;
182 }
183
185 }
186
191 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
192 double g = 0;
193 if (rowno == consobj) {
194 double sum = 0.0;
195 for (int i = 0; i < nobs; i++) {
196 sum += Math.pow(x[varres[i]], 2);
197 }
198 g = sum;
199 }
200 else {
201 int k = rowno * dimx;
202 double sum = 0.0;
203 for (int i = 0; i < dimx; i++) {
204 sum += A[k] * x[varx[i]] + B[k] * Math.pow(x[varx[i]], 2);
205 k++;
206 }
207 g = sum;
208 }
209
210 return g;
211 }
212
217 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
218 assert x.length == jac.length;
219
220 if (rowno == consobj) {
221 for (int i = 0; i < nobs; i++) {
222 jac[varres[i]] = 2 * x[varres[i]];
223 }
224 }
225 else {
226 int k = rowno * dimx;
227 for (int i = 0; i < dimx; i++) {
228 jac[varx[i]] = A[k] + 2 * B[k] * x[varx[i]];
229 k++;
230 }
231 }
232 }
233
238 public void evaluateSDLagrangian(double x[], double u[], int[] hessianrow, int[] hessiancol, double[] hessianval) {
239
240 // Evaluation mode
241 for (int i = 0; i < dimx; i++)
242 hessianval[varx[i]] = 0.0;
243
244 for (int i = 0; i < nobs; i++)
245 hessianval[varres[i]] = 2.0 * u[nobs];
246 }
247}
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 Sense Minimize
Definition Sense.java:25
static void main(String argv[])
Definition leastsq2.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
setSDLagrangianStructure(self, rownum, colnum)
sets the structure of the second derivatives of the Lagrangian
Definition conopt.py:2889
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 evaluateSDLagrangian(double x[], double u[], int[] hessianrow, int[] hessiancol, double[] hessianval)
Computes and returns the numerical values of the Lagrangian of the Hessian.
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
int Qrow[NQ]
Definition qp1.c:25
int Qcol[NQ]
Definition qp1.c:26