-
Notifications
You must be signed in to change notification settings - Fork 0
/
D0.java
77 lines (73 loc) · 2.4 KB
/
D0.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
import java.io.*;
import java.util.*;
import ilog.concert.*;
import ilog.cplex.*;
import java.lang.System;
public class D0 {
public static void main(String[] args) throws FileNotFoundException {
try {
double start = System.currentTimeMillis();
// initializes a new cplex object
IloCplex cplex = new IloCplex();
// Create the graph and get all properties
Graph2 graph = new Graph2("graph1.txt");
int numberNodes = graph.getNumberOfNodes();
// get the weights of all nodes
double[] nodeWeights = new double[numberNodes];
ArrayList<Graph2.Node> nodeList = graph.getNodeList();
for (Graph2.Node node: nodeList) {
nodeWeights[node.getID()] = node.getWeight();
}
// get the weights of all edges
double[][] weightMatrix = new double[numberNodes][numberNodes];
ArrayList<Graph2.Edge> edgeList = graph.getEdgelist();
// Start as unreachable
for (int i = 0; i < numberNodes; i++) {
for (int j = 0; j < numberNodes; j++) {
weightMatrix[i][j] = 0;
}
}
// populate weights where possible
// we do not actually need this
// we just need which nodes are connected to which other nodes
for (Graph2.Edge edge : edgeList) {
weightMatrix[edge.getFirstNodeOfEdge()][edge.getSecondNodeOfEdge()] = edge.getWeight();
}
IloNumVar[] nodes = cplex.numVarArray(numberNodes, 0, 1, IloNumVarType.Int);
IloLinearNumExpr obj = cplex.linearNumExpr();
for (int i = 0; i < numberNodes; i++) {
obj.addTerm(nodes[i], nodeWeights[i]);
}
cplex.addMinimize(obj);
// add constraints
for (int i = 0; i < numberNodes; i++) {
for (int j = 0; j < numberNodes; j++) {
if (i != j) {
if (weightMatrix[i][j] != 0) {
IloLinearNumExpr constraint = cplex.linearNumExpr();
constraint.addTerm(nodes[j], 1);
constraint.addTerm(nodes[i], 1);
cplex.addGe(constraint, 1);
}
}
}
}
// solve ILP
cplex.solve();
// output the optimal solution value of the objective function
System.out.println();
System.out.println("Value " + cplex.getObjValue());
System.out.println();
// print values
for (int i = 0; i < numberNodes; i++) {
System.out.println("Node " + i + ": " + cplex.getValue(nodes[i]));
}
System.out.println();
double end = System.currentTimeMillis();
System.out.println("Running time = " + (end-start) + "ms");
// close cplex object
cplex.end();
}
catch (IloException e) {}
}
}