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);
        }
        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 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);
            }
        }