1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
7 | using HeuristicLab.Operators;
|
---|
8 | using HeuristicLab.Optimization;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.EvolutionaryTracking {
|
---|
13 | /// <summary>
|
---|
14 | /// This operator basically aggregates together a scope list of trees in the current generation and the genealogy graph so far
|
---|
15 | /// in a new object that can be used as Content for the InteractiveIndividualHistoryView
|
---|
16 | /// </summary>
|
---|
17 | [StorableClass]
|
---|
18 | public class GenealogyAggregator : SingleSuccessorOperator {
|
---|
19 | private const string PopulationGraphParameterName = "PopulationGraph";
|
---|
20 | private const string LineageExplorerParameterName = "LineageExplorer";
|
---|
21 | private const string ResultsParameterName = "Results";
|
---|
22 | private const string GenerationsParameterName = "Generations";
|
---|
23 | private const string MaximumGenerationsParameterName = "MaximumGenerations";
|
---|
24 |
|
---|
25 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
26 | private const string SymbolicExpressionTreeQualityParameterName = "Quality";
|
---|
27 |
|
---|
28 | #region Parameter properties
|
---|
29 | public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
30 | get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
31 | }
|
---|
32 | public IScopeTreeLookupParameter<DoubleValue> SymbolicExpressionTreeQualityParameter {
|
---|
33 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[SymbolicExpressionTreeQualityParameterName]; }
|
---|
34 | }
|
---|
35 | public LookupParameter<ResultCollection> ResultsParameter {
|
---|
36 | get { return (LookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
37 | }
|
---|
38 | public LookupParameter<IntValue> GenerationsParameter {
|
---|
39 | get { return (LookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
|
---|
40 | }
|
---|
41 | public LookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
42 | get { return (LookupParameter<IntValue>)Parameters[MaximumGenerationsParameterName]; }
|
---|
43 | }
|
---|
44 | #endregion
|
---|
45 | #region Properties
|
---|
46 | public bool EnabledByDefault {
|
---|
47 | get { return true; }
|
---|
48 | }
|
---|
49 | public ResultCollection Results {
|
---|
50 | get { return ResultsParameter.ActualValue; }
|
---|
51 | }
|
---|
52 | public IntValue Generations {
|
---|
53 | get { return GenerationsParameter.ActualValue; }
|
---|
54 | }
|
---|
55 | public IntValue MaximumGenerations {
|
---|
56 | get { return MaximumGenerationsParameter.ActualValue; }
|
---|
57 | }
|
---|
58 | #endregion
|
---|
59 |
|
---|
60 | [StorableHook(HookType.AfterDeserialization)]
|
---|
61 | private void AfterDeserialization() { }
|
---|
62 |
|
---|
63 | #region IStatefulItem members
|
---|
64 | public override void InitializeState() {
|
---|
65 | base.InitializeState();
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override void ClearState() {
|
---|
69 | base.ClearState();
|
---|
70 | }
|
---|
71 | #endregion
|
---|
72 |
|
---|
73 | [StorableConstructor]
|
---|
74 | private GenealogyAggregator(bool deserializing) : base(deserializing) { }
|
---|
75 | private GenealogyAggregator(GenealogyAggregator original, Cloner cloner) : base(original, cloner) { }
|
---|
76 | public override IDeepCloneable Clone(Cloner cloner) { return new GenealogyAggregator(this, cloner); }
|
---|
77 | public GenealogyAggregator() {
|
---|
78 | #region Add parameters
|
---|
79 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far."));
|
---|
80 | Parameters.Add(new LookupParameter<IntValue>(MaximumGenerationsParameterName, "The maximum number of generations"));
|
---|
81 | Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
|
---|
82 | Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze"));
|
---|
83 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(SymbolicExpressionTreeQualityParameterName, "The qualities of the symbolic expression trees"));
|
---|
84 | #endregion
|
---|
85 | }
|
---|
86 |
|
---|
87 | public override IOperation Apply() {
|
---|
88 | if (!Results.ContainsKey(PopulationGraphParameterName)) return base.Apply();
|
---|
89 | var graph = (SymbolicExpressionTreeGenealogyGraph)Results[PopulationGraphParameterName].Value;
|
---|
90 | var trees = SymbolicExpressionTreeParameter.ActualValue;
|
---|
91 | var qualities = SymbolicExpressionTreeQualityParameter.ActualValue;
|
---|
92 | var tuples = trees.Zip(qualities, (t, q) => new Tuple<ISymbolicExpressionTree, double>(t, q.Value)).ToList();
|
---|
93 | var lineageExplorer = new LineageExplorer { GenealogyGraph = graph, Trees = tuples, Generations = Generations.Value };
|
---|
94 | if (!Results.ContainsKey(LineageExplorerParameterName))
|
---|
95 | Results.Add(new Result(LineageExplorerParameterName, lineageExplorer));
|
---|
96 | else Results[LineageExplorerParameterName].Value = lineageExplorer;
|
---|
97 |
|
---|
98 | return base.Apply();
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|