本文整理汇总了C#中IVRPProblemInstance.Feasible方法的典型用法代码示例。如果您正苦于以下问题:C# IVRPProblemInstance.Feasible方法的具体用法?C# IVRPProblemInstance.Feasible怎么用?C# IVRPProblemInstance.Feasible使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IVRPProblemInstance
的用法示例。
在下文中一共展示了IVRPProblemInstance.Feasible方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
//.........这里部分代码省略.........
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;
indexOfMinimumCost2 = j;
}
}
} else {
double cost = tourCost;
bool feasible = problemInstance.Feasible(eval);
if (cost < minimumCost && feasible) {
customer = unrouted;
minimumCost = cost;
indexOfMinimumCost = i;
}
}
tour.Stops.RemoveAt(i);
}
}
if (indexOfMinimumCost == -1 && vehicles.Count == 0) {
indexOfMinimumCost = tour.Stops.Count;
indexOfMinimumCost2 = tour.Stops.Count + 1;
customer = unroutedCustomers[0];
}
// insert customer if found
if (indexOfMinimumCost != -1) {
tour.Stops.Insert(indexOfMinimumCost, customer);
if (pdp != null) {
tour.Stops.Insert(indexOfMinimumCost2, pdp.GetPickupDeliveryLocation(customer));
}
unroutedCustomers.Remove(customer);
} else { // no feasible customer found
tour = new Tour();
result.Tours.Add(tour);
currentCustomer = unroutedCustomers[0];
unroutedCustomers.RemoveAt(0);
currentDepot = depotAssignment[currentCustomer];
示例3: ApplyManipulation
public static void ApplyManipulation(IRandom random, PotvinEncoding individual, IVRPProblemInstance instance, int maxIterations) {
//only apply to feasible individuals
if (instance.Feasible(individual)) {
bool insertionFound;
int iterations = 0;
do {
insertionFound = false;
int length = 3;
while (length > 0 && !insertionFound) {
int tour = 0;
while (tour < individual.Tours.Count && !insertionFound) {
int city = 0;
while (city <= individual.Tours[tour].Stops.Count - length && !insertionFound) {
int insertionTour, insertionPlace;
if (FindBetterInsertionPlace(individual, instance, tour, city, length,
out insertionTour, out insertionPlace)) {
insertionFound = true;
List<int> toBeInserted = individual.Tours[tour].Stops.GetRange(city, length);
individual.Tours[tour].Stops.RemoveRange(city, length);
individual.Tours[insertionTour].Stops.InsertRange(
insertionPlace,
toBeInserted);
}
city++;
}
tour++;
}
length--;
}
iterations++;
} while (insertionFound &&
iterations < maxIterations);
IList<Tour> toBeRemoved = new List<Tour>();
foreach (Tour tour in individual.Tours) {
if (tour.Stops.Count == 0)
toBeRemoved.Add(tour);
}
foreach (Tour tour in toBeRemoved) {
individual.Tours.Remove(tour);
}
}
}
示例4: 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;
}
}
示例5: 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;
}