Exemplo n.º 1
0
        public void stopFire()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2, 3);
            world.AddWay(1, 2, 2);
            world.AddWay(3, 2, 2);
            world.AddWay(3, 1, 10);
            world.SetFire(2, 3);
            world.PutWater(2);

            FireFighter agent = new FireFighter(1);

            //go to the place with water
            (agent.GetNextAction(world))(world);
            Assert.AreEqual(agent.CurrentLocation, 2);
            Assert.AreEqual(agent.TotalCost, 2);
            double lastCost = agent.TotalCost;

            //PickWater
            (agent.GetNextAction(world)).Invoke(world);
            Assert.AreEqual(agent.CurrentLocation, 2);
            Assert.AreEqual(agent.TotalCost - lastCost, Costs.Instance.Pickup);
            Assert.IsFalse(world.HaveWater(2));
            lastCost = agent.TotalCost;

            //stop fire
            (agent.GetNextAction(world))(world);
            Assert.AreEqual(agent.CurrentLocation, 3);
            Assert.AreEqual(agent.TotalCost - lastCost, 2 * world.getCostWay(2, 3));
            Assert.IsFalse(agent.CarryWater);
            Assert.IsTrue(world.isClear(2, 3));
        }
Exemplo n.º 2
0
        public void MainUristicCheckAstar()
        {
            //create a world where the least cost is to pickup water and go throgh a fire
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2, 3, 4);
            world.AddWay(1, 2, 1);
            world.AddWay(3, 2, 1);
            world.AddWay(3, 4, 1);
            world.AddWay(2, 4, 100);
            world.PutWater(1);
            world.SetFire(3, 4);
            world.PickupCost = 1;
            int goal = 4;
            //the path should be 2->1->pickup->2->3->4

            RealTimeAStarAgent agnet = new RealTimeAStarAgent(new OneFireHuristic(goal).Run, 2, goal, 3);

            Assert.IsTrue(agnet.GetNextAction(world)(world));
            Assert.AreEqual(1, agnet.CurrentLocation);

            Assert.IsTrue(agnet.GetNextAction(world)(world));
            Assert.AreEqual(1, agnet.CurrentLocation);

            Assert.IsTrue(agnet.GetNextAction(world)(world));
            Assert.AreEqual(2, agnet.CurrentLocation);

            Assert.IsTrue(agnet.GetNextAction(world)(world));
            Assert.AreEqual(3, agnet.CurrentLocation);

            Assert.IsTrue(agnet.GetNextAction(world)(world));
            Assert.AreEqual(4, agnet.CurrentLocation);
        }
Exemplo n.º 3
0
        public void notFollwingOrgPath()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2, 3, 4);
            world.AddWay(1, 2, 1);
            world.AddWay(3, 2, 1);
            world.AddWay(3, 4, 1);
            world.AddWay(2, 4, 100);
            world.PutWater(1);
            world.SetFire(3, 4);
            world.PickupCost = 1;
            int goal = 4;
            //the path should be 2->1->2->3->4

            RealTimeAStarAgent agnet = new RealTimeAStarAgent(new OneFireHuristic(goal).Run, 2, goal, 3);

            Assert.IsTrue(agnet.GetNextAction(world)(world));
            Assert.AreEqual(1, agnet.CurrentLocation);

            world.StopFire(3, 4);

            Assert.IsTrue(agnet.GetNextAction(world)(world));
            Assert.AreEqual(2, agnet.CurrentLocation);

            Assert.IsTrue(agnet.GetNextAction(world)(world));
            Assert.AreEqual(3, agnet.CurrentLocation);

            Assert.IsTrue(agnet.GetNextAction(world)(world));
            Assert.AreEqual(4, agnet.CurrentLocation);
        }
Exemplo n.º 4
0
        public override ActionType GetNextAction(TravelWorld currrWorld)
        {
            while (true)
            {
                Console.WriteLine("HUMAN:what is my next action?");
                string action = Console.ReadLine();

                if (action == "no-op")
                {
                    return(noOpertion);
                }
                if (action == "pickup")
                {
                    return(pickupWater);
                }
                if (action.StartsWith("start"))
                {
                    int place = int.Parse(action.Split(' ')[1]);
                    return(new ActionType(world => startAfire(world, place)));
                }
                if (action.StartsWith("drive"))
                {
                    int place = int.Parse(action.Split(' ')[1]);
                    return(new ActionType(world => drive(world, place)));
                }

                Console.WriteLine("not known command!");
            }
        }
Exemplo n.º 5
0
        public void cheapestFirePathTest()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2, 3, 4);
            world.AddWay(1, 2, 10);
            world.AddWay(2, 3, 5);
            world.AddWay(4, 3, 5);
            world.SetFire(1, 2);
            world.SetFire(4, 3);
            var paths = world.findCheapestFirePaths(2).ToArray();

            if (paths.Count() != 2)
            {
                Assert.Fail();
            }

            var path1    = paths[0];
            var path2    = paths[1];
            var resPath1 = new TravelPath();

            resPath1.Add(1, 10);
            var resPath2 = new TravelPath();

            resPath2.Add(3, 5);
            resPath2.Add(4, 5);
            Assert.IsTrue(
                path1.Equals(resPath1) && path2.Equals(resPath2) ||
                path1.Equals(resPath2) && path2.Equals(resPath1));
        }
Exemplo n.º 6
0
        public void cheapestWaterPathWithFire()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2, 3, 4);
            world.AddWay(1, 2, 10);
            world.AddWay(2, 3, 1);
            world.AddWay(4, 3, 1);
            world.PutWater(1, 4);
            world.SetFire(2, 3);
            world.SetFire(4, 3);


            var paths = world.findCheapestWaterPaths(2);

            if (paths.Count() != 1)
            {
                Assert.Fail();
            }
            var path    = paths.First();
            var resPath = new TravelPath();

            resPath.Add(1, 10);
            Assert.AreEqual(path, resPath);
        }
Exemplo n.º 7
0
        public void TakeWaterTest()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2, 3, 4);
            world.AddWay(1, 2, 10);
            world.AddWay(3, 2, 1);
            world.AddWay(3, 4, 1);
            world.AddWay(1, 4, 10);
            world.PutWater(1);
            world.PickupCost = 6;

            GreedySearchAgent agent = new GreedySearchAgent(takeWaterHuristic, 4);
            var action = agent.GetNextAction(world);

            //check if the world is the same before doing an action
            Assert.AreEqual(world.GetGraph().VertexCount, 4);
            Assert.AreEqual(world.GetGraph().EdgeCount, 4);
            Assert.AreEqual(world.GetWaterPlaces().Count(), 1);
            Assert.IsTrue(world.GetWaterPlaces().Contains(1));

            //go to 1
            Assert.IsTrue(action(world));
            Assert.AreEqual(agent.CurrentLocation, 1);
            Assert.AreEqual(agent.TotalCost, world.getCostWay(4, 1));
            double prevCost = agent.TotalCost;

            //pickup water
            action = agent.GetNextAction(world);
            Assert.IsTrue(action(world));
            Assert.AreEqual(agent.CurrentLocation, 1);
            Assert.IsTrue(agent.CarryWater);
            Assert.AreEqual(agent.TotalCost, prevCost + world.PickupCost);
        }
Exemplo n.º 8
0
        //Example1
        public void CoperativeGame()
        {
            TravelWorld world = CommonWorld();

            Human human = CommonHuman();

            var agent = new CoopertiveAgent(1, 3, human, 2, 10);

            agent.costs = CommonCosts();

            world.AddPlayer(human);
            world.AddPlayer(agent);

            //pickupwater
            agent.GetNextAction(world)(world);
            Assert.AreEqual(1, agent.CurrentLocation);
            Assert.IsTrue(!world.HaveWater(1));

            human.noOpertion(world);
            //drive to 3
            agent.GetNextAction(world)(world);
            Assert.AreEqual(3, agent.CurrentLocation);
            Assert.IsTrue(world.isClear(1, 3));

            human.drive(world, 3);

            Assert.AreEqual(3, agent.CurrentLocation);
            Assert.AreEqual(3, agent.TotalCost);
            Assert.AreEqual(1.05, human.TotalCost);
        }
Exemplo n.º 9
0
        protected virtual StateActionsCollection getActions(TravelSearchState state)
        {
            //basic :only pickup water and drive
            TravelWorld            world = state.ToWorld();
            StateActionsCollection res   = new StateActionsCollection();

            if (CanPickupWaterNow(ref state, world))
            {
                if (!_actionsMap.ContainsKey(PickupWater))
                {
                    ActionType      action      = pickupWater;
                    StateActionType stateAction = PickupWater;
                    _actionsMap.Add(stateAction, action);
                }
                res.Add(PickupWater);
            }

            var actions = getDriveActions(state, world);

            foreach (var action in actions)
            {
                var stateAction = ConvertToStateAction(action);
                _actionsMap.Add(stateAction, action);
                res.Add(stateAction);
            }
            return(res);
        }
Exemplo n.º 10
0
        public void basicGame2()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(2, 3, 4);
            world.AddWay(3, 2, 1);
            world.AddWay(3, 4, 1);
            world.AddWay(2, 4, 10);

            Human human = new Human(2);

            human.costs.Pickup = 2;
            human.costs.Fire   = 6;

            agent = new CompetetiveAgent(2, 4, human, 3, 10);
            agent.costs.Pickup = 2;
            agent.costs.Fire   = 6;

            world.AddPlayer(human);
            world.AddPlayer(agent);

            agent.GetNextAction(world)(world);
            Assert.AreEqual(3, agent.CurrentLocation);

            agent.GetNextAction(world)(world);
            Assert.AreEqual(4, agent.CurrentLocation);
        }
Exemplo n.º 11
0
        //Example 2
        public void RivalsGame()
        {
            TravelWorld world = CommonWorld();

            Human human = CommonHuman();

            var agent = new CompetetiveAgent(1, 3, human, 2, 10);

            agent.costs = CommonCosts();

            world.AddPlayer(human);
            world.AddPlayer(agent);

            //set fire to 2
            agent.GetNextAction(world)(world);
            Assert.AreEqual(2, agent.CurrentLocation);
            Assert.IsTrue(!world.isClear(2, 1));

            human.pickupWater(world);

            //set fire to 3
            agent.GetNextAction(world)(world);
            Assert.AreEqual(3, agent.CurrentLocation);
            Assert.IsTrue(!world.isClear(2, 3));

            human.drive(world, 3);

            Assert.AreEqual(3, agent.TotalCost - human.TotalCost);
        }
Exemplo n.º 12
0
 public Simulator(TravelWorld world, params BaseTraveler[] agents)
 {
     _world     = world;
     _agents    = agents;
     _human     = agents[0];
     _gameAgent = agents[1];
 }
Exemplo n.º 13
0
        public void TestMethod1()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2, 3, 4, 5, 6, 7, 8, 9);
            world.AddWay(1, 2, 1);
            world.AddWay(3, 2, 1);
            world.AddWay(3, 9, 1);
            world.AddWay(8, 9, 10);
            world.AddWay(8, 7, 10);
            world.AddWay(6, 7, 10);
            world.AddWay(6, 5, 10);
            world.AddWay(4, 5, 10);
            world.AddWay(4, 1, 10);
            world.SetFire(1, 2);
            world.PutWater(2);

            var agents = new List <BaseAgent <bool, TravelWorld> >();

            Simulator <TravelWorld, bool> sim = new Simulator <TravelWorld, bool>(world,
                                                                                  new FireFighter(3), new Greedy(1, 3));

            Console.SetOut(new StreamWriter("run.txt"));
            sim.Run(50);
        }
Exemplo n.º 14
0
        private static void CompareSearchAgents(TravelWorld world)
        {
            #region create agents
            Console.WriteLine("what is the goal place of search agents?");
            int goal = int.Parse(Console.ReadLine());
            Console.WriteLine("what is the start place of search agents?");
            int start = int.Parse(Console.ReadLine());
            Console.WriteLine("what is the limit of real time search agent?");
            int limit = int.Parse(Console.ReadLine());
            var h     = new OneFireHuristic(goal);
            List <BaseSearchAgent> agents = new List <BaseSearchAgent>();
            agents.Add(new AStartAgent(h.Run, start, goal));
            agents.Add(new GreedySearchAgent(h.Run, start));
            agents.Add(new RealTimeAStarAgent(h.Run, start, goal, limit));
            #endregion

            foreach (var agent in agents)
            {
                TravelWorld worldCpy = world.Clone() as TravelWorld;
                while (agent.CurrentLocation != goal)
                {
                    agent.GetNextAction(world)(worldCpy);
                }
            }
            int[] fValues = { 1, 100, 10000 };
            foreach (var f in fValues)
            {
                Console.WriteLine("f={0}", f);
                foreach (var agent in agents)
                {
                    Console.WriteLine("{3}: S={0},T={1},P={2}", agent.TotalCost, agent.Expanstions, f * agent.TotalCost + agent.Expanstions, agent.Name);
                }
            }
        }
Exemplo n.º 15
0
        private int GetDistanceFromGoal(TravelWorld world, int currentLoc, int goal, out bool realDistance)
        {
            if (currentLoc == goal)
            {
                realDistance = true;
                return(0);
            }

            var path = world.findCheapestCleartPath(currentLoc, goal);

            if (path != null)
            {
                realDistance = true;
                return(path.Count);
            }
            else
            {
                realDistance = false;
                path         = world.findCheapestPath(currentLoc, goal);
                if (path == null)
                {
                    return(int.MaxValue);
                }
                else
                {
                    return(path.Count);
                }
            }
        }
Exemplo n.º 16
0
        public override ActionType GetNextAction(TravelWorld world)
        {
            var actions = getActions(world);

            if (actions == null || actions.Count() == 0)
            {
                return(noOpertion);
            }

            //there is not point to evealute the huristic function
            if (actions.Count() == 1)
            {
                return(_actionsMap[actions.First()]);
            }

            var             searchState  = ToSearchState(world);
            double          min          = double.MaxValue;
            StateActionType chosenAction = null;

            foreach (var action in actions)
            {
                var cost = _heuristic(action(searchState).State);
                if (cost < min)
                {
                    min          = cost;
                    chosenAction = action;
                }
            }

            return(_actionsMap[chosenAction]);
        }
Exemplo n.º 17
0
        public void HaveWaterFar()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2, 3);
            world.AddWay(1, 2, 2);
            world.AddWay(3, 2, 2);
            world.AddWay(3, 1, 10);
            world.PutWater(3);
            FireFighter agent = new FireFighter(1);

            //go to place 2
            (agent.GetNextAction(world)).Invoke(world);
            Assert.AreEqual(agent.CurrentLocation, 2);
            Assert.AreEqual(agent.TotalCost, world.getCostWay(1, 2));
            double lastCost = agent.TotalCost;

            //go to place 3
            (agent.GetNextAction(world)).Invoke(world);
            Assert.AreEqual(agent.CurrentLocation, 3);
            Assert.AreEqual(agent.TotalCost - lastCost, world.getCostWay(3, 2));
            lastCost = agent.TotalCost;
            //pickwater
            (agent.GetNextAction(world)).Invoke(world);
            Assert.AreEqual(agent.CurrentLocation, 3);
            Assert.AreEqual(agent.TotalCost - lastCost, Costs.Instance.Pickup);
            Assert.IsFalse(world.HaveWater(3));
        }
Exemplo n.º 18
0
        public void simpleWaterTest()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlace(1);
            world.PutWater(1);
            Assert.IsTrue(world.HaveWater(1));
        }
Exemplo n.º 19
0
 protected override ActionType InnerGetNextAction(TravelWorld world)
 {
     if (_path == null || _path.Count == 0)
     {
         _path = GetNewPath(world);
     }
     return(_path.TakeNextStep());
 }
Exemplo n.º 20
0
 public HumanTest()
 {
     world = new TravelWorld();
     world.AddPlaces(1, 2, 3, 4);
     world.AddWay(1, 2, 1);
     world.AddWay(3, 2, 1);
     world.AddWay(3, 4, 1);
     world.PutWater(4);
 }
Exemplo n.º 21
0
        public void FireTest()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2);
            world.AddWay(1, 2, 1);
            world.SetFire(1, 2);
            Assert.IsFalse(world.isClear(1, 2));
        }
Exemplo n.º 22
0
        public void SetFireTest()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2, 3);
            world.AddWay(1, 3, 1);
            world.SetFire(1, 3);
            world.StopFire(1, 3);
            Assert.IsTrue(world.isClear(1, 3));
        }
Exemplo n.º 23
0
        public void TakeWaterTest()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlace(1);
            world.PutWater(1);
            bool res = world.TakeWater(1);

            Assert.IsTrue(res == true && !world.HaveWater(1));
        }
Exemplo n.º 24
0
 public override ActionType GetNextAction(TravelWorld world)
 {
     if (CurrentLocation == Goal)
     {
         return(noOpertion);
     }
     else
     {
         return(InnerGetNextAction(world));
     }
 }
Exemplo n.º 25
0
 public override ActionType GetNextAction(TravelWorld world)
 {
     if (CarryWater)
     {
         return(actionToStopFire(world));
     }
     else
     {
         return(actionToFindWater(world));
     }
 }
Exemplo n.º 26
0
        private TravelPath findFirePath(TravelWorld world)
        {
            var paths = world.findCheapestFirePaths(CurrentLocation);

            if (paths == null || paths.Count() == 0)
            {
                return(null);
            }

            return(findMinDest(paths));
        }
Exemplo n.º 27
0
        public override ActionType GetNextAction(TravelWorld world)
        {
            if (CurrentLocation == Goal)
            {
                return(noOpertion);
            }

            var stateAction = _algo.Run(ToState(world), getActions, _otherPlayer.getActions, _cuttFunc, _evalFunc);

            return(_actionsMap[stateAction]);
        }
Exemplo n.º 28
0
        protected TravelSearchState ToSearchState(TravelWorld newWorld)
        {
            var newState = new TravelSearchState();

            newState.CarryWatter     = CarryWater;
            newState.CurrentLocation = CurrentLocation;
            newState.WaterPlaces     = new List <int>(newWorld.GetWaterPlaces());
            newState.FireWays        = new List <TravelEdge>(newWorld.GetFireWays());
            newState.WorldGraph      = newWorld.GetGraph();
            return(newState);
        }
Exemplo n.º 29
0
        public void NoClearWays()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2, 3);
            world.AddWay(1, 3, 1);
            world.SetFire(1, 3);
            var ways = world.GetClearWays(1);

            Assert.IsTrue(ways.Count() == 0);
        }
Exemplo n.º 30
0
        public TravelWorld ToWorld()
        {
            var world = new TravelWorld(WorldGraph);

            world.PutWater(WaterPlaces.ToArray());
            foreach (var fireWay in FireWays)
            {
                world.SetFire(fireWay.Source, fireWay.Target);
            }
            return(world);
        }