/// <summary> /// Extract the route starting at currentGenPos /// </summary> /// <param name="genotype"></param> /// <param name="currentGenPos">Will be set to the index of the allele following the route</param> /// <param name="startTimeOfDay"></param> /// <returns></returns> private Route GetRoute(Genotype genotype, ref int currentGenPos, int startTimeOfDay) { var waypoints = new List <Waypoint> { // add start at home new Waypoint() { StartTime = startTimeOfDay, VisitId = Constants.VisitIdHome, } }; while (currentGenPos < genotype.Count && !PopulationGenerator.IsSeparator(genotype[currentGenPos])) { int visitId = GetVisitId(genotype[currentGenPos]); var previousWaypoint = waypoints.Last(); int startTime = previousWaypoint.StartTime; if (waypoints.Count == 1) { // first real visit startTime += input.Visits.First(v => v.Id == visitId).WayCostFromHome; } else { // not first visit startTime += input.Visits[previousWaypoint.VisitId].Duration + input.RouteCosts[previousWaypoint.VisitId, visitId]; } waypoints.Add(new Waypoint() { StartTime = startTime, VisitId = visitId, }); currentGenPos++; } currentGenPos++; if (waypoints.Skip(1).All(wp => input.Visits[wp.VisitId].IsBreak)) { // breaks only waypoints.Clear(); } if (waypoints.Count > 0) { // add return to home var previousWaypoint = waypoints.Last(); waypoints.Add(new Waypoint() { StartTime = previousWaypoint.StartTime + input.Visits[previousWaypoint.VisitId].Duration + input.Visits[previousWaypoint.VisitId].WayCostToHome, VisitId = Constants.VisitIdHome, }); } return(new Route() { Waypoints = waypoints.ToArray(), }); }
/// <summary> /// Split genotype into separate routes /// </summary> /// <param name="genotype"></param> /// <returns></returns> private List <Genotype>[] SplitGenotyp(Genotype genotype) { var ret = new List <Genotype> [input.Days.Length]; { for (int i = 0; i < ret.Length; i++) { ret[i] = new List <Genotype>(); } var routesPerDay = genotype.CountRoutes() / input.Days.Length; int day = 0; int santa = 0; for (int i = 0; i <= genotype.Count; i++) { // add Genotype in any case ret[day].Add(new Genotype()); while (i < genotype.Count && !PopulationGenerator.IsSeparator(genotype[i])) { ret[day][santa].Add(genotype[i]); i++; } if (i < genotype.Count) { // Add separator ret[day][santa].Add(genotype[i]); } santa++; if (santa >= routesPerDay) { santa = 0; day++; } } } return(ret); }