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());
            }
        }
예제 #3
0
        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 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);
            }
        }