source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/SolutionsCaching/SolutionCachingAnalyzer.cs @ 9728

Last change on this file since 9728 was 9728, checked in by ascheibe, 6 years ago

#1886 worked on solution caching

File size: 3.9 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
32  [Item("SolutionCachingAnalyzer", "An operator that records solutions.")]
33  [StorableClass]
34  public class SolutionCachingAnalyzer : SingleSuccessorOperator, IAnalyzer {
35    private const string ResultsParameterName = "Results";
36
37    #region IAnalyzer Members
38    public bool EnabledByDefault {
39      get { return false; }
40    }
41    #endregion
42
43    #region Parameter properties
44    public ILookupParameter<ResultCollection> ResultsParameter {
45      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
46    }
47    public ILookupParameter<IntValue> GenerationsParameter {
48      get { return (ILookupParameter<IntValue>)Parameters["Generations"]; }
49    }
50    public ScopeTreeLookupParameter<Permutation> SolutionParameter {
51      get { return (ScopeTreeLookupParameter<Permutation>)Parameters["Solution"]; }
52    }
53    #endregion
54
55    #region Properties
56    public ResultCollection Results {
57      get { return ResultsParameter.ActualValue; }
58    }
59    #endregion
60
61    [StorableConstructor]
62    private SolutionCachingAnalyzer(bool deserializing) : base(deserializing) { }
63
64    private SolutionCachingAnalyzer(SolutionCachingAnalyzer original, Cloner cloner)
65      : base(original, cloner) {
66    }
67
68    public SolutionCachingAnalyzer()
69      : base() {
70      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
71      Parameters.Add(new LookupParameter<IntValue>("Generations", "Nr of generations."));
72      Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Solution", "The solutions that should be stored.", "TSPTour", 1));
73    }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new SolutionCachingAnalyzer(this, cloner);
77    }
78
79    public override IOperation Apply() {
80      ItemArray<Permutation> solutions = SolutionParameter.ActualValue;
81      PermutationSolutionDictionary<PermutationWrapper, PermutationInformation> solDict;
82
83      if (!Results.ContainsKey("SolutionCache")) {
84        solDict = new PermutationSolutionDictionary<PermutationWrapper, PermutationInformation>();
85        Results.Add(new Result("SolutionCache", solDict));
86      } else {
87        solDict = (PermutationSolutionDictionary<PermutationWrapper, PermutationInformation>)Results["SolutionCache"].Value;
88      }
89
90      foreach (var sol in solutions) {
91        PermutationInformation info = new PermutationInformation();
92        info.Generation = GenerationsParameter.ActualValue.Value;
93        info.ProducedBy = ProducedBy.Mutation;
94       
95        solDict.Add(new PermutationWrapper(sol), info);
96      }
97      return base.Apply();
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.