Free cookie consent management tool by TermsFeed Policy Generator

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

Fixed some Problems with the Alba Encoding (#1177)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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  }
Note: See TracChangeset for help on using the changeset viewer.