Exemplo n.º 1
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);
        }
 private void InputComboBox_DropDown(object sender, EventArgs e)
 {
     InputComboBox.Items.Clear();
     InputComboBox.Items.AddRange(InputProgram.GetAvailablePrograms(cameraThread));
 }
        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);
            }
        }