Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/ExhaustiveInversionImprover.cs @ 17209

Last change on this file since 17209 was 14776, checked in by abeham, 7 years ago

#2457: working on MemPR integration

File size: 3.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading;
6using System.Threading.Tasks;
7using HeuristicLab.Algorithms.MemPR.Permutation.LocalSearch;
8using HeuristicLab.Common;
9using HeuristicLab.Core;
10using HeuristicLab.Data;
11using HeuristicLab.Encodings.PermutationEncoding;
12using HeuristicLab.Operators;
13using HeuristicLab.Optimization;
14using HeuristicLab.Parameters;
15using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
16
17namespace HeuristicLab.Algorithms.MemPR.Permutation {
18  [Item("Exhaustive Inversion Improver", "")]
19  [StorableClass]
20  public class ExhaustiveInversionImprover : SingleSuccessorOperator, IImprovementOperator {
21    #region Parameter properties
22    public ILookupParameter<DoubleMatrix> DistanceMatrixParameter {
23      get { return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
24    }
25    public ILookupParameter<IRandom> RandomParameter {
26      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
27    }
28    public ILookupParameter<Encodings.PermutationEncoding.Permutation> PermutationParameter {
29      get { return (ILookupParameter<Encodings.PermutationEncoding.Permutation>)Parameters["Permutation"]; }
30    }
31    public ILookupParameter<IntValue> LocalEvaluatedSolutionsParameter {
32      get { return (ILookupParameter<IntValue>)Parameters["LocalEvaluatedSolutions"]; }
33    }
34    #endregion
35
36    [StorableConstructor]
37    private ExhaustiveInversionImprover(bool deserializing) : base(deserializing) { }
38    private ExhaustiveInversionImprover(ExhaustiveInversionImprover original, Cloner cloner) : base(original, cloner) { }
39    public ExhaustiveInversionImprover()
40      : base() {
41      #region Create parameters
42      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
43      Parameters.Add(new LookupParameter<IRandom>("Random", "A pseudo random number generator."));
44      Parameters.Add(new ValueLookupParameter<Encodings.PermutationEncoding.Permutation>("Permutation", "The solution to be improved. This parameter is used for name translation only."));
45      Parameters.Add(new LookupParameter<IntValue>("LocalEvaluatedSolutions", "The amount of locally evaluated solutions."));
46      #endregion
47    }
48
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new ExhaustiveInversionImprover(this, cloner);
51    }
52
53    public override IOperation Apply() {
54      var random = RandomParameter.ActualValue;
55      var perm = PermutationParameter.ActualValue;
56
57      var distances = DistanceMatrixParameter.ActualValue;
58      Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval = (p, _) => {
59        double length = 0;
60        for (int i = 0; i < p.Length - 1; i++)
61          length += distances[p[i], p[i + 1]];
62        length += distances[p[p.Length - 1], p[0]];
63        return length;
64      };
65      var quality = eval(perm, CancellationToken.None);
66      var path = Exhaustive2Opt.HillClimb(random, perm, quality, false, eval, CancellationToken.None);
67      var final = path.Last();
68
69      LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(final.Item3);
70
71      return base.Apply();
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.