CONOPT
Loading...
Searching...
No Matches
qp4.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import jconopt.*;
11
14public class qp4 {
15 public static void main(String argv[]){
16 System.loadLibrary("conopt4_java");
17
18 String name = "qp4";
19
20 Conopt conopt = new Conopt(name);
21 qp4_TutModelData model = new qp4_TutModelData();
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("LICENSE_INT_1"));
32 int license_int_2 = Integer.parseInt(System.getenv("LICENSE_INT_2"));
33 int license_int_3 = Integer.parseInt(System.getenv("LICENSE_INT_3"));
34 String license_text = System.getenv("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(), 59978.0, 0.001);
49
50 msghdlr.close();
51
52 System.exit(retcode);
53 }
54}
55
56class qp4_TutModelData extends ModelData {
57 private int NN;
58 private int NQ;
59 private double[] target;
60 private double[] Qdiag;
61 private double[] Qlowerdiag;
62
63 public qp4_TutModelData() {
64 super();
65
66 setConstants();
67 }
68
69 private void setConstants() {
70 NN = 1000;
71 NQ = NN*2 - 1;
72 target = new double[NN];
73 Qdiag = new double[NN];
74 Qlowerdiag = new double[NN];
75 for (int i = 0; i < NN; i++)
76 {
77 target[i] = 10;
78 Qdiag[i] = 1;
79 }
80
81 for (int i = 0; i < NN - 1; i++)
82 Qlowerdiag[i] = 0.1;
83 }
84
89 public void buildModel() {
90 // adding the variables to the model
91 for (int i = 0; i < NN; i++)
93
94 // adding the constraints to the model
95 // first we create some arrays to help build the model.
96 int[] varindex = new int[NN];
97 int[] zeros = new int[NN];
98 int[] ones = new int[NN];
99 for (int i = 0; i < NN; i++)
100 {
101 varindex[i] = i;
102 zeros[i] = 0;
103 ones[i] = 1;
104 }
105
106 // the first constraint is the quadratic objective
107 addConstraint(ConstraintType.Free, 0.0, varindex,
108 Arrays.stream(zeros).asDoubleStream().toArray(), ones);
109
110 // the second constraint is the summation constraint: sum(x) == 1
111 addConstraint(ConstraintType.Eq, 1.0, varindex,
112 Arrays.stream(ones).asDoubleStream().toArray(), zeros);
113
114 // setting the objective constraint
116
117 // setting the optimisation direction
119
120 // setting the second derivative evaluation type
122
123 // setting the structure of the hessian of the lagrangian
124 setLagrangianStructure();
125 }
126
127 private void setLagrangianStructure() {
128 int[] hessianrow = new int[NQ];
129 int[] hessiancol = new int[NQ];
130
131 // defining the non-zero structure of the hessian
132 for (int i = 0; i < NN - 1; i++)
133 {
134 hessianrow[2*i] = i;
135 hessianrow[2*i + 1] = i + 1;
136
137 hessiancol[2*i] = i;
138 hessiancol[2*i + 1] = i;
139 }
140 hessianrow[NQ - 1] = NN - 1;
141 hessiancol[NQ - 1] = NN - 1;
142
143 // setting the structure of the hessian of the lagrangian
144 setSDLagrangianStructure(hessianrow, hessiancol);
145 }
146
151 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
152 {
153 double g = 0;
154 if (rowno == 0)
155 {
156 for (int i = 0; i < NN; i++)
157 g += (x[i] - target[i])*Qdiag[i]*(x[i] - target[i]);
158 for (int i = 0; i < NN - 1; i++)
159 g += 2*(x[i + 1] - target[i + 1])*Qlowerdiag[i]*(x[i] - target[i]);
160 }
161
162 return g/2;
163 }
164
169 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
170 assert x.length == jac.length;
171
172 if (rowno == 0)
173 {
174 for (int i = 0; i < NN; i++)
175 jac[i] += Qdiag[i]*(x[i] - target[i]);
176
177 for (int i = 0; i < NN - 1; i++)
178 {
179 jac[i + 1] += Qlowerdiag[i]*(x[i] - target[i]);
180 jac[i] += Qlowerdiag[i]*(x[i + 1] - target[i + 1]);
181 }
182 }
183 }
184
185
190 public void evaluateDirectionalSD(double[] x, double[] dx, double[] d2g, int rowno, int[] jacnum, int thread) {
191 assert x.length == d2g.length;
192 assert dx.length == d2g.length;
193
194 for (int i = 0; i < NN; i++)
195 d2g[i] = Qdiag[i]*dx[i];
196
197 for (int i = 0; i < NN - 1; i++)
198 {
199 d2g[i + 1] += Qlowerdiag[i]*dx[i];
200 d2g[i] += Qlowerdiag[i]*dx[i + 1];
201 }
202 }
203
204
209 public void evaluateSDLagrangian(double[] x, double[] u, int[] hessianrow, int[] hessiancol, double[] hessianval) {
210 assert hessianrow.length == hessianval.length;
211 assert hessiancol.length == hessianval.length;
212
213 for (int i = 0; i < NN - 1; i++)
214 {
215 hessianval[2*i] = Qdiag[i]*u[0];
216 hessianval[2*i + 1] = Qlowerdiag[i]*u[0];
217 }
218 hessianval[NQ - 1] = Qdiag[NN - 1]*u[0];
219 }
220}
static final ConstraintType Eq
static final ConstraintType Free
A class that can be extended to build and solve a model using Conopt.
static final ObjectiveElement Constraint
static final SDEvaluationType Constraint
static final Sense Minimize
Definition Sense.java:25
license_text
Definition qp4.py:144
msghdlr
Definition qp4.py:133
static void main(String argv[])
Definition qp4.java:15
name
Definition qp4.py:129
license_int_1
Definition qp4.py:141
license_int_2
Definition qp4.py:142
conopt
Definition qp4.py:131
retcode
Definition qp4.py:152
license_int_3
Definition qp4.py:143
model
Definition qp4.py:132
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:16
void setObjectiveElement(ObjectiveElement elem, int elemindex)
void setOptimizationSense(Sense sense)
int addVariable(double lower, double upper, double curr, int varstatus)
int addConstraint(ConstraintType constype, double rhs, int slackstatus)
void setSDLagrangianStructure(int[] rownum, int[] colnum)
void setSDEvaluationType(SDEvaluationType sdevaltype)
void evaluateSDLagrangian(double[] x, double[] u, int[] hessianrow, int[] hessiancol, double[] hessianval)
Computes and returns the numerical values of the Lagrangian of the Hessian.
Definition qp4.java:209
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 qp4.java:169
void evaluateDirectionalSD(double[] x, double[] dx, double[] d2g, int rowno, int[] jacnum, int thread)
computes the directional second derivative for a single constraint
Definition qp4.java:190
void buildModel()
adds variables and constraints to the model
Definition qp4.java:89
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row
Definition qp4.java:151
static final double CONOPT_INF
Definition qp4.py:1
Definition std.py:1
#define NN
Definition qp1.c:21
#define NQ
Definition qp1.c:22