public static void Test_Genetic(string mesh_type)
        {
            int[] sizes_mesh = new int[21] {
                10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50
            };
            int[] final_positions = new int[21] {
                99, 143, 195, 255, 323, 399, 483, 575, 675, 783, 899, 1023, 1155, 1295, 1443, 1599, 1763, 1935, 2115, 2303, 2499
            };

            for (int i = 0; i < sizes_mesh.Length; i++)
            {
                string size      = "_" + sizes_mesh[i] + "x" + sizes_mesh[i] + ".txt";
                string file_name = mesh_type + size;

                Console.WriteLine("Init Execution -> " + file_name);

                //-------------------------------------------------------------------
                //Create the mesh environment
                MeshEnvironment env = new MeshEnvironment(_start: 0, _final: final_positions[i], _file_name: file_name, sizes_mesh[i]);
                env.InitEnviroment(type_mesh: mesh_type);

                //-------------------------------------------------------------------
                for (int j = 0; j < num_test; j++)
                {
                    env.FillDistanceMatrix();
                    DijkstraAlgorithm dk1 = new DijkstraAlgorithm();
                    dk1.Execute(ref env);

                    GeneticAlgorithm gn1 = new GeneticAlgorithm();
                    gn1.Execute(ref env);


                    //Insert the obstacle in a middle zone of the current optimal solution
                    InsertSeveralObstacle(ref env, num_obstacle: 2, num_routes: 2);

                    env.FillDistanceMatrix();
                    DijkstraAlgorithm dk2 = new DijkstraAlgorithm();
                    dk2.Execute(ref env);

                    GeneticAlgorithm gn2 = new GeneticAlgorithm();
                    gn2.Execute(ref env);

                    ////Store variables in a txt
                    Console.WriteLine("Execution {0} of " + file_name, j);

                    StoreVariable_Genetic(mesh_type, ref env, ref dk1, ref gn1, ref dk2, ref gn2);

                    env.ClearObstacles();
                }

                Console.WriteLine("End Execution -> " + file_name);
                Console.WriteLine("---------------------------------------------");
            }
        }
        public static void InsertSeveralObstacle(ref MeshEnvironment env, int num_obstacle, int num_routes)
        {
            for (int i = 0; i < num_routes; i++)
            {
                env.FillDistanceMatrix();
                DijkstraAlgorithm dk = new DijkstraAlgorithm();
                dk.Execute(ref env);

                //Ver bien como poner esto
                env.InsertObstacle(ref dk.final_best_path, num_obstacle);
            }
        }
Example #3
0
        //---------------------------------------------------------------------
        public static void StoreVariable_RW1_and_ACOv0(string mesh_type, ref MeshEnvironment env, ref AntColonyOptimizationv0 aco, ref RandomWalkv1 rw1)
        {
            env.FillDistanceMatrix();
            DijkstraJustCost dijkstra  = new DijkstraJustCost();
            Double           best_cost = dijkstra.DijkstraAlgo(graph: env.distances, source: env.start_node, verticesCount: env.final_node + 1);


            Double percentage_learning = MeasureFunctions.CalculatePercentageLearning(ref env);
            string separator           = " ";
            string line = mesh_type + separator;

            line += env.world.Count + separator;
            line += env.edges.Count + separator;
            line += best_cost + separator;
            line += aco.max_ants + separator;
            line += aco.alpha + separator;
            line += aco.beta + separator;
            line += aco.tau_0 + separator;
            line += aco.evaporation_factor + separator;
            line += aco.percentage_convergence + separator;
            line += aco.episode_counter + separator;
            line += aco.execution_time + separator;
            line += aco.stuck_roads + separator;
            line += MeasureFunctions.GetVisitedNodes(ref env) + separator;
            line += MeasureFunctions.GetVisitedEdges(ref env) + separator;
            line += Math.Round(aco.best_cost, 2) + separator;
            line += percentage_learning + separator;
            line += rw1.num_random_walks + separator;
            line += rw1.random_movements + separator;
            line += rw1.evaporation_factor + separator;
            line += rw1.best_cost + separator;
            line += rw1.execution_time;

            string path           = @"data_sets/results/acov0_rw1/" + mesh_type + "/";
            string variables_file = "variablesarw1" + "_" + mesh_type + "_" + env.size + "x" + env.size + ".txt";

            using (StreamWriter file =
                       File.AppendText(path + variables_file))
            {
                file.WriteLine(line);
            }

            string path2           = @"data_sets/results/acov0_rw1/";
            string variables_file2 = "variablesrw1" + "_" + mesh_type + ".txt";

            using (StreamWriter file =
                       File.AppendText(path2 + variables_file2))
            {
                file.WriteLine(line);
            }
        }
        public static void Test_RWv2_ACOv0(string mesh_type)
        {
            int[] sizes_mesh = new int[21] {
                10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50
            };
            int[] final_positions = new int[21] {
                99, 143, 195, 255, 323, 399, 483, 575, 675, 783, 899, 1023, 1155, 1295, 1443, 1599, 1763, 1935, 2115, 2303, 2499
            };

            for (int i = 0; i < sizes_mesh.Length; i++)
            {
                string size      = "_" + sizes_mesh[i] + "x" + sizes_mesh[i] + ".txt";
                string file_name = mesh_type + size;

                Console.WriteLine("Init Execution -> " + file_name);

                //-------------------------------------------------------------------
                //Create the mesh environment
                MeshEnvironment env = new MeshEnvironment(_start: 0, _final: final_positions[i], _file_name: file_name, sizes_mesh[i]);
                env.InitEnviroment(type_mesh: mesh_type);

                //-------------------------------------------------------------------
                for (int j = 0; j < num_test; j++)
                {
                    //............................................................................
                    // ROUTE 1
                    env.FillDistanceMatrix();
                    DijkstraAlgorithm dk1 = new DijkstraAlgorithm();
                    dk1.Execute(ref env);

                    RandomWalkv2 rw1 = new RandomWalkv2();
                    rw1.ExecuteRandomWalk(ref env);

                    AntColonyOptimizationv0 aco1 = new AntColonyOptimizationv0(_random_walk: true);
                    aco1.ExecuteACO(ref env);

                    //............................................................................
                    //Insert the obstacle in a middle zone of the current optimal solution
                    InsertSeveralObstacle(ref env, num_obstacle: 2, num_routes: 2);
                    //............................................................................
                    // ROUTE 2 - REROUTING

                    env.FillDistanceMatrix();
                    DijkstraAlgorithm dk2 = new DijkstraAlgorithm();
                    dk2.Execute(ref env);

                    RandomWalkv2 rw2 = new RandomWalkv2();
                    rw2.ExecuteRandomWalk(ref env);

                    AntColonyOptimizationv0 aco2 = new AntColonyOptimizationv0(_random_walk: true);
                    aco2.ExecuteACO(ref env);

                    //............................................................................
                    ////Store variables in a txt
                    Console.WriteLine("Execution {0} of " + file_name, j);
                    StoreVariable_RWv2_ACOv0(mesh_type, ref env, ref dk1, ref aco1, ref dk2, ref aco2);

                    //............................................................................
                    //Reset environment
                    env.InitPheromones(0);
                    env.ClearObstacles();
                }

                Console.WriteLine("End Execution -> " + file_name);
                Console.WriteLine("---------------------------------------------");
            }
        }
Example #5
0
        //-------------------------------------------------------------------------------

        public void Execute(ref MeshEnvironment env)
        {
            //Fill distances to objective
            env.FillDistancesToFinal();

            //Init the node tags list;
            int size = env.size * env.size;

            InitNodeTags(size);

            //Get the distance between node. Adjacency Matrix
            env.FillDistanceMatrix();
            Double[,] distances_matrix = env.distances;


            //Get initial and final nodes
            int initial_position = env.start_node;
            int final_position   = env.final_node;

            //Get world of the env
            List <Node> world = env.world;

            //Init the values of the initial node and its tag
            node_tags[initial_position].local_goal  = 0.0;
            node_tags[initial_position].global_goal = MeasureHeuristic(initial_position, ref env);

            //Fijar la posicion actual
            Tag current_position = node_tags[initial_position];

            //Nodos que seran testeados
            List <Tag> tested_nodes_list = new List <Tag>();


            tested_nodes_list.Add(node_tags[initial_position]);

            //Time variable
            var watch = System.Diagnostics.Stopwatch.StartNew();


            while (tested_nodes_list.Count != 0) // && current_position.node_id != env.final_node)
            {
                //Ordenar la lista de acuerdo al objetivo global en orden ascendente
                tested_nodes_list = tested_nodes_list.OrderBy(tag => tag.global_goal).ToList();

                //Remover de la lista si el nodo analizado ya ha sido visitado
                while (tested_nodes_list.Count != 0 && tested_nodes_list[0].visited)
                {
                    tested_nodes_list.RemoveAt(0);
                }

                //Si la lista es vacia salimos del bucle
                if (tested_nodes_list.Count == 0)
                {
                    break;
                }

                //Inicializamos nuestra posicion actual
                current_position                    = tested_nodes_list[0];
                current_position.visited            = true;
                tested_nodes_list[0]                = current_position;
                node_tags[current_position.node_id] = current_position;


                //Obtengo los posibles nodos a los cuales me puedo mover
                List <int> list_posible_nodes = GetPosibleNodes(current_position.node_id, ref env);

                //Itero por los posibles nodos
                foreach (var neighbour_id in list_posible_nodes)
                {
                    int current_id = current_position.node_id;

                    Double possible_lower_goal = current_position.local_goal + distances_matrix[current_id, neighbour_id];

                    if (possible_lower_goal < node_tags[neighbour_id].local_goal && !node_tags[neighbour_id].visited)
                    {
                        Tag new_tag = node_tags[neighbour_id];
                        new_tag.parent          = current_id;
                        new_tag.local_goal      = possible_lower_goal;
                        new_tag.global_goal     = new_tag.local_goal + MeasureHeuristic(neighbour_id, ref env);
                        node_tags[neighbour_id] = new_tag;

                        if (tested_nodes_list.Contains(new_tag))
                        {
                            int idx = tested_nodes_list.IndexOf(new_tag);
                            tested_nodes_list[idx] = new_tag;
                        }
                    }

                    //Si los nodos analizados aun no han sido marcados como visitados
                    //Los aƱadimos a lista de testeo
                    if (!node_tags[neighbour_id].visited && !tested_nodes_list.Contains(node_tags[neighbour_id]))
                    {
                        tested_nodes_list.Add(node_tags[neighbour_id]);
                    }
                }
            }


            List <int> optimal_path = GetPath(final_position, initial_position);
            Double     optimal_cost = ExtraTools.GetCost(ref optimal_path, ref env);

            final_best_path = optimal_path;
            final_best_path.Reverse();
            final_best_cost = optimal_cost;

            //Execution Time
            watch.Stop();
            execution_time = watch.ElapsedMilliseconds;
        }