CONOPT
Loading...
Searching...
No Matches
qp5.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
12
13
16public class qp5 {
17 public static void main(String argv[]) {
18 System.loadLibrary("conoptjni4");
19
20 String name = "qp5";
21
22 Conopt conopt = new Conopt(name);
23 qp5_TutModelData model = new qp5_TutModelData();
24 MsgHandler msghdlr = new MsgHandler(name);
25
26 model.buildModel();
27
28 conopt.loadModel(model);
29 conopt.setMessageHandler(msghdlr);
30
31 // try to set the license using the environment variables
32 try {
33 int license_int_1 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_1"));
34 int license_int_2 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_2"));
35 int license_int_3 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_3"));
36 String license_text = System.getenv("CONOPT_LICENSE_TEXT");
37
38 conopt.setLicense(license_int_1, license_int_2, license_int_3,
39 license_text);
40 } catch (Exception e) {
41 System.out.println("Unable to set license: " + e.getMessage());
42 }
43
44 conopt.solve();
45
46 conopt.printStatus();
47
48 std s = new std();
49 int retcode = s.checkSolve(name, conopt.modelStatus(), conopt.solutionStatus(),
50 conopt.objectiveValue(), 59978.0, 0.001);
51
52 msghdlr.close();
53
54 System.exit(retcode);
55 }
56}
57
58class qp5_TutModelData extends ModelData {
59 private int NN;
60 private int NQ;
61 private double[] target;
62 private double[] Qdiag;
63 private double[] Qlowerdiag;
64
65 public qp5_TutModelData() {
66 super();
67
68 setConstants();
69 }
70
71 private void setConstants() {
72 NN = 1000;
73 NQ = NN*2 - 1;
74 target = new double[NN];
75 Qdiag = new double[NN];
76 Qlowerdiag = new double[NN];
77 for (int i = 0; i < NN; i++) {
78 target[i] = 10;
79 Qdiag[i] = 1;
80 }
81
82 for (int i = 0; i < NN - 1; i++)
83 Qlowerdiag[i] = 0.1;
84 }
85
86
91 public void buildModel() {
92 // adding the variables to the model
93 for (int i = 0; i < NN; i++)
95
96 // adding the constraints to the model
97 // first we create some arrays to help build the model.
98 int[] varindex = new int[NN];
99 int[] zeros = new int[NN];
100 int[] ones = new int[NN];
101 for (int i = 0; i < NN; i++) {
102 varindex[i] = i;
103 zeros[i] = 0;
104 ones[i] = 1;
105 }
106
107 // the first constraint is the quadratic objective
108 addConstraint(ConstraintType.Free, 0.0, varindex,
109 Arrays.stream(zeros).asDoubleStream().toArray(), ones);
110
111 // the second constraint is the summation constraint: sum(x) == 1
112 addConstraint(ConstraintType.Eq, 1.0, varindex,
113 Arrays.stream(ones).asDoubleStream().toArray(), zeros);
114
115 // setting the objective constraint
117
118 // setting the optimisation direction
120 }
121
126 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
127 double g = 0;
128 if (rowno == 0) {
129 for (int i = 0; i < NN; i++)
130 g += (x[i] - target[i])*Qdiag[i]*(x[i] - target[i]);
131 for (int i = 0; i < NN - 1; i++)
132 g += 2*(x[i + 1] - target[i + 1])*Qlowerdiag[i]*(x[i] - target[i]);
133 }
134
135 return g/2;
136 }
137
142 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
143 assert x.length == jac.length;
144
145 if (rowno == 0) {
146 for (int i = 0; i < NN; i++)
147 jac[i] += Qdiag[i]*(x[i] - target[i]);
148
149 for (int i = 0; i < NN - 1; i++) {
150 jac[i + 1] += Qlowerdiag[i]*(x[i] - target[i]);
151 jac[i] += Qlowerdiag[i]*(x[i + 1] - target[i + 1]);
152 }
153 }
154 }
155}
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
Main program. A simple setup and call of CONOPT.
Definition qp5.java:16
static void main(String argv[])
Definition qp5.java:17
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
void buildModel()
adds variables and constraints to the model
Definition qp5.java:91
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row
Definition qp5.java:126
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 qp5.java:142
Definition std.py:1