Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5201


Ignore:
Timestamp:
01/03/11 16:18:37 (14 years ago)
Author:
svonolfe
Message:

Fixed some Problems with the Alba Encoding (#1177)

Location:
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/AlbaEncoding.cs

    r4752 r5201  
    102102      route.RemoveAt(routeParam.Count - 1);
    103103     
    104       int cities = 0;
    105       for (int i = 0; i < route.Count; i++) {
    106         if (route[i] != 0) {
    107           cities++;
    108         }
    109       }
     104      int cities = instance.Cities.Value;
    110105
    111106      int vehicle = cities;
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Creators/PushForwardInsertionCreator.cs

    r4752 r5201  
    3030using HeuristicLab.Problems.VehicleRouting.Variants;
    3131using HeuristicLab.Common;
     32using HeuristicLab.Problems.VehicleRouting.Interfaces;
    3233
    3334namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
     
    8384
    8485    // use the Box-Mueller transform in the polar form to generate a N(0,1) random variable out of two uniformly distributed random variables
    85     private double Gauss(IRandom random) {
     86    private static double Gauss(IRandom random) {
    8687      double u = 0.0, v = 0.0, s = 0.0;
    8788      do {
     
    9394    }
    9495
    95     private double N(double mu, double sigma, IRandom random) {
     96    private static double N(double mu, double sigma, IRandom random) {
    9697      return mu + (sigma * Gauss(random)); // transform the random variable sampled from N(0,1) to N(mu,sigma)
    9798    }
    9899
    99     private double Distance(int start, int end) {
    100       return ProblemInstance.GetDistance(start, end);
    101     }
    102 
    103     private double TravelDistance(List<int> route, int begin) {
     100    private static double Distance(IVRPProblemInstance problemInstance, int start, int end) {
     101      return problemInstance.GetDistance(start, end);
     102    }
     103
     104    private static double TravelDistance(IVRPProblemInstance problemInstance, List<int> route, int begin) {
    104105      double distance = 0;
    105106      for (int i = begin; i < route.Count - 1 && (i == begin || route[i] != 0); i++) {
    106         distance += Distance(route[i], route[i + 1]);
     107        distance += Distance(problemInstance, route[i], route[i + 1]);
    107108      }
    108109      return distance;
    109110    }
    110111
    111     private bool SubrouteConstraintsOK(List<int> route, int begin) {     
    112       Tour subroute = new Tour();
    113       subroute.Stops.AddRange(route.GetRange(begin,
    114           route.Count - begin));
    115 
    116       return ProblemInstance.Feasible(subroute);
    117     }
    118 
    119     protected override List<int> CreateSolution() {
     112    private static bool SubrouteConstraintsOK(IVRPProblemInstance problemInstance, List<int> route, int begin) {
     113      AlbaEncoding solution = AlbaEncoding.ConvertFrom(route, problemInstance);
     114
     115      return problemInstance.Feasible(solution);
     116    }
     117
     118    public static List<int> CreateSolution(IVRPProblemInstance problemInstance, IRandom random,
     119      double alphaValue = 0.7, double betaValue = 0.1, double gammaValue = 0.2,
     120      double alphaVariance = 0.5, double betaVariance = 0.07, double gammaVariance = 0.14) {
    120121      double alpha, beta, gamma;
    121       alpha = N(Alpha.Value.Value, Math.Sqrt(AlphaVariance.Value.Value), RandomParameter.ActualValue);
    122       beta = N(Beta.Value.Value, Math.Sqrt(BetaVariance.Value.Value), RandomParameter.ActualValue);
    123       gamma = N(Gamma.Value.Value, Math.Sqrt(GammaVariance.Value.Value), RandomParameter.ActualValue);
    124 
    125       double x0 = ProblemInstance.Coordinates[0, 0];
    126       double y0 = ProblemInstance.Coordinates[0, 1];
     122      alpha = N(alphaValue, Math.Sqrt(alphaVariance), random);
     123      beta = N(betaValue, Math.Sqrt(betaVariance), random);
     124      gamma = N(gammaValue, Math.Sqrt(gammaVariance), random);
     125
     126      double x0 = problemInstance.Coordinates[0, 0];
     127      double y0 = problemInstance.Coordinates[0, 1];
    127128      double distance = 0;
    128129      double cost = 0;
     
    138139       *-----------------------------------------------------------------------------
    139140       */
    140       for (int i = 1; i <= ProblemInstance.Cities.Value; i++) {
    141         distance = Distance(i, 0);
    142         if (ProblemInstance.Coordinates[i, 0] < x0) distance = -distance;
     141      for (int i = 1; i <= problemInstance.Cities.Value; i++) {
     142        distance = Distance(problemInstance, i, 0);
     143        if (problemInstance.Coordinates[i, 0] < x0) distance = -distance;
    143144
    144145        cost = -alpha * distance + // distance 0 <-> City[i]
    145                beta * (ProblemInstance as ITimeWindowedProblemInstance).DueTime[i] + // latest arrival time
    146                gamma * (Math.Asin((ProblemInstance.Coordinates[i, 1] - y0) / distance) / 360 * distance); // polar angle
     146               beta * (problemInstance as ITimeWindowedProblemInstance).DueTime[i] + // latest arrival time
     147               gamma * (Math.Asin((problemInstance.Coordinates[i, 1] - y0) / distance) / 360 * distance); // polar angle
    147148
    148149        index = 0;
     
    161162      int customer = -1;
    162163      int subTourCount = 1;
    163       List<int> route = new List<int>(ProblemInstance.Cities.Value + ProblemInstance.Vehicles.Value - 1);
     164      List<int> route = new List<int>(problemInstance.Cities.Value + problemInstance.Vehicles.Value - 1);
    164165      minimumCost = double.MaxValue;
    165166      indexOfMinimumCost = -1;
     
    176177            route.Insert(i, (int)unroutedList[c]);
    177178            if (route[currentRoute] != 0) { throw new Exception("currentRoute not depot"); }
    178             cost = TravelDistance(route, currentRoute);
    179             if (cost < minimumCost && SubrouteConstraintsOK(route, currentRoute)) {
     179            cost = TravelDistance(problemInstance, route, currentRoute);
     180            if (cost < minimumCost && SubrouteConstraintsOK(problemInstance, route, currentRoute)) {
    180181              minimumCost = cost;
    181182              indexOfMinimumCost = i;
     
    207208        customer = -1;
    208209      } while (unroutedList.Count > 0);
    209       while (route.Count < ProblemInstance.Cities.Value + ProblemInstance.Vehicles.Value)
     210      while (route.Count < problemInstance.Cities.Value + problemInstance.Vehicles.Value)
    210211        route.Add(0);
    211212
    212213      return route;
     214    }
     215
     216    protected override List<int> CreateSolution() {
     217      return CreateSolution(ProblemInstance, RandomParameter.ActualValue,
     218        Alpha.Value.Value, Beta.Value.Value, Gamma.Value.Value,
     219        AlphaVariance.Value.Value, BetaVariance.Value.Value, GammaVariance.Value.Value);
    213220    }
    214221  }
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Moves/LambdaInterchange/AlbaStochasticLambdaInterchangeSingleMoveGenerator.cs

    r4752 r5201  
    6767      List<Tour> tours = individual.GetTours();
    6868
    69       int route1Index = rand.Next(tours.Count);
    70       Tour route1 = tours[route1Index];
     69      if (tours.Count > 1) {
     70        int route1Index = rand.Next(tours.Count);
     71        Tour route1 = tours[route1Index];
    7172
    72       int route2Index = rand.Next(tours.Count - 1);
    73       if (route2Index >= route1Index)
    74         route2Index += 1;
    75       Tour route2 = tours[route2Index];
     73        int route2Index = rand.Next(tours.Count - 1);
     74        if (route2Index >= route1Index)
     75          route2Index += 1;
    7676
    77       int length1 = rand.Next(Math.Min(lambda + 1, route1.Stops.Count + 1));
    78       int index1 = rand.Next(route1.Stops.Count - length1 + 1);
     77        Tour route2 = tours[route2Index];
    7978
    80       int l2Min = 0;
    81       if (length1 == 0)
    82         l2Min = 1;
    83       int length2 = rand.Next(l2Min, Math.Min(lambda + 1, route2.Stops.Count + 1));
    84       int index2 = rand.Next(route2.Stops.Count - length2 + 1);
     79        int length1 = rand.Next(Math.Min(lambda + 1, route1.Stops.Count + 1));
     80        int index1 = rand.Next(route1.Stops.Count - length1 + 1);
    8581
    86       return new AlbaLambdaInterchangeMove(route1Index, index1, length1, route2Index, index2, length2, individual);
     82        int l2Min = 0;
     83        if (length1 == 0)
     84          l2Min = 1;
     85        int length2 = rand.Next(l2Min, Math.Min(lambda + 1, route2.Stops.Count + 1));
     86        int index2 = rand.Next(route2.Stops.Count - length2 + 1);
     87
     88        return new AlbaLambdaInterchangeMove(route1Index, index1, length1, route2Index, index2, length2, individual);
     89      } else {
     90        return new AlbaLambdaInterchangeMove(0, 0, 0, 0, 0, 0, individual);
     91      }
    8792    }
    8893
Note: See TracChangeset for help on using the changeset viewer.