Exemple #1
0
        public Cromosoma(Cromosoma padre, Cromosoma madre, List <Node> lstNodes)
        {
            this.capacidad = padre.capacidad;
            this.depot     = padre.depot;
            this.rutas     = new List <List <Node> >();
            for (int i = 0; i < padre.rutas.Count; i++)
            {
                rutas.Add(new List <Node>());
            }

            Random r = new Random();

            foreach (Node n in lstNodes)
            {
                if (n.Id != depot.Id)
                {
                    int progenitor = r.Next(0, 2);
                    int ruta       = 0;
                    if (progenitor == 0)
                    {
                        ruta = GetIdRuta(padre, n);
                    }
                    else
                    {
                        ruta = GetIdRuta(madre, n);
                    }
                    rutas[ruta].Add(n);
                }
            }

            ReordenarNodosRutasMinimizandoCapacidad();
            ReordenarNodosRutasMinimizandoDistancia();
        }
Exemple #2
0
 private int GetIdRuta(Cromosoma c, Node n)
 {
     for (int i = 0; i < c.rutas.Count; i++)
     {
         for (int j = 0; j < c.rutas[i].Count; j++)
         {
             if (c.rutas[i][j].Id == n.Id)
             {
                 return(i);
             }
         }
     }
     return(0);
 }
Exemple #3
0
        public Solution RunGA(int tamañoPoblacionInicial, int numerogeneraciones, int probabilidadCruce, int probabilidadMutacion)
        {
            List <double> lstMinFitness             = new List <double>();
            int           numRutas                  = Filecvrp.NumRutas;
            List <Node>   lstNodes                  = Filecvrp.LstNodes;
            Node          depot                     = Filecvrp.LstNodes.Where(e => e.Id == Filecvrp.IdNodeDepot).First();
            int           numeroCrucesPorGeneracion = tamañoPoblacionInicial / 2;

            Random           r             = new Random();
            List <Cromosoma> poblacion     = new List <Cromosoma>();
            Cromosoma        bestCromosoma = null;

            for (int i = 0; i < tamañoPoblacionInicial; i++)
            {
                Cromosoma c = new Cromosoma(numRutas, lstNodes, depot, Filecvrp.Capacity);
                c.CalculateFitness();
                int maxIterations = 1000;
                int iteration     = 0;
                while (!c.Feasible && iteration < maxIterations)
                {
                    c = new Cromosoma(numRutas, lstNodes, depot, Filecvrp.Capacity);
                    c.CalculateFitness();
                    iteration++;
                }
                poblacion.Add(c);
            }

            for (int i = 0; i < numerogeneraciones; i++)
            {
                for (int j = 0; j < numeroCrucesPorGeneracion; j++)
                {
                    int indexPadre = r.Next(0, poblacion.Count);
                    int indexMadre = r.Next(0, poblacion.Count);
                    if (r.Next(0, 100) < probabilidadCruce)
                    {
                        Cromosoma h1 = new Cromosoma(poblacion[indexPadre], poblacion[indexMadre], lstNodes);
                        if (r.Next(0, 100) < probabilidadMutacion)
                        {
                            h1.Mutar();
                        }
                        h1.CalculateFitness();
                        Cromosoma h2 = new Cromosoma(poblacion[indexPadre], poblacion[indexMadre], lstNodes);
                        if (r.Next(0, 100) < probabilidadMutacion)
                        {
                            h2.Mutar();
                        }
                        h2.CalculateFitness();

                        if (h1.Fitness < poblacion[indexPadre].Fitness && h1.Feasible)
                        {
                            poblacion[indexPadre] = h1;
                        }
                        if (h2.Fitness < poblacion[indexMadre].Fitness && h2.Feasible)
                        {
                            poblacion[indexMadre] = h2;
                        }
                    }
                }
                double minFitness = poblacion.Select(e => e.Fitness).Min();
                Console.WriteLine("Generacion: " + i + " Min Fitness: " + minFitness);
                lstMinFitness.Add(minFitness);
                bestCromosoma = poblacion.Where(e => e.Fitness == minFitness).First();
            }

            Solution s = new Solution();

            s.LstMinFitness    = lstMinFitness;
            s.BestRutas        = bestCromosoma.Rutas;
            s.FitnessBestRutas = bestCromosoma.Fitness;
            return(s);
        }