本文整理汇总了C#中IVRPProblemInstance.EvaluateTour方法的典型用法代码示例。如果您正苦于以下问题:C# IVRPProblemInstance.EvaluateTour方法的具体用法?C# IVRPProblemInstance.EvaluateTour怎么用?C# IVRPProblemInstance.EvaluateTour使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IVRPProblemInstance
的用法示例。
在下文中一共展示了IVRPProblemInstance.EvaluateTour方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateSolution
public static PotvinEncoding CreateSolution(IVRPProblemInstance instance, IRandom random, bool adhereTimeWindows) {
PotvinEncoding result = new PotvinEncoding(instance);
IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
List<int> customers = new List<int>();
for (int i = 1; i <= instance.Cities.Value; i++)
if (pdp == null || pdp.GetDemand(i) >= 0)
customers.Add(i);
customers.Sort((city1, city2) => {
double angle1 = CalculateAngleToDepot(instance, city1);
double angle2 = CalculateAngleToDepot(instance, city2);
return angle1.CompareTo(angle2);
});
Tour currentTour = new Tour();
result.Tours.Add(currentTour);
int j = random.Next(customers.Count);
for (int i = 0; i < customers.Count; i++) {
int index = (i + j) % customers.Count;
int stopIdx = 0;
if (currentTour.Stops.Count > 0)
stopIdx = result.FindBestInsertionPlace(currentTour, customers[index]);
currentTour.Stops.Insert(stopIdx, customers[index]);
if (pdp != null) {
stopIdx = result.FindBestInsertionPlace(currentTour, pdp.GetPickupDeliveryLocation(customers[index]));
currentTour.Stops.Insert(stopIdx, pdp.GetPickupDeliveryLocation(customers[index]));
}
CVRPEvaluation evaluation = instance.EvaluateTour(currentTour, result) as CVRPEvaluation;
if (result.Tours.Count < instance.Vehicles.Value &&
((adhereTimeWindows && !instance.Feasible(evaluation)) || ((!adhereTimeWindows) && evaluation.Overload > double.Epsilon))) {
currentTour.Stops.Remove(customers[index]);
if (pdp != null)
currentTour.Stops.Remove(pdp.GetPickupDeliveryLocation(customers[index]));
if (currentTour.Stops.Count == 0)
result.Tours.Remove(currentTour);
currentTour = new Tour();
result.Tours.Add(currentTour);
currentTour.Stops.Add(customers[index]);
if (pdp != null) {
currentTour.Stops.Add(pdp.GetPickupDeliveryLocation(customers[index]));
}
}
}
if (currentTour.Stops.Count == 0)
result.Tours.Remove(currentTour);
return result;
}
示例2: CreateSolution
public static PotvinEncoding CreateSolution(IVRPProblemInstance problemInstance, IRandom random,
double alphaValue = 0.7, double betaValue = 0.1, double gammaValue = 0.2,
double alphaVariance = 0.5, double betaVariance = 0.07, double gammaVariance = 0.14) {
PotvinEncoding result = new PotvinEncoding(problemInstance);
IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
IMultiDepotProblemInstance mdp = problemInstance as IMultiDepotProblemInstance;
double alpha, beta, gamma;
alpha = N(alphaValue, Math.Sqrt(alphaVariance), random);
beta = N(betaValue, Math.Sqrt(betaVariance), random);
gamma = N(gammaValue, Math.Sqrt(gammaVariance), random);
List<int> unroutedCustomers = new List<int>();
for (int i = 1; i <= problemInstance.Cities.Value; i++) {
if (pdp == null || (problemInstance.GetDemand(i) >= 0))
unroutedCustomers.Add(i);
}
List<int> depots = new List<int>();
if (mdp != null) {
for (int i = 0; i < mdp.Depots.Value; i++) {
depots.Add(i);
}
} else {
depots.Add(0);
}
Dictionary<int, List<int>> vehicles = new Dictionary<int, List<int>>();
foreach (int depot in depots) {
vehicles[depot] = new List<int>();
int vehicleCount = problemInstance.Vehicles.Value;
if (mdp != null) {
for (int vehicle = 0; vehicle < mdp.VehicleDepotAssignment.Length; vehicle++) {
if (mdp.VehicleDepotAssignment[vehicle] == depot) {
vehicles[depot].Add(vehicle);
}
}
} else {
for (int vehicle = 0; vehicle < vehicleCount; vehicle++) {
vehicles[depot].Add(vehicle);
}
}
}
RemoveUnusedDepots(depots, vehicles);
Dictionary<int, int> depotAssignment = new Dictionary<int, int>();
unroutedCustomers = SortCustomers(
problemInstance, unroutedCustomers, depots, depotAssignment,
alpha, beta, gamma);
/////////
Tour tour = new Tour();
result.Tours.Add(tour);
int currentCustomer = unroutedCustomers[0];
unroutedCustomers.RemoveAt(0);
int currentDepot = depotAssignment[currentCustomer];
int currentVehicle = vehicles[currentDepot][0];
vehicles[currentDepot].RemoveAt(0);
if (RemoveUnusedDepots(depots, vehicles)) {
unroutedCustomers = SortCustomers(
problemInstance, unroutedCustomers, depots, depotAssignment,
alpha, beta, gamma);
}
result.VehicleAssignment[result.Tours.Count - 1] = currentVehicle;
tour.Stops.Add(currentCustomer);
if (pdp != null) {
tour.Stops.Add(pdp.GetPickupDeliveryLocation(currentCustomer));
}
////////
while (unroutedCustomers.Count > 0) {
double minimumCost = double.MaxValue;
int customer = -1;
int indexOfMinimumCost = -1;
int indexOfMinimumCost2 = -1;
foreach (int unrouted in unroutedCustomers) {
VRPEvaluation eval = problemInstance.EvaluateTour(tour, result);
double originalCosts = eval.Quality;
for (int i = 0; i <= tour.Stops.Count; i++) {
tour.Stops.Insert(i, unrouted);
eval = problemInstance.EvaluateTour(tour, result);
double tourCost = eval.Quality - originalCosts;
if (pdp != null) {
for (int j = i + 1; j <= tour.Stops.Count; j++) {
bool feasible;
double cost = tourCost +
problemInstance.GetInsertionCosts(eval, result, pdp.GetPickupDeliveryLocation(unrouted), 0, j, out feasible);
if (cost < minimumCost && feasible) {
customer = unrouted;
minimumCost = cost;
indexOfMinimumCost = i;
//.........这里部分代码省略.........
示例3: Apply
public static void Apply(PotvinEncoding solution, PotvinPDShiftMove move, IVRPProblemInstance problemInstance) {
bool newTour = false;
if (move.Tour >= solution.Tours.Count) {
solution.Tours.Add(new Tour());
newTour = true;
}
Tour tour = solution.Tours[move.Tour];
Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));
oldTour.Stops.Remove(move.City);
if (problemInstance is IPickupAndDeliveryProblemInstance) {
IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
int location = pdp.GetPickupDeliveryLocation(move.City);
Tour oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));
oldTour2.Stops.Remove(location);
solution.InsertPair(tour, move.City, location, problemInstance);
} else {
int place = solution.FindBestInsertionPlace(tour, move.City);
tour.Stops.Insert(place, move.City);
}
if (newTour) {
List<int> vehicles = new List<int>();
for (int i = move.Tour; i < problemInstance.Vehicles.Value; i++) {
vehicles.Add(solution.GetVehicleAssignment(i));
}
double bestQuality = double.MaxValue;
int bestVehicle = -1;
int originalVehicle = solution.GetVehicleAssignment(move.Tour);
foreach (int vehicle in vehicles) {
solution.VehicleAssignment[move.Tour] = vehicle;
double quality = problemInstance.EvaluateTour(tour, solution).Quality;
if (quality < bestQuality) {
bestQuality = quality;
bestVehicle = vehicle;
}
}
solution.VehicleAssignment[move.Tour] = originalVehicle;
int index = -1;
for (int i = move.Tour; i < solution.VehicleAssignment.Length; i++) {
if (solution.VehicleAssignment[i] == bestVehicle) {
index = i;
break;
}
}
solution.VehicleAssignment[index] = originalVehicle;
solution.VehicleAssignment[move.Tour] = bestVehicle;
}
solution.Repair();
}
示例4: InsertPair
public void InsertPair(Tour tour, int source, int target, IVRPProblemInstance problemInstance, int positionToAvoid = -1, int positionToAvoid2 = -1) {
int stops = tour.Stops.Count;
VRPEvaluation eval = problemInstance.EvaluateTour(tour, this);
double minCosts = double.MaxValue;
int sourceLocation = -1;
int targetLocation = -1;
for (int i = 0; i <= stops; i++) {
tour.Stops.Insert(i, source);
VRPEvaluation tourEval = problemInstance.EvaluateTour(tour, this);
double sourceCosts = tourEval.Quality - eval.Quality;
for (int j = i + 1; j <= stops + 1; j++) {
if (positionToAvoid != i || positionToAvoid2 != j || stops == 0) {
bool feasible;
double targetCosts = problemInstance.GetInsertionCosts(tourEval, this, target, 0, j, out feasible);
double costs = sourceCosts + targetCosts;
if (costs < minCosts) {
minCosts = costs;
sourceLocation = i;
targetLocation = j;
}
}
}
tour.Stops.Remove(source);
}
tour.Stops.Insert(sourceLocation, source);
tour.Stops.Insert(targetLocation, target);
}
示例5: ReplacePair
private static PotvinEncoding ReplacePair(PotvinEncoding individual, IVRPProblemInstance instance, int replaced, int replacing, bool allowInfeasible) {
individual = individual.Clone() as PotvinEncoding;
IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
int replacedDest = pdp.GetPickupDeliveryLocation(replaced);
int replacedSource, replacedTarget;
if (pdp.GetDemand(replaced) >= 0) {
replacedSource = replaced;
replacedTarget = replacedDest;
} else {
replacedSource = replacedDest;
replacedTarget = replaced;
}
Tour replacedSourceTour = individual.Tours.Find(t => t.Stops.Contains(replacedSource));
Tour replacedTargetTour = individual.Tours.Find(t => t.Stops.Contains(replacedTarget));
int replacingDest = pdp.GetPickupDeliveryLocation(replacing);
int replacingSource, replacingTarget;
if (pdp.GetDemand(replacing) >= 0) {
replacingSource = replacing;
replacingTarget = replacingDest;
} else {
replacingSource = replacingDest;
replacingTarget = replacing;
}
Tour replacingSourceTour = individual.Tours.Find(t => t.Stops.Contains(replacingSource));
Tour replacingTargetTour = individual.Tours.Find(t => t.Stops.Contains(replacingTarget));
replacingSourceTour.Stops.Remove(replacingSource);
replacingTargetTour.Stops.Remove(replacingTarget);
replacedSourceTour.Stops[replacedSourceTour.Stops.IndexOf(replacedSource)] = replacingSource;
if (!allowInfeasible && !instance.TourFeasible(replacedSourceTour, individual))
return null;
replacedTargetTour.Stops[replacedTargetTour.Stops.IndexOf(replacedTarget)] = replacingTarget;
if (!allowInfeasible && !instance.TourFeasible(replacedTargetTour, individual))
return null;
double bestQuality = double.MaxValue;
int bestTour = -1;
int bestPositionSource = -1;
int bestPositionTarget = -1;
int routeToAvoid = individual.Tours.IndexOf(replacingSourceTour);
for (int tourIdx = 0; tourIdx < individual.Tours.Count; tourIdx++) {
if (tourIdx != routeToAvoid) {
Tour tour = individual.Tours[tourIdx];
VRPEvaluation eval = instance.EvaluateTour(tour, individual);
individual.InsertPair(tour, replacedSource, replacedTarget, instance);
VRPEvaluation evalNew = instance.EvaluateTour(tour, individual);
double delta = evalNew.Quality - eval.Quality;
if (delta < bestQuality &&
(instance.Feasible(evalNew) || allowInfeasible)) {
bestQuality = delta;
bestTour = tourIdx;
bestPositionSource = tour.Stops.IndexOf(replacedSource);
bestPositionTarget = tour.Stops.IndexOf(replacedTarget);
}
tour.Stops.Remove(replacedSource);
tour.Stops.Remove(replacedTarget);
}
}
if (bestTour != -1) {
if (bestPositionTarget < bestPositionSource) {
individual.Tours[bestTour].Stops.Insert(bestPositionTarget, replacedTarget);
individual.Tours[bestTour].Stops.Insert(bestPositionSource, replacedSource);
} else {
individual.Tours[bestTour].Stops.Insert(bestPositionSource, replacedSource);
individual.Tours[bestTour].Stops.Insert(bestPositionTarget, replacedTarget);
}
return individual;
} else {
return null;
}
}
示例6: PairwiseMove
public static bool PairwiseMove(PotvinEncoding individual, IVRPProblemInstance instance, int city, bool allowInfeasible) {
bool success;
IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
if (pdp != null) {
Tour route1 = individual.Tours.Find(t => t.Stops.Contains(city));
int i = route1.Stops.IndexOf(city);
int dest = pdp.GetPickupDeliveryLocation(city);
Tour destRoute = individual.Tours.Find(t => t.Stops.Contains(dest));
int j = destRoute.Stops.IndexOf(dest);
route1.Stops.Remove(city);
destRoute.Stops.Remove(dest);
int routeToAvoid = -1;
if (route1 == destRoute)
routeToAvoid = individual.Tours.IndexOf(route1);
int source, target;
if (instance.GetDemand(city) >= 0) {
source = city;
target = dest;
} else {
source = dest;
target = city;
}
double bestQuality = double.MaxValue;
int bestTour = -1;
int bestPositionSource = -1;
int bestPositionTarget = -1;
for (int tourIdx = 0; tourIdx < individual.Tours.Count; tourIdx++) {
if (tourIdx != routeToAvoid) {
Tour tour = individual.Tours[tourIdx];
VRPEvaluation eval = instance.EvaluateTour(tour, individual);
individual.InsertPair(tour, source, target, instance);
VRPEvaluation evalNew = instance.EvaluateTour(tour, individual);
double delta = evalNew.Quality - eval.Quality;
if (delta < bestQuality &&
(instance.Feasible(evalNew) || allowInfeasible)) {
bestQuality = delta;
bestTour = tourIdx;
bestPositionSource = tour.Stops.IndexOf(source);
bestPositionTarget = tour.Stops.IndexOf(target);
}
tour.Stops.Remove(source);
tour.Stops.Remove(target);
}
}
if (bestTour >= 0) {
if (bestPositionTarget < bestPositionSource) {
individual.Tours[bestTour].Stops.Insert(bestPositionTarget, target);
individual.Tours[bestTour].Stops.Insert(bestPositionSource, source);
} else {
individual.Tours[bestTour].Stops.Insert(bestPositionSource, source);
individual.Tours[bestTour].Stops.Insert(bestPositionTarget, target);
}
success = true;
} else {
if (j < i) {
destRoute.Stops.Insert(j, dest);
route1.Stops.Insert(i, city);
} else {
route1.Stops.Insert(i, city);
destRoute.Stops.Insert(j, dest);
}
success = false;
}
} else {
success = false;
}
return success;
}