Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TravelingSalesman/TravelingSalesmanImprovementOperator.cs @ 7775

Last change on this file since 7775 was 7775, checked in by jkarder, 12 years ago

#1331:

  • added operators for TestFunctions problems
  • added more path relinking operators for the TravelingSalesman and the Knapsack problem
  • added 2-tier version of the SolutionPoolUpdateMethod
  • added parameters and adjusted types
  • added some exception handling
  • adjusted event handling
  • updated plugin dependencies
  • minor code improvements
File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.Knapsack;
32using HeuristicLab.Problems.TravelingSalesman;
33
34namespace HeuristicLab.Algorithms.ScatterSearch.TravelingSalesman {
35  /// <summary>
36  /// An operator that improves traveling salesman solutions.
37  /// </summary>
38  [Item("TravelingSalesmanImprovementOperator", "An operator that improves traveling salesman solutions.")]
39  [StorableClass]
40  public sealed class TravelingSalesmanImprovementOperator : SingleSuccessorOperator, ILocalImprovementOperator, IScatterSearchTargetProcessor {
41    #region Problem properties
42    public Type ProblemType {
43      get { return typeof(KnapsackProblem); }
44    }
45    [Storable]
46    private KnapsackProblem problem;
47    public IProblem Problem {
48      get { return problem; }
49      set { problem = (KnapsackProblem)value; }
50    }
51    #endregion
52
53    #region Parameter properties
54    public ScopeParameter CurrentScopeParameter {
55      get { return (ScopeParameter)Parameters["CurrentScope"]; }
56    }
57    public IValueLookupParameter<DoubleMatrix> CoordinatesParameter {
58      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
59    }
60    public IValueLookupParameter<IEvaluator> EvaluatorParameter {
61      get { return (IValueLookupParameter<IEvaluator>)Parameters["Evaluator"]; }
62    }
63    public IValueLookupParameter<IntValue> ImprovementAttemptsParameter {
64      get { return (IValueLookupParameter<IntValue>)Parameters["ImprovementAttempts"]; }
65    }
66    public IValueLookupParameter<IRandom> RandomParameter {
67      get { return (IValueLookupParameter<IRandom>)Parameters["Random"]; }
68    }
69    public IValueLookupParameter<IItem> TargetParameter {
70      get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
71    }
72    #region ILocalImprovementOperator Parameters
73    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
74      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
75    }
76    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
77      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
78    }
79    public ILookupParameter<ResultCollection> ResultsParameter {
80      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
81    }
82    #endregion
83    #endregion
84
85    #region Properties
86    public IScope CurrentScope {
87      get { return CurrentScopeParameter.ActualValue; }
88    }
89    public DoubleMatrix Coordinates {
90      get { return CoordinatesParameter.ActualValue; }
91      set { CoordinatesParameter.ActualValue = value; }
92    }
93    public IEvaluator Evaluator {
94      get { return EvaluatorParameter.ActualValue; }
95      set { EvaluatorParameter.ActualValue = value; }
96    }
97    public IntValue ImprovementAttempts {
98      get { return ImprovementAttemptsParameter.ActualValue; }
99      set { ImprovementAttemptsParameter.ActualValue = value; }
100    }
101    public IRandom Random {
102      get { return RandomParameter.ActualValue; }
103      set { RandomParameter.ActualValue = value; }
104    }
105    private IItem Target {
106      get { return TargetParameter.ActualValue; }
107    }
108    #endregion
109
110    [StorableConstructor]
111    private TravelingSalesmanImprovementOperator(bool deserializing) : base(deserializing) { }
112    private TravelingSalesmanImprovementOperator(TravelingSalesmanImprovementOperator original, Cloner cloner)
113      : base(original, cloner) {
114      this.problem = cloner.Clone(original.problem);
115    }
116    public TravelingSalesmanImprovementOperator()
117      : base() {
118      #region Create parameters
119      Parameters.Add(new ScopeParameter("CurrentScope"));
120      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Coordinates"));
121      Parameters.Add(new ValueLookupParameter<IEvaluator>("Evaluator"));
122      Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", new IntValue(100)));
123      Parameters.Add(new ValueLookupParameter<IRandom>("Random"));
124      Parameters.Add(new ValueLookupParameter<IItem>("Target"));
125      #endregion
126      TargetParameter.ActualName = "TSPTour"; // temporary solution for the traveling salesman problem
127    }
128
129    public override IDeepCloneable Clone(Cloner cloner) {
130      return new TravelingSalesmanImprovementOperator(this, cloner);
131    }
132
133    public override IOperation Apply() {
134      var currSol = CurrentScope.Variables[TargetParameter.ActualName].Value as Permutation;
135      var bestSol = currSol;
136      double currLength = TSPEuclideanPathEvaluator.Apply(Evaluator as TSPCoordinatesPathEvaluator, Coordinates, currSol);
137      double bestLength = currLength;
138
139      for (int i = 0; i < ImprovementAttempts.Value; i++) {
140        int a = Random.Next(currSol.Length);
141        int b = Random.Next(currSol.Length);
142        Invert(currSol, a, b);
143        currLength = TSPEuclideanPathEvaluator.Apply(Evaluator as TSPCoordinatesPathEvaluator, Coordinates, currSol);
144        if (currLength < bestLength) {
145          bestLength = currLength;
146          bestSol = currSol.Clone() as Permutation;
147        }
148        Invert(currSol, a, b);
149      }
150
151      CurrentScope.Variables[TargetParameter.ActualName].Value = bestSol;
152
153      return base.Apply();
154    }
155
156    private void Invert(Permutation sol, int i, int j) {
157      if (i != j)
158        for (int a = 0; a < Math.Abs(i - j) / 2; a++)
159          if (sol[(i + a) % sol.Length] != sol[(j - a + sol.Length) % sol.Length]) {
160            // XOR swap
161            sol[(i + a) % sol.Length] ^= sol[(j - a + sol.Length) % sol.Length];
162            sol[(j - a + sol.Length) % sol.Length] ^= sol[(i + a) % sol.Length];
163            sol[(i + a) % sol.Length] ^= sol[(j - a + sol.Length) % sol.Length];
164          }
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.