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.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;
|
---|
33 | using GeneticExchangeMapType = HeuristicLab.Core.ItemList<HeuristicLab.Core.IItem>;
|
---|
34 | using TraceMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItemList<HeuristicLab.Core.IItem>>;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.EvolutionaryTracking {
|
---|
37 | /// <summary>
|
---|
38 | /// An operator which performs some cleanup at the end of the main loop.
|
---|
39 | /// </summary>
|
---|
40 | [Item("CleanupOperator", "This operator should be the last to run in the main loop and clean up the tracking-related data structures.")]
|
---|
41 | [StorableClass]
|
---|
42 | public sealed class CleanupOperator : SingleSuccessorOperator {
|
---|
43 | #region Parameter Names
|
---|
44 | private const string GlobalTraceMapParameterName = "GlobalTraceMap";
|
---|
45 | private const string GlobalCloneMapParameterName = "GlobalCloneMap";
|
---|
46 | private const string GlobalFragmentMapParameterName = "GlobalFragmentMap";
|
---|
47 | private const string GlobalGeneticExchangeMapParameterName = "GlobalGeneticExchangeMap";
|
---|
48 | private const string PopulationGraphParameterName = "PopulationGraph";
|
---|
49 | private const string GenerationsParameterName = "Generations";
|
---|
50 | private const string MaximumGenerationsParameterName = "MaximumGenerations";
|
---|
51 | private const string ResultsParameterName = "Results";
|
---|
52 | private const string CleanGenealogyGraphParameterName = "CleanGenealogyGraph";
|
---|
53 | private const string UpdateGenealogyGraphParameterName = "UpdateGenealogyGraph";
|
---|
54 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
55 | private const string SymbolicExpressionTreeQualityParameterName = "Quality";
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | #region Parameter Properties
|
---|
59 | public LookupParameter<TraceMapType> GlobalTraceMapParameter {
|
---|
60 | get { return (LookupParameter<TraceMapType>)Parameters[GlobalTraceMapParameterName]; }
|
---|
61 | }
|
---|
62 | public LookupParameter<CloneMapType> GlobalCloneMapParameter {
|
---|
63 | get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; }
|
---|
64 | }
|
---|
65 | public LookupParameter<CloneMapType> GlobalFragmentMapParameter {
|
---|
66 | get { return (LookupParameter<CloneMapType>)Parameters[GlobalFragmentMapParameterName]; }
|
---|
67 | }
|
---|
68 | public LookupParameter<GeneticExchangeMapType> GlobalGeneticExchangeMapParameter {
|
---|
69 | get { return (LookupParameter<GeneticExchangeMapType>)Parameters[GlobalGeneticExchangeMapParameterName]; }
|
---|
70 | }
|
---|
71 | public LookupParameter<IntValue> GenerationsParameter {
|
---|
72 | get { return (LookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
|
---|
73 | }
|
---|
74 | public LookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
75 | get { return (LookupParameter<IntValue>)Parameters[MaximumGenerationsParameterName]; }
|
---|
76 | }
|
---|
77 | public LookupParameter<ResultCollection> ResultsParameter {
|
---|
78 | get { return (LookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
79 | }
|
---|
80 | public ValueParameter<BoolValue> CleanGenealogyGraphParameter {
|
---|
81 | get { return (ValueParameter<BoolValue>)Parameters[CleanGenealogyGraphParameterName]; }
|
---|
82 | }
|
---|
83 | public ValueParameter<BoolValue> UpdateGenealogyGraphParameter {
|
---|
84 | get { return (ValueParameter<BoolValue>)Parameters[UpdateGenealogyGraphParameterName]; }
|
---|
85 | }
|
---|
86 | public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
87 | get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
88 | }
|
---|
89 | public IScopeTreeLookupParameter<DoubleValue> SymbolicExpressionTreeQualityParameter {
|
---|
90 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[SymbolicExpressionTreeQualityParameterName]; }
|
---|
91 | }
|
---|
92 | #endregion
|
---|
93 |
|
---|
94 | #region Properties
|
---|
95 | private CloneMapType GlobalCloneMap { get { return GlobalCloneMapParameter.ActualValue; } }
|
---|
96 | private TraceMapType GlobalTraceMap { get { return GlobalTraceMapParameter.ActualValue; } }
|
---|
97 | private CloneMapType GlobalFragmentMap { get { return GlobalFragmentMapParameter.ActualValue; } }
|
---|
98 | private GeneticExchangeMapType GlobalGeneticExchangeMap { get { return GlobalGeneticExchangeMapParameter.ActualValue; } }
|
---|
99 | private IntValue Generations { get { return GenerationsParameter.ActualValue; } }
|
---|
100 | private IntValue MaximumGenerations { get { return MaximumGenerationsParameter.ActualValue; } }
|
---|
101 | private ResultCollection Results { get { return ResultsParameter.ActualValue; } }
|
---|
102 | public BoolValue CleanGenealogyGraph { get { return CleanGenealogyGraphParameter.Value; } }
|
---|
103 | public BoolValue UpdateGenealogyGraph { get { return UpdateGenealogyGraphParameter.Value; } }
|
---|
104 | private IEnumerable<DoubleValue> Quality { get { return SymbolicExpressionTreeQualityParameter.ActualValue; } }
|
---|
105 | private IEnumerable<ISymbolicExpressionTree> SymbolicExpressionTree { get { return SymbolicExpressionTreeParameter.ActualValue; } }
|
---|
106 | #endregion
|
---|
107 |
|
---|
108 | [StorableHook(HookType.AfterDeserialization)]
|
---|
109 | private void AfterDeserialization() {
|
---|
110 | if (!Parameters.ContainsKey(GlobalGeneticExchangeMapParameterName))
|
---|
111 | Parameters.Add(new LookupParameter<GeneticExchangeMapType>(GlobalGeneticExchangeMapParameterName));
|
---|
112 | if (!Parameters.ContainsKey(CleanGenealogyGraphParameterName))
|
---|
113 | Parameters.Add(new ValueParameter<BoolValue>(CleanGenealogyGraphParameterName));
|
---|
114 | if (!Parameters.ContainsKey(UpdateGenealogyGraphParameterName))
|
---|
115 | Parameters.Add(new ValueParameter<BoolValue>(UpdateGenealogyGraphParameterName));
|
---|
116 | if (!Parameters.ContainsKey(SymbolicExpressionTreeParameterName))
|
---|
117 | Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
|
---|
118 | if (!Parameters.ContainsKey(SymbolicExpressionTreeQualityParameterName))
|
---|
119 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(SymbolicExpressionTreeQualityParameterName, "The qualities of the symbolic expression trees"));
|
---|
120 | }
|
---|
121 |
|
---|
122 | [StorableConstructor]
|
---|
123 | private CleanupOperator(bool deserializing) : base(deserializing) { }
|
---|
124 | private CleanupOperator(SingleSuccessorOperator original, Cloner cloner) : base(original, cloner) { }
|
---|
125 | public override IDeepCloneable Clone(Cloner cloner) { return new CleanupOperator(this, cloner); }
|
---|
126 |
|
---|
127 | public CleanupOperator()
|
---|
128 | : base() {
|
---|
129 | Parameters.Add(new LookupParameter<TraceMapType>(GlobalTraceMapParameterName));
|
---|
130 | Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName));
|
---|
131 | Parameters.Add(new LookupParameter<CloneMapType>(GlobalFragmentMapParameterName));
|
---|
132 | Parameters.Add(new LookupParameter<GeneticExchangeMapType>(GlobalGeneticExchangeMapParameterName));
|
---|
133 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName));
|
---|
134 | Parameters.Add(new LookupParameter<IntValue>(MaximumGenerationsParameterName));
|
---|
135 | Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
136 | Parameters.Add(new ValueParameter<BoolValue>(CleanGenealogyGraphParameterName));
|
---|
137 | Parameters.Add(new ValueParameter<BoolValue>(UpdateGenealogyGraphParameterName));
|
---|
138 | Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
|
---|
139 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(SymbolicExpressionTreeQualityParameterName, "The qualities of the symbolic expression trees"));
|
---|
140 | }
|
---|
141 |
|
---|
142 | public override IOperation Apply() {
|
---|
143 | if (GlobalCloneMap != null) GlobalCloneMap.Clear();
|
---|
144 | if (GlobalFragmentMap != null) GlobalFragmentMap.Clear();
|
---|
145 | if (GlobalTraceMap != null) GlobalTraceMap.Clear();
|
---|
146 | if (GlobalGeneticExchangeMap != null) GlobalGeneticExchangeMap.Clear();
|
---|
147 |
|
---|
148 | if (!Results.ContainsKey(PopulationGraphParameterName)) return base.Apply();
|
---|
149 | var graph = (SymbolicExpressionTreeGenealogyGraph)Results[PopulationGraphParameterName].Value;
|
---|
150 | if (graph == null) return base.Apply();
|
---|
151 |
|
---|
152 | if (UpdateGenealogyGraph.Value) {
|
---|
153 | // update vertex qualities if the trees in the scope were optimized by the ConstantOptimizationAnalyzer
|
---|
154 | ISymbolicExpressionTree[] trees = SymbolicExpressionTree.ToArray();
|
---|
155 | double[] qualities = Quality.Select(x => x.Value).ToArray();
|
---|
156 |
|
---|
157 | for (int i = 0; i != trees.Length; ++i) {
|
---|
158 | foreach (var graphNode in graph.GetGraphNodes(trees[i])) {
|
---|
159 | graphNode.Quality = qualities[i];
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | // if we are at the end of the run, then we clear the genealogy graph (to save memory)
|
---|
165 | if (CleanGenealogyGraph.Value) {
|
---|
166 | if (Generations.Value == MaximumGenerations.Value)
|
---|
167 | if (Results.ContainsKey(PopulationGraphParameterName)) {
|
---|
168 | Results.Remove(PopulationGraphParameterName);
|
---|
169 | graph.Dispose();
|
---|
170 | }
|
---|
171 | }
|
---|
172 | return base.Apply();
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|