CONOPT
Loading...
Searching...
No Matches
tutoriali.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import jconopt.*;
11
14public class tutoriali {
15 public static void main(String argv[]){
16 System.loadLibrary("conopt4_java");
17
18 String name = "tutoriali";
19
20 Conopt conopt = new Conopt(name);
21 TutiModelData model = new TutiModelData();
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 TutiModelData extends ModelData {
57 private double Al;
58 private double Ak;
59 private double Ainp;
60 private double Rho;
61 private double K;
62
63 // helper variables to store the values
64 private double L;
65 private double Inp;
66 private double Out;
67 private double P;
68
69 private double hold1;
70 private double hold2;
71 private double hold3;
72
73 public TutiModelData() {
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 // adding the variables to the model
97
98 // adding the constraints to the model
99 // Constraint 1
100 {
101 int[] index = {0, 1, 2, 3};
102 double[] value = {-1, -1, 0, 0};
103 int[] nlflag = {0, 0, 1, 1};
104
105 addConstraint(ConstraintType.Free, -0.1, index, value, nlflag);
106 }
107
108 // Constraint 2
109 {
110 int[] index = {0, 1, 2};
111 double[] value = {0, 0, -1};
112 int[] nlflag = {1, 1, 0};
113 addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
114 }
115
116 // Constraint 3
117 {
118 int[] index = {2, 3};
119 double[] value = {1, 2};
120 int[] nlflag = {0, 0};
121 addConstraint(ConstraintType.Eq, 4.0, index, value, nlflag);
122 }
123
124 // setting the objective constraint
126
127 // setting the optimisation direction
129 }
130
135 public void initFDEvaluation(double[] x, int[] rowlist, EvaluationMode mode,
136 int numthread, boolean ignerr)
137 {
138 L = x[0];
139 Inp = x[1];
140 Out = x[2];
141 P = x[3];
142
143 hold1 = (Al*Math.pow(L, (-Rho)) + Ak*Math.pow(K, (-Rho)) +
144 Ainp*Math.pow(Inp, (-Rho)));
145 hold2 = Math.pow(hold1,( -1./Rho ));
146 hold3 = hold2 / hold1;
147 }
148
153 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
154 {
155 double g = 0;
156 if (rowno == 0)
157 {
158 g = P * Out;
159 }
160 else if (rowno == 1)
161 {
162 g = hold2;
163 }
164
165 return g;
166 }
167
172 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr,
173 int thread) {
174 assert x.length == jac.length;
175
176 if (rowno == 0)
177 {
178 jac[2] = P;
179 jac[3] = Out;
180 }
181 else if (rowno == 1)
182 {
183 jac[0] = hold3 * Al * Math.pow(L ,(-Rho-1.));
184 jac[1] = hold3 * Ainp * Math.pow(Inp,(-Rho-1.));
185 }
186 }
187}
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 buildModel()
adds variables and constraints to the model
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 initFDEvaluation(double[] x, int[] rowlist, EvaluationMode mode, int numthread, boolean ignerr)
callback method for initialising the first derivative evaluation.
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row
static final double CONOPT_INF
Definition std.py:1