Esempio n. 1
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);
        }
        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);
        }
        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);
        }
        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);
        }
Esempio n. 5
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));
        }
Esempio n. 6
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));
        }
Esempio n. 7
0
        public void simpleWaterTest()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlace(1);
            world.PutWater(1);
            Assert.IsTrue(world.HaveWater(1));
        }
Esempio n. 8
0
        public void HaveWaterHere()
        {
            TravelWorld world = new TravelWorld();

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

            (agent.GetNextAction(world)).Invoke(world);
            Assert.AreEqual(agent.CurrentLocation, 1);
            Assert.AreEqual(agent.TotalCost, Costs.Instance.Pickup);
            Assert.IsFalse(world.HaveWater(1));
            Assert.IsTrue(world.HaveWater(3));
        }
Esempio n. 9
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);
 }
Esempio n. 10
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));
        }
Esempio n. 11
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);
        }
Esempio n. 12
0
        private static TravelWorld CommonWorld()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2, 3);
            world.AddWay(1, 2, 10);
            world.AddWay(1, 3, 1);
            world.AddWay(3, 2, 10);
            world.SetFire(3, 1);
            world.PutWater(1);
            return(world);
        }
Esempio n. 13
0
        public TravelWorld ToWorld()
        {
            var world = new TravelWorld(WorldGraph);

            world.PutWater(WaterPlaces.ToArray());
            foreach (var fireWay in FireWays)
            {
                world.SetFire(fireWay.Source, fireWay.Target);
            }
            foreach (var item in locations)
            {
                world.SetLocation(item.Key, item.Value);
            }
            return(world);
        }
Esempio n. 14
0
        public static TravelWorld CreateWorld(string fileName)
        {
            List <string> lines = File.ReadLines(fileName).ToList();

            if (!lines[0].StartsWith("#V"))
            {
                throw new Exception("file not in correct format");
            }
            TravelWorld world = new TravelWorld();
            //get number of vertices
            int amount = int.Parse(lines[0].Split(' ')[1]);

            int[] places = new int[amount];
            for (int i = 0; i < amount; i++)
            {
                places[i] = i + 1;
            }
            world.AddPlaces(places);
            for (int i = 1; i < lines.Count; i++)
            {
                string   line  = lines[i];
                string[] param = line.Split(' ');
                if (line.StartsWith("#E"))
                {
                    bool clear = param[4] == "C";

                    double weight = 0;
                    if (param[3].StartsWith("W"))
                    {
                        weight = int.Parse(param[3][1].ToString());
                    }
                    world.AddWay(int.Parse(param[1]), int.Parse(param[2]), weight, clear);
                }

                if (line.StartsWith("#W"))
                {
                    world.PutWater(int.Parse(param[1]));
                }
            }

            //TODO: move to agents
            //Console.Write("enter Cost of pick up:");
            //Costs.Instance.Pickup= int.Parse(Console.ReadLine());
            return(world);
        }
Esempio n. 15
0
        public void MainUristicCheck()
        {
            //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(1, 3, 1);
            world.AddWay(3, 2, 1);
            world.AddWay(3, 4, 1);
            world.PutWater(1);
            world.SetFire(1, 2);
            world.PickupCost = 10;
            int goal = 4;
            //the path should be 1->3->4

            GreedySearchAgent agnet = new GreedySearchAgent(new OneFireHuristic(goal).Run, 1);

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

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