Changeset 5201
- Timestamp:
- 01/03/11 16:18:37 (14 years ago)
- 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 102 102 route.RemoveAt(routeParam.Count - 1); 103 103 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; 110 105 111 106 int vehicle = cities; -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Creators/PushForwardInsertionCreator.cs
r4752 r5201 30 30 using HeuristicLab.Problems.VehicleRouting.Variants; 31 31 using HeuristicLab.Common; 32 using HeuristicLab.Problems.VehicleRouting.Interfaces; 32 33 33 34 namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba { … … 83 84 84 85 // 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) { 86 87 double u = 0.0, v = 0.0, s = 0.0; 87 88 do { … … 93 94 } 94 95 95 private double N(double mu, double sigma, IRandom random) {96 private static double N(double mu, double sigma, IRandom random) { 96 97 return mu + (sigma * Gauss(random)); // transform the random variable sampled from N(0,1) to N(mu,sigma) 97 98 } 98 99 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) { 104 105 double distance = 0; 105 106 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]); 107 108 } 108 109 return distance; 109 110 } 110 111 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) { 120 121 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]; 127 128 double distance = 0; 128 129 double cost = 0; … … 138 139 *----------------------------------------------------------------------------- 139 140 */ 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; 143 144 144 145 cost = -alpha * distance + // distance 0 <-> City[i] 145 beta * ( ProblemInstance as ITimeWindowedProblemInstance).DueTime[i] + // latest arrival time146 gamma * (Math.Asin(( ProblemInstance.Coordinates[i, 1] - y0) / distance) / 360 * distance); // polar angle146 beta * (problemInstance as ITimeWindowedProblemInstance).DueTime[i] + // latest arrival time 147 gamma * (Math.Asin((problemInstance.Coordinates[i, 1] - y0) / distance) / 360 * distance); // polar angle 147 148 148 149 index = 0; … … 161 162 int customer = -1; 162 163 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); 164 165 minimumCost = double.MaxValue; 165 166 indexOfMinimumCost = -1; … … 176 177 route.Insert(i, (int)unroutedList[c]); 177 178 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)) { 180 181 minimumCost = cost; 181 182 indexOfMinimumCost = i; … … 207 208 customer = -1; 208 209 } while (unroutedList.Count > 0); 209 while (route.Count < ProblemInstance.Cities.Value + ProblemInstance.Vehicles.Value)210 while (route.Count < problemInstance.Cities.Value + problemInstance.Vehicles.Value) 210 211 route.Add(0); 211 212 212 213 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); 213 220 } 214 221 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Moves/LambdaInterchange/AlbaStochasticLambdaInterchangeSingleMoveGenerator.cs
r4752 r5201 67 67 List<Tour> tours = individual.GetTours(); 68 68 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]; 71 72 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; 76 76 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]; 79 78 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); 85 81 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 } 87 92 } 88 93
Note: See TracChangeset
for help on using the changeset viewer.