CONOPT
Loading...
Searching...
No Matches
tutorialk.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
14public class tutorialk {
15 public static void main(String argv[]) {
16 System.loadLibrary("conoptjni4");
17
18 String name = "tutorialk";
19
20 Conopt conopt = new Conopt(name);
21 TutKModelData model = new TutKModelData();
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 TutKModelData extends ModelData {
57 /* declaring the parameters */
58 private double Al;
59 private double Ak;
60 private double Ainp;
61 private double Rho;
62
63 /* declaring the variable and constraint indices. */
64 private int varl;
65 private int vark;
66 private int varinp;
67 private int varout;
68 private int varp;
69 private int consobj;
70 private int consprod;
71
72 public TutKModelData() {
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 }
84
89 public void buildModel() {
90 // adding the variables to the model
91 varl = addVariable(0.1, Conopt.Infinity, 0.5);
92 vark = addVariable(4.0, 4.0, 4.0);
93 varinp = addVariable(0.1, Conopt.Infinity, 0.5);
94 varout = addVariable(0.0, Conopt.Infinity);
95 varp = addVariable(0.0, Conopt.Infinity);
96
97 // adding the constraints to the model
98 // Constraint 1
99 {
100 int[] index = {varl, varinp, varout, varp};
101 double[] value = {-1, -1, 0, 0};
102 int[] nlflag = {0, 0, 1, 1};
103
104 consobj = addConstraint(ConstraintType.Free, -0.1, index, value, nlflag);
105 }
106
107 // Constraint 2
108 {
109 int[] index = {varl, vark, varinp, varout};
110 double[] value = {0, 0, 0, -1};
111 int[] nlflag = {1, 1, 1, 0};
112 consprod = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
113 }
114
115 // Constraint 3
116 {
117 int[] index = {varout, varp};
118 double[] value = {1, 2};
119 int[] nlflag = {0, 0};
120 addConstraint(ConstraintType.Eq, 4, index, value, nlflag);
121 }
122
123 // setting the objective constraint
125
126 // setting the optimisation direction
128 }
129
134 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
135 double L = x[varl];
136 double K = x[vark];
137 double Inp = x[varinp];
138 double Out = x[varout];
139 double P = x[varp];
140
141 double g = 0;
142 if (rowno == consobj) {
143 g = P * Out;
144 }
145 else if (rowno == consprod) {
146 double hold1 = (Al * Math.pow(L, (-Rho)) + Ak * Math.pow(K, (-Rho)) + Ainp * Math.pow(Inp, (-Rho)));
147 double hold2 = Math.pow(hold1,( -1. / Rho ));
148
149 g = hold2;
150 }
151
152 return g;
153 }
154
159 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
160 assert x.length == jac.length;
161
162 double L = x[varl];
163 double K = x[vark];
164 double Inp = x[varinp];
165 double Out = x[varout];
166 double P = x[varp];
167
168 if (rowno == consobj) {
169 jac[varout] = P;
170 jac[varp] = Out;
171 }
172 else if (rowno == consprod) {
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[varl] = hold3 * Al * Math.pow(L, (-Rho - 1.));
178 jac[vark] = hold3 * Ak * Math.pow(K, (-Rho - 1.));
179 jac[varinp] = hold3 * Ainp * Math.pow(Inp, (-Rho - 1.));
180 }
181 }
182}
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
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 std.py:1
double L
Definition tutoriali.c:16
double K
Definition tutoriali.c:27
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