CONOPT
Loading...
Searching...
No Matches
qp1.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import jconopt.*;
11
12
13
16public class qp1 {
17 public static void main(String argv[]){
18 System.loadLibrary("conopt4_java");
19
20 String name = "qp1";
21
22 Conopt conopt = new Conopt(name);
23 qp1_TutModelData model = new qp1_TutModelData();
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("LICENSE_INT_1"));
34 int license_int_2 = Integer.parseInt(System.getenv("LICENSE_INT_2"));
35 int license_int_3 = Integer.parseInt(System.getenv("LICENSE_INT_3"));
36 String license_text = System.getenv("LICENSE_TEXT");
37
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 qp1_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 qp1_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 {
79 target[i] = 10;
80 Qdiag[i] = 1;
81 }
82
83 for (int i = 0; i < NN - 1; i++)
84 Qlowerdiag[i] = 0.1;
85 }
86
87
92 public void buildModel() {
93 // adding the variables to the model
94 for (int i = 0; i < NN; i++)
96
97 // adding the constraints to the model
98 // first we create some arrays to help build the model.
99 int[] varindex = new int[NN];
100 int[] zeros = new int[NN];
101 int[] ones = new int[NN];
102 for (int i = 0; i < NN; i++)
103 {
104 varindex[i] = i;
105 zeros[i] = 0;
106 ones[i] = 1;
107 }
108
109 // the first constraint is the quadratic objective
110 addConstraint(ConstraintType.Free, 0.0, varindex,
111 Arrays.stream(zeros).asDoubleStream().toArray(), ones);
112
113 // the second constraint is the summation constraint: sum(x) == 1
114 addConstraint(ConstraintType.Eq, 1.0, varindex,
115 Arrays.stream(ones).asDoubleStream().toArray(), zeros);
116
117 // setting the objective constraint
119
120 // setting the optimisation direction
122 }
123
128 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
129 {
130 double g = 0;
131 if (rowno == 0)
132 {
133 for (int i = 0; i < NN; i++)
134 g += (x[i] - target[i])*Qdiag[i]*(x[i] - target[i]);
135 for (int i = 0; i < NN - 1; i++)
136 g += 2*(x[i + 1] - target[i + 1])*Qlowerdiag[i]*(x[i] - target[i]);
137 }
138
139 return g/2;
140 }
141
146 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
147 assert x.length == jac.length;
148
149 if (rowno == 0)
150 {
151 for (int i = 0; i < NN; i++)
152 jac[i] += Qdiag[i]*(x[i] - target[i]);
153
154 for (int i = 0; i < NN - 1; i++)
155 {
156 jac[i + 1] += Qlowerdiag[i]*(x[i] - target[i]);
157 jac[i] += Qlowerdiag[i]*(x[i + 1] - target[i + 1]);
158 }
159 }
160 }
161}
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 Sense Minimize
Definition Sense.java:25
retcode
Definition qp1.py:112
conopt
Definition qp1.py:91
name
Definition qp1.py:89
license_int_1
Definition qp1.py:101
static void main(String argv[])
Definition qp1.java:17
license_text
Definition qp1.py:104
license_int_2
Definition qp1.py:102
model
Definition qp1.py:92
msghdlr
Definition qp1.py:93
license_int_3
Definition qp1.py:103
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 buildModel()
adds variables and constraints to the model
Definition qp1.java:92
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 qp1.java:146
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row
Definition qp1.java:128
static final double CONOPT_INF
Definition qp1.py:1
Definition std.py:1