Object[] edges = graphPlusDelaysAsNodes.edges().toArray();
HashMap edgeProfitsMap = new HashMap();
for (int j = 0; j < edges.length; j++) {
Edge edge = (Edge) edges[j];
Node source = edge.source();
Node sink = edge.sink();
//_edgeCostsMap.put(edge, _edgeCosts.toInt());
// For all the edges that have at least one delay
if (_edgeCosts.toInt(edge) != 0) {
graphPlusDelaysAsNodes.removeEdge(edge);
int delays = _edgeCosts.toInt(edge);
for (int i = 0; i < delays; i++) {
Node addedNode = graphPlusDelaysAsNodes.addNodeWeight("D"
+ j + i);
_delayNodeList.add(addedNode);
Edge addedEdge = graphPlusDelaysAsNodes.addEdge(source,
addedNode);
edgeProfitsMap.put(addedEdge, Double.valueOf(0.0));
source = addedNode;
}
Edge lastAddedEdge = graphPlusDelaysAsNodes.addEdge(source,
sink);
edgeProfitsMap.put(lastAddedEdge, Double.valueOf(_edgeProfits
.toDouble(edge)));
} else {
edgeProfitsMap.put(edge, Double.valueOf(_edgeProfits
.toDouble(edge)));
}
}
HashMap D = new HashMap(_delayNodeList.size());
edges = graphPlusDelaysAsNodes.edges().toArray();
// compute the first order longest path matrix
HashMap predecessorMap = new HashMap();
for (Iterator delayNodes = _delayNodeList.iterator(); delayNodes
.hasNext();) {
Node delayNode = (Node) delayNodes.next();
DirectedGraph thisRoundGraph = (DirectedGraph) graphPlusDelaysAsNodes
.clone();
HashMap delayGraphProfitMap = new HashMap();
for (int j = 0; j < edges.length; j++) {
Edge edge = (Edge) edges[j];
Node source = edge.source();
Node sink = edge.sink();
if (sink == delayNode) {
predecessorMap.put(delayNode, source);
}
if (_delayNodeList.contains(source)
|| _delayNodeList.contains(sink)) {
if (source == delayNode) {
delayGraphProfitMap.put(edge, edgeProfitsMap.get(edge));
}
if (sink == delayNode) {
thisRoundGraph.removeEdge(edge);
}
if ((source != delayNode) && (sink != delayNode)) {
if (_delayNodeList.contains(source)) {
thisRoundGraph.removeEdge(edge);
} else {
delayGraphProfitMap.put(edge, edgeProfitsMap
.get(edge));
}
}
} else {
delayGraphProfitMap.put(edge, edgeProfitsMap.get(edge));
}
}
SingleSourceLongestPathAnalysis longestPath = null;
longestPath = new SingleSourceLongestPathAnalysis(thisRoundGraph,
delayNode, new ToDoubleMapMapping(delayGraphProfitMap));
D.put(delayNode, longestPath);
}
_makeFirstOrderLongestPathMatrix(D, graphPlusDelaysAsNodes,
predecessorMap);
// create the delay graph on which the maximum cycle mean is going to
// be executed.
DirectedGraph delayGraph = new DirectedGraph();
HashMap delayGraphEdgeProfits = new HashMap();
for (int i = 0; i < _delayNodeList.size(); i++) {
delayGraph.addNode((Node) _delayNodeList.get(i));
}
for (int i = 0; i < _delayNodeList.size(); i++) {
for (int j = 0; j < _delayNodeList.size(); j++) {
Node source = (Node) _delayNodeList.get(i);
Node sink = (Node) _delayNodeList.get(j);
if (_firstOrderLongestPathMatrix[i][j] >= 0) {
if (!((source == sink) && (_firstOrderLongestPathMatrix[i][j] == 0))) {
Edge addedEdge = delayGraph.addEdge(source, sink);
delayGraphEdgeProfits.put(addedEdge, Double
.valueOf(_firstOrderLongestPathMatrix[i][j]));
}
}
}
}
double result = _computeMCM(delayGraph, new ToDoubleMapMapping(
delayGraphEdgeProfits));
// creating the cycle that leads to the result
edges = graphPlusDelaysAsNodes.edges().toArray();
Object[] delayNodes = _delayCycle.toArray();
for (int i = 0; i < delayNodes.length; i++) {
Node delayNode = (Node) delayNodes[i];
for (int j = 0; j < delayNodes.length; j++) {
if ((i != j) || (delayNodes.length != 1)) {
Node endDelayNode = (Node) delayNodes[j];
List path = ((SingleSourceLongestPathAnalysis) (D
.get(delayNode))).path(endDelayNode);
for (int k = 0; k < path.size(); k++) {
if (!(_delayNodeList.contains(path.get(k)))) {
_maximumProfitToCostRatioCycle.add(path.get(k));
}
}
} else if (delayNodes.length == 1) {
Node predecessor = (Node) (predecessorMap.get(delayNode));
if (!(_delayNodeList.contains(predecessor))) {
List path = ((SingleSourceLongestPathAnalysis) (D
.get(delayNode))).path(predecessor);