Beispiel #1
0
 public PyromaniacTest()
 {
     world.AddPlaces(1, 2, 3, 4);
     world.AddWay(1, 2, 1);
     world.AddWay(2, 3, 2);
     world.AddWay(4, 3, 3);
 }
Beispiel #2
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 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);
        }
Beispiel #4
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));
        }
Beispiel #5
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);
        }
Beispiel #6
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));
        }
Beispiel #7
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));
        }
        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 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);
        }
Beispiel #10
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);
 }
Beispiel #11
0
        public void NoFireTest()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2, 3);
            world.AddWay(1, 2, 1);
            world.AddWay(3, 2, 1);
            world.SetFire(2, 3);
            Assert.IsTrue(world.isClear(1, 2));
        }
Beispiel #12
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);
        }
Beispiel #13
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);
        }
Beispiel #14
0
        public void GoToDest(BaseTraveler agent)
        {
            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);
            Assert.IsTrue((agent.GetNextAction(world)).Invoke(world));
            Assert.AreEqual(agent.CurrentLocation, 3);
            Assert.IsTrue((agent.GetNextAction(world)).Invoke(world));
            Assert.AreEqual(agent.CurrentLocation, 4);
        }
Beispiel #15
0
        public void noWater()
        {
            TravelWorld world = new TravelWorld();

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

            (agent.GetNextAction(world)).Invoke(world);
            Assert.AreEqual(agent.CurrentLocation, 1);
            Assert.AreEqual(agent.TotalCost, agent.costs.Epsilon);
        }
Beispiel #16
0
        public void clearWays()
        {
            TravelWorld world = new TravelWorld();

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

            Assert.AreEqual(ways.Count(), 1);
            Assert.IsFalse(!world.isClear(ways.First()));
        }
Beispiel #17
0
        public void noOperation(BaseTraveler agent)
        {
            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.SetFire(1, 2);
            double prevCost = agent.TotalCost;

            (agent.GetNextAction(world)).Invoke(world);
            Assert.AreEqual(agent.TotalCost - prevCost, Costs.Instance.Epsilon);
            Assert.AreEqual(agent.CurrentLocation, 2);
        }
Beispiel #18
0
        public void GoToDestOnce(BaseTraveler agent)
        {
            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, 20);

            double prevCost = agent.TotalCost;

            Assert.IsTrue((agent.GetNextAction(world)).Invoke(world));
            Assert.AreEqual(agent.TotalCost - prevCost, 20);
            Assert.AreEqual(agent.CurrentLocation, 4);
        }
Beispiel #19
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));
        }
Beispiel #20
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));
        }
Beispiel #21
0
        public void ClearPathTest()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(1, 2, 3, 4, 5);
            for (int i = 1; i <= 4; i++)
            {
                world.AddWay(i, i + 1, 1);
            }
            world.AddWay(5, 1, 1);
            world.SetFire(1, 2);
            var path    = world.ShortestClearPath(1, 3);
            var resPath = new TravelPath();

            resPath.Add(5, 1);
            resPath.Add(4, 1);
            resPath.Add(3, 1);

            Assert.IsTrue(resPath.Equals(path));
        }
Beispiel #22
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);
        }
Beispiel #23
0
        public void greedyTest()
        {
            TravelWorld world = new TravelWorld();

            world.AddPlaces(2, 3, 4);
            world.AddWay(3, 2, 1);
            world.AddWay(3, 4, 1);
            world.AddWay(2, 4, 10);
            AStartAgent agent = new AStartAgent(greedyHuristic(4), 2, 4);

            //chose the long path because  its cost is mush lower even the huristic
            //is for chosing the shortest path.

            //go to 3
            Assert.IsTrue(agent.GetNextAction(world)(world));
            Assert.AreEqual(3, agent.CurrentLocation);

            //go to 4
            Assert.IsTrue(agent.GetNextAction(world)(world));
            Assert.AreEqual(agent.CurrentLocation, 4);
        }
Beispiel #24
0
        public void cheapestFirePathTest2()
        {
            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);
            var paths = world.findCheapestFirePaths(2).ToArray();

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

            var path1    = paths.First();
            var resPath1 = new TravelPath();

            resPath1.Add(1, 10);
            Assert.AreEqual(path1, resPath1);
        }
Beispiel #25
0
        public void NaturalGame()
        {
            TravelWorld world = new TravelWorld();

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


            Human human = new Human(2);

            human.costs.Fire   = 1;
            human.costs.Pickup = 5;
            human.Goal         = 4;

            BasicCuttof cuttOf = new BasicCuttof();

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

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

            //eval.SetParams(agent, human, 10);
            cuttOf.SetParams(agent, 3);

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

            agent.GetNextAction(world)(world);
            Assert.AreEqual(4, agent.CurrentLocation);
            Assert.IsTrue(world.isClear(4, 3));
        }
        public void goToLowerPlaceTest()
        {
            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(2, 4, 10);

            GreedySearchAgent agent = new GreedySearchAgent(goToLowerPlace, 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);

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

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

            world.SetFire(1, 2);
            //do nothing
            action = agent.GetNextAction(world);
            Assert.IsTrue(action(world));
            Assert.AreEqual(agent.CurrentLocation, 1);
            Assert.AreEqual(agent.TotalCost, prevCost + world.EpsilonCost);
        }
        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);
        }
Beispiel #28
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);
        }