Exemple #1
0
        //---------------------------------------------------------------------------------------
        // FITNESS FUNCTION
        // fitness function = for each car route
        // (sum of all city distances + distance from depot to first city
        //                  + distance from last city back to depot)
        //          + sum of all service times in each city
        public int EvaluateCost(Solution soln)
        {
            // go through array of routes
            for (int x = 0; x < soln.GetLength(); x++)
            {
                LinkedList<int> route = soln.GetRoute(x);

                if (route.Count > 0)
                {
                    // distance from depot to first city
                    int city = Convert.ToInt16(route.First.Value);
                    soln.AddCost(depot[city]); //distance from depot to first
                    city = Convert.ToInt16(route.Last.Value);
                    soln.AddCost(depot[city]); //distance from depot to last

                    // go through route's cities and add distances
                    for (int a = 1; a < route.Count; a++)
                    {
                        //start with city -1 and city and add distances
                        soln.AddCost(distance[route.ElementAt(a - 1), route.ElementAt(a)]);

                        //add service times for each city
                        soln.AddCost(service[route.ElementAt(a - 1)]);
                    }
                }
            }
            return soln.GetCost();
        }
Exemple #2
0
        public bool SolutionAcceptanceConstraint(Solution best, Solution candidate)
        {
            if (CheckConstraint(candidate) == false)
            {
                return false; // reject candidate solution
            }

            if (best.GetCost() - candidate.GetCost() > 0) // candidate cost is less
            {
                return true; // candidate is accepted
            }
            else
            {
                // accpetance probability = e^-cost/temp
                int negcost = candidate.GetCost() - (2 * candidate.GetCost());
                double exp = (double)negcost / (double)temperature;
                double prob = Math.Exp(exp); // acceptance probability

                double threshold = r.NextDouble(); // acceptance threshold - random

                if (prob.CompareTo(threshold) > 0) // if accept prob > accept thresh
                {
                    //Console.WriteLine("probability of acceptance: {0}\nthreshold of acceptance: {1}",
                    //    prob, threshold);
                    return true; // candidate accepted
                }
            }
            return false; // candidate not accepted
        }