CONOPT
Loading...
Searching...
No Matches
tutorial.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import jconopt.*;
11
14public class tutorial {
15 public static void main(String argv[]){
16 System.loadLibrary("conopt4_java");
17
18 String name = "tutorial";
19
20 Conopt conopt = new Conopt(name);
21 TutModelData model = new TutModelData();
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 TutModelData 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 TutModelData() {
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
125 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
126 {
127 double L = x[0];
128 double Inp = x[1];
129 double Out = x[2];
130 double P = x[3];
131
132 double g = 0;
133 if (rowno == 0)
134 {
135 g = P * Out;
136 }
137 else if (rowno == 1)
138 {
139 double hold1 = (Al*Math.pow(L,(-Rho)) + Ak*Math.pow(K,(-Rho)) + Ainp*Math.pow(Inp,(-Rho)));
140 double hold2 = Math.pow(hold1,( -1./Rho ));
141
142 g = hold2;
143 }
144
145 return g;
146 }
147
152 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
153 assert x.length == jac.length;
154
155 double L = x[0];
156 double Inp = x[1];
157 double Out = x[2];
158 double P = x[3];
159
160 if (rowno == 0)
161 {
162 jac[2] = P;
163 jac[3] = Out;
164 }
165 else if (rowno == 1)
166 {
167 double hold1 = (Al*Math.pow(L,(-Rho)) + Ak*Math.pow(K,(-Rho)) + Ainp*Math.pow(Inp,(-Rho)));
168 double hold2 = Math.pow(hold1,( -1./Rho ));
169 double hold3 = hold2 / hold1;
170
171 jac[0] = hold3 * Al * Math.pow(L ,(-Rho-1.));
172 jac[1] = hold3 * Ainp * Math.pow(Inp,(-Rho-1.));
173 }
174 }
175}
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[])
Definition tutorial.java:15
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)
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row
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
void buildModel()
adds variables and constraints to the model
Definition tutorial.java:81
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