CONOPT
Loading...
Searching...
No Matches
tutorial2.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import jconopt.*;
11
14public class tutorial2 {
15 public static void main(String argv[]){
16 System.loadLibrary("conopt4_java");
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("LICENSE_INT_1"));
32 int license_int_2 = Integer.parseInt(System.getenv("LICENSE_INT_2"));
33 int license_int_3 = Integer.parseInt(System.getenv("LICENSE_INT_3"));
34 String license_text = System.getenv("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 public Tut2ModelData() {
64 super();
65
66 setConstants();
67 }
68
69 private void setConstants() {
70 Al = 0.16;
71 Ak = 2.0;
72 Ainp = 0.16;
73 Rho = 1.0;
74 K = 4.0;
75 }
76
81 public void buildModel() {
82 // adding the variables to the model
87
88 // adding the constraints to the model
89 // Constraint 1
90 {
91 int[] index = {0, 1, 2, 3};
92 double[] value = {-1, -1, 0, 0};
93 int[] nlflag = {0, 0, 1, 1};
94
95 addConstraint(ConstraintType.Free, -0.1, index, value, nlflag);
96 }
97
98 // Constraint 2
99 {
100 int[] index = {0, 1, 2};
101 double[] value = {0, 0, -1};
102 int[] nlflag = {1, 1, 0};
103 addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
104 }
105
106 // Constraint 3
107 {
108 int[] index = {2, 3};
109 double[] value = {1, 2};
110 int[] nlflag = {0, 0};
111 addConstraint(ConstraintType.Eq, 4.0, index, value, nlflag);
112 }
113
114 // setting the objective constraint
116
117 // setting the optimisation direction
119
120 // setting the structure of the hessian
121 int[] rownum = {0, 1, 1, 3};
122 int[] colnum = {0, 0, 1, 2};
123 setSDLagrangianStructure(rownum, colnum);
124 }
125
130 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
131 {
132 double L = x[0];
133 double Inp = x[1];
134 double Out = x[2];
135 double P = x[3];
136
137 double g = 0;
138 if (rowno == 0)
139 {
140 g = P * Out;
141 }
142 else if (rowno == 1)
143 {
144 double hold1 = (Al*Math.pow(L,(-Rho)) + Ak*Math.pow(K,(-Rho)) + Ainp*Math.pow(Inp,(-Rho)));
145 double hold2 = Math.pow(hold1,( -1./Rho ));
146
147 g = hold2;
148 }
149
150 return g;
151 }
152
157 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr,
158 int thread) {
159 assert x.length == jac.length;
160
161 double L = x[0];
162 double Inp = x[1];
163 double Out = x[2];
164 double P = x[3];
165
166 if (rowno == 0)
167 {
168 jac[2] = P;
169 jac[3] = Out;
170 }
171 else if (rowno == 1)
172 {
173 double hold1 = (Al*Math.pow(L,(-Rho)) + Ak*Math.pow(K,(-Rho)) + Ainp*Math.pow(Inp,(-Rho)));
174 double hold2 = Math.pow(hold1,( -1./Rho ));
175 double hold3 = hold2 / hold1;
176
177 jac[0] = hold3 * Al * Math.pow(L ,(-Rho-1.));
178 jac[1] = hold3 * Ainp * Math.pow(Inp,(-Rho-1.));
179 }
180 }
181
186 public void evaluateSDLagrangian(double x[], double u[], int[] hessianrow, int[] hessiancol, double[] hessianval) {
187 double L = x[0];
188 double Inp = x[1];
189 double Out = x[2];
190 double P = x[3];
191 double hold1, hold2, hold3, hold4;
192
193
194 // Normal Evaluation mode
195 hessianval[3] = u[0]; // the second derivative of constraint 0 is 1
196 if ( u[1] != 0.0 )
197 {
198 hold1 = (Al*Math.pow(L,-Rho) + Ak*Math.pow(K,-Rho) + Ainp*Math.pow(Inp,-Rho));
199 hold2 = Math.pow( hold1,-1.0/Rho);
200 hold3 = hold2 / hold1;
201
202 /* The code for the first derivative was:
203 * jac(1) = hold3 * Al * L ** (-Rho-1.0)
204 * jac(2) = hold3 * Ainp * Inp ** (-Rho-1.0)
205 * Define Hold4 as the derivative of Hold3 with respect to Hold1
206 */
207 hold4 = hold3 / hold1 * (-1.0/Rho-1.0);
208
209 /* The second derivatives are computed as:
210 * (1) The derivative of Hold3 multiplied by the other terms PLUS
211 * (2) Hold3 multiplied by the derivative of the other terms
212 * where the derivative of Hold3 is Hold4 * the derivative of Hold1
213 * with respect to the variable in question.
214 */
215
216 hessianval[0] = hold4 * (-Rho) * Math.pow(Al*Math.pow(L,-Rho-1.0),2) +
217 hold3 * Al * (-Rho-1.0)*Math.pow(L,-Rho-2.0);
218 hessianval[1] = hold4 * (-Rho) * (Al*Math.pow(L,-Rho-1.0)) *
219 (Ainp*Math.pow(Inp,-Rho-1.0));
220 hessianval[2] = hold4 * (-Rho) * Math.pow(Ainp*Math.pow(Inp,-Rho-1.0),2) +
221 hold3 * Ainp * (-Rho-1.0)*Math.pow(Inp,-Rho-2.0);
222
223
224 // Scale the derivatives with the Lagrange multiplier, u[1]:
225 for (int i = 0; i < 3; i++)
226 hessianval[i] = hessianval[i] * u[1];
227 }
228 }
229}
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 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:16
static void main(String argv[])
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 setSDLagrangianStructure(int[] rownum, int[] colnum)
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
static final double CONOPT_INF
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