/// <summary> /// Generates a problem instance based on a DAO map file. /// TODO: Fix code dup with GenerateProblemInstance and Import later. /// </summary> /// <param name="agentsNum"></param> /// <returns></returns> public ProblemInstance GenerateDragonAgeProblemInstance(string mapFileName, int agentsNum) { /** * Randomization based on timer is disabled for purposes of getting * reproducible experiments. */ //Random rand = new Random(); m_mapFileName = mapFileName; m_agentNum = agentsNum; TextReader input = new StreamReader(mapFileName); string[] lineParts; string line; line = input.ReadLine(); Debug.Assert(line.StartsWith("type octile")); // Read grid dimensions line = input.ReadLine(); lineParts = line.Split(' '); Debug.Assert(lineParts[0].StartsWith("height")); int maxX = int.Parse(lineParts[1]); line = input.ReadLine(); lineParts = line.Split(' '); Debug.Assert(lineParts[0].StartsWith("width")); int maxY = int.Parse(lineParts[1]); line = input.ReadLine(); Debug.Assert(line.StartsWith("map")); bool[][] grid = new bool[maxX][]; char cell; for (int i = 0; i < maxX; i++) { grid[i] = new bool[maxY]; line = input.ReadLine(); for (int j = 0; j < maxY; j++) { cell = line.ElementAt(j); if (cell == '@' || cell == 'O' || cell == 'T' || cell == 'W' /* Water isn't traversable from land */) { grid[i][j] = true; } else { grid[i][j] = false; } } } int x; int y; Agent[] agentGoals = new Agent[agentsNum]; AgentState[] agentStates = new AgentState[agentsNum]; bool[][] goals = new bool[maxX][]; for (int i = 0; i < maxX; i++) { goals[i] = new bool[maxY]; } // Choose random valid unclaimed goal locations for (int i = 0; i < agentsNum; i++) { x = rand.Next(maxX); y = rand.Next(maxY); if (goals[x][y] || grid[x][y]) { i--; } else { goals[x][y] = true; agentGoals[i] = new Agent(x, y, i); } } // Select random start/goal locations for every agent by performing a random walk for (int i = 0; i < agentsNum; i++) { agentStates[i] = new AgentState(agentGoals[i].Goal.x, agentGoals[i].Goal.y, agentGoals[i]); } ProblemInstance problem = new ProblemInstance(); problem.parameters[ProblemInstance.GRID_NAME_KEY] = Path.GetFileNameWithoutExtension(mapFileName); problem.Init(agentStates, grid); for (int j = 0; j < RANDOM_WALK_STEPS; j++) { for (int i = 0; i < agentsNum; i++) { goals[agentStates[i].lastMove.x][agentStates[i].lastMove.y] = false; // We're going to move the goal somewhere else // Move in a random legal direction: while (true) { Move.Direction op = (Move.Direction)rand.Next(0, 5); // TODO: fixme agentStates[i].lastMove.Update(op); if (problem.IsValid(agentStates[i].lastMove) && !goals[agentStates[i].lastMove.x][agentStates[i].lastMove.y]) // This spot isn't another agent's goal { break; } else { agentStates[i].lastMove.setOppositeMove(); // Rollback } } goals[agentStates[i].lastMove.x][agentStates[i].lastMove.y] = true; // Claim agent's new goal } } // Zero the agents' timesteps foreach (AgentState agentStart in agentStates) { agentStart.lastMove.time = 0; } return(problem); }
/// <summary> /// Generates a problem instance, including a board, start and goal locations of desired number of agents /// and desired precentage of obstacles /// TODO: Refactor to use operators. /// </summary> /// <param name="gridSize"></param> /// <param name="agentsNum"></param> /// <param name="obstaclesNum"></param> /// <returns></returns> public ProblemInstance GenerateProblemInstance(int gridSize, int agentsNum, int obstaclesNum) { m_mapFileName = "GRID" + gridSize + "X" + gridSize; m_agentNum = agentsNum; /** * Randomization based on timer is disabled for purposes of getting * reproducible experiments. */ //Random rand = new Random(); if (agentsNum + obstaclesNum + 1 > gridSize * gridSize) { throw new Exception("Not enough room for " + agentsNum + ", " + obstaclesNum + " and one empty space in a " + gridSize + "x" + gridSize + "map."); } int x; int y; Agent[] aGoals = new Agent[agentsNum]; AgentState[] aStart = new AgentState[agentsNum]; bool[][] grid = new bool[gridSize][]; bool[][] goals = new bool[gridSize][]; // Generate a random grid for (int i = 0; i < gridSize; i++) { grid[i] = new bool[gridSize]; goals[i] = new bool[gridSize]; } for (int i = 0; i < obstaclesNum; i++) { x = rand.Next(gridSize); y = rand.Next(gridSize); if (grid[x][y]) // Already an obstacle { i--; } grid[x][y] = true; } // Choose random goal locations for (int i = 0; i < agentsNum; i++) { x = rand.Next(gridSize); y = rand.Next(gridSize); if (goals[x][y] || grid[x][y]) { i--; } else { goals[x][y] = true; aGoals[i] = new Agent(x, y, i); } } // Select random start/goal locations for every agent by performing a random walk for (int i = 0; i < agentsNum; i++) { aStart[i] = new AgentState(aGoals[i].Goal.x, aGoals[i].Goal.y, aGoals[i]); } // Initialized here only for the IsValid() call. TODO: Think how this can be sidestepped elegantly. ProblemInstance problem = new ProblemInstance(); problem.Init(aStart, grid); for (int j = 0; j < RANDOM_WALK_STEPS; j++) { for (int i = 0; i < agentsNum; i++) { goals[aStart[i].lastMove.x][aStart[i].lastMove.y] = false; // We're going to move the goal somewhere else while (true) { Move.Direction op = (Move.Direction)rand.Next(0, 5); // TODO: fixme aStart[i].lastMove.Update(op); if (problem.IsValid(aStart[i].lastMove) && !goals[aStart[i].lastMove.x][aStart[i].lastMove.y]) // this spot isn't another agent's goal { break; } else { aStart[i].lastMove.setOppositeMove(); // Rollback } } goals[aStart[i].lastMove.x][aStart[i].lastMove.y] = true; // Claim agent's new goal } } // Zero the agents' timesteps foreach (AgentState agentStart in aStart) { agentStart.lastMove.time = 0; } // TODO: There is some repetition here of previous instantiation of ProblemInstance. Think how to elegantly bypass this. problem = new ProblemInstance(); problem.Init(aStart, grid); return(problem); }
/// <summary> /// Imports a problem instance from a given file /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static ProblemInstance Import(string fileName) { TextReader input = new StreamReader(fileName); string[] lineParts; string line; int instanceId = 0; string gridName = "Random Grid"; // The default line = input.ReadLine(); if (line.StartsWith("Grid:") == false) { lineParts = line.Split(','); instanceId = int.Parse(lineParts[0]); if (lineParts.Length > 1) { gridName = lineParts[1]; } line = input.ReadLine(); } //instanceId = int.Parse(fileName.Split('-')[4]); // First/second line is Grid: Debug.Assert(line.StartsWith("Grid:")); // Read grid dimensions line = input.ReadLine(); lineParts = line.Split(','); int maxX = int.Parse(lineParts[0]); int maxY = int.Parse(lineParts[1]); bool[][] grid = new bool[maxX][]; char cell; for (int i = 0; i < maxX; i++) { grid[i] = new bool[maxY]; line = input.ReadLine(); for (int j = 0; j < maxY; j++) { cell = line.ElementAt(j); if (cell == '@' || cell == 'O' || cell == 'T' || cell == 'W' /* Water isn't traversable from land */) { grid[i][j] = true; } else { grid[i][j] = false; } } } // Next line is Agents: line = input.ReadLine(); Debug.Assert(line.StartsWith("Agents:")); // Read the number of agents line = input.ReadLine(); int numOfAgents = int.Parse(line); // Read the agents' start and goal states AgentState[] states = new AgentState[numOfAgents]; AgentState state; Agent agent; int agentNum; int goalX; int goalY; int startX; int startY; for (int i = 0; i < numOfAgents; i++) { line = input.ReadLine(); lineParts = line.Split(EXPORT_DELIMITER); agentNum = int.Parse(lineParts[0]); goalX = int.Parse(lineParts[1]); goalY = int.Parse(lineParts[2]); startX = int.Parse(lineParts[3]); startY = int.Parse(lineParts[4]); agent = new Agent(goalX, goalY, agentNum); state = new AgentState(startX, startY, agent); states[i] = state; } // Generate the problem instance ProblemInstance instance = new ProblemInstance(); instance.Init(states, grid); instance.instanceId = instanceId; instance.parameters[ProblemInstance.GRID_NAME_KEY] = gridName; instance.ComputeSingleAgentShortestPaths(); return(instance); }
/// <summary> /// Generates a problem instance based on a DAO map file. /// TODO: Fix code dup with GenerateProblemInstance and Import later. /// </summary> /// <param name="agentsNum"></param> /// <returns></returns> public ProblemInstance GenerateDragonAgeProblemInstance ( string mapFileName, int agentsNum ) { m_mapFileName = mapFileName; m_agentNum = agentsNum; TextReader input = new StreamReader(mapFileName); string[] lineParts; string line; line = input.ReadLine(); Debug.Assert(line.StartsWith("type octile")); // Read grid dimensions line = input.ReadLine(); lineParts = line.Split(' '); Debug.Assert(lineParts[0].StartsWith("height")); int maxX = int.Parse(lineParts[1]); line = input.ReadLine(); lineParts = line.Split(' '); Debug.Assert(lineParts[0].StartsWith("width")); int maxY = int.Parse(lineParts[1]); line = input.ReadLine(); Debug.Assert(line.StartsWith("map")); bool[][] grid = new bool[maxX][]; char cell; for (int i = 0; i < maxX; i++) { grid[i] = new bool[maxY]; line = input.ReadLine(); for (int j = 0; j < maxY; j++) { cell = line.ElementAt(j); if (cell == '@' || cell == 'O' || cell == 'T' || cell == 'W' /* Water isn't traversable from land */) { grid[i][j] = true; } else { grid[i][j] = false; } } } int x; int y; MAM_AgentState[] aStart = new MAM_AgentState[agentsNum]; bool[][] starts = new bool[maxX][]; for (int i = 0; i < maxX; i++) { starts[i] = new bool[maxY]; } // Choose random valid unclaimed goal locations for (int i = 0; i < agentsNum; i++) { x = rand.Next(maxX); y = rand.Next(maxY); if (starts[x][y] || grid[x][y]) { i--; } else { starts[x][y] = true; aStart[i] = new MAM_AgentState(x, y, i, 0); } } ProblemInstance problem = new ProblemInstance(); problem.parameters[ProblemInstance.GRID_NAME_KEY] = Path.GetFileNameWithoutExtension(mapFileName); problem.Init(aStart, grid); return(problem); }
/// <summary> /// Generates a problem instance, including a board, start and goal locations of desired number of agents /// and desired precentage of obstacles /// TODO: Refactor to use operators. /// </summary> /// <param name="gridSize"></param> /// <param name="agentsNum"></param> /// <param name="obstaclesNum"></param> /// <returns></returns> public ProblemInstance GenerateProblemInstance ( int gridSize, int agentsNum, int obstaclesNum ) { m_mapFileName = "GRID" + gridSize + "X" + gridSize; m_agentNum = agentsNum; if (agentsNum + obstaclesNum > gridSize * gridSize) { throw new Exception("Not enough room for " + agentsNum + ", " + obstaclesNum + " and one empty space in a " + gridSize + "x" + gridSize + "map."); } int x; int y; MAM_AgentState[] aStart = new MAM_AgentState[agentsNum]; bool[][] grid = new bool[gridSize][]; bool[][] starts = new bool[gridSize][]; // Generate a random grid for (int i = 0; i < gridSize; i++) { grid[i] = new bool[gridSize]; starts[i] = new bool[gridSize]; } for (int i = 0; i < obstaclesNum; i++) { x = rand.Next(gridSize); y = rand.Next(gridSize); if (grid[x][y]) // Already an obstacle { i--; } grid[x][y] = true; } // Choose random start locations for (int i = 0; i < agentsNum; i++) { x = rand.Next(gridSize); y = rand.Next(gridSize); if (starts[x][y] || grid[x][y]) { i--; } else { starts[x][y] = true; aStart[i] = new MAM_AgentState(x, y, i, 0); } } ProblemInstance problem = new ProblemInstance(); problem = new ProblemInstance(); problem.Init(aStart, grid); return(problem); }
/// <summary> /// Generates a problem instance, including a board, start and goal locations of desired number of agents /// and desired precentage of obstacles /// TODO: Refactor to use operators. /// </summary> /// <param name="gridSize"></param> /// <param name="agentsNum"></param> /// <param name="obstaclesNum"></param> /// <returns></returns> public ProblemInstance GenerateProblemInstance ( int gridSize, int agentsNum, int obstaclesNum ) { m_mapFileName = "GRID" + gridSize + "X" + gridSize; m_agentNum = agentsNum; if (agentsNum + obstaclesNum > gridSize * gridSize) { throw new Exception("Not enough room for " + agentsNum + ", " + obstaclesNum + " and one empty space in a " + gridSize + "x" + gridSize + "map."); } int x; int y; MAM_AgentState[] aStart = new MAM_AgentState[agentsNum]; bool[][] grid = new bool[gridSize][]; bool[][] starts = new bool[gridSize][]; // Generate a random grid for (int i = 0; i < gridSize; i++) { grid[i] = new bool[gridSize]; starts[i] = new bool[gridSize]; } for (int i = 0; i < obstaclesNum; i++) { x = rand.Next(gridSize); y = rand.Next(gridSize); if (grid[x][y]) // Already an obstacle { i--; } grid[x][y] = true; } // Choose random start locations for (int i = 0; i < agentsNum; i++) { x = rand.Next(gridSize); y = rand.Next(gridSize); if (starts[x][y] || grid[x][y]) { i--; } else { starts[x][y] = true; aStart[i] = new MAM_AgentState(x, y, i, 0); } } ProblemInstance problem = new ProblemInstance(); problem = new ProblemInstance(); problem.Init(aStart, grid); IndependentDetection id = new IndependentDetection(problem, problem.m_vAgents[0].lastMove); List <List <TimedMove> > connectionCheck = new List <List <TimedMove> >(); if (id.bfsToStartPositions(problem.m_vAgents[0].lastMove).Count != problem.m_vAgents.Length) { problem = GenerateProblemInstance(gridSize, agentsNum, obstaclesNum); } return(problem); }