private void SetWallOnBorder() { EmbASPManager controller = new EmbASPManager(); controller.InitializeEmbASP(); InputProgram input = controller.Input; Handler handler = controller.Handler; input.AddFilesPath(encodingFolder + "1-set_wall_on_border.asp"); handler.AddProgram(input); InputProgram facts = new ASPInputProgram(); facts.AddProgram("col(1.." + maxColumns + ")."); facts.AddProgram("row(1.." + maxRows + ")."); facts.AddProgram("max_col(" + maxColumns + ")."); facts.AddProgram("max_row(" + maxRows + ")."); handler.AddProgram(facts); Output o = handler.StartSync(); AnswerSets answers = (AnswerSets)o; AnswerSetToCellMatrix(answers); }
public int[,] SolveMatrix() { InputProgram facts = new ASPInputProgram(); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (riddleGrid[i, j] != 0 && solvedGrid[i, j] != 1) { try { solvedGrid[i, j] = 1; facts.AddObjectInput(new Value(i, j, riddleGrid[i, j])); } catch (Exception e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } } } } handler.AddProgram(facts); Output o = handler.StartSync(); AnswerSets answers = (AnswerSets)o; foreach (AnswerSet a in answers.Answersets) { try { foreach (object obj in a.Atoms) { if (!(obj is NewValue)) { continue; } NewValue newValue = (NewValue)obj; riddleGrid[newValue.getRow(), newValue.getColumn()] = newValue.getValue(); } //Console.ReadLine(); } catch (Exception e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } } return(riddleGrid); }
public static void Run(string csvFile, string solversDir, string inputFile, string optionFile) { FileManager.WriteToFile(csvFile, inputFile, true, true); foreach (string solver in SolverEnum.GetSolvers()) { if (!FileManager.SolverPresent(solversDir, solver + ".solver")) { continue; } ASPInputProgram inputProgram = new ASPInputProgram(); DesktopHandler handler = new DesktopHandler(SolverEnum.GetService(solversDir, solver)); List <ISet <object> > answerSets = new List <ISet <object> >(); int optionID = handler.AddOption(new OptionDescriptor(SolverEnum.GetOutputOption(solver))); if (string.IsNullOrWhiteSpace(optionFile)) { FileManager.ReadFilters(optionFile, solver).ForEach(filter => handler.AddOption(new OptionDescriptor(" " + filter))); } handler.AddProgram(inputProgram); inputProgram.AddFilesPath(inputFile); List <string> sortedOutput = OutputManager.ProcessRawOutput(solver, ((AnswerSets)(handler.StartSync())).OutputString); handler.RemoveOption(optionID); var watch = System.Diagnostics.Stopwatch.StartNew(); foreach (AnswerSet answerSet in ((AnswerSets)(handler.StartSync())).Answersets) { answerSets.Add(answerSet.Atoms); } watch.Stop(); FileManager.WriteToFile(csvFile, solver + ":" + watch.ElapsedMilliseconds.ToString(), null, true); if (answerSets.Count > 0) { foreach (ISet <object> answerSet in answerSets) { string tmp = string.Join(" ", SortFacts(answerSet)); if (!sortedOutput.Contains(tmp)) { Console.WriteLine("ERROR! Original " + solver + " output does not contain:\n" + tmp + "\n"); } ; } } } Console.WriteLine("END"); }
private void GeneratePartitionGraph() { EmbASPManager controller = new EmbASPManager(); controller.InitializeEmbASP(randomAnswersetNumber); InputProgram input = controller.Input; Handler handler = controller.Handler; input.AddFilesPath(encodingFolder + "3-partition_graph_generator.asp"); handler.AddProgram(input); InputProgram facts = new ASPInputProgram(); facts.AddProgram("col(1.." + maxColumns + ")."); facts.AddProgram("row(1.." + maxRows + ")."); foreach (Cell cell in matrixCells.SetCells) { facts.AddObjectInput(cell); } handler.AddProgram(facts); Output o = handler.StartSync(); AnswerSets answers = (AnswerSets)o; IList <AnswerSet> answerSetsList = answers.Answersets; StringBuilder debugConnected = new StringBuilder(); if (answerSetsList.Count > 0) { int index = randomGenerator.Next(answerSetsList.Count); AnswerSet a = answerSetsList[index]; foreach (Object obj in a.Atoms) { if (obj is Connected8) { Connected8 connected8 = (Connected8)obj; connections.Add(connected8); debugConnected.Append(connected8.ToString() + "\n"); } } } if (IS_DEBUG_MODE) { UnityEngine.Debug.Log(debugConnected.ToString()); } }
public void SetPrograms() { InputProgram encoding = new ASPInputProgram(); encoding.AddFilesPath(encodingResource); handler.AddProgram(encoding); // register the class for reflection try { ASPMapper.Instance.RegisterClass(typeof(NewValue)); } catch (Exception e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } }
public SymbolicConstant GetNextMove(InputProgram facts) { SymbolicConstant move = new SymbolicConstant(); string encodingResource = @".\encodings\pacman.asp"; //string encodingResource2 = @"encodings\min_distances_5.asp"; //Debug.Log("DLV Started: " + numberOfSteps++); Handler handler = new DesktopHandler(new DLVDesktopService(@".\lib\dlv.exe")); InputProgram encoding = new ASPInputProgram(); encoding.AddFilesPath(encodingResource); //InputProgram encoding2 = new ASPInputProgram(); //encoding.AddFilesPath(encodingResource2); handler.AddProgram(encoding); //handler.AddProgram(encoding2); handler.AddProgram(facts); handler.AddOption(new OptionDescriptor("-filter=next")); Output o = handler.StartSync(); //EmbaspCall++; AnswerSets answers = (AnswerSets)o; System.Random r = new System.Random(); int answer = r.Next(answers.Answersets.Count); AnswerSet a = answers.Answersets[answer]; foreach (object obj in a.Atoms) { //Debug.Log(obj.ToString()); if (obj is Next) { Next nextAction = (Next)obj; move = nextAction.getAction(); return(move); //Debug.Log("Next Action: " + move); } } return(move); }
private void GenerateFacts(int dimension) { string encodingResource = @".\encodings\min_distances_" + dimension + ".asp"; //Debug.Log("DLV Started: " + numberOfSteps++); Handler handler = new DesktopHandler(new DLVDesktopService(@".\lib\dlv.exe")); InputProgram encoding = new ASPInputProgram(); encoding.AddFilesPath(encodingResource); handler.AddProgram(encoding); Output o = handler.StartSync(); AnswerSets answers = (AnswerSets)o; //Debug.Log("Answers: " + o.OutputString); AnswerSet a = answers.Answersets[0]; foreach (object obj in a.Atoms) { //Debug.Log(obj.ToString()); if (obj is Distance) { Distance d = (Distance)obj; if (dimension == 10) { distances_10[d.getX1(), d.getY1()].Add(d); } else if (dimension == 5) { distances_5[d.getX1(), d.getY1()].Add(d); } //move = nextAction.getAction(); //Debug.Log("Next Action: " + move); } } }
public void SudokuTest() { try { Handler handler = new DesktopHandler(new DLVDesktopService(GetPath())); InputProgram inputProgram = new ASPInputProgram(); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (sudokuMatrix[i, j] != 0) { inputProgram.AddObjectInput(new Cell(i, j, sudokuMatrix[i, j])); } } } inputProgram.AddFilesPath(".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "test-resources" + Path.DirectorySeparatorChar + "asp" + Path.DirectorySeparatorChar + "sudoku"); handler.AddProgram(inputProgram); handler.StartAsync(new CallbackAction(o => { if (!(o is AnswerSets)) { return; } answerSets = (AnswerSets)o; @lock.Signal(); })); @lock.Wait(new TimeSpan(0, 0, 0, 0, 5000)); Assert.IsNotNull(answerSets); Assert.IsTrue(String.IsNullOrEmpty(answerSets.ErrorsString), "Found error in the Plan\n" + answerSets.ErrorsString); if (answerSets.Answersets.Count == 0) { return; } AnswerSet @as = answerSets.Answersets[0]; foreach (object obj in @as.Atoms) { Cell cell = (Cell)obj; sudokuMatrix[cell.getRow(), cell.getColumn()] = cell.getValue(); } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Console.Write(sudokuMatrix[i, j] + " "); if (sudokuMatrix[i, j] == 0) { Assert.Fail("NumberNotValid"); } } Console.WriteLine(); } } catch (Exception e) { Assert.Fail("Exception " + e.Message); } }
public void Run() { reason = true; IMapper sensorMapper = mapper.getMapper(typeof(AdvancedSensor)); while (reason) { if (!brain.executeReasonerOn.Equals("When Sensors are ready")) { lock (brain.toLock) { MyDebugger.MyDebug("going to wait for pulse by brain"); brain.solverWaiting = true; Monitor.Wait(brain.toLock); } } try { factsPath = Path.GetTempFileName(); using (StreamWriter fs = new StreamWriter(factsPath, true)) { string toAppend = SensorsManager.GetSensorsMapping(brain); if (!reason) { return; } fs.Write(toAppend); fs.Close(); } } catch (Exception e) { MyDebugger.MyDebug("CAUGHT EXECPTION!!!!"); MyDebugger.MyDebug(e.Message); MyDebugger.MyDebug(e.StackTrace); } Handler handler = new DesktopHandler(new DLV2DesktopService(@".\lib\dlv2.exe")); //With DLV2DesktopService I get a Error during parsing: --> Invalid #show directive: setOnActuator/1--competition-output. //With DLVDesktopService the AS, obviously, are wrongly parsed InputProgram encoding = new ASPInputProgram(); MyDebugger.MyDebug("adding encoding"); encoding.AddFilesPath(Path.GetFullPath(brain.ASPFilePath)); InputProgram facts = new ASPInputProgram(); MyDebugger.MyDebug("adding facts"); facts.AddFilesPath(factsPath); handler.AddProgram(encoding); handler.AddProgram(facts); handler.AddOption(new OptionDescriptor("--filter=setOnActuator/1 ")); stopwatch.Restart(); MyDebugger.MyDebug("starting sync"); Output o = handler.StartSync(); if (!o.ErrorsString.Equals("")) { MyDebugger.MyDebug(o.ErrorsString + " " + o.OutputString); } AnswerSets answers = (AnswerSets)o; stopwatch.Stop(); brain.asSteps++; brain.asTotalMS += stopwatch.ElapsedMilliseconds; MyDebugger.MyDebug("num of AS " + answers.Answersets.Count); if (answers.Answersets.Count > 0) { lock (brain.toLock) { foreach (SimpleActuator actuator in brain.getActuators()) { actuator.parse(answers.Answersets[0]); } brain.setActuatorsReady(true); } } if (!brain.maintainFactFile) { File.Delete(factsPath); } } }
public SymbolicConstant ASPMove() { //PRENDE LA POSIZIONE DEL PACMAN Vector3 currentPos = new Vector3(Pacman.transform.position.x + 0.499f, Pacman.transform.position.y + 0.499f); var currentTile = Tiles[manager.Index((int)currentPos.x, (int)currentPos.y)]; //Debug.Log("PACMAN POS --> X:" + currentPos.x + " Y: " + currentPos.y + "\n\nTile --> X: " + currentTile.x + " Y: " + currentTile.y); //FIND ADJACENT TILES TO THE CURRENT ONE TileManager.Tile down = currentTile.down; TileManager.Tile up = currentTile.up; TileManager.Tile left = currentTile.left; TileManager.Tile right = currentTile.right; InputProgram facts = new ASPInputProgram(); StringBuilder disjunction = new StringBuilder(); Pacman myPacman = new Pacman((int)currentPos.x, (int)currentPos.y); if (down != null) { if (disjunction.Length > 0) { disjunction.Append("|"); } disjunction.Append("next(down)"); } if (left != null) { if (disjunction.Length > 0) { disjunction.Append("|"); } disjunction.Append("next(left)"); } if (up != null) { if (disjunction.Length > 0) { disjunction.Append("|"); } disjunction.Append("next(up)"); } if (right != null) { if (disjunction.Length > 0) { disjunction.Append("|"); } disjunction.Append("next(right)"); } disjunction.Append("."); facts.AddProgram(disjunction.ToString()); if (PreviousMove != null) { facts.AddProgram("previous_action(" + PreviousMove.Value + ")."); } facts.AddObjectInput(myPacman); GameObject[] pacdots = GameObject.FindGameObjectsWithTag("pacdot"); //Debug.Log("SIZE DOT: " + pacdots.Length); int count = 0; foreach (TileManager.Tile t in Tiles) { if (!t.occupied) { count++; } } //Debug.Log("SIZE TILE: " + count); //GameObject[] energizer; // sono pacdot anche loro //energizer = GameObject.FindGameObjectsWithTag("energizer"); //CHECK THE CONTENT OF A TILE //Debug.Log("PacDot[0].pos = (" + pacdots[0].transform.position.x + "," + pacdots[0].transform.position.y + ")"); //for (int i = 0; i < 28; i++) // for (int j = 0; j < 32; j++) // foreach (Distance d in distances[i,j]) // facts.AddObjectInput(d); for (int i = -1; i <= 1; i++) { if (myPacman.getX() + i > 0 && myPacman.getX() + i < 28) { if (GameManager.scared) { foreach (Distance d in distances_10[myPacman.getX() + i, myPacman.getY()]) { facts.AddObjectInput(d); } } else { foreach (Distance d in distances_5[myPacman.getX() + i, myPacman.getY()]) { facts.AddObjectInput(d); } } } if (myPacman.getY() + i > 0 && myPacman.getY() + i < 32) { if (GameManager.scared) { foreach (Distance d in distances_10[myPacman.getX(), myPacman.getY() + i]) { facts.AddObjectInput(d); } } else { foreach (Distance d in distances_5[myPacman.getX(), myPacman.getY() + i]) { facts.AddObjectInput(d); } } } } if (GameManager.scared) { facts.AddProgram("powerup."); } foreach (GameObject p in pacdots) { facts.AddProgram("pellet(" + (int)p.transform.position.x + "," + (int)p.transform.position.y + ")."); } facts.AddProgram("ghost(" + (int)blinky.transform.position.x + "," + (int)blinky.transform.position.y + ",blinky)."); facts.AddProgram("ghost(" + (int)inky.transform.position.x + "," + (int)inky.transform.position.y + ",inky)."); facts.AddProgram("ghost(" + (int)clyde.transform.position.x + "," + (int)clyde.transform.position.y + ",clyde)."); facts.AddProgram("ghost(" + (int)pinky.transform.position.x + "," + (int)pinky.transform.position.y + ",pinky)."); TileManager.Tile pacmanTile = new TileManager.Tile((int)Pacman.transform.position.x, (int)Pacman.transform.position.y); TileManager.Tile first_min = new TileManager.Tile((int)pacdots[0].transform.position.x, (int)pacdots[0].transform.position.y); var minDistance = 10E6;// manager.distance(pacmanTile, first_min); foreach (GameObject p in pacdots) { TileManager.Tile pacdotsTile = new TileManager.Tile((int)p.transform.position.x, (int)p.transform.position.y); var myDistance = manager.distance(pacmanTile, pacdotsTile); if (myDistance < minDistance) { minDistance = myDistance; first_min = pacdotsTile; } } facts.AddProgram("closestPellet(" + first_min.x + "," + first_min.y + ")."); facts.AddProgram("distanceClosestPellet(" + (int)minDistance + ")."); foreach (TileManager.Tile p in Tiles) { if (!p.occupied) { facts.AddProgram("tile(" + p.x + "," + p.y + ")."); } } SymbolicConstant move = GetNextMove(facts); PreviousMove = move; //Debug.Log("CurrentMove: " + move); return(move); }
public void Run() { reason = true; IMapper sensorMapper = mapper.getMapper(typeof(AdvancedSensor)); //Debug.Log("mapper " + sensorMapper); while (reason) { //Thread.Sleep(1000); //Debug.Log("executing thread"); lock (brain.toLock) { brain.solverWaiting = true; Monitor.Wait(brain.toLock); try { stopwatch.Restart(); factsPath = Path.GetTempFileName(); using (StreamWriter fs = new StreamWriter(factsPath, true)) { //Debug.Log("creating file "+ factsPath); string toAppend = ""; foreach (AdvancedSensor sensor in brain.getSensors()) { //Stopwatch temp = new Stopwatch(); //temp.Start(); toAppend += sensor.Map(); //temp.Stop(); //Debug.Log(toAppend); //Debug.Log(toAppend); } //Debug.Lof(fs.) fs.Write(toAppend); fs.Close(); //Debug.Log("closing stream"); } stopwatch.Stop(); factsSteps++; factsAvgTime += stopwatch.ElapsedMilliseconds; } catch (Exception e) { UnityEngine.Debug.LogError(e.Message); UnityEngine.Debug.LogError(e.StackTrace); } } //Debug.Log(Path.GetFullPath(@".\lib\dlv.exe")); Handler handler = new DesktopHandler(new DLVDesktopService(@".\lib\dlv2.exe")); InputProgram encoding = new ASPInputProgram(); encoding.AddFilesPath(Path.GetFullPath(brain.ASPFilePath)); InputProgram facts = new ASPInputProgram(); facts.AddFilesPath(factsPath); handler.AddProgram(encoding); handler.AddProgram(facts); handler.AddOption(new OptionDescriptor("--filter=setOnActuator/1")); stopwatch.Restart(); //Debug.Log("reasoning"); Output o = handler.StartSync(); if (!o.ErrorsString.Equals("")) { Debug.Log(o.ErrorsString + " " + o.OutputString); } AnswerSets answers = (AnswerSets)o; stopwatch.Stop(); asSteps++; asAvgTime += stopwatch.ElapsedMilliseconds; //Debug.Log("debugging answer set"); //Debug.Log("there are "+answers.Answersets.Count); //Debug.Log("error: " + answers.ErrorsString); if (answers.Answersets.Count > 0) { /*string asPath = Path.GetTempFileName(); * using (StreamWriter fs = new StreamWriter(asPath, true)) * { * fs.Write(o.OutputString); * fs.Close(); * }*/ lock (brain.toLock) { foreach (SimpleActuator actuator in brain.getActuators()) { Debug.Log("input fact " + factsPath); Debug.Log("parsing " + actuator.actuatorName); if (answers.Answersets[0].GetAnswerSet().Count > 0) { Debug.Log(answers.Answersets[0].GetAnswerSet()[0]); } actuator.parse(answers.Answersets[0]); } brain.setActuatorsReady(true); } } if (!brain.maintainFactFile) { File.Delete(factsPath); } } }
public void ShortestPathTest() { try { DesktopHandler handler = new DesktopHandler(new DLV2DesktopService(GetPath())); ASPMapper.Instance.RegisterClass(typeof(Edge)); ASPMapper.Instance.RegisterClass(typeof(Path)); InputProgram input = new ASPInputProgram(); from = 0; // source node to = 7; // destination node String rules = "from(" + from + ").to(" + to + ")." + "path(X,Y,W) | notPath(X,Y,W) :- from(X), edge(X,Y,W)." + "path(X,Y,W) | notPath(X,Y,W) :- path(_,X,_), edge(X,Y,W), not to(X)." + "visited(X) :- path(_,X,_)." + ":- to(X), not visited(X)." + ":~ path(X,Y,W). [W@1 ,X,Y]"; input.AddProgram(rules); foreach (Edge edge in getEdges()) { input.AddObjectInput(edge); } handler.AddProgram(input); DLV2AnswerSets answerSets = (DLV2AnswerSets)handler.StartSync(); Assert.IsNotNull(answerSets); Assert.IsTrue(answerSets.GetOptimalAnswerSets().Count != 0); Assert.IsTrue(answerSets.ErrorsString == "", "Found error:\n" + answerSets.ErrorsString); AnswerSet answerSet = answerSets.GetOptimalAnswerSets()[0]; List <Path> path = new List <Path>(); // edges in the shortest path (unsorted) int sum = 0; // total weight of the path foreach (object obj in answerSet.Atoms) { if (typeof(Path).IsInstanceOfType(obj)) { path.Add((Path)obj); sum += ((Path)obj).getWeight(); } } sortedPath = new List <int>(); // edges in the shorted path (sorted) sortedPath.Add(from); join(from, path, sortedPath); // sorts the edges print(sortedPath, sum); // shows the result } catch (Exception e) { Assert.Fail("Exception " + e.Message); } }
public void BuildWalls(Partition partition) { //if (partition.Size >= 200 && !partition.Type.Equals("\"empty\"") && partition.Type.Equals("\"hollow\"")) // partition.Type = "\"corridor\""; EmbASPManager controller = new EmbASPManager(); controller.InitializeEmbASP(randomAnswersetNumber); InputProgram input = controller.Input; Handler handler = controller.Handler; switch (partition.Type) { case "\"hollow\"": input.AddFilesPath(encodingFolder + "5-generate_room.asp"); break; case "\"corridor\"": input.AddFilesPath(encodingFolder + "5-generate_corridor.asp"); break; case "\"empty\"": input.AddFilesPath(encodingFolder + "5-generate_empty.asp"); break; default: break; } handler.AddProgram(input); InputProgram facts = new ASPInputProgram(); StringBuilder debugBuildWall = new StringBuilder(); debugBuildWall.Append("col(" + partition.getMinCol() + ".." + partition.getMaxCol() + ")."); debugBuildWall.Append("row(" + partition.getMinRow() + ".." + partition.getMaxRow() + ")."); debugBuildWall.Append("min_row(" + partition.getMinRow() + ")."); debugBuildWall.Append("max_row(" + partition.getMaxRow() + ")."); debugBuildWall.Append("min_col(" + partition.getMinCol() + ")."); debugBuildWall.Append("max_col(" + partition.getMaxCol() + ")."); facts.AddProgram("col(" + partition.getMinCol() + ".." + partition.getMaxCol() + ")."); facts.AddProgram("row(" + partition.getMinRow() + ".." + partition.getMaxRow() + ")."); facts.AddProgram("min_row(" + partition.getMinRow() + ")."); facts.AddProgram("max_row(" + partition.getMaxRow() + ")."); facts.AddProgram("min_col(" + partition.getMinCol() + ")."); facts.AddProgram("max_col(" + partition.getMaxCol() + ")."); foreach (Pair <int, int> door_coordinate in partition.Doors) { if (partition.Type.Equals("\"empty\"")) { matrixCells.Cells[door_coordinate].setType("wall"); } else { Cell door = matrixCells.Cells[door_coordinate]; facts.AddProgram("cell(" + door.getRow() + "," + door.getColumn() + ",\"" + door.getType() + "\")."); debugBuildWall.Append("cell(" + door.getRow() + "," + door.getColumn() + ",\"" + door.getType() + "\")."); } } if (IS_DEBUG_MODE) { UnityEngine.Debug.Log("INPUT BUILDWALLS: \n" + debugBuildWall.ToString()); } handler.AddProgram(facts); Output o = handler.StartSync(); AnswerSets answers = (AnswerSets)o; AnswerSetToCellMatrix(answers, null); }
private void PartitionObjectTypeAssignment() { EmbASPManager controller = new EmbASPManager(); controller.InitializeEmbASP(randomAnswersetNumber); InputProgram input = controller.Input; Handler handler = controller.Handler; input.AddFilesPath(encodingFolder + "6-partition_object_type_assignment.asp"); handler.AddProgram(input); InputProgram facts = new ASPInputProgram(); facts.AddProgram("maximum_locked_door_number(5)."); facts.AddProgram("object_id(1..5)."); facts.AddProgram("type(\"avatar\")."); facts.AddProgram("type(\"goal\")."); facts.AddProgram("type(\"none\")."); facts.AddProgram("type(\"obstacle\")."); facts.AddProgram("type(\"key\")."); facts.AddProgram("type(\"locked\")."); foreach (Connected8 connected8 in connections) { Partition partition1 = new Partition(connected8.getMinRow1(), connected8.getMinCol1(), connected8.getMaxRow1(), connected8.getMaxCol1()); Partition partition2 = new Partition(connected8.getMinRow2(), connected8.getMinCol2(), connected8.getMaxRow2(), connected8.getMaxCol2()); facts.AddProgram("connected(p(" + partition1.getMinRow() + "," + partition1.getMinCol() + "," + partition1.getMaxRow() + "," + partition1.getMaxCol() + "),p(" + partition2.getMinRow() + "," + partition2.getMinCol() + "," + partition2.getMaxRow() + "," + partition2.getMaxCol() + "))."); } foreach (Partition partition in partitions) { facts.AddProgram("assignment(p(" + partition.getMinRow() + "," + partition.getMinCol() + "," + partition.getMaxRow() + "," + partition.getMaxCol() + ")" + "," + partition.Type + ")."); } handler.AddProgram(facts); Output o = handler.StartSync(); AnswerSets answers = (AnswerSets)o; IList <AnswerSet> answerSetsList = answers.Answersets; if (answerSetsList.Count > 0) { int index = randomGenerator.Next(answerSetsList.Count); AnswerSet a = answerSetsList[index]; StringBuilder debugObjectAssignment = new StringBuilder(); foreach (Object obj in a.Atoms) { if (obj is ObjectAssignment) { ObjectAssignment objectAssignment = (ObjectAssignment)obj; debugObjectAssignment.Append(objectAssignment.ToString() + "\n"); Partition partition = new Partition(objectAssignment.getMinRow(), objectAssignment.getMinCol(), objectAssignment.getMaxRow(), objectAssignment.getMaxCol()); int partition_index = partitions.IndexOf(partition); partitions[partition_index].Type = objectAssignment.getType(); } } if (IS_DEBUG_MODE) { UnityEngine.Debug.Log(debugObjectAssignment.ToString()); } } }
private void PartitioningTypeAssignment() { EmbASPManager controller = new EmbASPManager(); controller.InitializeEmbASP(randomAnswersetNumber); InputProgram input = controller.Input; Handler handler = controller.Handler; input.AddFilesPath(encodingFolder + "4-partition_type_assignment.asp"); handler.AddProgram(input); InputProgram facts = new ASPInputProgram(); StringBuilder inputProgramString = new StringBuilder(); facts.AddProgram("num_partitions(" + partitions.Count + ")."); facts.AddProgram("empty_percentage_range(10,20)."); facts.AddProgram("type(\"hollow\")."); facts.AddProgram("type(\"empty\")."); facts.AddProgram("type(\"corridor\")."); inputProgramString.Append("num_partitions(" + partitions.Count + ").\n"); inputProgramString.Append("empty_percentage_range(10,20).\n"); inputProgramString.Append("type(\"hollow\").\n"); inputProgramString.Append("type(\"empty\").\n"); inputProgramString.Append("type(\"corridor\").\n"); int start_partition_index = randomGenerator.Next(partitions.Count); Partition start_partition = partitions[start_partition_index]; facts.AddProgram("start_partition(p(" + start_partition.getMinRow() + "," + start_partition.getMinCol() + "," + +start_partition.getMaxRow() + "," + start_partition.getMaxCol() + "))."); inputProgramString.Append("start_partition(p(" + start_partition.getMinRow() + "," + start_partition.getMinCol() + "," + +start_partition.getMaxRow() + "," + start_partition.getMaxCol() + ")).\n"); foreach (Partition partition in partitions) { facts.AddProgram("partition(p(" + partition.getMinRow() + "," + partition.getMinCol() + "," + partition.getMaxRow() + "," + partition.getMaxCol() + "))."); inputProgramString.Append("partition(p(" + partition.getMinRow() + "," + partition.getMinCol() + "," + partition.getMaxRow() + "," + partition.getMaxCol() + ")).\n"); } foreach (Connected8 connected8 in connections) { Partition partition1 = new Partition(connected8.getMinRow1(), connected8.getMinCol1(), connected8.getMaxRow1(), connected8.getMaxCol1()); Partition partition2 = new Partition(connected8.getMinRow2(), connected8.getMinCol2(), connected8.getMaxRow2(), connected8.getMaxCol2()); facts.AddProgram("connected(p(" + partition1.getMinRow() + "," + partition1.getMinCol() + "," + partition1.getMaxRow() + "," + partition1.getMaxCol() + "),p(" + partition2.getMinRow() + "," + partition2.getMinCol() + "," + partition2.getMaxRow() + "," + partition2.getMaxCol() + "))."); inputProgramString.Append("connected(p(" + partition1.getMinRow() + "," + partition1.getMinCol() + "," + partition1.getMaxRow() + "," + partition1.getMaxCol() + "),p(" + partition2.getMinRow() + "," + partition2.getMinCol() + "," + partition2.getMaxRow() + "," + partition2.getMaxCol() + ")).\n"); } handler.AddProgram(facts); Output o = handler.StartSync(); AnswerSets answers = (AnswerSets)o; IList <AnswerSet> answerSetsList = answers.Answersets; if (answerSetsList.Count > 0) { int index = randomGenerator.Next(answerSetsList.Count); AnswerSet a = answerSetsList[index]; StringBuilder debugTypeAssignment = new StringBuilder(); foreach (Object obj in a.Atoms) { if (obj is Assignment) { Assignment assignment = (Assignment)obj; debugTypeAssignment.Append(assignment.ToString() + "\n"); Partition partition = new Partition(assignment.getMinRow(), assignment.getMinCol(), assignment.getMaxRow(), assignment.getMaxCol()); int partition_index = partitions.IndexOf(partition); partitions[partition_index].Type = assignment.getType(); if (assignment.getType().Equals("\"empty\"")) { numEmptyPartitions++; } else { numPartitionsToBuild++; } } } if (IS_DEBUG_MODE) { UnityEngine.Debug.Log("ASSIGNMENTS: \n" + debugTypeAssignment.ToString()); UnityEngine.Debug.Log("INPUT PROGRAM: " + inputProgramString.ToString()); UnityEngine.Debug.Log("OUTPUT: " + o.OutputString + "\nERROR: " + o.ErrorsString); } } }
public void SpacePartitioning(bool horizontal, Partition nextPartitioned) { if (nextPartitioned.Size < minRoomSize) { AddPartition(nextPartitioned); return; } double rNumber = randomGenerator.NextDouble(); // if (rNumber < pruningPercentage * (1 - nextPartitioned.getSize() / mapSize)) { // addPartition(nextPartitioned); // return; // } if (rNumber < sameOrientationPercentage) { horizontal = !horizontal; } EmbASPManager controller = new EmbASPManager(); controller.InitializeEmbASP(randomAnswersetNumber); InputProgram input = controller.Input; Handler handler = controller.Handler; input.AddFilesPath(encodingFolder + "2-space_partitioning.asp"); handler.AddProgram(input); InputProgram facts = new ASPInputProgram(); int orientationIndex = (horizontal) ? 1 : 0; facts.AddProgram("row(" + nextPartitioned.getMinRow() + ".." + nextPartitioned.getMaxRow() + ")."); facts.AddProgram("col(" + nextPartitioned.getMinCol() + ".." + nextPartitioned.getMaxCol() + ")."); facts.AddProgram("max_row(" + nextPartitioned.getMaxRow() + ")."); facts.AddProgram("max_col(" + nextPartitioned.getMaxCol() + ")."); facts.AddProgram("min_row(" + nextPartitioned.getMinRow() + ")."); facts.AddProgram("min_col(" + nextPartitioned.getMinCol() + ")."); facts.AddProgram("min_distance_wall(" + minDistanceWall + ")."); facts.AddProgram("orientation(" + orientation[orientationIndex] + ")."); foreach (Cell cell in matrixCells.SetCells) { if (cell.getRow() >= nextPartitioned.getMinRow() && cell.getRow() <= nextPartitioned.getMaxRow() && cell.getColumn() >= nextPartitioned.getMinCol() && cell.getColumn() <= nextPartitioned.getMaxCol()) { facts.AddObjectInput(cell); } } handler.AddProgram(facts); Output o = handler.StartSync(); AnswerSets answers = (AnswerSets)o; NewDoor door = AnswerSetToCellMatrix(answers, nextPartitioned); if (door != null) { if (door.getType().Equals("hdoor")) { SpacePartitioning(!horizontal, new Partition(nextPartitioned.getMinRow(), nextPartitioned.getMinCol(), door.getRow(), nextPartitioned.getMaxCol())); SpacePartitioning(!horizontal, new Partition(door.getRow(), nextPartitioned.getMinCol(), nextPartitioned.getMaxRow(), nextPartitioned.getMaxCol())); } else if (door.getType().Equals("vdoor")) { SpacePartitioning(!horizontal, new Partition(nextPartitioned.getMinRow(), nextPartitioned.getMinCol(), nextPartitioned.getMaxRow(), door.getColumn())); SpacePartitioning(!horizontal, new Partition(nextPartitioned.getMinRow(), door.getColumn(), nextPartitioned.getMaxRow(), nextPartitioned.getMaxCol())); } } else { AddPartition(nextPartitioned); } }