CONOPT
Loading...
Searching...
No Matches
leastsq.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
14public class leastsq {
15 public static void main(String argv[]) {
16 System.loadLibrary("conoptjni4");
17
18 String name = "leastsq";
19
20 Conopt conopt = new Conopt(name);
21 LeastSqModelData model = new LeastSqModelData(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 LeastSqModelData 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 LeastSqModelData(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
180 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
181 double g = 0;
182 if (rowno == consobj) {
183 double sum = 0.0;
184 for (int i = 0; i < nobs; i++) {
185 sum += Math.pow(x[varres[i]], 2);
186 }
187 g = sum;
188 }
189 else {
190 int k = rowno * dimx;
191 double sum = 0.0;
192 for (int i = 0; i < dimx; i++) {
193 sum += A[k] * x[varx[i]] + B[k] * Math.pow(x[varx[i]], 2);
194 k++;
195 }
196 g = sum;
197 }
198
199 return g;
200 }
201
206 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
207 assert x.length == jac.length;
208
209 if (rowno == consobj) {
210 for (int i = 0; i < nobs; i++) {
211 jac[varres[i]] = 2 * x[varres[i]];
212 }
213 }
214 else {
215 int k = rowno * dimx;
216 for (int i = 0; i < dimx; i++) {
217 jac[varx[i]] = A[k] + 2 * B[k] * x[varx[i]];
218 k++;
219 }
220 }
221 }
222}
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
license_int_2
Definition leastsq.py:172
license_text
Definition leastsq.py:174
license_int_3
Definition leastsq.py:173
static void main(String argv[])
Definition leastsq.java:15
license_int_1
Definition leastsq.py:171
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
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row
Definition leastsq.java:180
void buildModel()
adds variables and constraints to the model
Definition leastsq.java:116
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
Definition leastsq.java:206
Definition std.py:1
real *8, parameter k
Definition tutoriali.f90:30