CONOPT
Loading...
Searching...
No Matches
square2.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
14public class square2 {
15 public static void main(String argv[]) {
16 System.loadLibrary("conoptjni4");
17
18 String name = "square2";
19
20 Conopt conopt = new Conopt(name);
21 Square2ModelData model = new Square2ModelData();
23
24 // adding the message handler to the conopt interface
25 conopt.setMessageHandler(msghdlr);
26
27 // building the model
28 model.buildModel();
29
30 // loading the model in the conopt object
31 conopt.loadModel(model);
32
33 // tell conopt this is a square2 system
34 conopt.squareModel(1);
35
36 // try to set the license using the environment variables
37 try {
38 int license_int_1 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_1"));
39 int license_int_2 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_2"));
40 int license_int_3 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_3"));
41 String license_text = System.getenv("CONOPT_LICENSE_TEXT");
42
45 } catch (Exception e) {
46 System.out.println("Unable to set license: " + e.getMessage());
47 }
48
49 int coi_error = conopt.solve();
50 std s = new std();
51 int retcode = 0;
52
53 if (coi_error != 0) {
54 s.java_log(name, "Errors encountered during solution: " + coi_error);
55 retcode = -1;
56 }
57 // checking the status and objective value
58 else if (conopt.solutionStatus() != 1 || conopt.modelStatus() < 4 || conopt.modelStatus() > 5) {
59 s.java_log(name, "Solver or Model status not as expected (1,4) or (1,5)");
60 retcode = -1;
61 }
62 else {
63 // printing the final status of the optimisation
64 conopt.printStatus();
65 s.java_log(name, "Successful Solve");
66 }
67
68 msghdlr.close();
69 System.exit(retcode);
70 }
71}
72
73class Square2ModelData extends ModelData {
74
75 private int x0;
76 private int x1;
77 private int cons1;
78 private int cons2;
79 private int cons3;
80
81 public Square2ModelData() {
82 super();
83 }
84
89 public void buildModel() {
90 // Model x0 + x1 = 10
91 // x0 - x1 = 0
92 // x0 + x1 <= 9
93 // adding the variables to the model
96
97 // adding the constraints to the model
98 // Constraint 1
99 // Rhs = 10 and type Equality
100 {
101 int[] index = {x0, x1};
102 double[] value = {1, 1};
103 int[] nlflag = {0, 0};
104
105 cons1 = addConstraint(ConstraintType.Eq, 10.0, index, value, nlflag);
106 }
107
108 // Constraint 2
109 // Rhs = 0 and type Equality
110 {
111 int[] index = {x0, x1};
112 double[] value = {0, 0};
113 int[] nlflag = {1, 1};
114
115
116 cons2 = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
117 }
118
119 // Constraint 3
120 // Rhs = 9 and type Less Than Inequality
121 {
122 int[] index = {x0, x1};
123 double[] value = {0, 0};
124 int[] nlflag = {1, 1};
125
126
127 cons3 = addConstraint(ConstraintType.Eq, 9.0, index, value, nlflag);
128 }
129 }
130
135 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
136 double g = 0;
137 if (rowno == cons1) {}
138
139 else if (rowno == cons2) {
140 g = x[x0] - x[x1] ;
141 }
142 else if (rowno == cons3) {
143 g = x[x0] + x[x1] ;
144 }
145
146 return g;
147 }
148
153 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
154 assert x.length == jac.length;
155
156 if (rowno == cons1) {}
157
158 else if (rowno == cons2) {
159 jac[x0] = 1 ;
160 jac[x1] = -1 ;
161 }
162 else if (rowno == cons2) {
163 jac[x0] = 1 ;
164 jac[x1] = 1 ;
165 }
166 }
167}
The Conopt class.
Definition conopt.py:1380
static final ConstraintType Eq
A class that can be extended to build and solve a model using Conopt.
Definition conopt.py:2407
license_int_1
Definition square2.py:97
static void main(String argv[])
Definition square2.java:15
license_int_3
Definition square2.py:99
license_text
Definition square2.py:100
license_int_2
Definition square2.py:98
static void java_log(final String name, final String message)
Definition std.java:10
addConstraint(self, *args)
Overload 1: adds a constraint to the problem.
Definition conopt.py:2621
addVariable(self, *args)
Overload 1: adds a variable to the model.
Definition conopt.py:2677
void buildModel()
adds variables and constraints to the model
Definition square2.java:89
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
Definition square2.java:153
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row
Definition square2.java:135
Definition std.py:1