Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.StructureIdentification/PopulationAnalyser.cs @ 5750

Last change on this file since 5750 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 7.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Operators;
28using HeuristicLab.Functions;
29using HeuristicLab.Data;
30
31namespace HeuristicLab.StructureIdentification {
32  public class PopulationAnalyser : OperatorBase {
33    public override string Description {
34      get { return @"Generates statistics about the population. (for debugging)"; }
35    }
36
37    public PopulationAnalyser()
38      : base() {
39    }
40
41    public override IOperation Apply(IScope topScope) {
42
43      Scope resultsScope = new Scope("Pop. analyzer results");
44      IScope scope = topScope.SubScopes[0];
45
46      var functionTrees = scope.SubScopes.Select(s => s.GetVariableValue<IFunction>("OperatorTree", false));
47      var treeSizes = scope.SubScopes.Select(s => s.GetVariableValue<IntData>("TreeSize", false).Data);
48      var treeHeights = scope.SubScopes.Select(s => s.GetVariableValue<IntData>("TreeHeight", false).Data);
49      var qualities = scope.SubScopes.Select(s => s.GetVariableValue<DoubleData>("Quality", false).Data);
50
51      var allFunctions = functionTrees.Select(t => AllNodes(t)).Aggregate((x, y) => { x.AddRange(y); return x; });
52
53      //var results = scope.SubScopes.Select(s => (DoubleArrayData)s.GetVariableValue<DoubleArrayData>("Results", false));
54      //var resultSums = results.Select(a => a.Data.Aggregate(0.0, (x, y) => x + y));
55
56      var functionsHistogram = from f in allFunctions
57                               group f by ((StringData)f.GetVariable("TypeId").Value).Data into g
58                               select new { FunctionType = g.Key, Functions = g };
59
60      resultsScope.AddVariable(new HeuristicLab.Core.Variable("AvgTreeSize", new DoubleData(treeSizes.Average())));
61      resultsScope.AddVariable(new HeuristicLab.Core.Variable("MinTreeSize", new DoubleData(treeSizes.Min())));
62      resultsScope.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new DoubleData(treeSizes.Max())));
63
64      resultsScope.AddVariable(new HeuristicLab.Core.Variable("AvgTreeHeight", new DoubleData(treeHeights.Average())));
65      resultsScope.AddVariable(new HeuristicLab.Core.Variable("MinTreeHeight", new DoubleData(treeHeights.Min())));
66      resultsScope.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new DoubleData(treeHeights.Max())));
67
68      resultsScope.AddVariable(new HeuristicLab.Core.Variable("AvgQuality", new DoubleData(qualities.Average())));
69      resultsScope.AddVariable(new HeuristicLab.Core.Variable("MinQuality", new DoubleData(qualities.Min())));
70      resultsScope.AddVariable(new HeuristicLab.Core.Variable("MaxQuality", new DoubleData(qualities.Max())));
71
72      ItemList list = new ItemList();
73      PrefixVisitor visitor = new PrefixVisitor();
74      foreach(IFunction function in functionTrees) {
75        visitor.Reset();
76        function.Accept(visitor);
77        list.Add(new StringData(visitor.Representation));
78      }
79
80      resultsScope.AddVariable(new HeuristicLab.Core.Variable("Functions", list));
81
82      topScope.SubScopes[1].AddSubScope(resultsScope);
83      return null;
84    }
85
86    private List<IFunction> AllNodes(IFunction f) {
87      List<IFunction> result = new List<IFunction>();
88      result.Add(f);
89      if(f.SubFunctions.Count == 0) {
90        return result;
91      } else return f.SubFunctions.Select(subFunction => AllNodes(subFunction)).Aggregate(result, (x, y) => { x.AddRange(y); return x; });
92    }
93
94    private class PrefixVisitor : IFunctionVisitor {
95      private string representation;
96      public string Representation {
97        get { return representation; }
98      }
99
100      public void Reset() {
101        representation = "";
102      }
103
104      private void VisitFunction(string name, IFunction f) {
105        representation += "(" + name;
106        foreach(IFunction subFunction in f.SubFunctions) {
107          representation += " ";
108          subFunction.Accept(this);
109        }
110        representation += ")";
111      }
112
113      #region IFunctionVisitor Members
114
115      public void Visit(IFunction function) {
116        representation += function.Name;
117      }
118
119      public void Visit(Addition addition) {
120        VisitFunction("+", addition);
121      }
122
123      public void Visit(Constant constant) {
124        representation += constant.Value.Data.ToString("F2");
125      }
126
127      public void Visit(Cosinus cosinus) {
128        VisitFunction("cos", cosinus);
129      }
130
131      public void Visit(Division division) {
132        VisitFunction("/", division);
133      }
134
135      public void Visit(Exponential exponential) {
136        VisitFunction("expt", exponential);
137      }
138
139      public void Visit(Logarithm logarithm) {
140        VisitFunction("log", logarithm);
141      }
142
143      public void Visit(Multiplication multiplication) {
144        VisitFunction("*", multiplication);
145      }
146
147      public void Visit(Power power) {
148        VisitFunction("pow", power);
149      }
150
151      public void Visit(Signum signum) {
152        VisitFunction("signum", signum);
153      }
154
155      public void Visit(Sinus sinus) {
156        VisitFunction("sinus", sinus);
157      }
158
159      public void Visit(Sqrt sqrt) {
160        VisitFunction("sqrt", sqrt);
161      }
162
163      public void Visit(Substraction substraction) {
164        VisitFunction("-", substraction);
165      }
166
167      public void Visit(Tangens tangens) {
168        VisitFunction("tan", tangens);
169      }
170
171      public void Visit(HeuristicLab.Functions.Variable variable) {
172        string offsetStr;
173
174        if(variable.SampleOffset > 0) {
175          offsetStr = "(+ t " + variable.SampleOffset + ")";
176        } else if(variable.SampleOffset < 0) {
177          offsetStr = "(- t " + -variable.SampleOffset  + ")";
178        } else {
179          offsetStr = "(t)";
180        }
181        int v = variable.VariableIndex;
182        representation+= "(* " + variable.Weight.ToString("F2") + " " + variable.Name + v + offsetStr + ")";
183      }
184
185      public void Visit(And and) {
186        VisitFunction("and", and);
187      }
188
189      public void Visit(Average average) {
190        VisitFunction("average", average);
191      }
192
193      public void Visit(IfThenElse ifThenElse) {
194        VisitFunction("if-then-else", ifThenElse);
195      }
196
197      public void Visit(Not not) {
198        VisitFunction("not", not);
199      }
200
201      public void Visit(Or or) {
202        VisitFunction("or", or);
203      }
204
205      public void Visit(Xor xor) {
206        VisitFunction("xor", xor);
207      }
208
209      public void Visit(Equal equal) {
210        VisitFunction("eq?", equal);
211      }
212
213      public void Visit(LessThan lessThan) {
214        VisitFunction("<", lessThan);
215      }
216
217      #endregion
218    }
219  }
220}
Note: See TracBrowser for help on using the repository browser.