#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.TravelingSalesman; namespace HeuristicLab.Algorithms.ScatterSearch.TravelingSalesman { /// /// An operator that improves traveling salesman solutions. /// [Item("TravelingSalesmanImprovementOperator", "An operator that improves traveling salesman solutions.")] [StorableClass] public sealed class TravelingSalesmanImprovementOperator : SingleSuccessorOperator, IImprovementOperator { #region Parameter properties public ScopeParameter CurrentScopeParameter { get { return (ScopeParameter)Parameters["CurrentScope"]; } } public ILookupParameter DistanceMatrixParameter { get { return (ILookupParameter)Parameters["DistanceMatrix"]; } } public ILookupParameter EvaluatorParameter { get { return (ILookupParameter)Parameters["Evaluator"]; } } public IValueParameter ImprovementAttemptsParameter { get { return (IValueParameter)Parameters["ImprovementAttempts"]; } } public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } public IValueLookupParameter TargetParameter { get { return (IValueLookupParameter)Parameters["Target"]; } } #endregion #region Properties public IScope CurrentScope { get { return CurrentScopeParameter.ActualValue; } } public DistanceMatrix DistanceMatrix { get { return DistanceMatrixParameter.ActualValue; } set { DistanceMatrixParameter.ActualValue = value; } } public IEvaluator Evaluator { get { return EvaluatorParameter.ActualValue; } set { EvaluatorParameter.ActualValue = value; } } public IntValue ImprovementAttempts { get { return ImprovementAttemptsParameter.Value; } set { ImprovementAttemptsParameter.Value = value; } } public IRandom Random { get { return RandomParameter.ActualValue; } set { RandomParameter.ActualValue = value; } } private IItem Target { get { return TargetParameter.ActualValue; } } #endregion [StorableConstructor] private TravelingSalesmanImprovementOperator(bool deserializing) : base(deserializing) { } private TravelingSalesmanImprovementOperator(TravelingSalesmanImprovementOperator original, Cloner cloner) : base(original, cloner) { } public TravelingSalesmanImprovementOperator() : base() { #region Create parameters Parameters.Add(new ScopeParameter("CurrentScope")); Parameters.Add(new LookupParameter("DistanceMatrix")); Parameters.Add(new LookupParameter("Evaluator")); Parameters.Add(new ValueParameter("ImprovementAttempts", new IntValue(100))); Parameters.Add(new LookupParameter("Random")); Parameters.Add(new ValueLookupParameter("Target")); #endregion TargetParameter.ActualName = "TSPTour"; // temporary solution for the traveling salesman problem } public override IDeepCloneable Clone(Cloner cloner) { return new TravelingSalesmanImprovementOperator(this, cloner); } public override IOperation Apply() { Permutation currSol = CurrentScope.Variables[TargetParameter.ActualName].Value as Permutation; if (currSol.PermutationType != PermutationTypes.RelativeUndirected) throw new ArgumentException("Cannot improve solution because the permutation type is not supported."); for (int i = 0; i < ImprovementAttempts.Value; i++) { int a = Random.Next(currSol.Length); int b = Random.Next(currSol.Length); double oldFirstEdgeLength = DistanceMatrix[currSol[a], currSol[(a - 1 + currSol.Length) % currSol.Length]]; double oldSecondEdgeLength = DistanceMatrix[currSol[b], currSol[(b + 1) % currSol.Length]]; double newFirstEdgeLength = DistanceMatrix[currSol[b], currSol[(a - 1 + currSol.Length) % currSol.Length]]; double newSecondEdgeLength = DistanceMatrix[currSol[a], currSol[(b + 1 + currSol.Length) % currSol.Length]]; if (newFirstEdgeLength + newSecondEdgeLength < oldFirstEdgeLength + oldSecondEdgeLength) Invert(currSol, a, b); } return base.Apply(); } private void Invert(Permutation sol, int i, int j) { if (i != j) for (int a = 0; a < Math.Abs(i - j) / 2; a++) if (sol[(i + a) % sol.Length] != sol[(j - a + sol.Length) % sol.Length]) { // XOR swap sol[(i + a) % sol.Length] ^= sol[(j - a + sol.Length) % sol.Length]; sol[(j - a + sol.Length) % sol.Length] ^= sol[(i + a) % sol.Length]; sol[(i + a) % sol.Length] ^= sol[(j - a + sol.Length) % sol.Length]; } } } }