Example #1
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));
            //}
        }
Example #2
0
        public void StatesCanBeCompared()
        {
            State s0 = new State();

            s0.AddItem("Item 1");
            s0.Relations.Add(new Tuple <string, string, string>("Valid Configuration", "Disc Small", "Disc Medium"));
            s0.PlanningActions.Add(new PlanningAction("Use hammer"));

            State s1 = new State();

            s1.AddItem("Item 1");
            s1.Relations.Add(new Tuple <string, string, string>("Valid Configuration", "Disc Small", "Disc Medium"));
            s1.PlanningActions.Add(new PlanningAction("Use hammer"));

            Assert.IsTrue(s0.Equals(s1));
            Assert.IsTrue(s1.Equals(s0));
            s0.AddItem("Item 2");
            s0.Relations.Add(new Tuple <string, string, string>("InValid Configuration", "Disc Small", "Disc Medium"));
            s0.PlanningActions.Add(new PlanningAction("Use sledgehammer"));
            s1.AddItem("Item 2");
            s1.Relations.Add(new Tuple <string, string, string>("InValid Configuration", "Disc Small", "Disc Medium"));
            s1.PlanningActions.Add(new PlanningAction("Use sledgehammer"));

            Assert.IsTrue(s0.Equals(s1));
            Assert.IsTrue(s1.Equals(s0));

            s0.RemoveItem("Item 1");
            Assert.IsFalse(s0.Equals(s1));
            Assert.IsFalse(s1.Equals(s0));
            s1.RemoveItem("Item 1");

            Assert.IsTrue(s0.Equals(s1));
            Assert.IsTrue(s1.Equals(s0));

            s0.Items["Item 2"] += 4;

            Assert.IsFalse(s0.Equals(s1));
            Assert.IsFalse(s1.Equals(s0));

            s1.Items["Item 2"] += 4;
            Assert.IsTrue(s0.Equals(s1));
            Assert.IsTrue(s1.Equals(s0));

            s0.Relations.RemoveAt(0);
            Assert.IsFalse(s0.Equals(s1));
            Assert.IsFalse(s1.Equals(s0));

            s1.Relations.RemoveAt(0);
            Assert.IsTrue(s0.Equals(s1));
            Assert.IsTrue(s1.Equals(s0));
        }
Example #3
0
    private static List <State> GenerateNextValidStates(HashSet <State> seen, State curr)
    {
        List <State> possibilities  = new();
        List <Item>  thisFloorItems = curr.ItemsByFloor[curr.Elevator];

        void ProcessForMove(int i, int floorShift)
        {
            // Process the state where we move just one item
            State next   = curr.Clone();
            Item  toMove = curr.ItemsByFloor[curr.Elevator][i];

            next.Elevator += floorShift;
            next.ItemsByFloor[curr.Elevator].Remove(toMove);
            next.AddItem(curr.Elevator + floorShift, toMove);
            if (next.IsValid && !seen.Contains(next))
            {
                possibilities.Add(next);
                seen.Add(next);
            }

            // Also consider the current item paired with each of the other items on the current floor
            for (int j = i + 1; j < thisFloorItems.Count; j++)
            {
                State next2   = next.Clone();
                Item  toMove2 = curr.ItemsByFloor[curr.Elevator][j];
                next2.ItemsByFloor[curr.Elevator].Remove(toMove2);
                next2.AddItem(curr.Elevator + floorShift, toMove2);
                if (next2.IsValid && !seen.Contains(next2))
                {
                    possibilities.Add(next2);
                    seen.Add(next2);
                }
            }
        }

        for (int i = 0; i < thisFloorItems.Count; i++)
        {
            // Check the floor above unless we are on the top floor
            if (curr.Elevator + 1 < State.Floors)
            {
                ProcessForMove(i, 1);
            }

            // Check the floor below unless we are on the bottom floor
            if (curr.Elevator - 1 >= 0)
            {
                ProcessForMove(i, -1);
            }
        }

        return(possibilities);
    }
Example #4
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));
        }
Example #5
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));
        }
Example #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));
        }
Example #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));
        }
Example #8
0
        public void Execute(State s)
        {
            foreach (var c in _consumes)
            {
                s.ReduceItem(c.Key, c.Value);
            }
            if (multiProducer)
            {
                int quantity = s.ItemQuantity(_requires.Keys.First());
                foreach (var p in _produces)
                {
                    s.AddItem(p.Key, p.Value * quantity);
                }

            }
            else
            {
                foreach (var p in _produces)
                {
                    s.AddItem(p.Key, p.Value);
                }
            }
            _postActions.ForEach(pa => pa(s));
        }
Example #9
0
        public void AddSystem(IGraphicSystem system)
        {
            var item = new SystemItemViewModel(system);

            State.AddItem(item);
        }