-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxStable.java
More file actions
100 lines (72 loc) · 2.33 KB
/
MaxStable.java
File metadata and controls
100 lines (72 loc) · 2.33 KB
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
package Code;
import ilog.concert.IloException;
import ilog.concert.IloLinearNumExpr;
import ilog.concert.IloNumVar;
import ilog.cplex.IloCplex;
import ilog.cplex.IloCplex.UnknownObjectException;
public class MaxStable {
int [][] M;
int n; //nombre de sommet
IloCplex model; //modele cplex
IloNumVar [] x; // variable
public MaxStable(int [][] M) throws IloException {
super();
this.M=M;
n=M.length;
model=new IloCplex();
creationModel();
//System.out.println(model.toString());
}
private void creationModel() throws IloException {
// TODO Auto-generated method stub
creationVariables();
creationConstraints();
creationObjective();
}
private void creationVariables() throws IloException {
// TODO Auto-generated method stub
x=model.boolVarArray(n);
}
public void creationConstraints() throws IloException {
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) { // Changed i to j
if(M[i][j] == 1) {
IloLinearNumExpr lin = model.linearNumExpr();
lin.addTerm(1, x[i]);
lin.addTerm(1, x[j]);
model.addLe(lin, 1); // Changed Le to addLe
}
}
}
}
private void creationObjective() throws IloException {
IloLinearNumExpr fo =model.linearNumExpr();
for(int i=0;i<n;i++) {
fo.addTerm(x[i], 1);
}
model.addMaximize(fo); //MAX
}
public boolean solve() throws IloException {
return model.solve();
}
public double [] getX() throws UnknownObjectException, IloException {
double[] d= new double[n];
if (solve()) {d=model.getValues(x);
}
return d;
}
public void getMaxStable() throws UnknownObjectException, IloException {
double [] d= getX();
System.out.println("S={");
for(int i=0;i<d.length;i++) {
if(d[i]==1)
System.out.println("V " +(i+1)+" ");
}
System.out.println("}");
}
public static void main(String[] args) throws IloException {
int [][] M = {{0,1,1,0,0,0,0,0},{1,0,0,0,0,1,0,0},{1,0,0,0,0,0,1,0},{0,0,0,0,1,0,0,1},{0,0,0,1,0,1,0,0},{0,1,0,0,1,0,1,0},{0,0,1,0,0,1,0,1},{0,0,0,1,0,0,1,0}};
MaxStable M1 =new MaxStable(M);
M1.getMaxStable();
}
}