public List <IState> transformToPlan(TSPSolutionPath solution) { List <IState> result = new List <IState>(); IState initialState = (IState)visitAllproblem.GetInitialState(); int[] currentValues = new int[initialState.GetSize()]; for (int i = 0; i < currentValues.Length; i++) { currentValues[i] = 1; } int currentPosition = solution.startNode; int previous = solution.endNode; int successor = 0; for (int i = 0; i < solution.inp.nodesCount - 1; i++) { if (currentValues.Skip(1).All(v => v == 0)) //everything waas visited { return(result); } int currentNodeID = getNodeIDByTSPNodeID(currentPosition, solution); currentValues[0] = currentNodeID; if (variableNoByNodeID.ContainsKey(currentNodeID)) { currentValues[variableNoByNodeID[currentNodeID]] = 0; } successor = solution.getSuccessor(currentPosition, previous); int successorNodeID = getNodeIDByTSPNodeID(successor, solution); if (currentNodeID == successorNodeID) { continue; } if (connected[currentNodeID, successorNodeID]) { previous = currentPosition; IState currentState = new State(currentValues); result.Add(currentState); } else { addPath(result, currentPosition, successor, out previous, currentValues, solution); } currentPosition = successor; } currentValues[0] = getNodeIDByTSPNodeID(currentPosition, solution); currentValues[variableNoByNodeID[getNodeIDByTSPNodeID(currentPosition, solution)]] = 0; result.Add(new State(currentValues)); return(result); }
private void addPath(List <IState> result, int currentPositionTSP, int targetPositionTSP, out int previousTSP, int[] currentValues, TSPSolutionPath solution) { //VisitAllVisualizer vis = new VisitAllVisualizer(this); //vis.draw(new VisitAllState(new State(visitAllproblem, currentValues.ToList().ToArray()), this)); int pos = currentPositionTSP, succ = 0, prev = 0; var targetNode = this.nodes[getNodeIDByTSPNodeID(targetPositionTSP, solution)]; var node = this.nodes[getNodeIDByTSPNodeID(currentPositionTSP, solution)]; IState currentState = new State(currentValues); result.Add(currentState); do { if (node.gridCoordX < targetNode.gridCoordX) { succ = node.successors.Where(s => s.gridCoordX > node.gridCoordX).Single().ID; } if (node.gridCoordX > targetNode.gridCoordX) { succ = node.successors.Where(s => s.gridCoordX < node.gridCoordX).Single().ID; } if (node.gridCoordY < targetNode.gridCoordY) { succ = node.successors.Where(s => s.gridCoordY > node.gridCoordY).Single().ID; } if (node.gridCoordY > targetNode.gridCoordY) { succ = node.successors.Where(s => s.gridCoordY < node.gridCoordY).Single().ID; } currentValues[0] = succ; currentValues[variableNoByNodeID[succ]] = 0; currentState = new State(currentValues); result.Add(currentState); prev = pos; pos = succ; node = nodes[pos]; } while (node.ID != targetNode.ID); previousTSP = Enumerable.Range(0, solution.inp.nodesCount).Where(r => getNodeIDByTSPNodeID(r, solution) == prev).Single(); }
protected int getNodeIDByTSPNodeID(int TSPNodeID, TSPSolutionPath solution) { var TSPNode = solution.inp.getPoint(TSPNodeID); return(nodeIDByCoordinates[(int)TSPNode.x][(int)TSPNode.y]); }