本文整理汇总了C#中Route.Clone方法的典型用法代码示例。如果您正苦于以下问题:C# Route.Clone方法的具体用法?C# Route.Clone怎么用?C# Route.Clone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Route
的用法示例。
在下文中一共展示了Route.Clone方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MeasureTheDistance
public virtual int MeasureTheDistance(List<Track> railRoad, Route route)
{
var traveledDistance = 0;
var routeToBeMeasured = (Route) route.Clone();
while (routeToBeMeasured.Cities.Count > 1)
{
EvaluateOriginAndDestination(routeToBeMeasured);
var connection = GetTheNextTrack(railRoad);
traveledDistance += connection.Distance;
}
return traveledDistance;
}
示例2: FindNumberOfPossibleRoutes
private void FindNumberOfPossibleRoutes(List<Track> railRoad, City origin, Route runningRoute)
{
var possibleTracks = railRoad.FindAll(track => track.Origin.Equals(origin));
foreach (var track in possibleTracks)
{
var route = (Route)runningRoute.Clone();
route.AppendCity(track.Destination);
if (ThisRouteMatchWithTheRequisites(route))
possibleRoutesCounter++;
if (ShouldKeepRunningInThisRoute(route))
FindNumberOfPossibleRoutes(railRoad, track.Destination, route);
}
}
示例3: Crossover
/// <summary>
/// Applies crossover to 2 parents to create a child.
/// </summary>
/// <param name="first">
/// The first parent of the crossover.
/// </param>
/// <param name="second">
/// The second parent of the crossover.
/// </param>
/// <returns>
/// If the operation is successful then the result is returned, otherwise null.
/// </returns>
public Critter[] Crossover(Critter first, Critter second)
{
var random = Random.GetInstance();
List<NodeWrapper<INetworkNode>> firstNodes = first.Route;
List<NodeWrapper<INetworkNode>> secondNodes = second.Route;
var crossoverPoints = new List<KeyValuePair<int, int>>();
for (int i = 0; i < firstNodes.Count; i++)
{
for (int j = 0; j < secondNodes.Count; j++)
{
if (firstNodes[i].Node.Equals(secondNodes[j].Node))
{
crossoverPoints.Add(new KeyValuePair<int, int>(i, j));
break;
}
}
}
if (crossoverPoints.Count == 0)
{
// throw new Exception("StandardBreeder.cs: The crossover points are undefined.");
// crossoverPoints.Add(new KeyValuePair<int, int>(random.Next(firstNodes.Count - 1), random.Next(secondNodes.Count - 1)));
return null;
}
var firstChild = new Route(-1);
var secondChild = new Route(-1);
KeyValuePair<int, int> crossoverPoint = crossoverPoints[random.Next(crossoverPoints.Count - 1)];
firstChild.AddRange(firstNodes.GetRange(0, crossoverPoint.Key));
firstChild.AddRange(secondNodes.GetRange(crossoverPoint.Value, secondNodes.Count - crossoverPoint.Value));
secondChild.AddRange(secondNodes.GetRange(0, crossoverPoint.Value));
secondChild.AddRange(firstNodes.GetRange(crossoverPoint.Key, firstNodes.Count - crossoverPoint.Key));
var output = new[]
{
new Critter((Route)firstChild.Clone(), new Fitness()),
new Critter((Route)secondChild.Clone(), new Fitness())
};
output[0].DepartureTime = second.DepartureTime;
output[1].DepartureTime = first.DepartureTime;
Assert.That(output[0].DepartureTime != default(DateTime));
Assert.That(output[1].DepartureTime != default(DateTime));
if (output == null || output[0] == null || output[1] == null)
{
throw new Exception("StandardBreeder.cs: One or more decendants of crossover are null.");
}
return output;
}
示例4: Mutate
/// <summary>
/// Mutates a critter
/// </summary>
/// <param name="child">
/// The critter that is to be mutated.
/// </param>
/// <returns>
/// A mutated critter.
/// </returns>
public Critter Mutate(Critter child)
{
var rand = Random.GetInstance();
double u1 = rand.NextDouble(); // these are uniform(0,1) random doubles
double u2 = rand.NextDouble();
double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); // random no
int crossoverWeight = (int)Tools.Clamp(10 * randStdNormal, -30, 30);
child.DepartureTime = this.properties.DepartureTime.AddMinutes(crossoverWeight);
Assert.That(child.DepartureTime != default(DateTime));
var random = Random.GetInstance();
List<NodeWrapper<INetworkNode>> nodes = child.Route;
if (nodes.Count == 1)
{
return child;
}
int startIndex = random.Next(0, nodes.Count - 2);
int endIndex = random.Next(startIndex + 1, nodes.Count - 1);
NodeWrapper<INetworkNode> begin = nodes[startIndex];
NodeWrapper<INetworkNode> end = nodes[endIndex];
Route newSegment = this.properties.RouteGenerator.Generate(
begin.Node, end.Node);
var newRoute = new Route(Guid.NewGuid().GetHashCode());
newRoute.AddRange(nodes.GetRange(0, startIndex));
newRoute.AddRange(newSegment);
newRoute.AddRange(nodes.GetRange(endIndex + 1, nodes.Count - 1 - endIndex));
return new Critter((Route)newRoute.Clone(), new Fitness()) { DepartureTime = child.DepartureTime };
// return child;
}
示例5: Crossover
/// <summary>
/// Applies crossover to 2 parents to create a child.
/// </summary>
/// <param name="first">
/// The first parent of the crossover.
/// </param>
/// <param name="second">
/// The second parent of the crossover.
/// </param>
/// <returns>
/// If the operation is successful then the result is returned, otherwise null.
/// </returns>
public Critter[] Crossover(Critter first, Critter second)
{
var random = Random.GetInstance();
List<NodeWrapper<INetworkNode>> firstNodes = first.Route;
List<NodeWrapper<INetworkNode>> secondNodes = second.Route;
var crossoverPoints = new List<KeyValuePair<int, int>>();
for (int i = 0; i < firstNodes.Count; i++)
{
for (int j = 0; j < secondNodes.Count; j++)
{
if (firstNodes[i].Node.Equals(secondNodes[j].Node))
{
crossoverPoints.Add(new KeyValuePair<int, int>(i, j));
break;
}
}
}
if (crossoverPoints.Count == 0)
{
// throw new Exception("StandardBreeder.cs: The crossover points are undefined.");
// crossoverPoints.Add(new KeyValuePair<int, int>(random.Next(firstNodes.Count - 1), random.Next(secondNodes.Count - 1)));
return null;
}
var firstChild = new Route(-1);
var secondChild = new Route(-1);
KeyValuePair<int, int> crossoverPoint = crossoverPoints[random.Next(crossoverPoints.Count - 1)];
firstChild.AddRange(firstNodes.GetRange(0, crossoverPoint.Key));
firstChild.AddRange(secondNodes.GetRange(crossoverPoint.Value, secondNodes.Count - crossoverPoint.Value));
secondChild.AddRange(secondNodes.GetRange(0, crossoverPoint.Value));
secondChild.AddRange(firstNodes.GetRange(crossoverPoint.Key, firstNodes.Count - crossoverPoint.Key));
var output = new[]
{
new Critter((Route)firstChild.Clone(), new Fitness()),
new Critter((Route)secondChild.Clone(), new Fitness())
};
var rand = Random.GetInstance();
double u1 = rand.NextDouble(); // these are uniform(0,1) random doubles
double u2 = rand.NextDouble();
double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); // random no
double crossOverWeight = Tools.Clamp(0.5 + (0.5 * randStdNormal));
long difference = Math.Abs(first.DepartureTime.Ticks - second.DepartureTime.Ticks);
long diffTime = (long)(difference * crossOverWeight);
var newDepart = new DateTime(Math.Min(first.DepartureTime.Ticks, second.DepartureTime.Ticks) + diffTime);
output[0].DepartureTime = newDepart;
output[1].DepartureTime = newDepart;
Assert.That(output[0].DepartureTime != default(DateTime));
Assert.That(output[1].DepartureTime != default(DateTime));
if (output == null || output[0] == null || output[1] == null)
{
throw new Exception("StandardBreeder.cs: One or more decendants of crossover are null.");
}
return output;
}