本文整理汇总了C#中Problem.Weight方法的典型用法代码示例。如果您正苦于以下问题:C# Problem.Weight方法的具体用法?C# Problem.Weight怎么用?C# Problem.Weight使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Problem
的用法示例。
在下文中一共展示了Problem.Weight方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CalculateBestPlacementInGenome
/// <summary>
/// Calculates the best place to insert a city.
/// </summary>
/// <param name="problem"></param>
/// <param name="calculator"></param>
/// <param name="genome"></param>
/// <param name="city_to_place"></param>
/// <returns></returns>
public static BestPlacementResult CalculateBestPlacementInGenome(
Problem problem,
FitnessCalculator calculator,
Genome genome,
int city_to_place)
{
// initialize the best placement result.
BestPlacementResult result
= new BestPlacementResult();
result.RoundIdx = -1;
result.City = city_to_place;
// initialize the best increase.
double increase = 0;
if (genome.Count > 0)
{
if (genome.Count == 1)
{ // when there is only on city in the round, try placing after and calculate again.
// calculate the increase.
increase = problem.Weight(genome[0], city_to_place)
+ (problem.Weight(city_to_place, genome[0]));
// set the result city.
result.CityIdx = 1;
}
else
{ // when there are multiple cities try to place in all position and keep the best.
for (int idx = 0; idx < genome.Count - 1; idx++)
{
// calculate the new weights.
double new_weights =
problem.Weight(genome[idx], city_to_place)
+ (problem.Weight(city_to_place, genome[idx + 1]));
// calculate the old weights.
double old_weight =
problem.Weight(genome[idx], genome[idx + 1]);
// calculate the difference to know the increase.
double new_increase =
new_weights - old_weight;
//if (increase == null || new_increase < increase)
if (new_increase < increase)
{
// set the new increase.
increase = new_increase;
// set the result city.
result.CityIdx = 1;
}
}
}
}
else
{
throw new Exception("Cannot do best placement on an empty round!");
}
// calculate the fitness.
result.Increase = increase;
// return result.
return result;
}