-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC0.java
More file actions
68 lines (64 loc) · 2.11 KB
/
C0.java
File metadata and controls
68 lines (64 loc) · 2.11 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
import java.io.*;
import java.util.*;
import ilog.concert.*;
import ilog.cplex.*;
import java.lang.System;
public class C0 {
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("instance1.txt");
int numberNodes = graph.getNumberOfNodes();
int source = graph.getSource();
int dest = graph.getDest();
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
for (Graph2.Edge edge : edgeList) {
weightMatrix[edge.getFirstNodeOfEdge()][edge.getSecondNodeOfEdge()] = edge.getWeight();
}
IloNumVar[] nodes = cplex.numVarArray(numberNodes, 0, Float.MAX_VALUE, IloNumVarType.Float);
cplex.addMaximize(nodes[dest]);
cplex.addEq(nodes[source], 0);
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.addLe(constraint, weightMatrix[i][j]);
}
}
}
}
// solve ILP
cplex.solve();
// output the optimal solution value of the objective function
System.out.println("~~~~~~~Optimal Value~~~~~~~\n" + cplex.getObjValue() + "\n");
// 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();
}
// if an exception is thrown, catch it
catch (IloException e) {
// output the stack trace for debugging
e.printStackTrace();
}
}
}