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 |
|
---|
22 | using HEAL.Attic;
|
---|
23 | using HeuristicLab.Analysis;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
30 | /// <summary>
|
---|
31 | /// An operator for analyzing the frequency of alleles in solutions of Traveling Salesman Problems given in path representation.
|
---|
32 | /// </summary>
|
---|
33 | [Item("TSPAlleleFrequencyAnalyzer", "An operator for analyzing the frequency of alleles in solutions of Traveling Salesman Problems given in path representation.")]
|
---|
34 | [StorableType("43a9ec34-c917-43f8-bd14-4d42e2d0a458")]
|
---|
35 | public sealed class TSPAlleleFrequencyAnalyzer : AlleleFrequencyAnalyzer<Permutation> {
|
---|
36 | [Storable] public ILookupParameter<ITSPData> TSPDataParameter { get; private set; }
|
---|
37 |
|
---|
38 | [StorableConstructor]
|
---|
39 | private TSPAlleleFrequencyAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
40 | private TSPAlleleFrequencyAnalyzer(TSPAlleleFrequencyAnalyzer original, Cloner cloner)
|
---|
41 | : base(original, cloner) {
|
---|
42 | TSPDataParameter = cloner.Clone(original.TSPDataParameter);
|
---|
43 | }
|
---|
44 | public TSPAlleleFrequencyAnalyzer()
|
---|
45 | : base() {
|
---|
46 | Parameters.Add(TSPDataParameter = new LookupParameter<ITSPData>("TSPData", "The main parameters of the TSP."));
|
---|
47 | }
|
---|
48 |
|
---|
49 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
50 | return new TSPAlleleFrequencyAnalyzer(this, cloner);
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected override Allele[] CalculateAlleles(Permutation solution) {
|
---|
54 | var tspData = TSPDataParameter.ActualValue;
|
---|
55 | Allele[] alleles = new Allele[solution.Length];
|
---|
56 | int source, target, h;
|
---|
57 | double impact;
|
---|
58 |
|
---|
59 | for (int i = 0; i < solution.Length - 1; i++) {
|
---|
60 | source = solution[i];
|
---|
61 | target = solution[i + 1];
|
---|
62 | if (source > target) { h = source; source = target; target = h; }
|
---|
63 | impact = tspData.GetDistance(source, target);
|
---|
64 | alleles[i] = new Allele(source.ToString() + "-" + target.ToString(), impact);
|
---|
65 | }
|
---|
66 | source = solution[solution.Length - 1];
|
---|
67 | target = solution[0];
|
---|
68 | if (source > target) { h = source; source = target; target = h; }
|
---|
69 | impact = tspData.GetDistance(source, target);
|
---|
70 | alleles[alleles.Length - 1] = new Allele(source.ToString() + "-" + target.ToString(), impact);
|
---|
71 |
|
---|
72 | return alleles;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|