#region License Information /* HeuristicLab * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin; using HeuristicLab.Problems.VehicleRouting.Encodings; using HeuristicLab.Problems.VehicleRouting; namespace HeuristicLab.Analysis.FitnessLandscape.VRP { [Item("TwoOptStarManipulator", "Two opt star manipulation")] [StorableClass] public sealed class TwoOptStarManipulator : PotvinManipulator { [StorableConstructor] private TwoOptStarManipulator(bool deserializing) : base(deserializing) { } private TwoOptStarManipulator(TwoOptStarManipulator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new TwoOptStarManipulator(this, cloner); } public TwoOptStarManipulator() : base() { } private static bool RouteFeasible(Tour tour, DoubleArray demand, DoubleValue capacity) { double routeLoad = 0; for (int i = 0; i < tour.Cities.Count; i++) { routeLoad += demand[tour.Cities[i]]; } return routeLoad <= capacity.Value; } public static void Apply(IRandom random, PotvinEncoding individual, DoubleArray demand, DoubleValue capacity, bool allowInfeasible) { bool feasible; //consider creating new tour individual.Tours.Add(new Tour()); do { feasible = true; int route1Idx = random.Next(individual.Tours.Count); int route2Idx = random.Next(individual.Tours.Count - 1); if (route2Idx >= route1Idx) route2Idx++; Tour route1 = individual.Tours[route1Idx]; Tour route2 = individual.Tours[route2Idx]; int x1 = random.Next(route1.Cities.Count + 1); int x2 = random.Next(route2.Cities.Count + 1); if (!allowInfeasible) { bool originalFeasible = RouteFeasible(route1, demand, capacity) && RouteFeasible(route2, demand, capacity); if (originalFeasible) { double routeLoad = 0; for (int i = 0; i < x1; i++) routeLoad += demand[route1.Cities[i]]; for (int i = x2; i < route2.Cities.Count; i++) routeLoad += demand[route2.Cities[i]]; if (routeLoad > capacity.Value) { feasible = false; } else { routeLoad = 0; for (int i = 0; i < x2; i++) routeLoad += demand[route2.Cities[i]]; for (int i = x1; i < route1.Cities.Count; i++) routeLoad += demand[route1.Cities[i]]; if (routeLoad > capacity.Value) { feasible = false; } } } } if (feasible) { int count = route1.Cities.Count - x1; List segmentX1 = new List(); if (count > 0) { segmentX1 = route1.Cities.GetRange(x1, count); route1.Cities.RemoveRange(x1, count); } count = route2.Cities.Count - x2; List segmentX2 = new List(); if (count > 0) { segmentX2 = route2.Cities.GetRange(x2, count); route2.Cities.RemoveRange(x2, count); } route1.Cities.AddRange(segmentX2); route2.Cities.AddRange(segmentX1); } } while (!feasible); individual.Tours.RemoveAll(t => t.Cities.Count == 0); } protected override void Manipulate(IRandom random, PotvinEncoding individual) { BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue; DoubleMatrix coordinates = CoordinatesParameter.ActualValue; DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix); DoubleArray demand = DemandParameter.ActualValue; DoubleValue capacity = CapacityParameter.ActualValue; bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; Apply(random, individual, demand, capacity, allowInfeasible); } } }