CONOPT
Loading...
Searching...
No Matches
tutorial.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
14public class tutorial {
15 public static void main(String argv[]) {
16 System.loadLibrary("conoptjni4");
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("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 TutModelData extends ModelData {
57 /* declaring the parameters */
58 private double Al;
59 private double Ak;
60 private double Ainp;
61 private double Rho;
62 private double K;
63
64 /* declaring the variable and constraint indices. */
65 private int varl;
66 private int varinp;
67 private int varout;
68 private int varp;
69 private int consobj;
70 private int consprod;
71
72 public TutModelData() {
73 super();
74
75 setConstants();
76 }
77
78 private void setConstants() {
79 Al = 0.16;
80 Ak = 2.0;
81 Ainp = 0.16;
82 Rho = 1.0;
83 K = 4.0;
84
85 varl = -1;
86 varinp = -1;
87 varout = -1;
88 varp = -1;
89 consobj = -1;
90 consprod = -1;
91 }
92
97 public void buildModel() {
98 // adding the variables to the model
99 varl = addVariable(0.1, Conopt.Infinity, 0.5);
100 varinp = addVariable(0.1, Conopt.Infinity, 0.5);
101 varout = addVariable(0.0, Conopt.Infinity);
102 varp = addVariable(0.0, Conopt.Infinity);
103
104 // adding the constraints to the model
105 // Constraint 1
106 {
107 int[] index = {varl, varinp, varout, varp};
108 double[] value = {-1, -1, 0, 0};
109 int[] nlflag = {0, 0, 1, 1};
110
111 consobj = addConstraint(ConstraintType.Free, -0.1, index, value, nlflag);
112 }
113
114 // Constraint 2
115 {
116 int[] index = {varl, varinp, varout};
117 double[] value = {0, 0, -1};
118 int[] nlflag = {1, 1, 0};
119 consprod = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
120 }
121
122 // Constraint 3
123 {
124 int[] index = {varout, varp};
125 double[] value = {1, 2};
126 int[] nlflag = {0, 0};
127 addConstraint(ConstraintType.Eq, 4.0, index, value, nlflag);
128 }
129
130 // setting the objective constraint
132
133 // setting the optimisation direction
135 }
136
141 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
142 double L = x[varl];
143 double Inp = x[varinp];
144 double Out = x[varout];
145 double P = x[varp];
146
147 double g = 0;
148 if (rowno == consobj) {
149 g = P * Out;
150 }
151 else if (rowno == consprod) {
152 double hold1 = (Al*Math.pow(L,(-Rho)) + Ak*Math.pow(K,(-Rho)) + Ainp*Math.pow(Inp,(-Rho)));
153 double hold2 = Math.pow(hold1,( -1./Rho ));
154
155 g = hold2;
156 }
157
158 return g;
159 }
160
165 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
166 assert x.length == jac.length;
167
168 double L = x[varl];
169 double Inp = x[varinp];
170 double Out = x[varout];
171 double P = x[varp];
172
173 if (rowno == consobj) {
174 jac[varout] = P;
175 jac[varp] = Out;
176 }
177 else if (rowno == consprod) {
178 double hold1 = (Al*Math.pow(L,(-Rho)) + Ak*Math.pow(K,(-Rho)) + Ainp*Math.pow(Inp,(-Rho)));
179 double hold2 = Math.pow(hold1,( -1./Rho ));
180 double hold3 = hold2 / hold1;
181
182 jac[varl] = hold3 * Al * Math.pow(L ,(-Rho-1.));
183 jac[varinp] = hold3 * Ainp * Math.pow(Inp,(-Rho-1.));
184 }
185 }
186}
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[])
Definition tutorial.java:15
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
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:97
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