Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/Improvers/TSPImprovementOperator.cs @ 17241

Last change on this file since 17241 was 17241, checked in by abeham, 5 years ago

#2521: worked on refactoring TSP

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Problems.TravelingSalesman {
33  /// <summary>
34  /// An operator that improves traveling salesman solutions.
35  /// </summary>
36  /// <remarks>
37  /// The operator tries to improve the traveling salesman solution by swapping two randomly chosen edges for a certain number of times.
38  /// </remarks>
39  [Item("TSPImprovementOperator", "An operator that improves traveling salesman solutions. The operator tries to improve the traveling salesman solution by swapping two randomly chosen edges for a certain number of times.")]
40  [StorableType("1b3cbc66-6dcc-4f61-9cbe-8b50cc54413a")]
41  public sealed class TSPImprovementOperator : SingleSuccessorOperator, ISingleObjectiveImprovementOperator {
42   
43    [Storable] public ILookupParameter<ITSPData> TSPDataParameter { get; private set; }
44    [Storable] public IValueParameter<IntValue> ImprovementAttemptsParameter { get; private set; }
45    [Storable] public ILookupParameter<IRandom> RandomParameter { get; private set; }
46    [Storable] public IValueLookupParameter<IItem> SolutionParameter { get; private set; }
47
48    public int ImprovementAttempts {
49      get { return ImprovementAttemptsParameter.Value.Value; }
50      set { ImprovementAttemptsParameter.Value.Value = value; }
51    }
52
53    [StorableConstructor]
54    private TSPImprovementOperator(StorableConstructorFlag _) : base(_) { }
55    private TSPImprovementOperator(TSPImprovementOperator original, Cloner cloner)
56      : base(original, cloner) {
57      TSPDataParameter = cloner.Clone(original.TSPDataParameter);
58      ImprovementAttemptsParameter = cloner.Clone(original.ImprovementAttemptsParameter);
59      RandomParameter = cloner.Clone(original.RandomParameter);
60      SolutionParameter = cloner.Clone(original.SolutionParameter);
61    }
62    public TSPImprovementOperator()
63      : base() {
64      #region Create parameters
65      Parameters.Add(TSPDataParameter = new LookupParameter<ITSPData>("TSPData", "The main parameters of the TSP."));
66      Parameters.Add(ImprovementAttemptsParameter = new ValueParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
67      Parameters.Add(RandomParameter = new LookupParameter<IRandom>("Random", "A pseudo random number generator."));
68      Parameters.Add(SolutionParameter = new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only."));
69      #endregion
70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new TSPImprovementOperator(this, cloner);
74    }
75
76    public override IOperation Apply() {
77      var random = RandomParameter.ActualValue;
78      var solution = ExecutionContext.Scope.Variables[SolutionParameter.ActualName].Value as Permutation;
79      if (solution == null)
80        throw new ArgumentException("Cannot improve solution because it has the wrong type.");
81      if (solution.PermutationType != PermutationTypes.RelativeUndirected)
82        throw new ArgumentException("Cannot improve solution because the permutation type is not supported.");
83      var tspData = TSPDataParameter.ActualValue;
84
85      for (int i = 0; i < ImprovementAttempts; i++) {
86        var move = StochasticInversionSingleMoveGenerator.Apply(solution, random);
87        double moveQualtiy = TSPInversionMoveEvaluator.CalculateTourLengthDelta(tspData, solution, move);
88        if (moveQualtiy < 0)
89          InversionManipulator.Apply(solution, move.Index1, move.Index2);
90      }
91
92      ExecutionContext.Scope.Variables.Add(new Variable("LocalEvaluatedSolutions", new IntValue(ImprovementAttempts)));
93
94      return base.Apply();
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.