CONOPT
Loading...
Searching...
No Matches
qp4.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
14public class qp4 {
15 public static void main(String argv[]) {
16 System.loadLibrary("conoptjni4");
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("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(), 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 target[i] = 10;
77 Qdiag[i] = 1;
78 }
79
80 for (int i = 0; i < NN - 1; i++)
81 Qlowerdiag[i] = 0.1;
82 }
83
88 public void buildModel() {
89 // adding the variables to the model
90 for (int i = 0; i < NN; i++)
92
93 // adding the constraints to the model
94 // first we create some arrays to help build the model.
95 int[] varindex = new int[NN];
96 int[] zeros = new int[NN];
97 int[] ones = new int[NN];
98 for (int i = 0; i < NN; i++) {
99 varindex[i] = i;
100 zeros[i] = 0;
101 ones[i] = 1;
102 }
103
104 // the first constraint is the quadratic objective
105 addConstraint(ConstraintType.Free, 0.0, varindex,
106 Arrays.stream(zeros).asDoubleStream().toArray(), ones);
107
108 // the second constraint is the summation constraint: sum(x) == 1
109 addConstraint(ConstraintType.Eq, 1.0, varindex,
110 Arrays.stream(ones).asDoubleStream().toArray(), zeros);
111
112 // setting the objective constraint
114
115 // setting the optimisation direction
117
118 // setting the second derivative evaluation type
120
121 // setting the structure of the hessian of the lagrangian
122 setLagrangianStructure();
123 }
124
125 private void setLagrangianStructure() {
126 int[] hessianrow = new int[NQ];
127 int[] hessiancol = new int[NQ];
128
129 // defining the non-zero structure of the hessian
130 for (int i = 0; i < NN - 1; i++) {
131 hessianrow[2*i] = i;
132 hessianrow[2*i + 1] = i + 1;
133
134 hessiancol[2*i] = i;
135 hessiancol[2*i + 1] = i;
136 }
137 hessianrow[NQ - 1] = NN - 1;
138 hessiancol[NQ - 1] = NN - 1;
139
140 // setting the structure of the hessian of the lagrangian
141 setSDLagrangianStructure(hessianrow, hessiancol);
142 }
143
148 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
149 double g = 0;
150 if (rowno == 0) {
151 for (int i = 0; i < NN; i++)
152 g += (x[i] - target[i])*Qdiag[i]*(x[i] - target[i]);
153 for (int i = 0; i < NN - 1; i++)
154 g += 2*(x[i + 1] - target[i + 1])*Qlowerdiag[i]*(x[i] - target[i]);
155 }
156
157 return g/2;
158 }
159
164 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
165 assert x.length == jac.length;
166
167 if (rowno == 0) {
168 for (int i = 0; i < NN; i++)
169 jac[i] += Qdiag[i]*(x[i] - target[i]);
170
171 for (int i = 0; i < NN - 1; i++) {
172 jac[i + 1] += Qlowerdiag[i]*(x[i] - target[i]);
173 jac[i] += Qlowerdiag[i]*(x[i + 1] - target[i + 1]);
174 }
175 }
176 }
177
178
183 public void evaluateDirectionalSD(double[] x, double[] dx, double[] d2g, int rowno, int[] jacnum, int thread) {
184 assert x.length == d2g.length;
185 assert dx.length == d2g.length;
186
187 for (int i = 0; i < NN; i++)
188 d2g[i] = Qdiag[i]*dx[i];
189
190 for (int i = 0; i < NN - 1; i++) {
191 d2g[i + 1] += Qlowerdiag[i]*dx[i];
192 d2g[i] += Qlowerdiag[i]*dx[i + 1];
193 }
194 }
195
196
201 public void evaluateSDLagrangian(double[] x, double[] u, int[] hessianrow, int[] hessiancol, double[] hessianval) {
202 assert hessianrow.length == hessianval.length;
203 assert hessiancol.length == hessianval.length;
204
205 for (int i = 0; i < NN - 1; i++) {
206 hessianval[2*i] = Qdiag[i]*u[0];
207 hessianval[2*i + 1] = Qlowerdiag[i]*u[0];
208 }
209 hessianval[NQ - 1] = Qdiag[NN - 1]*u[0];
210 }
211}
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
license_text
Definition qp4.py:160
msghdlr
Definition qp4.py:149
static void main(String argv[])
Definition qp4.java:15
name
Definition qp4.py:145
license_int_1
Definition qp4.py:157
license_int_2
Definition qp4.py:158
retcode
Definition qp4.py:176
license_int_3
Definition qp4.py:159
model
Definition qp4.py:148
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
setSDLagrangianStructure(self, rownum, colnum)
sets the structure of the second derivatives of the Lagrangian
Definition conopt.py:2889
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:201
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:164
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:183
void buildModel()
adds variables and constraints to the model
Definition qp4.java:88
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row
Definition qp4.java:148
Definition qp4.py:1
Definition std.py:1
#define NN
Definition qp1.c:21
#define NQ
Definition qp1.c:22