1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 | using HeuristicLab.Algorithms.MemPR.Permutation.LocalSearch;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using HeuristicLab.Core;
|
---|
10 | using HeuristicLab.Data;
|
---|
11 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
12 | using HeuristicLab.Operators;
|
---|
13 | using HeuristicLab.Optimization;
|
---|
14 | using HeuristicLab.Parameters;
|
---|
15 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
16 |
|
---|
17 | namespace 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 | }
|
---|