private int calculateCost(List <List <TimedMove> > plan, CFMAStar.CostFunction costFunction) { if (costFunction == CFMAStar.CostFunction.SOC) { return(sumSolutionCost(this.plan)); } plan.Sort((x, y) => y.Count - x.Count); return(plan[0].Count - 1); }
public void reduce(CFMAStar.CostFunction costFunction) { timer = Stopwatch.StartNew(); if (CreateNFProblem(costFunction)) { ImportToMCMFAlgorithm(); } timer.Stop(); }
/// <summary> /// Creates an initial network flow problem reduced from the given problem /// </summary> private bool CreateNFProblem(CFMAStar.CostFunction costFunction) { this.superSink = new NFReducerNode(-1, -1, -1); NFNodes.Add(this.superSink); ReducerOpenList <NFReducerNode> openList = new ReducerOpenList <NFReducerNode>(); openList.Enqueue(new NFReducerNode(0, goalState.x, goalState.y)); while (openList.Count != 0) { //if (timer.ElapsedMilliseconds > Constants.MCMF_MAX_TIME) // return false; NFReducerNode node = openList.Dequeue(); LinkedList <NFReducerNode> nodeSons = new LinkedList <NFReducerNode>(); if (l == -1 || (l != -1 && node.nodeTime != T)) { nodeSons = GetSons(node, openList); } foreach (NFReducerNode son in nodeSons) { son.AddEdgeTo(node); node.AddEdgeFrom(son); if (!openList.Contains(son)) { openList.Enqueue(son); } if (l == -1 && IsStartPosition(son) && this.startPositionsDict[new KeyValuePair <int, int>(son.x, son.y)] == 1) { this.startPositionsDict[new KeyValuePair <int, int>(son.x, son.y)] = 0; startPositionsToDiscover--; } if (l == -1 && startPositionsToDiscover == 0) { l = son.nodeTime; if (costFunction == CFMAStar.CostFunction.SOC) { T = l + startPositions.Length - 1; } else { T = l; } } } if (!NFNodes.Contains(node)) { AddAfterDuplicationAndSinkConnection(node); } } return(true); }
public long solve(CFMAStar.CostFunction costFunction) { // Independence Detection List <List <TimedMove> > nonConflictsPaths = null; IndependentDetection id = new IndependentDetection(this.problemInstance, this.goalState); MAM_AgentState[] newStartPositions = id.Detect(out nonConflictsPaths); if (newStartPositions.Length != 0) { this.problemInstance = problemInstance.ReplanProblem(newStartPositions); this.reducer = new CFMAM_MCMF_Reducer(this.problemInstance, this.goalState); reducer.reduce(costFunction); if (reducer.outputProblem == null) { return(-1); } MinCostMaxFlow mcmfSolver = new MinCostMaxFlow(reducer.outputProblem); timer = Stopwatch.StartNew(); solution = mcmfSolver.SolveMinCostFlow(); List <TimedMove>[] partialPlan = this.reducer.GetCFMAMSolution(this.solution, this.mcmfTime, true); if (costFunction == CFMAStar.CostFunction.MakeSpan) { while (!isPathForEachAgent(partialPlan)) { this.reducer.addNetworkLayer(); mcmfSolver = new MinCostMaxFlow(reducer.outputProblem); solution = mcmfSolver.SolveMinCostFlow(); partialPlan = this.reducer.GetCFMAMSolution(this.solution, this.mcmfTime, true); } } timer.Stop(); this.plan = mergePlans(partialPlan, nonConflictsPaths); this.mcmfTime = timer.ElapsedMilliseconds; this.solutionCost = calculateCost(this.plan, costFunction); } else { this.plan = nonConflictsPaths; this.solutionCost = calculateCost(this.plan, costFunction); } return(this.solutionCost); }