Exemple #1
0
        public void Decide()
        {
            if (!PlEngine.IsInitialized)
            {
                string[] param = { "-q" };
                PlEngine.Initialize(param);
                PlQuery.PlCall("consult('HutBuiAct')");
                PlQuery.PlCall("assert(done(" + this.action + "))");
                PlQuery.PlCall("assert(in(" + this.InnerPosition.X + "," + this.InnerPosition.Y + "))");
                PlQuery.PlCall("assert(facing(" + this.a + "))");
                PlQuery.PlCall("assert(timePassed(" + this.timePassed + "))");
                PlQuery.PlCall("assert(leftmoves(" + this.leftmoves + "))");

                Console.WriteLine("assert(done(" + this.action + "))");
                Console.WriteLine("assert(in(" + this.InnerPosition.X + "," + this.InnerPosition.Y + "))");
                Console.WriteLine("assert(facing(" + this.a + "))");
                Console.WriteLine("assert(timePassed(" + this.timePassed + "))");
                Console.WriteLine("assert(leftmoves(" + this.leftmoves + "))");
                if (this.isDirty())
                {
                    PlQuery.PlCall("assert(dirty(" + this.InnerPosition.X + "," + this.InnerPosition.Y + "))");
                    Console.WriteLine("assert(dirty(" + this.InnerPosition.X + "," + this.InnerPosition.Y + "))");
                }
                for (int i = 0; i < 4; i++)
                {
                    string OsbtacleType = this.TypeOfObstacle(this.upFront());
                    if (OsbtacleType != null)
                    {
                        Point upfront = this.Outer2Inner(this.upFront());
                        Console.WriteLine("assert(obstacle(" + upfront.X + "," + upfront.Y + ",both))");
                        Console.WriteLine("assert(obstacle(" + upfront.X + "," + upfront.Y + "," + OsbtacleType + "))");
                        PlQuery.PlCall("assert(obstacle(" + upfront.X + "," + upfront.Y + ",both))");
                        PlQuery.PlCall("assert(obstacle(" + upfront.X + "," + upfront.Y + "," + OsbtacleType + "))");
                    }
                    this.Turn90();
                }
                foreach (Point w in this.weight.Keys)
                {
                    if (double.IsPositiveInfinity(this.weight[w]))
                    {
                        PlQuery.PlCall("assert(wasweight(" + w.X + "," + w.Y + ",inf))");
                        Console.WriteLine("assert(wasweight(" + w.X + "," + w.Y + ",inf))");
                    }
                    else
                    {
                        PlQuery.PlCall("assert(wasweight(" + w.X + "," + w.Y + "," + this.weight[w] + "))");
                        Console.WriteLine("assert(wasweight(" + w.X + "," + w.Y + "," + this.weight[w] + "))");
                    }
                }
                using (var q = new PlQuery("do(A)"))
                {
                    PlQueryVariables v = q.SolutionVariables.ElementAt(0);
                    this.action = v["A"].ToString();
                    Console.WriteLine("do(" + this.action + ")");
                }
                PlEngine.PlCleanup();
            }
        }
Exemple #2
0
        public void UpdateInternalState()
        {
            if (!PlEngine.IsInitialized)
            {
                string[] param = { "-q" };
                PlEngine.Initialize(param);
                PlQuery.PlCall("consult('HutBuiUpdate')");
                PlQuery.PlCall("assert(done(" + this.action + "))");
                Console.WriteLine("assert(done(" + this.action + "))");
                if (this.action != "start")
                {
                    PlQuery.PlCall("assert(wasin(" + this.InnerPosition.X + "," + this.InnerPosition.Y + "))");
                    PlQuery.PlCall("assert(wasfacing(" + this.a + "))");
                    PlQuery.PlCall("assert(wastimePassed(" + this.timePassed + "))");
                    PlQuery.PlCall("assert(wasleftmoves(" + this.leftmoves + "))");

                    Console.WriteLine("assert(wasin(" + this.InnerPosition.X + "," + this.InnerPosition.Y + "))");
                    Console.WriteLine("assert(wasfacing(" + this.a + "))");
                    Console.WriteLine("assert(wastimePassed(" + this.timePassed + "))");
                    Console.WriteLine("assert(wasleftmoves(" + this.leftmoves + "))");
                    foreach (Point w in this.weight.Keys)
                    {
                        if (double.IsPositiveInfinity(this.weight[w]))
                        {
                            PlQuery.PlCall("assert(wasweight(" + w.X + "," + w.Y + ",inf))");
                            Console.WriteLine("assert(wasweight(" + w.X + "," + w.Y + ",inf))");
                        }
                        else
                        {
                            PlQuery.PlCall("assert(wasweight(" + w.X + "," + w.Y + "," + this.weight[w] + "))");
                            Console.WriteLine("assert(wasweight(" + w.X + "," + w.Y + "," + this.weight[w] + "))");
                        }
                    }
                    foreach (Point d in this.discover)
                    {
                        PlQuery.PlCall("assert(discovered(" + d.X + "," + d.Y + "))");
                        Console.WriteLine("assert(discovered(" + d.X + "," + d.Y + "))");
                    }
                }
                using (var q = new PlQuery("in(X, Y)"))
                {
                    if (q.Solutions.Count() > 0)
                    {
                        PlQueryVariables v = q.SolutionVariables.ElementAt(0);
                        this.InnerPosition.X = Int32.Parse(v["X"].ToString());
                        this.InnerPosition.Y = Int32.Parse(v["Y"].ToString());
                        Console.WriteLine("in(" + this.InnerPosition.X + ", " + this.InnerPosition.Y + ")");
                    }
                }
                using (var q = new PlQuery("facing(X)"))
                {
                    if (q.Solutions.Count() > 0)
                    {
                        PlQueryVariables v = q.SolutionVariables.ElementAt(0);
                        this.a = Int32.Parse(v["X"].ToString());
                        Console.WriteLine("facing(" + this.a + ")");
                    }
                }
                for (int i = 0; i < 4; i++)
                {
                    string OsbtacleType = this.TypeOfObstacle(this.upFront());
                    if (OsbtacleType != null)
                    {
                        Point upfront = this.Outer2Inner(this.upFront());
                        Console.WriteLine("assert(obstacle(" + upfront.X + "," + upfront.Y + ",both))");
                        Console.WriteLine("assert(obstacle(" + upfront.X + "," + upfront.Y + "," + OsbtacleType + "))");
                        PlQuery.PlCall("assert(obstacle(" + upfront.X + "," + upfront.Y + ",both))");
                        PlQuery.PlCall("assert(obstacle(" + upfront.X + "," + upfront.Y + "," + OsbtacleType + "))");
                    }
                    this.Turn90();
                }
                using (var q = new PlQuery("weight(X, Y, W)"))
                {
                    foreach (PlQueryVariables v in q.SolutionVariables)
                    {
                        double W = 0;
                        if (v["W"].ToString() == "inf" || Double.TryParse(v["W"].ToString(), out W))
                        {
                            if (v["W"].ToString() == "inf")
                            {
                                W = double.PositiveInfinity;
                            }
                            this.weight[new Point(Int32.Parse(v["X"].ToString()), Int32.Parse(v["Y"].ToString()))] = W;
                            Console.WriteLine("weight(" + Int32.Parse(v["X"].ToString()) + ", " + Int32.Parse(v["Y"].ToString()) + ", " + v["W"].ToString() + ")");
                        }
                    }
                }
                using (var q = new PlQuery("discover(X, Y)"))
                {
                    foreach (PlQueryVariables v in q.SolutionVariables)
                    {
                        this.discover.Add(new Point(Int32.Parse(v["X"].ToString()), Int32.Parse(v["Y"].ToString())));
                        Console.WriteLine("discover(" + Int32.Parse(v["X"].ToString()) + ", " + Int32.Parse(v["Y"].ToString()) + ")");
                    }
                }
                using (var q = new PlQuery("leftmoves(X)"))
                {
                    foreach (PlQueryVariables v in q.SolutionVariables)
                    {
                        this.leftmoves = Int32.Parse(v["X"].ToString());
                        Console.WriteLine("leftmoves(" + Int32.Parse(v["X"].ToString()) + ")");
                    }
                }
                using (var q = new PlQuery("timePassed(X)"))
                {
                    if (q.Solutions.Count() > 0)
                    {
                        PlQueryVariables v = q.SolutionVariables.ElementAt(0);
                        this.timePassed = Int32.Parse(v["X"].ToString());
                        Console.WriteLine("timePassed(" + Int32.Parse(v["X"].ToString()) + ")");
                    }
                }
                PlEngine.PlCleanup();
            }
        }