private NewDoor AnswerSetToCellMatrix(AnswerSets answers)
        {
            IList <AnswerSet> answerSetsList = answers.Answersets;

            NewDoor door = null;

            if (answerSetsList.Count > 0)
            {
                int       index = randomGenerator.Next(answerSetsList.Count);
                AnswerSet a     = answers.Answersets[index];

                try {
                    foreach (object obj in a.Atoms)
                    {
                        if (obj is Cell)
                        {
                            matrixCells.AddCell((Cell)obj);
                        }
                        else if (obj is NewDoor)
                        {
                            door = (NewDoor)obj;
                        }
                    }
                }
                catch (Exception e) {
                    UnityEngine.Debug.Log(e.ToString());
                    UnityEngine.Debug.Log(e.StackTrace);
                }
            }
            return(door);
        }
        private NewDoor AnswerSetToCellMatrix(AnswerSets answers, Partition oldRoom)
        {
            IList <AnswerSet> answerSetsList = answers.Answersets;

            NewDoor door = null;

            if (answerSetsList.Count > 0)
            {
                int       index = randomGenerator.Next(answerSetsList.Count);
                AnswerSet a     = answerSetsList[index];

                foreach (Object obj in a.Atoms)
                {
                    if (obj is Cell)
                    {
                        matrixCells.AddCell((Cell)obj);
                    }
                    else if (obj is NewDoor)
                    {
                        door = (NewDoor)obj;
                    }
                }
            }
            return(door);
        }
        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);
        }
Exemplo n.º 4
0
    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);
    }
        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());
            }
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
                }
            }
        }
Exemplo n.º 8
0
        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);
            }
        }
Exemplo n.º 9
0
        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 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 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);
            }
        }