void CalculateRoute() { var pokestopsNotOnCooldown = bot.Map.Pokestops.Where(p => p.IsActive); route = RouteOptimizer.Optimize(pokestopsNotOnCooldown, bot.Player.Position); Route.Clear(); Route.AddRange(route.Select(r => r.Position)); RaisePropertyChanged(nameof(Route)); }
/// <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; }
/// <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; }
/// <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; }