#region License Information /* HeuristicLab * Copyright (C) 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 System.Collections.Generic; using System.Linq; using System.Threading; using HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; namespace HeuristicLab.Problems.PTSP { /// /// An operator that improves probabilistic traveling salesman solutions. /// /// /// The operator tries to improve the probabilistic traveling salesman solution by inserting a city in the tour between two other cities for a certain number of times. /// [Item("pTSP Estimated Insertion Local Improvement", "An operator that improves probabilistic traveling salesman solutions. The operator tries to improve the probabilistic traveling salesman solution by swapping two randomly chosen edges for a certain number of times.")] [StorableType("B2D60579-A97D-4E44-B11C-61CDA6EBEBA7")] public sealed class PTSPEstimatedInsertionLocalImprovement : SingleSuccessorOperator, IEstimatedPTSPOperator, IPermutationLocalImprovementOperator { public ILookupParameter LocalIterationsParameter { get { return (ILookupParameter)Parameters["LocalIterations"]; } } public IValueLookupParameter MaximumIterationsParameter { get { return (IValueLookupParameter)Parameters["MaximumIterations"]; } } public ILookupParameter EvaluatedSolutionsParameter { get { return (ILookupParameter)Parameters["EvaluatedSolutions"]; } } public ILookupParameter ResultsParameter { get { return (ILookupParameter)Parameters["Results"]; } } public ILookupParameter PermutationParameter { get { return (ILookupParameter)Parameters["Permutation"]; } } public ILookupParameter QualityParameter { get { return (ILookupParameter)Parameters["Quality"]; } } public ILookupParameter MaximizationParameter { get { return (ILookupParameter)Parameters["Maximization"]; } } public ILookupParameter ProbabilisticTSPDataParameter { get { return (ILookupParameter)Parameters["PTSP Data"]; } } public ILookupParameter> RealizationDataParameter { get { return (ILookupParameter>)Parameters["RealizationData"]; } } [StorableConstructor] private PTSPEstimatedInsertionLocalImprovement(StorableConstructorFlag _) : base(_) { } private PTSPEstimatedInsertionLocalImprovement(PTSPEstimatedInsertionLocalImprovement original, Cloner cloner) : base(original, cloner) { } public PTSPEstimatedInsertionLocalImprovement() : base() { Parameters.Add(new LookupParameter("Permutation", "The solution as permutation.")); Parameters.Add(new LookupParameter("LocalIterations", "The number of iterations that have already been performed.")); Parameters.Add(new ValueLookupParameter("MaximumIterations", "The maximum amount of iterations that should be performed (note that this operator will abort earlier when a local optimum is reached).", new IntValue(10000))); Parameters.Add(new LookupParameter("EvaluatedSolutions", "The amount of evaluated solutions (here a move is counted only as 4/n evaluated solutions with n being the length of the permutation).")); Parameters.Add(new LookupParameter("Results", "The collection where to store results.")); Parameters.Add(new LookupParameter("Quality", "The quality value of the assignment.")); Parameters.Add(new LookupParameter("Maximization", "True if the problem should be maximized or minimized.")); Parameters.Add(new LookupParameter("PTSP Data", "The main parameters of the pTSP.")); Parameters.Add(new LookupParameter>("RealizationData", "The list of samples drawn from all possible stochastic instances.")); } public override IDeepCloneable Clone(Cloner cloner) { return new PTSPEstimatedInsertionLocalImprovement(this, cloner); } public static void Improve(Permutation assignment, IProbabilisticTSPData data, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, IEnumerable realizations, CancellationToken cancellation) { for (var i = localIterations.Value; i < maxIterations; i++) { TranslocationMove bestMove = null; double bestQuality = 0; // we have to make an improvement, so 0 is the baseline double evaluations = 0.0; foreach (var move in ExhaustiveInsertionMoveGenerator.Generate(assignment)) { double moveQuality = PTSPEstimatedInsertionMoveEvaluator.EvaluateMove(assignment, move, data, realizations); evaluations += realizations.Count() * 6.0 / (assignment.Length * assignment.Length); if (maximization && moveQuality > bestQuality || !maximization && moveQuality < bestQuality) { bestQuality = moveQuality; bestMove = move; } } evaluatedSolutions.Value += (int)Math.Ceiling(evaluations); if (bestMove == null) break; TranslocationManipulator.Apply(assignment, bestMove.Index1, bestMove.Index2, bestMove.Index3); quality.Value += bestQuality; localIterations.Value++; cancellation.ThrowIfCancellationRequested(); } } public override IOperation Apply() { var maxIterations = MaximumIterationsParameter.ActualValue.Value; var assignment = PermutationParameter.ActualValue; var maximization = MaximizationParameter.ActualValue.Value; var quality = QualityParameter.ActualValue; var localIterations = LocalIterationsParameter.ActualValue; var evaluations = EvaluatedSolutionsParameter.ActualValue; var data = ProbabilisticTSPDataParameter.ActualValue; var realizations = RealizationDataParameter.ActualValue; if (localIterations == null) { localIterations = new IntValue(0); LocalIterationsParameter.ActualValue = localIterations; } Improve(assignment, data, quality, localIterations, evaluations, maximization, maxIterations, realizations, CancellationToken); localIterations.Value = 0; return base.Apply(); } } }