Example #1
0
 public double DistanceTo(AlgCity city)
 {
     //int xDistance = Math.Abs(this.x - city.x);
     //int yDistance = Math.Abs(this.y - city.y);
     //return Math.Sqrt(xDistance * xDistance + yDistance * yDistance);
     if (!TourManager.IsRoutesListSet())
     {
         using (LogisticsProject.Models.ApplicationDbContext db = new LogisticsProject.Models.ApplicationDbContext())
         {
             List <LogisticsProject.Models.Route> routes = (from r in db.Routes.Include("cityFrom").Include("cityTo")
                                                            select r).ToList();
             TourManager.SetRoutesList(routes);
             LogisticsProject.Models.Route route = (from r in db.Routes
                                                    where (r.cityFrom.CityId == this.CityId &&
                                                           r.cityTo.CityId == city.CityId)
                                                    select r).First();
             return(route.distance);
         }
     }
     else
     {
         List <LogisticsProject.Models.Route> routes = TourManager.GetRoutesList();
         LogisticsProject.Models.Route        route  = (from r in routes
                                                        where (r.cityFrom.CityId == this.CityId &&
                                                               r.cityTo.CityId == city.CityId)
                                                        select r).First();
         return(route.distance);
     }
 }
Example #2
0
 public AlgCity(AlgCity algCity)
 {
     this.CityId    = algCity.CityId;
     this.CityName  = algCity.CityName;
     this.Latitude  = algCity.Latitude;
     this.Longitude = algCity.Longitude;
     this.Weight    = algCity.Weight;
     this.Truck     = algCity.Truck;
 }
        private static void Mutate(Tour tour)
        {
            for (int tourPos1 = 1; tourPos1 < tour.TourSize(); tourPos1++)
            {
                Boolean choosed = false;
                //Apply mutation rate
                if (rand.Next(0, 10) / 10.0 < mutationRate)
                {
                    int tourPos2 = (int)((tour.TourSize() - 1) * rand.Next(0, 10) / 10.0 + 1);

                    AlgCity city1 = new AlgCity(tour.GetCity(tourPos1));
                    AlgCity city2 = new AlgCity(tour.GetCity(tourPos2));


                    tour.trucksLoad[city1.Truck] += city1.Weight;
                    tour.trucksLoad[city2.Truck] += city2.Weight;
                    if (city1.Weight < city2.Weight)
                    {
                        AlgCity citySwitch = city1;
                        city1 = city2;
                        city2 = citySwitch;
                    }
                    //city1.Truck = rand.Next(1, 4);
                    //city2.Truck = rand.Next(1, 4);
                    while (!choosed)
                    {
                        city1.Truck = rand.Next(1, 4);
                        if (tour.trucksLoad[city1.Truck] >= city1.Weight)
                        {
                            //tour.trucksLoad[tour.GetCity(tourPos2).Truck] += tour.GetCity(tourPos2).Weight;
                            tour.trucksLoad[city1.Truck] -= city1.Weight;
                            tour.SetCity(tourPos2, city1);
                            choosed = true;
                        }
                    }

                    choosed = false;

                    while (!choosed)
                    {
                        city2.Truck = rand.Next(1, 4);
                        if (tour.trucksLoad[city2.Truck] >= city2.Weight)
                        {
                            //tour.trucksLoad[tour.GetCity(tourPos1).Truck] += tour.GetCity(tourPos1).Weight;
                            tour.trucksLoad[city2.Truck] -= city2.Weight;
                            tour.SetCity(tourPos1, city2);
                            choosed = true;
                        }
                    }

                    //tour.SetCity(tourPos2, city1);
                    //tour.SetCity(tourPos1, city2);
                }
            }
        }
Example #4
0
 //Check if tour constains a city
 public Boolean ContainsCity(AlgCity city)
 {
     foreach (AlgCity algCity in tour)
     {
         if (algCity != null && algCity.CityName == city.CityName)
         {
             return(true);
         }
     }
     return(false);
 }
Example #5
0
 // Creates random tour
 public void GenerateIndividual(double fuel, Random random)
 {
     for (int cityIndex = 0; cityIndex < TourManager.NumberOfCities(); cityIndex++)
     {
         Boolean choosed = false;
         AlgCity algCity = new AlgCity(TourManager.GetCity(cityIndex));
         algCity.Truck = random.Next(1, 4);
         while (!choosed)
         {
             algCity.Truck = random.Next(1, 4);
             if (this.trucksLoad[algCity.Truck] >= algCity.Weight)
             {
                 if (this.tour[0] != null)
                 {
                     this.trucksLoad[algCity.Truck] -= algCity.Weight;
                 }
                 SetCity(cityIndex, algCity);
                 choosed = true;
             }
         }
     }
     SetMaxFuel(fuel);
 }
        private static Tour Crossover(Tour parent1, Tour parent2)
        {
            Tour   child  = new Tour();
            Random random = new Random();

            double startPos = random.Next(0, 11) / 10.0 * parent1.TourSize();
            double endPos   = random.Next(0, 11) / 10.0 * parent1.TourSize();

            for (int i = 0; i < child.TourSize(); i++)
            {
                if (startPos < endPos && i > startPos && i < endPos)
                {
                    if (i != 0)
                    {
                        child.trucksLoad[parent1.GetCity(i).Truck] -= parent1.GetCity(i).Weight;
                    }
                    child.SetCity(i, parent1.GetCity(i));
                }
                else if (startPos > endPos)
                {
                    if (!(i < startPos && i > endPos))
                    {
                        if (i != 0)
                        {
                            child.trucksLoad[parent1.GetCity(i).Truck] -= parent1.GetCity(i).Weight;
                        }
                        child.SetCity(i, parent1.GetCity(i));
                    }
                    ;
                }
            }

            //Loop throught parent2's city tour
            for (int i = 0; i < parent2.TourSize(); i++)
            {
                if (!child.ContainsCity(parent2.GetCity(i)))
                {
                    for (int j = 0; j < child.TourSize(); j++)
                    {
                        if (child.GetCity(j) == null)
                        {
                            AlgCity city    = new AlgCity(parent2.GetCity(i));
                            Boolean choosed = false;
                            while (!choosed)
                            {
                                city.Truck = rand.Next(1, 4);
                                if (child.trucksLoad[city.Truck] >= city.Weight)
                                {
                                    if (i != 0)
                                    {
                                        child.trucksLoad[city.Truck] -= city.Weight;
                                    }
                                    child.SetCity(j, city);

                                    choosed = true;
                                }
                            }
                            break;
                        }
                    }
                }
            }

            return(child);
        }
Example #7
0
        public int GetTruckDistance(int truckId)
        {
            double  truckTourDistance = 0;
            AlgCity fromCity          = null;
            AlgCity destinationCity   = null;

            for (int cityIndex = 0; cityIndex < TourSize(); cityIndex++)
            {
                if (cityIndex == 0 || GetCity(cityIndex).Truck == truckId)
                {
                    // First origin (happen when cityIndex == 0)
                    if (fromCity == null)
                    {
                        fromCity = GetCity(cityIndex);
                    }
                    // Next origins are previous destinations
                    else
                    {
                        fromCity = destinationCity;
                    }

                    // searching next destination

                    if (cityIndex < (TourSize() - 1) && GetCity(cityIndex + 1).Truck == truckId)
                    {
                        destinationCity = GetCity(cityIndex + 1);
                    }
                    while (cityIndex < (TourSize() - 1) && GetCity(cityIndex + 1).Truck != truckId)
                    {
                        cityIndex++;
                        if (cityIndex + 1 >= TourSize())
                        {
                            // Back to start point
                            destinationCity = GetCity(0);
                            break;
                        }
                        else if (GetCity(cityIndex + 1).Truck == truckId)
                        {
                            // Next destination for this truck
                            destinationCity = GetCity(cityIndex + 1);
                            break;
                        }
                    }
                    if (fromCity.CityName == destinationCity.CityName)
                    {
                        break;
                    }

                    int    actDist    = (int)fromCity.DistanceTo(destinationCity);
                    double prevWeight = GetAllPreviousWeight(cityIndex);

                    //fuel consumption
                    //truckTourDistance += (actDist / 100) * GetMaxFuel() *
                    //(1 + ((GetTourWeight() - prevWeight) / GetTruckLoad())) * (1 / GetWeight(cityIndex));

                    //distance
                    truckTourDistance += actDist / 1000;
                }
            }
            int singleDistance = (int)truckTourDistance;

            return(singleDistance);
        }
Example #8
0
 public void SetCity(int cityIndex, AlgCity city)
 {
     tour[cityIndex] = city;
     fitness         = 0;
     distance        = 0;
 }