/// <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); //return new ProblemInstance(); // DELETE ME!!! 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++) { try { grid[i] = new bool[maxY]; line = input.ReadLine(); for (int j = 0; j < maxY; j++) { try { 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; } } catch { } } } catch { } } // 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; AgentState newSingleState; Agent newSingleAgent; Agent agent; int agentNum; int agentNumSingle = 0; int goalX; int goalY; int startX; int startY; // Run on each of the agents 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; // ******** Some of the project modifications: ******** // numberOfRun 1 is the first solver of the MAPF problem // here we are building a list of single agent problem, it will be used to automate the calculation of the middle positions as described in the project report) if (Program.numberOfRun == 1) { newSingleAgent = new Agent(goalX, goalY, agentNumSingle); newSingleState = new AgentState(startX, startY, newSingleAgent); AgentState[] singleStates = new AgentState[1]; singleStates[0] = newSingleState; // Generate the single agents problem instance ProblemInstance instanceSingle = new ProblemInstance(); instanceSingle.Init(singleStates, grid); instanceSingle.instanceId = i; instanceSingle.parameters[ProblemInstance.GRID_NAME_KEY] = gridName; // Saving the single agent problems in an array Program.onlySingleAgentsInstances[i] = instanceSingle; } } // Generate the regular problem instance ProblemInstance instance = new ProblemInstance(); instance.Init(states, grid); instance.instanceId = instanceId; instance.parameters[ProblemInstance.GRID_NAME_KEY] = gridName; if (Program.numberOfRun == 1) { instance.ComputeSingleAgentShortestPaths(); } return(instance); }
/// <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); }