示例#1
0
        public void IncrementalPlanningSearch()
        {
            List <string> bpath = new List <string>();

            bpath.Add("Initial score: " + hanoiDomain.Goal.Fulfillment(hanoiDomain.State));

            IPlan p = new DFSPlan().SetMaxSearchDepth(4);

            p.Search(hanoiDomain.State, hanoiDomain.Goal);
            p.Search(p.GetFinalState(), hanoiDomain.Goal);

            Assert.GreaterOrEqual(hanoiDomain.Goal.Fulfillment(p.GetFinalState()), 1.0);
        }
示例#2
0
        static void Main(string[] args)
        {
            var s0 = new State();

            s0.AddItem("Cash", 500);
            s0.AddItem("Factory Blueprint", 1);
            s0.PlanningActions.Add(new PlanningAction("Build Harvester").Requires("Factory").Consumes("Cash", 100).Produces("Harvester"));
            s0.PlanningActions.Add(new PlanningAction("Build Factory").Consumes("Cash", 400).Produces("Factory").Requires("Factory Blueprint"));
            s0.PlanningActions.Add(new PlanningAction("Harvest").Requires("Harvester").Produces("Cash", 100).MultiProducer());
            s0.PlanningActions.Add(new PlanningAction("Upgrade Harvester(s)").Consumes("Cash", 0).AssignPostAction(ls => ls.PlanningActions.Find(l => l.Name == "Harvest").Produces("Cash", 1259)));
            s0.PlanningActions.Add(new PlanningAction("Sell Factory Blueprint").Consumes("Factory Blueprint").Produces("Cash", 5000));

            var g0 = new Goal("Build Fortune").Target("Factory", 2).Target("Cash", 10000);
            //var g0 = new Goal("Build Fortune").Target("Cash", 20000);
            double score = g0.Fulfillment(s0);
            //a1.Execute(s0);
            double score2 = g0.Fulfillment(s0);

            var s1 = s0.Clone();

            IPlan p = new DFSPlan();

            p.Search(s0, g0);


            List <string> bpath = new List <string>();

            bpath.Add("Initial score: " + g0.Fulfillment(s0));
            //foreach (var bs in p.GetPath().Reverse())
            //{
            //    var pa = s0.PlanningActions.Where(l => l.Name == bs.Name).First();
            //    pa.Execute(s0);
            //    bpath.Add(bs +" score: "+ g0.Fulfillment(s0));
            //}
        }
示例#3
0
        public void PerformOnlyActionInPlan()
        {
            string actionName = "Build Harvester";
            var state = new State();
            state.PlanningActions.Add(new PlanningAction(actionName).Produces("Harvester"));
            var g0 = new Goal("Build Harvester").Target("Harvester", 1);

            IPlan p = new DFSPlan();
            p.Search(state, g0);

            Assert.That(p.GetPath()[0].Name, Is.EqualTo(actionName));
        }
示例#4
0
        public void SolveTowerOfHanoiWith3Discs()
        {
            List <string> bpath = new List <string>();

            bpath.Add("Initial score: " + hanoiDomain.Goal.Fulfillment(hanoiDomain.State));


            IPlan p = new DFSPlan().SetMaxSearchDepth(7);

            p.Search(hanoiDomain.State, hanoiDomain.Goal);

            Assert.GreaterOrEqual(hanoiDomain.Goal.Fulfillment(p.GetFinalState()), 1.0);
        }
示例#5
0
        public void PerformOnlyActionInPlan()
        {
            string actionName = "Build Harvester";
            var    state      = new State();

            state.PlanningActions.Add(new PlanningAction(actionName).Produces("Harvester"));
            var g0 = new Goal("Build Harvester").Target("Harvester", 1);

            IPlan p = new DFSPlan();

            p.Search(state, g0);

            Assert.That(p.GetPath()[0].Name, Is.EqualTo(actionName));
        }
示例#6
0
        public void LimitDepthFirstPlanSearch()
        {
            var state = new State();
            state.AddItem("Cash", 0);
            var action = new PlanningAction("Get Salary").Produces("Cash",100);
            state.PlanningActions.Add(action);

            var g0 = new Goal("Accumulate Cash").Target("Cash", 1000);

            IPlan p = new DFSPlan().SetMaxSearchDepth(3);
            p.Search(state, g0);

            Assert.That(p.GetPath().Count, Is.EqualTo(3));
        }
示例#7
0
        public void DontPerformAnyActionWhenNoActionIsAvailable()
        {
            var state = new State();
            state.AddItem("Cash", 99);
            var action = new PlanningAction("Not affordable").Consumes("Cash", 100).Produces("Harvester");
            state.PlanningActions.Add(action);

            var g0 = new Goal("Build Harvester").Target("Harvester", 1);

            IPlan p = new DFSPlan();
            p.Search(state, g0);

            Assert.That(p.GetPath().ToList().Count, Is.EqualTo(0));
        }
示例#8
0
        public void LimitDepthFirstPlanSearch()
        {
            var state = new State();

            state.AddItem("Cash", 0);
            var action = new PlanningAction("Get Salary").Produces("Cash", 100);

            state.PlanningActions.Add(action);

            var g0 = new Goal("Accumulate Cash").Target("Cash", 1000);

            IPlan p = new DFSPlan().SetMaxSearchDepth(3);

            p.Search(state, g0);

            Assert.That(p.GetPath().Count, Is.EqualTo(3));
        }
示例#9
0
        public void DontPerformAnyActionWhenNoActionIsAvailable()
        {
            var state = new State();

            state.AddItem("Cash", 99);
            var action = new PlanningAction("Not affordable").Consumes("Cash", 100).Produces("Harvester");

            state.PlanningActions.Add(action);

            var g0 = new Goal("Build Harvester").Target("Harvester", 1);

            IPlan p = new DFSPlan();

            p.Search(state, g0);

            Assert.That(p.GetPath().ToList().Count, Is.EqualTo(0));
        }
示例#10
0
        public void SolveTowerOfHanoiWith3Discs()
        {
            List<string> bpath = new List<string>();
            bpath.Add("Initial score: " + hanoiDomain.Goal.Fulfillment(hanoiDomain.State));

            IPlan p = new DFSPlan().SetMaxSearchDepth(7);
            p.Search(hanoiDomain.State, hanoiDomain.Goal);

            Assert.GreaterOrEqual(hanoiDomain.Goal.Fulfillment(p.GetFinalState()), 1.0);
        }
示例#11
0
        public void IncrementalPlanningSearch()
        {
            List<string> bpath = new List<string>();
            bpath.Add("Initial score: " + hanoiDomain.Goal.Fulfillment(hanoiDomain.State));

            IPlan p = new DFSPlan().SetMaxSearchDepth(4);
            p.Search(hanoiDomain.State, hanoiDomain.Goal);
            p.Search(p.GetFinalState(), hanoiDomain.Goal);

            Assert.GreaterOrEqual(hanoiDomain.Goal.Fulfillment(p.GetFinalState()), 1.0);
        }
示例#12
0
        public void Solve()
        {
            puzzle = new Domain().BuildLogic((domain, state) =>
            {
                //
                //    01 02 03 04
                //    05 06 07 08
                //    09 10 11 12
                //    13 14 15
                //
                //
                string[] invalid_north = new string[] { "1", "2", "3", "4" };
                string[] invalid_east = new string[] { "4", "8", "12", "16" };
                string[] invalid_south = new string[] { "13", "14", "15", "16" };
                string[] invalid_west = new string[] { "1", "5", "9", "13" };

                Func<State, string> BlankPosition = (st) => { return st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item2 == "Blank").Item3; };
                Func<State, bool> CanFlipNorth = (st) => { return !invalid_north.Contains(BlankPosition(st)); };
                Func<State, bool> CanFlipEast = (st) => { return !invalid_east.Contains(BlankPosition(st)); };
                Func<State, bool> CanFlipSouth = (st) => { return !invalid_south.Contains(BlankPosition(st)); };
                Func<State, bool> CanFlipWest = (st) => { return !invalid_west.Contains(BlankPosition(st)); };

                Action<State> FlipNorth = (State st) =>
                {
                    var slot = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item2 == "Blank").Item3;
                    int pos = int.Parse(slot);
                    string brick = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos - 4).ToString()).Item2;
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos - 4).ToString()));
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos).ToString()));
                    st.Relations.Add(new Tuple<string, string, string>("Board", brick, pos.ToString()));
                    st.Relations.Add(new Tuple<string, string, string>("Board", "Blank", (pos - 4).ToString()));
                };
                Action<State> FlipEast = (State st) =>
                {
                    var slot = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item2 == "Blank").Item3;
                    int pos = int.Parse(slot);
                    string brick = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos + 1).ToString()).Item2;
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos + 1).ToString()));
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos).ToString()));
                    st.Relations.Add(new Tuple<string, string, string>("Board", brick, pos.ToString()));
                    st.Relations.Add(new Tuple<string, string, string>("Board", "Blank", (pos + 1).ToString()));
                };
                Action<State> FlipSouth = (State st) =>
                {
                    var slot = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item2 == "Blank").Item3;
                    int pos = int.Parse(slot);
                    string brick = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos + 4).ToString()).Item2;
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos + 4).ToString()));
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos).ToString()));
                    st.Relations.Add(new Tuple<string, string, string>("Board", brick, pos.ToString()));
                    st.Relations.Add(new Tuple<string, string, string>("Board", "Blank", (pos + 4).ToString()));
                };
                Action<State> FlipWest = (State st) =>
                {
                    var slot = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item2 == "Blank").Item3;
                    int pos = int.Parse(slot);
                    string brick = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos - 1).ToString()).Item2;
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos - 1).ToString()));
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos).ToString()));
                    st.Relations.Add(new Tuple<string, string, string>("Board", brick, pos.ToString()));
                    st.Relations.Add(new Tuple<string, string, string>("Board", "Blank", (pos - 1).ToString()));
                };

                state.PlanningActions.Add(new PlanningAction("Flip North")
                                       .AssignPrejudicate(st => CanFlipNorth(st))
                                       .AssignPostAction(st => FlipNorth(st))
                                      );
                state.PlanningActions.Add(new PlanningAction("Flip East")
                                         .AssignPrejudicate(st => CanFlipEast(st))
                                         .AssignPostAction(st => FlipEast(st))
                                        );
                state.PlanningActions.Add(new PlanningAction("Flip South")
                                         .AssignPrejudicate(st => CanFlipSouth(st))
                                         .AssignPostAction(st => FlipSouth(st))
                                        );
                state.PlanningActions.Add(new PlanningAction("Flip West")
                                         .AssignPrejudicate(st => CanFlipWest(st))
                                         .AssignPostAction(st => FlipWest(st))
                                        );

                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 1", "1"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 2", "2"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 3", "13"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 4", "12"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 5", "11"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 6", "10"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 7", "9"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 8", "8"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 9", "7"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 10", "6"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 11", "5"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 12", "4"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 13", "3"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 14", "14"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Brick 15", "15"));
                state.Relations.Add(new Tuple<string, string, string>("Board", "Blank", "16"));
            }
            ).AssignGoal(
                new Goal("Solve Puzzle").RelationalTarget("Board", "Brick 15", "16")
            );

            // Todo: write custom goal that uses Manhattan Distance
            var cg =  new CustomGoal().AssignGoal(delegate(State x) { return 0.0; });
            var cg2 = new CustomGoal().AssignGoal( state => { return 0.0; });

            //puzzle.State.PlanningActions[0].Execute(puzzle.State);

            IPlan p = new DFSPlan().SetMaxSearchDepth(10);
            p.Search(puzzle.State, puzzle.Goal);

            Assert.GreaterOrEqual(puzzle.Goal.Fulfillment(p.GetFinalState()), 1.0);
        }
示例#13
0
        public void Solve()
        {
            puzzle = new Domain().BuildLogic((domain, state) =>
            {
                //
                //    01 02 03 04
                //    05 06 07 08
                //    09 10 11 12
                //    13 14 15
                //
                //
                string[] invalid_north = new string[] { "1", "2", "3", "4" };
                string[] invalid_east  = new string[] { "4", "8", "12", "16" };
                string[] invalid_south = new string[] { "13", "14", "15", "16" };
                string[] invalid_west  = new string[] { "1", "5", "9", "13" };

                Func <State, string> BlankPosition = (st) => { return(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item2 == "Blank").Item3); };
                Func <State, bool> CanFlipNorth    = (st) => { return(!invalid_north.Contains(BlankPosition(st))); };
                Func <State, bool> CanFlipEast     = (st) => { return(!invalid_east.Contains(BlankPosition(st))); };
                Func <State, bool> CanFlipSouth    = (st) => { return(!invalid_south.Contains(BlankPosition(st))); };
                Func <State, bool> CanFlipWest     = (st) => { return(!invalid_west.Contains(BlankPosition(st))); };

                Action <State> FlipNorth = (State st) =>
                {
                    var slot     = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item2 == "Blank").Item3;
                    int pos      = int.Parse(slot);
                    string brick = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos - 4).ToString()).Item2;
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos - 4).ToString()));
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos).ToString()));
                    st.Relations.Add(new Tuple <string, string, string>("Board", brick, pos.ToString()));
                    st.Relations.Add(new Tuple <string, string, string>("Board", "Blank", (pos - 4).ToString()));
                };
                Action <State> FlipEast = (State st) =>
                {
                    var slot     = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item2 == "Blank").Item3;
                    int pos      = int.Parse(slot);
                    string brick = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos + 1).ToString()).Item2;
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos + 1).ToString()));
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos).ToString()));
                    st.Relations.Add(new Tuple <string, string, string>("Board", brick, pos.ToString()));
                    st.Relations.Add(new Tuple <string, string, string>("Board", "Blank", (pos + 1).ToString()));
                };
                Action <State> FlipSouth = (State st) =>
                {
                    var slot     = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item2 == "Blank").Item3;
                    int pos      = int.Parse(slot);
                    string brick = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos + 4).ToString()).Item2;
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos + 4).ToString()));
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos).ToString()));
                    st.Relations.Add(new Tuple <string, string, string>("Board", brick, pos.ToString()));
                    st.Relations.Add(new Tuple <string, string, string>("Board", "Blank", (pos + 4).ToString()));
                };
                Action <State> FlipWest = (State st) =>
                {
                    var slot     = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item2 == "Blank").Item3;
                    int pos      = int.Parse(slot);
                    string brick = st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos - 1).ToString()).Item2;
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos - 1).ToString()));
                    st.Relations.Remove(st.Relations.Find(rel => rel.Item1 == "Board" && rel.Item3 == (pos).ToString()));
                    st.Relations.Add(new Tuple <string, string, string>("Board", brick, pos.ToString()));
                    st.Relations.Add(new Tuple <string, string, string>("Board", "Blank", (pos - 1).ToString()));
                };

                state.PlanningActions.Add(new PlanningAction("Flip North")
                                          .AssignPrejudicate(st => CanFlipNorth(st))
                                          .AssignPostAction(st => FlipNorth(st))
                                          );
                state.PlanningActions.Add(new PlanningAction("Flip East")
                                          .AssignPrejudicate(st => CanFlipEast(st))
                                          .AssignPostAction(st => FlipEast(st))
                                          );
                state.PlanningActions.Add(new PlanningAction("Flip South")
                                          .AssignPrejudicate(st => CanFlipSouth(st))
                                          .AssignPostAction(st => FlipSouth(st))
                                          );
                state.PlanningActions.Add(new PlanningAction("Flip West")
                                          .AssignPrejudicate(st => CanFlipWest(st))
                                          .AssignPostAction(st => FlipWest(st))
                                          );



                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 1", "1"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 2", "2"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 3", "13"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 4", "12"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 5", "11"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 6", "10"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 7", "9"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 8", "8"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 9", "7"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 10", "6"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 11", "5"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 12", "4"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 13", "3"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 14", "14"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Brick 15", "15"));
                state.Relations.Add(new Tuple <string, string, string>("Board", "Blank", "16"));
            }
                                             ).AssignGoal(
                new Goal("Solve Puzzle").RelationalTarget("Board", "Brick 15", "16")
                );

            // Todo: write custom goal that uses Manhattan Distance
            var cg  = new CustomGoal().AssignGoal(delegate(State x) { return(0.0); });
            var cg2 = new CustomGoal().AssignGoal(state => { return(0.0); });

            //puzzle.State.PlanningActions[0].Execute(puzzle.State);

            IPlan p = new DFSPlan().SetMaxSearchDepth(10);

            p.Search(puzzle.State, puzzle.Goal);

            Assert.GreaterOrEqual(puzzle.Goal.Fulfillment(p.GetFinalState()), 1.0);
        }