#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("ExchangeManipulator", "Exchange manipulation")] [StorableClass] public sealed class ExchangeManipulator : PotvinManipulator { [StorableConstructor] private ExchangeManipulator(bool deserializing) : base(deserializing) { } private ExchangeManipulator(ExchangeManipulator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new ExchangeManipulator(this, cloner); } public ExchangeManipulator() : 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; do { feasible = true; if (individual.Tours.Count > 1) { int tour1Idx = random.Next(individual.Tours.Count); int tour2Idx = random.Next(individual.Tours.Count - 1); if (tour2Idx >= tour1Idx) tour2Idx++; Tour tour1 = individual.Tours[tour1Idx]; Tour tour2 = individual.Tours[tour2Idx]; int index1 = random.Next(tour1.Cities.Count); int city1 = tour1.Cities[index1]; int index2 = random.Next(tour2.Cities.Count); int city2 = tour2.Cities[index2]; if (!allowInfeasible) { bool originalFeasible = RouteFeasible(tour1, demand, capacity) && RouteFeasible(tour2, demand, capacity); if (originalFeasible) { double routeLoad = 0; for (int i = 0; i < tour1.Cities.Count; i++) { if (i != index1) routeLoad += demand[tour1.Cities[i]]; } routeLoad += demand[city2]; if (routeLoad > capacity.Value) { feasible = false; } else { routeLoad = 0; for (int i = 0; i < tour2.Cities.Count; i++) { if (i != index2) routeLoad += demand[tour2.Cities[i]]; } routeLoad += demand[city1]; if (routeLoad > capacity.Value) { feasible = false; } } } } if (feasible) { tour1.Cities.RemoveAt(index1); tour1.Cities.Insert(index1, city2); tour2.Cities.RemoveAt(index2); tour2.Cities.Insert(index2, city1); } } } while (!feasible); } 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); } } }