Example #1
0
        public SASolution(SASolution ts, Node a, int routeIndex)
        {
            rutas = new List <List <Node> >();
            foreach (List <Node> lst in ts.rutas)
            {
                rutas.Add(new List <Node>());
                foreach (Node n in lst)
                {
                    rutas[rutas.Count - 1].Add(n);
                }
            }

            depot     = ts.depot;
            fitness   = ts.fitness;
            capacidad = ts.capacidad;
            feasible  = ts.feasible;

            int indexRouteNode = GetIdRuta(a);

            rutas[indexRouteNode].Remove(a);
            rutas[routeIndex].Add(a);

            ReordenarNodosRutasMinimizandoCapacidad();
            ReordenarNodosRutasMinimizandoDistancia();
        }
Example #2
0
        public Solution RunSA(double alpha, double temperature, double epsilon)
        {
            int iteration = -1;

            //the probability
            double proba;
            //double alpha = 0.9999;
            //double temperature = 400.0;
            //double epsilon = 0.0000000001;
            double delta;

            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();
            SASolution    currentSolutionSA = new SASolution(numRutas, lstNodes, depot, Filecvrp.Capacity);

            currentSolutionSA.CalculateFitness();
            double distance = currentSolutionSA.Fitness;
            Random r        = new Random();

            //while the temperature did not reach epsilon
            while (temperature > epsilon)
            {
                iteration++;

                //get the next random permutation of distances
                int selectedNode = r.Next(0, lstNodes.Count);
                while (depot.Id == lstNodes[selectedNode].Id)
                {
                    selectedNode = r.Next(0, lstNodes.Count);
                }
                Node       n              = lstNodes[selectedNode];
                int        routeindex     = r.Next(0, numRutas);
                SASolution nextSolutionSA = new SASolution(currentSolutionSA, n, routeindex);
                nextSolutionSA.CalculateFitness();
                //compute the distance of the new permuted configuration
                delta = nextSolutionSA.Fitness - distance;
                //if the new distance is better accept it and assign it
                if (delta < 0 && nextSolutionSA.Feasible)
                {
                    currentSolutionSA = nextSolutionSA;
                    distance          = delta + distance;
                }
                else
                {
                    proba = r.Next();
                    //if the new distance is worse accept
                    //it but with a probability level
                    //if the probability is less than
                    //E to the power -delta/temperature.
                    //otherwise the old value is kept
                    if (proba < Math.Exp(-delta / temperature))
                    {
                        currentSolutionSA = nextSolutionSA;
                        distance          = delta + distance;
                    }
                }
                //cooling process on every iteration
                temperature *= alpha;

                //lstMinFitness.Add(currentSolutionSA.Fitness);
            }

            distance = currentSolutionSA.Fitness;

            Solution s = new Solution();

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