CONOPT
Loading...
Searching...
No Matches
tutorial2r.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
14public class tutorial2r {
15 public static void main(String argv[]) {
16 System.loadLibrary("conoptjni4");
17
18 String name = "tutorial2r";
19
20 Conopt conopt = new Conopt(name);
21 Tut2rModelData model = new Tut2rModelData();
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 Tut2rModelData 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 varint;
69 private int consobj;
70 private int consprod;
71 private int consnew;
72
73 public Tut2rModelData() {
74 super();
75
76 setConstants();
77 }
78
79 private void setConstants() {
80 Al = 0.16;
81 Ak = 2.0;
82 Ainp = 0.16;
83 Rho = 1.0;
84 K = 4.0;
85 }
86
91 public void buildModel() {
92
93 double initL = 0.5;
94 double initInp = 0.5;
95
96 // adding the variables to the model
97 varl = addVariable(0.1, Conopt.Infinity, initL);
98 varinp = addVariable(0.1, Conopt.Infinity, initInp);
99 varout = addVariable(0.0, Conopt.Infinity);
100 varp = addVariable(0.0, Conopt.Infinity);
101
102 // Lower bound on the new Int is set to 0.01 and the initial
103 // value is defined from row 1
104 varint = addVariable(0., Conopt.Infinity,
105 Al * Math.pow(initL, -Rho) + Ak * Math.pow(K, -Rho) + Ainp * Math.pow(initInp, -Rho));
106
107 // adding the constraints to the model
108 // Constraint 1
109 {
110 int[] index = {varl, varinp, varout, varp};
111 double[] value = {-1, -1, 0, 0};
112 int[] nlflag = {0, 0, 1, 1};
113
114 consobj = addConstraint(ConstraintType.Free, -0.1, index, value, nlflag);
115 }
116
117 // Constraint 2
118 {
119 int[] index = {varl, varinp, varint};
120 double[] value = {0, 0, -1};
121 int[] nlflag = {1, 1, 0};
122 consprod = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
123 }
124
125 // Constraint 3
126 {
127 int[] index = {varout, varp};
128 double[] value = {1, 2};
129 int[] nlflag = {0, 0};
130 addConstraint(ConstraintType.Eq, 4.0, index, value, nlflag);
131 }
132
133 // Constraint 4
134 {
135 int[] index = {varout, varint};
136 double[] value = {-1, 0};
137 int[] nlflag = {0, 1};
138 consnew = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
139 }
140
141 // setting the objective constraint
143
144 // setting the optimisation direction
146
147 // setting the structure of the hessian
148 int[] rownum = {0, 1, 3, 4};
149 int[] colnum = {0, 1, 2, 4};
150 setSDLagrangianStructure(rownum, colnum);
151 }
152
157 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
158 double L = x[varl];
159 double Inp = x[varinp];
160 double Out = x[varout];
161 double P = x[varp];
162 double Int = x[varint];
163
164 double g = 0;
165 if (rowno == consobj) {
166 g = P * Out;
167 }
168 else if (rowno == consprod) {
169 g = (Al * Math.pow(L, (-Rho)) + Ak * Math.pow(K, (-Rho)) + Ainp * Math.pow(Inp, (-Rho)));
170 }
171 else if (rowno == consnew) {
172 g = Math.pow(Int, (-1.0 / Rho));
173 }
174
175 return g;
176 }
177
182 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr,
183 int thread) {
184 assert x.length == jac.length;
185
186 double L = x[varl];
187 double Inp = x[varinp];
188 double Out = x[varout];
189 double P = x[varp];
190 double Int = x[varint];
191
192 if (rowno == consobj) {
193 jac[varout] = P;
194 jac[varp] = Out;
195 }
196 else if (rowno == consprod) {
197 jac[varl] = Al * (-Rho) * Math.pow(L, (-Rho - 1.)); /* derivative w.r.t. L = X[0] */
198 jac[varinp] = Ainp * (-Rho) * Math.pow(Inp, (-Rho - 1.)); /* derivative w.r.t. Inp = X[1] */
199 }
200 else if (rowno == consnew) {
201 jac[varint] = (-1.0 / Rho) * Math.pow(Int, (-1.0 / Rho - 1.0));
202 }
203 }
204
209 public void evaluateSDLagrangian(double x[], double u[], int[] hessianrow, int[] hessiancol, double[] hessianval) {
210 double L;
211 double Inp;
212
213 // Normal Evaluation mode
214 hessianval[2] = u[consobj]; // the second derivative of constraint 0 is 1
215 if ( u[consprod] != 0.0 ) {
216 L = x[varl];
217 Inp = x[varinp];
218
219 hessianval[0] = (-Rho) * (-Rho - 1.0) * Al * Math.pow(L, -Rho - 2.0) * u[consprod];
220 hessianval[1] = (-Rho) * (-Rho - 1.0) * Ainp * Math.pow(Inp, -Rho - 2.0) * u[consprod];
221 };
222 if (u[consnew] != 0.0)
223 hessianval[3] = (-1.0 / Rho) * (-1.0 / Rho - 1.0) * Math.pow(x[varint], -1.0 / Rho - 2.0);
224 }
225}
226
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 Inp
Definition tutoriali.c:16
double Out
Definition tutoriali.c:16