CONOPT
Loading...
Searching...
No Matches
tutorial2.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
14public class tutorial2 {
15 public static void main(String argv[]) {
16 System.loadLibrary("conoptjni4");
17
18 String name = "tutorial2";
19
20 Conopt conopt = new Conopt(name);
21 Tut2ModelData model = new Tut2ModelData();
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(), 0.572943, 0.000001);
49
50 msghdlr.close();
51
52 System.exit(retcode);
53 }
54}
55
56class Tut2ModelData extends ModelData {
57 private double Al;
58 private double Ak;
59 private double Ainp;
60 private double Rho;
61 private double K;
62
63 /* declaring the variable and constraint indices. */
64 private int varl;
65 private int varinp;
66 private int varout;
67 private int varp;
68 private int consobj;
69 private int consprod;
70
71 public Tut2ModelData() {
72 super();
73
74 setConstants();
75 }
76
77 private void setConstants() {
78 Al = 0.16;
79 Ak = 2.0;
80 Ainp = 0.16;
81 Rho = 1.0;
82 K = 4.0;
83 }
84
89 public void buildModel() {
90 // adding the variables to the model
91 varl = addVariable(0.1, Conopt.Infinity, 0.5);
92 varinp = addVariable(0.1, Conopt.Infinity, 0.5);
93 varout = addVariable(0.0, Conopt.Infinity);
94 varp = addVariable(0.0, Conopt.Infinity);
95
96 // adding the constraints to the model
97 // Constraint 1
98 {
99 int[] index = {varl, varinp, varout, varp};
100 double[] value = {-1, -1, 0, 0};
101 int[] nlflag = {0, 0, 1, 1};
102
103 consobj = addConstraint(ConstraintType.Free, -0.1, index, value, nlflag);
104 }
105
106 // Constraint 2
107 {
108 int[] index = {varl, varinp, varout};
109 double[] value = {0, 0, -1};
110 int[] nlflag = {1, 1, 0};
111 consprod = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
112 }
113
114 // Constraint 3
115 {
116 int[] index = {varout, varp};
117 double[] value = {1, 2};
118 int[] nlflag = {0, 0};
119 addConstraint(ConstraintType.Eq, 4.0, index, value, nlflag);
120 }
121
122 // setting the objective constraint
124
125 // setting the optimisation direction
127
128 // setting the structure of the hessian
129 int[] rownum = {0, 1, 1, 3};
130 int[] colnum = {0, 0, 1, 2};
131 setSDLagrangianStructure(rownum, colnum);
132 }
133
138 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
139 double L = x[varl];
140 double Inp = x[varinp];
141 double Out = x[varout];
142 double P = x[varp];
143
144 double g = 0;
145 if (rowno == consobj) {
146 g = P * Out;
147 }
148 else if (rowno == consprod) {
149 double hold1 = (Al*Math.pow(L,(-Rho)) + Ak*Math.pow(K,(-Rho)) + Ainp*Math.pow(Inp,(-Rho)));
150 double hold2 = Math.pow(hold1,( -1./Rho ));
151
152 g = hold2;
153 }
154
155 return g;
156 }
157
162 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr,
163 int thread) {
164 assert x.length == jac.length;
165
166 double L = x[varl];
167 double Inp = x[varinp];
168 double Out = x[varout];
169 double P = x[varp];
170
171 if (rowno == consobj) {
172 jac[varout] = P;
173 jac[varp] = Out;
174 }
175 else if (rowno == consprod) {
176 double hold1 = (Al*Math.pow(L,(-Rho)) + Ak*Math.pow(K,(-Rho)) + Ainp*Math.pow(Inp,(-Rho)));
177 double hold2 = Math.pow(hold1,( -1./Rho ));
178 double hold3 = hold2 / hold1;
179
180 jac[varl] = hold3 * Al * Math.pow(L ,(-Rho-1.));
181 jac[varinp] = hold3 * Ainp * Math.pow(Inp,(-Rho-1.));
182 }
183 }
184
189 public void evaluateSDLagrangian(double x[], double u[], int[] hessianrow, int[] hessiancol, double[] hessianval) {
190 double L;
191 double Inp;
192 double Out;
193 double P;
194 double hold1, hold2, hold3, hold4;
195
196
197 // Normal Evaluation mode
198 hessianval[3] = u[consobj]; // the second derivative of constraint 0 is 1
199 if ( u[consprod] != 0.0 ) {
200 L = x[varl];
201 Inp = x[varinp];
202 Out = x[varout];
203 P = x[varp];
204 hold1 = (Al * Math.pow(L, -Rho) + Ak * Math.pow(K, -Rho) + Ainp * Math.pow(Inp, -Rho));
205 hold2 = Math.pow(hold1, -1.0 / Rho);
206 hold3 = hold2 / hold1;
207
208 /* The code for the first derivative was:
209 * jac(1) = hold3 * Al * L ** (-Rho-1.0)
210 * jac(2) = hold3 * Ainp * Inp ** (-Rho-1.0)
211 * Define Hold4 as the derivative of Hold3 with respect to Hold1
212 */
213 hold4 = hold3 / hold1 * (-1.0 / Rho - 1.0);
214
215 /* The second derivatives are computed as:
216 * (1) The derivative of Hold3 multiplied by the other terms PLUS
217 * (2) Hold3 multiplied by the derivative of the other terms
218 * where the derivative of Hold3 is Hold4 * the derivative of Hold1
219 * with respect to the variable in question.
220 */
221
222 hessianval[0] = hold4 * (-Rho) * Math.pow(Al * Math.pow(L, -Rho - 1.0), 2) +
223 hold3 * Al * (-Rho - 1.0) * Math.pow(L, -Rho - 2.0);
224 hessianval[1] = hold4 * (-Rho) * (Al * Math.pow(L, -Rho - 1.0)) *
225 (Ainp * Math.pow(Inp, -Rho - 1.0));
226 hessianval[2] = hold4 * (-Rho) * Math.pow(Ainp * Math.pow(Inp, -Rho - 1.0), 2) +
227 hold3 * Ainp * (-Rho - 1.0) * Math.pow(Inp, -Rho - 2.0);
228
229
230 // Scale the derivatives with the Lagrange multiplier, u[1]:
231 for (int i = 0; i < 3; i++)
232 hessianval[i] = hessianval[i] * u[consprod];
233 }
234 }
235}
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 Maximize
Definition Sense.java:29
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:20
static void main(String argv[])
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
setSDLagrangianStructure(self, rownum, colnum)
sets the structure of the second derivatives of the Lagrangian
Definition conopt.py:2889
void buildModel()
adds variables and constraints to the model
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row
void evaluateSDLagrangian(double x[], double u[], int[] hessianrow, int[] hessiancol, double[] hessianval)
Computes and returns the numerical values of the Lagrangian of the Hessian.
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 std.py:1
double L
Definition tutoriali.c:16
double P
Definition tutoriali.c:16
double hold2
Definition tutoriali.c:28
double hold3
Definition tutoriali.c:28
double hold1
Definition tutoriali.c:28
double Inp
Definition tutoriali.c:16
double Out
Definition tutoriali.c:16