Free cookie consent management tool by TermsFeed Policy Generator

Ticket #1583: SymbolicExpressionGalapagosPopulationAnalyzer.cs

File SymbolicExpressionGalapagosPopulationAnalyzer.cs, 7.9 KB (added by gkronber, 10 years ago)
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using System.Text;
33using System;
34
35namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
36  [Item("SymbolicExpressionGalapagosPopulationAnalyzer", "An operator to collect information about GP populations for further analysis with Galapagos.")]
37  [StorableClass]
38  public class SymbolicExpressionGalapagosPopulationAnalyzer : SingleSuccessorOperator, ISymbolicExpressionTreeAnalyzer {
39    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
40    private const string QualityParameterName = "Quality";
41    private const string ResultsParameterName = "Results";
42    private const string GalapagosInputParameterName = "GalapagosInput";
43
44    public bool EnabledByDefault {
45      get { return false; }
46    }
47
48    #region parameter properties
49    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
50      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
51    }
52    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
53      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
54    }
55    public ILookupParameter<StringValue> GalapagosInputParameter {
56      get { return (ILookupParameter<StringValue>)Parameters[GalapagosInputParameterName]; }
57    }
58    public ILookupParameter<ResultCollection> ResultsParameter {
59      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
60    }
61    #endregion
62
63    [StorableConstructor]
64    protected SymbolicExpressionGalapagosPopulationAnalyzer(bool deserializing) : base(deserializing) { }
65    protected SymbolicExpressionGalapagosPopulationAnalyzer(SymbolicExpressionGalapagosPopulationAnalyzer original, Cloner cloner) : base(original, cloner) { }
66    public SymbolicExpressionGalapagosPopulationAnalyzer()
67      : base() {
68      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
69      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The quality of the symbolic expression trees."));
70      Parameters.Add(new LookupParameter<StringValue>(GalapagosInputParameterName, "The data that can be loaded into Galapagos."));
71      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the symbol frequencies should be stored."));
72    }
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new SymbolicExpressionGalapagosPopulationAnalyzer(this, cloner);
75    }
76
77    [StorableHook(HookType.AfterDeserialization)]
78    private void AfterDeserialization() {
79    }
80
81    public override IOperation Apply() {
82      ItemArray<ISymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
83      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
84      ResultCollection results = ResultsParameter.ActualValue;
85      StringValue data = GalapagosInputParameter.ActualValue;
86      var galapagosStringBuilder = new StringBuilder();
87      if (data == null) {
88        //           definition:
89        //ops: (+,2) (-,2) (*,2) (%,2) (Log,2) (Pow,2)
90        //terms: MaxTFICF MaxTFIDF MaxCTD MaxOR MaxCC MaxGSS MaxCHI MaxAM MaxIG MaxDom sumDF DF sumTF TF TFIDF TFICF IDF CTD OR CE CC GSS CHI IG DOM GINI P' P AM
91
92        galapagosStringBuilder
93          .Append("definition:")
94          .AppendLine()
95          .Append("\tops:\t");
96
97        var grammar = expressions.First().Root.Grammar;
98        foreach (var symb in grammar.AllowedSymbols) {
99          if (grammar.GetMaximumSubtreeCount(symb) > 0) {
100            for (int a = grammar.GetMinimumSubtreeCount(symb); a <= grammar.GetMaximumSubtreeCount(symb); a++) {
101              galapagosStringBuilder.Append("(" + symb.Name + "_" + a + "," + a + ") ");
102            }
103          }
104        }
105        galapagosStringBuilder
106          .AppendLine()
107          .Append("\tterms:\t");
108
109        foreach (var symb in grammar.AllowedSymbols) {
110          if (grammar.GetMaximumSubtreeCount(symb) == 0) {
111            galapagosStringBuilder.Append(symb.Name + " ");
112          }
113        }
114
115        galapagosStringBuilder.AppendLine();
116
117        var newData = new StringValue(galapagosStringBuilder.ToString());
118        GalapagosInputParameter.ActualValue = newData;
119        results.Add(new Result("Galapagos Population Information", newData));
120      }
121      galapagosStringBuilder
122        .Append(data)
123        .Append("generation: ")
124        .AppendLine();
125      SymbolicExpressionTreeGalapagosFormatter formatter = new SymbolicExpressionTreeGalapagosFormatter();
126      //generation:
127      //  ind:  93.2329 * MaxOR AM
128      //  ind:  29.6386 + + MaxAM MaxCC - IG GSS
129      //  ind:  34.1767 - DF CC
130      //  ind:  94.9799 * + DOM AM + TFICF TFIDF
131      //  ind:  36.3855 % MaxOR MaxTFICF
132      for (int i = 0; i < expressions.Length; i++) {
133        galapagosStringBuilder
134          .Append("\tind:\t")
135          .AppendFormat("{0:f}\t", qualities[i].Value * 100.0);
136        string str = formatter.Format(expressions[i]);
137        galapagosStringBuilder.AppendLine(str);
138      }
139
140      //      rep: 7  -> 7
141      //rep: 43 10  -> 0
142      //rep: 43 10  -> 1
143      //rep: 49 15  -> 2
144      //rep: 49 15  -> 3
145      //rep: 23 37  -> 4
146      //rep: 23 37  -> 5
147      //rep: 48 20  -> 6
148      //rep: 48 20  -> 8
149      //rep: 44  -> 9
150      //rep: 11 32  -> 10
151      //rep: 11 32  -> 11
152      //rep: 26 4  -> 12
153      //rep: 26 4  -> 13
154
155      for (int i = 0; i < expressions.Length; i++) {
156        galapagosStringBuilder
157          .AppendLine("\trep:\t" + i + " -> " + i);
158      }
159
160      GalapagosInputParameter.ActualValue.Value = galapagosStringBuilder.ToString();
161      return base.Apply();
162    }
163
164    public static IEnumerable<KeyValuePair<string, double>> CalculateSymbolFrequencies(IEnumerable<ISymbolicExpressionTree> trees, bool aggregateDifferentNumberOfSubtrees = true) {
165      Dictionary<string, double> symbolFrequencies = new Dictionary<string, double>();
166      int totalNumberOfSymbols = 0;
167
168      foreach (var tree in trees) {
169        foreach (var node in tree.IterateNodesPrefix()) {
170          string symbolName;
171          if (aggregateDifferentNumberOfSubtrees) symbolName = node.Symbol.Name;
172          else symbolName = node.Symbol.Name + "-" + node.SubtreeCount;
173          if (symbolFrequencies.ContainsKey(symbolName)) symbolFrequencies[symbolName] += 1;
174          else symbolFrequencies.Add(symbolName, 1);
175          totalNumberOfSymbols++;
176        }
177      }
178
179      foreach (var pair in symbolFrequencies)
180        yield return new KeyValuePair<string, double>(pair.Key, pair.Value / totalNumberOfSymbols);
181    }
182  }
183}