/// <summary> /// robber for test uses /// </summary> public Robber(int startNode, Board board) { myPath = new List<int>(); ocpupiedNode = startNode; movesSoFar = 0; myNeighbors = new List<int>(); myNeighbors = board.findNeighbors(ocpupiedNode); }
/// <summary> /// creates ellipse on a board in Point and gives it number i /// </summary> public Cop(Board board, Point node, int i) { myNode = new Node(node); myNeighbors = new List<int>(); myNode.number = i; board.pointCop(node, myNode); myNeighbors = board.findNeighbors(myNode.number); }
/// <summary> /// creates ellipse on a board in Point and gives it number i /// </summary> public Robber(Board board, Point node, int i) { myNode = new Node(node); myNeighbors = new List<int>(); myNode.number = i; movesSoFar = 0; myPath = new List<int>(); board.pointRobber(node, myNode); myNeighbors = board.findNeighbors(myNode.number); }
public static List<List<int>> removePitfalls(Board board) { int numberOfNodes = board.neighborForClass.Count(); bool isEqual=true; for (int i = 0; i < numberOfNodes; i++) { List<int> nodeList = board.neighborForClass[i]; if (nodeList.Count != 0) for (int j = 0; j < nodeList.Count; j++) { isEqual = true; int node = nodeList[j]; List<int> robPos = new List<int>(nodeList); List<int> copPos = new List<int>(board.findNeighbors(node, true)); copPos.Add(node); robPos.Add(i); copPos.Sort(); robPos.Sort(); for (int k = 0; k < robPos.Count; k++) { if (!copPos.Contains(robPos[k])) { isEqual = false; break; } } if (isEqual) { // nodeList.Remove(i); foreach (List<int> tmp in board.neighborForClass) { if (tmp.Contains(i)) tmp.Remove(i); } nodeList.Clear(); board.neighborForClass = removePitfalls(board); nodeList = board.neighborForClass[0]; i = 0; j = 0; break; } } } return board.neighborForClass; }
internal static int robber_moves_MCTS(Tree<Node<Data>> MCTSTree, int robberNode, int copNode, int graphWidth, int graphDepth, Board board, out Tree<Node<Data>> MCTSTreeOut) { if (MCTSTree == null || MCTSTree.node.GetData().Counter == 0) { Data data = new Data(); data.CopPos[0] = copNode; data.RobberPos = robberNode; data.Counter = graphDepth; MCTSTree = new Tree<Node<Data>>(simulateGame(graphWidth, graphDepth, 1, new Node<Data>(null, data), board)); MCTSTree.node = PropagateChildrenProbability(MCTSTree.node); return getBestTreeNode(MCTSTree, out MCTSTreeOut, board); } else { return getBestTreeNode(MCTSTree, out MCTSTreeOut, board); } }
internal static int getBestTreeNode(Tree<Node<Data>> Tree, out Tree<Node<Data>> newTree, Board board) { LinkedList<Node<Data>> linkedList = new LinkedList<Node<Data>>(Tree.node.GetChildren()); int node = 0, iter = 0, nodeToMove = 0; double maxProbability = 0; foreach (Node<Data> treeNode in linkedList) { Data data = treeNode.GetData(); if (data.Probability > maxProbability) { nodeToMove = data.RobberPos; node = iter; maxProbability = data.Probability; } iter++; } newTree = new Tree<Node<Data>>(Tree.node.GetChild(node)); return nodeToMove; }
internal static Data FindChildOnBoard(List<int> neighborList, Data nodeInfo, int copnumber, out int robberNodePosition, Board board) { List<int> path = new List<int>(); Data newNode = new Data(); if (neighborList.Count != 0) { newNode.RobberPos = neighborList[board.random.Next(neighborList.Count)]; //neighbors of robber } for (int j = 0; j < copnumber; j++) { path = Dijkstra(nodeInfo.CopPos[j], nodeInfo.CopPos[j], newNode.RobberPos, board); //path from cop to robber in the same variable if (neighborList.Count > 0) { newNode.CopPos[j] = path[1]; if (newNode.RobberCopDistance > path.Count - 1 || newNode.RobberCopDistance == -1) newNode.RobberCopDistance = path.Count - 1; } } robberNodePosition = newNode.RobberPos; return newNode; }
public static int robber_moves_randomBeacon(Board board, int r, int goOnTime, Robber robber, Cop cop) { int node = 0; if ((robber.movesSoFar == goOnTime || robber.movesSoFar == 1 || robber.myPath.Count <= robber.movesSoFar)) { robber.movesSoFar = 1; robber.myPath = getBeaconNodes(r, 1, board, cop, robber); //robber.move(robber.myPath[robber.movesSoFar], board); node = robber.myPath[robber.movesSoFar]; robber.movesSoFar++; } else { //robber.move(robber.myPath[robber.movesSoFar], board); node = robber.myPath[robber.movesSoFar]; robber.movesSoFar++; } return node; }
public static int robber_moves_greedy_dijkstra(int copnumber, Board board, Cop cop, Robber robber) { List<int> currentPath = new List<int>(); List<int> bestPath = new List<int>(); List<List<int>> currentPathToCops = new List<List<int>>(); bool defeat = false; int pathLength = 0; if (robber.myNeighbors.Contains(cop.ocupiedNode)) defeat = true; foreach (int item in robber.myNeighbors) { if (item != cop.myNode.number) { currentPath = Dijkstra(robber.ocpupiedNode, item, cop.ocupiedNode, board); currentPathToCops.Add(currentPath); foreach (List<int> tmp in currentPathToCops) { if (pathLength == 0 || pathLength < tmp.Count) { pathLength = tmp.Count; currentPath = tmp; } } defeat = false; if (bestPath.Count < currentPath.Count) bestPath = currentPath; currentPathToCops.Clear(); } } if (!defeat) { try { return bestPath[0]; } catch (Exception ex) { MessageBox.Show("ruch złodzieja" + ex); } } return -1; }
public static List<int> getBeaconNodes(int r, int copnumber, Board board, Cop cop, Robber robber) { List<int> path = new List<int>(); List<int> bestPath = new List<int>(); List<List<int>> currentPathToCops = new List<List<int>>(); int bestNode = 0, pathLength = 0; int node; for (int randNumber = 0; randNumber < r; randNumber++) { node = board.random.Next(board.neighbor.Count); if (cop.ocupiedNode != node) { path = Dijkstra(node, node, cop.ocupiedNode, board); currentPathToCops.Add(path); } foreach (List<int> tmp in currentPathToCops) { if (pathLength == 0 || pathLength > tmp.Count) { pathLength = tmp.Count; path = tmp; } } if (path.Count > bestPath.Count || bestPath.Count == 0) { bestPath = path; bestNode = node; } currentPathToCops.Clear(); } return Dijkstra(robber.ocpupiedNode, robber.ocpupiedNode, bestNode, board); }
public static int alphabeta(int node, int depth, int alpha, int beta, bool player, int oponentNode, int startDepth, Board board) { int goNode = node; if (depth == 0 || node == oponentNode) //lub koniec gry { return getNodeValue(node, oponentNode, player, board); } if (player == true) { foreach (int neighbor in board.findNeighbors(node)) { int alpha2 = Math.Max(alpha, alphabeta(oponentNode, depth - 1, alpha, beta, !player, neighbor, startDepth, board)); if (alpha2 > alpha) { goNode = neighbor; alpha = alpha2; } if (alpha>=beta) { return beta; } } return alpha; } else { foreach (int neighbor in board.findNeighbors(oponentNode)) { beta = Math.Min(beta, alphabeta(oponentNode, depth - 1, alpha, beta, !player, neighbor, startDepth, board)); if (alpha>=beta) { return alpha; } } return beta; } }
/// <summary> /// creating new board robber and cops for new series of runaways /// </summary> private void newDataForTest(int copnumber) { List<int> notSafe = new List<int>(); cops = new List<Cop>(); ocupied = new List<int>(); board = new Board(nodenumberMin, nodenumberMax, maxNodeSt); int nodenumber = board.neighbor.Count; int i; for (i = 0; i < copnumber; i++) { cops.Add(new Cop(board.random.Next(nodenumber), board)); cops[i].copId = i; if (!ocupied.Contains(cops[i].ocupiedNode)) { ocupied.Add(cops[i].ocupiedNode); cops[i].startNode = cops[i].ocupiedNode; } else { do { cops[i] = new Cop(board.random.Next(nodenumber), board); cops[i].startNode = cops[i].ocupiedNode; } while (ocupied.Contains(cops[i].startNode)); ocupied.Add(cops[i].startNode); } } foreach (Cop cop in cops) { foreach (int a in cop.myNeighbors) { notSafe.Add(a); } } i = 0; do { i++; robber = new Robber(board.random.Next(nodenumber), board); robber.startNode = robber.ocpupiedNode; if (i > 100) newDataForTest(copnumber); } while (ocupied.Contains(robber.ocpupiedNode) || notSafe.Contains(robber.ocpupiedNode)); }
private void UserGame_OnLoad() { checkboard.Children.Clear(); gType = Convert.ToString(Application.Current.Properties["gType"]); gWidth = (gType == "koperta" || gType == "4-regularny c3" || gType == "dwunastoscian" || gType=="petersen") ? 0 : Convert.ToInt32(Application.Current.Properties["gWidth"]); gHeight = (gType == "koperta" || gType == "4-regularny c3" || gType == "dwunastoscian" || gType == "petersen") ? 0 : Convert.ToInt32(Application.Current.Properties["gHeight"]); gAlgorithm = Convert.ToString(Application.Current.Properties["gAlgorithm"]); nodenumber = gHeight * gWidth; board = new Board(gWidth, gHeight, gType, (int)checkboard.Width, (int)checkboard.Height); Board.removePitfalls(board); foreach (List<int> tmp in board.neighborForClass) { if (tmp.Count != 0) { board.isCopWinGraph = false; } } if (board.isCopWinGraph) lblKlasa.Text = "Cop-Win"; else lblKlasa.Text = "Robber-Win"; foreach (Node node in board.vertex) { foreach (int tmp in board.neighbor[node.number]) { Line linia = board.drawLine(node.number, tmp); checkboard.Children.Add(linia); } } foreach (Node node in board.vertex) checkboard.Children.Add(node.elly); }
/// <summary> /// move and find neighbor list /// </summary> /// <param name="node">node number to move to</param> /// <param name="board">board for witch we find neighborhood</param> public void move(int node, Board board) { ocupiedNode = node; myNeighbors = board.findNeighbors(ocupiedNode); }
/// <summary> /// cop for test uses /// </summary> public Cop(int startNode, Board board) { ocupiedNode = startNode; myNeighbors = new List<int>(); myNeighbors = board.findNeighbors(ocupiedNode); }
internal static Node<Data> simulateGame(int searchWidth, int searchDepth, int copnumber, Node<Data> node, Board board) { if (searchDepth != 0) { Data nodeInfo = node.GetData(); List<int> neighborList = new List<int>(board.findNeighbors(nodeInfo.RobberPos)); List<int> path = new List<int>(); int robberNodePosition; for (int i = 0; i < searchWidth; i++) { node.AddChild(FindChildOnBoard(neighborList, nodeInfo, copnumber, out robberNodePosition, board)); if (neighborList.Contains(robberNodePosition)) neighborList.Remove(robberNodePosition); } //obliczanie prawdopodobieństw do ruchu na zaraz int sum = 0, iter = 0, maxNode = 0; double maxProbNode = 0; foreach (Node<Data> child in node.GetChildren()) { sum += child.GetData().RobberCopDistance; } foreach (Node<Data> child in node.GetChildren()) { child.GetData().Probability = (double)child.GetData().RobberCopDistance / sum; if (child.GetData().Probability > maxProbNode) { maxProbNode = child.GetData().Probability; maxNode = iter; } iter++; } simulateGame(searchWidth, searchDepth - 1, copnumber, node.GetChild(maxNode), board); } return node; }
private static int getNodeValue(int node, int oponentNode, bool player, Board board) { List<int> length = new List<int>(); if (player) { if (node == oponentNode) return -999; length = Dijkstra(node, node, oponentNode, board); return -length.Count; } else { if (node == oponentNode) return 999; length = Dijkstra(node, node, oponentNode, board); return (length.Count); } }
public static List<int> Dijkstra(int from, int startNode, int finalNode, Board board) { nodeNumber = board.neighbor.Count; List<int> path = new List<int>(); List<List<int>> visited = new List<List<int>>(nodeNumber); for (int i = 0; i < nodeNumber; i++) { visited.Add(new List<int>()); } List<int> neighbors = board.findNeighbors(startNode); Queue<int> q = new Queue<int>(nodeNumber); int tmpNode; visited[startNode].Add(from); foreach (int i in neighbors) { if (i == finalNode) { path.Add(startNode); path.Add(finalNode); return path; } else { q.Enqueue(i); visited[i].Add(startNode); } } while (q.Count != 0) { tmpNode = q.Dequeue(); neighbors = board.findNeighbors(tmpNode); foreach (int node in neighbors) { if (node == finalNode) { q.Enqueue(node); visited[node].Add(tmpNode); createPathFromQueue(visited, out path, node, startNode); return path; } else { if (visited[node].Count == 0) { q.Enqueue(node); visited[node].Add(tmpNode); } } } } return path; }