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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
34 | [Item("AfterCrossoverSolutionCachingAnalyzer", "An operator that records solutions.")]
|
---|
35 | [StorableClass]
|
---|
36 | public class AfterCrossoverSolutionCachingAnalyzer : SingleSuccessorOperator {
|
---|
37 | private const string ResultsParameterName = "Results";
|
---|
38 |
|
---|
39 | #region Parameter properties
|
---|
40 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
41 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
42 | }
|
---|
43 | public ILookupParameter<IntValue> GenerationsParameter {
|
---|
44 | get { return (ILookupParameter<IntValue>)Parameters["Generations"]; }
|
---|
45 | }
|
---|
46 | public ILookupParameter<Permutation> SolutionParameter {
|
---|
47 | get { return (ILookupParameter<Permutation>)Parameters["Solution"]; }
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | #region Properties
|
---|
52 | public ResultCollection Results {
|
---|
53 | get { return ResultsParameter.ActualValue; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | [StorableConstructor]
|
---|
58 | private AfterCrossoverSolutionCachingAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
59 |
|
---|
60 | private AfterCrossoverSolutionCachingAnalyzer(AfterCrossoverSolutionCachingAnalyzer original, Cloner cloner)
|
---|
61 | : base(original, cloner) {
|
---|
62 | }
|
---|
63 |
|
---|
64 | public AfterCrossoverSolutionCachingAnalyzer()
|
---|
65 | : base() {
|
---|
66 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
67 | Parameters.Add(new LookupParameter<IntValue>("Generations", "Nr of generations."));
|
---|
68 | Parameters.Add(new LookupParameter<Permutation>("Solution"));
|
---|
69 | SolutionParameter.ActualName = "TSPTour";
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
73 | return new AfterCrossoverSolutionCachingAnalyzer(this, cloner);
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override IOperation Apply() {
|
---|
77 | var solution = SolutionParameter.ActualValue;
|
---|
78 | var parent2 = ExecutionContext.Scope.SubScopes.First();
|
---|
79 | var parent1 = ExecutionContext.Scope.SubScopes.Last();
|
---|
80 | PermutationSolutionDictionary solDict;
|
---|
81 |
|
---|
82 | if (!Results.ContainsKey("SolutionCache")) {
|
---|
83 | solDict = new PermutationSolutionDictionary();
|
---|
84 | Results.Add(new Result("SolutionCache", solDict));
|
---|
85 | } else {
|
---|
86 | solDict = (PermutationSolutionDictionary)Results["SolutionCache"].Value;
|
---|
87 | }
|
---|
88 |
|
---|
89 | PermutationInformation info = new PermutationInformation();
|
---|
90 | info.Generation = GenerationsParameter.ActualValue.Value;
|
---|
91 | info.ProducedBy = ProducedBy.Crossover;
|
---|
92 | info.ParentList = new List<PermutationWrapper>();
|
---|
93 | //TODO: solution name should be a parameter
|
---|
94 | var p1Pw = solDict.GetPermutationWrapperOfPermutation(parent1.Variables["TSPTour"].Value as Permutation);
|
---|
95 | var p2Pw = solDict.GetPermutationWrapperOfPermutation(parent2.Variables["TSPTour"].Value as Permutation);
|
---|
96 | info.ParentList.Add(p1Pw);
|
---|
97 | info.ParentList.Add(p2Pw);
|
---|
98 | solDict.Add(new PermutationWrapper((Permutation)solution.Clone(new Cloner())), info);
|
---|
99 |
|
---|
100 | return base.Apply();
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|