-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegerCplexAutomation.java
144 lines (131 loc) · 5.1 KB
/
IntegerCplexAutomation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import ilog.concert.*;
import ilog.concert.IloIntVar;
import ilog.cplex.IloCplex;
public class IntegerCplexAutomation {
private IloIntVar[] xs;
private IloCplex model;
private IloLinearIntExpr objective;
private Boolean objectiveSetted = false;
private Boolean conditionSetted = false;
IntegerCplexAutomation(int amountOfVariables){
try{
model = new IloCplex();
xs = new IloIntVar[amountOfVariables];
objective = model.linearIntExpr();
for(int i= 0; i<amountOfVariables;i++){
xs[i] = model.intVar(0,Integer.MAX_VALUE,("x"+(i+1)));
}
}catch(IloException e){
exception(e);
}
}
IntegerCplexAutomation(int amountOfVariables, int[] expoents, char type){
Boolean validType = ((type=='+')||(type=='-'));
if(validType&&(expoents.length==amountOfVariables)){
try{
model = new IloCplex();
xs = new IloIntVar[amountOfVariables];
objective = model.linearIntExpr();
for(int i= 0; i<amountOfVariables;i++){
xs[i] = model.intVar(0,Integer.MAX_VALUE,("x"+(i+1)));
}
for (int i = 0; i < xs.length; i++) {
objective.addTerm(expoents[i], xs[i]);
}
switch(type){
case '+':
model.addMaximize(objective);
break;
case '-':
model.addMinimize(objective);
break;
}
objectiveSetted=true;
}catch(IloException e){
exception(e);
}
}else{
System.out.println("Error: "+((validType==false)?(type)+" is not a valid type!":"invalid amount of expoents!"));
}
}
public void setObjectiveEquation(int[] expoents, char type){
Boolean validType = ((type=='+')||(type=='-'));
if(validType&&(expoents.length==xs.length)){
try {
for (int i = 0; i < xs.length; i++) {
objective.addTerm(expoents[i], xs[i]);
}
switch(type){
case '+':
model.addMaximize(objective);
break;
case '-':
model.addMinimize(objective);
break;
}
objectiveSetted=true;
} catch (IloException e) {
exception(e);
}
}else{
System.out.println("Error: "+((validType==false)?(type)+" is not a valid type!":"invalid amount of expoents!"));
}
}
public void addCondition(int[] expoents,String operation,int result){
boolean validOperation =(operation.equals("=="))||(operation.equals("<="))||(operation.equals(">="));
IloNumExpr[] expr;
if(objectiveSetted==true){
try{
if(validOperation&&(expoents.length==xs.length)){
expr = new IloNumExpr[xs.length];
for(int i =0;i< xs.length;i++) {
expr[i]= model.prod(expoents[i],this.xs[i]);
}
switch (operation){
case "==":
model.addEq(model.sum(expr),result);
break;
case "<=":
model.addLe(model.sum(expr),result);
break;
case ">=":
model.addGe(model.sum(expr),result);
break;
}
conditionSetted = true;
}else{
System.out.println("Error: "+((validOperation==false)?(operation)+" is not a valid operation!":"invalid amount of expoents!"));
}
}catch (IloException e){
exception(e);
}
}else{
System.out.println("The objective function has not been declared");
}
}
public String solve(){
String result = null;
if(conditionSetted==true) {
try {
if (model.solve()) {
result = model.getObjective().toString() + "\n";
for (int i = 0; i < xs.length; i++) {
result = result + "x" + (i + 1) + " = " + model.getValue(xs[i]) + ";\n";
}
} else {
result = "Unresponsive system";
}
} catch (IloException e) {
exception(e);
}
}else{
System.out.println("Any condition has not been declared");
}
return result;
}
private void exception(IloException e){
System.out.println("Error:");
System.out.println(e.getMessage());
e.printStackTrace();
}
}