Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9723 was 9723, checked in by ascheibe, 11 years ago

#1886 worked on solution caching

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