Example #1
0
 public void SetWeights()
 {
     for (int i = 0; i < IVertexList.Count; ++i)
     {
         for (int j = 0; j < IVertexList[i].GetEdgesListCount(); ++j)
         {
             IVertexList[i][j].SetWeight(ToolFunction.Distance(IVertexList[i].GetCirclePoint(), IVertexList[i][j].Destination.GetCirclePoint()));
         }
     }
 }
Example #2
0
        private bool PreyIsInSafePosition(Prey prey)
        {
            double distance = ToolFunction.Distance(prey.Location, prey.CurrentVertex.GetCirclePoint());

            // Multiplied by three for the incresed size of the shield
            if (distance <= (prey.CurrentVertex.Circle.Radius * 3))
            {
                return(true);
            }

            return(false);
        }
        private void CreateGraph()
        {
            if (ICircleList.Count > 0)
            {
                this.IGraph = new Graph();
                ICircleList = SortedCircleList();

                foreach (Circle circle in ICircleList)
                {
                    IGraph.AddVertex(new Vertex(circle, IGraph.GetVertexCount() + 1));
                }

                for (int i = 0; i < ICircleList.Count - 1; ++i)
                {
                    for (int j = i + 1; j < ICircleList.Count; ++j)
                    {
                        if (IsObstacleFree(IGraph[i].Circle, IGraph[j].Circle))
                        {
                            double distance = ToolFunction.Distance(IGraph[j].GetCirclePoint(), IGraph[i].GetCirclePoint());

                            if (ToolFunction.Distance(IGraph[i].GetCirclePoint(), Coords[0]) < 5)
                            {
                                IGraph[i].AddEdge(new Edge(distance, IGraph[j], new List <Point>(this.Coords), IGraph[i].ID.ToString()));
                                this.Coords.Reverse();
                                IGraph[j].AddEdge(new Edge(distance, IGraph[i], new List <Point>(this.Coords), IGraph[j].ID.ToString()));
                            }
                            else
                            {
                                IGraph[j].AddEdge(new Edge(distance, IGraph[i], new List <Point>(this.Coords), IGraph[i].ID.ToString()));
                                this.Coords.Reverse();
                                IGraph[i].AddEdge(new Edge(distance, IGraph[j], new List <Point>(this.Coords), IGraph[j].ID.ToString()));
                            }

                            Coords.Clear();
                        }
                    }
                }

                ModifyBitmapFromGraph();

                this.IMessage = "Grafo creado exitosamente.";
            }
            else
            {
                this.IMessage = "No existen elementos para crear un grafo.";
                this.IStatus  = false;
            }
        }
Example #4
0
        public Prey ExecuteSimulation()
        {
            Dijkstra dijkstra;
            Random   random = new Random();
            Dictionary <string, Edge> preyPath     = new Dictionary <string, Edge>();
            Dictionary <string, Edge> predatorPath = new Dictionary <string, Edge>();

            Survivor = null;
            Instance.SoundPlayer.SoundLocation = Instance.SoundtrackPath["Running"];

            if (Instance.CheckBox_Music.Checked)
            {
                Instance.SoundPlayer.PlayLooping();
            }

            Pause(500);

            foreach (Prey p in Instance.Preys)
            {
                dijkstra = new Dijkstra(Instance.Graph, p.CurrentVertex);
                dijkstra.DijkstraAlgorithm();
                p.Path = ToolFunction.ConvertToEdgeStack(dijkstra.BuildPath((Vertex)Instance.ComboBox_Target.SelectedItem), Instance.Graph);
            }

            SimulationRunningStatus();

            foreach (Predator p in Instance.Predators)
            {
                p.VisitedEdges.Clear();
                p.Location           = p.CurrentVertex.GetCirclePoint();
                predatorPath[p.Name] = p.CurrentVertex.GetEdges()[random.Next(p.CurrentVertex.GetEdgesListCount() - 1)];
                p.Iteration          = 1;
                p.VisitedEdges.Add(predatorPath[p.Name]);
            }

            while (!CompletedSimulation())
            {
                foreach (Prey p in Instance.Preys)
                {
                    if (p.Path.Count > 0)
                    {
                        preyPath[p.Name] = p.Path.Pop();
                        p.Iteration      = 1;
                    }
                    else
                    {
                        return(null);
                    }
                }

                while (!AllPreysFinish())
                {
                    for (int i = 0; i < Instance.Preys.Count; ++i)
                    {
                        if (Instance.Preys[i].UnderPredation)
                        {
                            double distance = ToolFunction.Distance(Instance.Preys[i].Location, Instance.Preys[i].Predator.Location);

                            if (distance > Instance.Preys[i].Predator.HuntingPerimeter)
                            {
                                Instance.Preys[i].Predator.Hunting = false;
                                Instance.Preys[i].Predator.Prey    = null;
                                Instance.Preys[i].UnderPredation   = false;
                                Instance.Preys[i].UpdatePredator();

                                if (Instance.Preys[i].Speed < 10)
                                {
                                    Instance.Preys[i].Speed = 10;
                                }
                            }
                        }

                        bool returnToOrigin = false;

                        if (Instance.Preys[i].UnderPredation)
                        {
                            double distancePrey     = ToolFunction.Distance(preyPath[Instance.Preys[i].Name].Destination.GetCirclePoint(), Instance.Preys[i].Location);
                            double distancePredator = ToolFunction.Distance(preyPath[Instance.Preys[i].Name].Destination.GetCirclePoint(), Instance.Preys[i].Predator.Location);

                            if (distancePrey > distancePredator)
                            {
                                returnToOrigin = true;
                            }
                        }

                        if (returnToOrigin)
                        {
                            if ((Instance.Preys[i].Iteration - Instance.Preys[i].Speed) > 0)
                            {
                                Instance.Preys[i].Iteration -= Instance.Preys[i].Speed;
                                Instance.Preys[i].Location   = preyPath[Instance.Preys[i].Name].Path[Instance.Preys[i].Iteration];
                            }
                            else
                            {
                                Instance.Preys[i].Iteration = 0;
                                Instance.Preys[i].Location  = Instance.Preys[i].CurrentVertex.GetCirclePoint();
                            }
                        }
                        else
                        {
                            if ((Instance.Preys[i].Iteration + Instance.Preys[i].Speed) < preyPath[Instance.Preys[i].Name].Path.Count)
                            {
                                if (Instance.Preys[i].Iteration != 0)
                                {
                                    Instance.Preys[i].Iteration += Instance.Preys[i].Speed;
                                    Instance.Preys[i].Location   = preyPath[Instance.Preys[i].Name].Path[Instance.Preys[i].Iteration];
                                }
                                else
                                {
                                    if (Instance.Preys[i].Location != preyPath[Instance.Preys[i].Name].Destination.GetCirclePoint())
                                    {
                                        Instance.Preys[i].Iteration += Instance.Preys[i].Speed;
                                        Instance.Preys[i].Location   = preyPath[Instance.Preys[i].Name].Path[Instance.Preys[i].Iteration];
                                    }
                                }
                            }
                            else
                            {
                                Instance.Preys[i].Iteration     = 0;
                                Instance.Preys[i].CurrentVertex = preyPath[Instance.Preys[i].Name].Destination;
                                Instance.Preys[i].Location      = Instance.Preys[i].CurrentVertex.GetCirclePoint();
                            }
                        }
                    }

                    for (int i = 0; i < Instance.Predators.Count; ++i)
                    {
                        Instance.Predators[i].VisitedEdges.Clear();

                        if ((Instance.Predators[i].Iteration + Instance.Predators[i].Speed) < predatorPath[Instance.Predators[i].Name].Path.Count)
                        {
                            Instance.Predators[i].Iteration += Instance.Predators[i].Speed;
                            Instance.Predators[i].Location   = predatorPath[Instance.Predators[i].Name].Path[Instance.Predators[i].Iteration];
                        }
                        else
                        {
                            Instance.Predators[i].Iteration     = 1;
                            Instance.Predators[i].CurrentVertex = predatorPath[Instance.Predators[i].Name].Destination;
                            Instance.Predators[i].Location      = Instance.Predators[i].CurrentVertex.GetCirclePoint();

                            Edge nextEdge = null;

                            if (!Instance.Predators[i].Hunting)
                            {
                                for (int j = 0; j < Instance.Predators[i].CurrentVertex.GetEdgesListCount(); ++j)
                                {
                                    nextEdge = Instance.Predators[i].CurrentVertex.GetEdges()[random.Next(Instance.Predators[i].CurrentVertex.GetEdgesListCount())];

                                    if (!Instance.Predators[i].VisitedEdges.Contains(nextEdge))
                                    {
                                        predatorPath[Instance.Predators[i].Name] = nextEdge;
                                    }
                                }
                            }
                            else
                            {
                                foreach (Edge edge in predatorPath[Instance.Predators[i].Name].Destination.GetEdges())
                                {
                                    if (edge.Destination.ID == preyPath[Instance.Predators[i].Prey.Name].Destination.ID)
                                    {
                                        nextEdge = edge;
                                    }
                                    else if (edge.Destination.ID == Instance.Predators[i].Prey.CurrentVertex.ID && nextEdge == null)
                                    {
                                        nextEdge = edge;
                                    }
                                }
                            }

                            if (nextEdge == null)
                            {
                                predatorPath[Instance.Predators[i].Name] = Instance.Predators[i].CurrentVertex.GetEdges()[0];
                            }
                        }

                        for (int j = 0; j < Instance.Preys.Count; ++j)
                        {
                            double distance = ToolFunction.Distance(Instance.Predators[i].Location, Instance.Preys[j].Location);

                            if (distance <= Instance.Predators[i].HuntingPerimeter)
                            {
                                if (distance <= Instance.Predators[i].CollitionRange)
                                {
                                    if (!PreyIsInSafePosition(Instance.Preys[j]))
                                    {
                                        if (Instance.Preys[j].UnderPredation)
                                        {
                                            if (Instance.Preys[j].Predator.Name == Instance.Predators[i].Name)
                                            {
                                                SimulationPreySlayedStatus(Instance.Predators[i], Instance.Preys[j]);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (!PreyIsInSafePosition(Instance.Preys[j]))
                                    {
                                        if (!Instance.Preys[j].UnderPredation)
                                        {
                                            SimulationPredatorFoundPrey(Instance.Predators[i], Instance.Preys[j]);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    RepaintScenario();
                }
            }

            Instance.SoundPlayer.Stop();

            return(Survivor);
        }