CONOPT
Loading...
Searching...
No Matches
square.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
14public class square {
15 public static void main(String argv[]) {
16 System.loadLibrary("conoptjni4");
17
18 String name = "square";
19
20 Conopt conopt = new Conopt(name);
21 SquareModelData model = new SquareModelData();
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 square 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 statuses and objective value
58 else if (conopt.modelStatus() != 16 || conopt.solutionStatus() != 1) {
59 s.java_log(name, "Solver or Model status not as expected (1,16)");
60 retcode = -1;
61 }
62 else {
63 // printing the final status of the optimisation
64 s.java_log(name, "Successful Solve");
65 conopt.printStatus();
66 }
67
68 msghdlr.close();
69 System.exit(retcode);
70 }
71}
72
73class SquareModelData extends ModelData {
74
75 private int x0;
76 private int x1;
77 private int cons1;
78 private int cons2;
79
80 public SquareModelData() {
81 super();
82 }
83
88 public void buildModel() {
89 // Model x0 + x1 = 10
90 // x0 - x1 = 0
91 // adding the variables to the model
94
95 // adding the constraints to the model
96 // Constraint 1
97 {
98 int[] index = {x0, x1};
99 double[] value = {1, 1};
100 int[] nlflag = {0, 0};
101
102 cons1 = addConstraint(ConstraintType.Eq, 10.0, index, value, nlflag);
103 }
104
105 // Constraint 2
106 {
107 int[] index = {x0, x1};
108 double[] value = {0, 0};
109 int[] nlflag = {1, 1};
110
111
112 cons2 = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
113 }
114 }
115
120 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
121 double g = 0;
122 if (rowno == cons1) {}
123
124 else if (rowno == cons2) {
125 g = x[x0] - x[x1] ;
126 }
127
128 return g;
129 }
130
135 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
136 assert x.length == jac.length;
137
138 if (rowno == cons1) {}
139
140 else if (rowno == cons2) {
141 jac[x0] = 1 ;
142 jac[x1] = -1 ;
143 }
144 }
145}
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
static void main(String argv[])
Definition square.java:15
coi_error
Definition square.py:103
license_int_2
Definition square.py:87
license_int_3
Definition square.py:88
license_int_1
Definition square.py:86
msghdlr
Definition square.py:75
license_text
Definition square.py:89
model
Definition square.py:74
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 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 square.java:135
void buildModel()
adds variables and constraints to the model
Definition square.java:88
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row
Definition square.java:120
Definition std.py:1