本文整理汇总了C#中IRoute.InsertAfter方法的典型用法代码示例。如果您正苦于以下问题:C# IRoute.InsertAfter方法的具体用法?C# IRoute.InsertAfter怎么用?C# IRoute.InsertAfter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IRoute
的用法示例。
在下文中一共展示了IRoute.InsertAfter方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InsertOne
/// <summary>
/// Re-inserts a customer in the route.
/// </summary>
/// <param name="weights"></param>
/// <param name="route"></param>
/// <param name="customer"></param>
/// <param name="difference"></param>
/// <returns></returns>
public static bool InsertOne(IProblemWeights weights, IRoute route, int customer,
out double difference)
{
// calculate placement.
CheapestInsertionResult result =
CheapestInsertionHelper.CalculateBestPlacement(weights, route, customer);
// place the customer.
if (result.CustomerAfter >= 0 && result.CustomerBefore >= 0)
{
//route.InsertAfterAndRemove(result.CustomerBefore, result.Customer, result.CustomerAfter);
route.InsertAfter(result.CustomerBefore, result.Customer);
difference =
-(weights.WeightMatrix[result.CustomerBefore][result.CustomerAfter]) +
(weights.WeightMatrix[result.CustomerBefore][result.Customer]) +
(weights.WeightMatrix[result.Customer][result.CustomerAfter]);
return true;
}
difference = 0;
return false;
}
示例2: CalculateRePlaceOptHelper
/// <summary>
/// Applies a local search strategy.
/// </summary>
/// <param name="problem"></param>
/// <param name="route"></param>
/// <returns></returns>
public static void CalculateRePlaceOptHelper(
IProblemWeights problem, IRoute route)
{
//bool improvement = true;
//while (improvement)
//{
//// reset improvement flag.
//improvement = false;
// try re-placement of one customer.
int before = -1;
int after = -1;
int found_customer = -1;
// loop over all customers and try to place it better.
HashSet<int> customers_to_place = new HashSet<int>(route);
foreach (int customer_to_place in customers_to_place)
{
// find the best place.
double current_cost = -1;
double other_cost = double.MaxValue;
int previous_before = -1;
int previous = -1;
foreach (int customer in route)
{
if (previous >= 0 && previous_before >= 0)
{
if (previous == customer_to_place)
{
current_cost = problem.Weight(previous_before, previous) +
problem.Weight(previous, customer);
}
else if (previous_before != customer_to_place &&
customer != customer_to_place)
{ // calculate the cost.
double cost = problem.Weight(previous_before, customer_to_place) +
problem.Weight(customer_to_place, previous);
if (cost < other_cost)
{
other_cost = cost;
before = previous_before;
after = previous;
found_customer = customer;
}
}
}
previous_before = previous;
previous = customer;
}
// determine if the cost is better.
if (current_cost > other_cost)
{ // the current cost is better.
route.Remove(found_customer);
//route.InsertAfterAndRemove(before, found_customer, after);
route.InsertAfter(before, found_customer);
break;
}
else
{ // current cost is not better.
before = -1;
after = -1;
found_customer = -1;
}
}
//if (found_customer >= 0)
//{ // a found customer.
// improvement = true;
//}
//}
}