Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/MwDruGrammar.cs @ 6689

Last change on this file since 6689 was 6284, checked in by mkommend, 14 years ago

#1479: Created branch for grammar editing.

File size: 6.6 KB
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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  [StorableClass]
31  [Item("MwDruGrammar", "Represents a grammar for modeling with fixed structures for the voest B1 MwDru target variable.")]
32  public class MwDruGrammar : SymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar {
33
34    [StorableConstructor]
35    protected MwDruGrammar(bool deserializing) : base(deserializing) { }
36    protected MwDruGrammar(MwDruGrammar original, Cloner cloner) : base(original, cloner) { }
37    public MwDruGrammar()
38      : base(ItemAttribute.GetName(typeof(MwDruGrammar)), ItemAttribute.GetDescription(typeof(TypeCoherentExpressionGrammar))) {
39      Initialize();
40    }
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new MwDruGrammar(this, cloner);
43    }
44
45    private HeuristicLab.Problems.DataAnalysis.Symbolic.Variable ggDru;
46    private HeuristicLab.Problems.DataAnalysis.Symbolic.Variable flamTmp;
47
48    private void Initialize() {
49      var add = new Addition();
50      var sub = new Subtraction();
51      var mul = new Multiplication();
52      var div = new Division();
53      var log = new Logarithm();
54      var exp = new Exponential();
55
56      var constant = new Constant();
57      constant.MinValue = -20;
58      constant.MaxValue = 20;
59      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
60
61      var fixedAdd1 = new Addition();
62      fixedAdd1.Name = "fixed Addition 1";
63      fixedAdd1.Fixed = true;
64
65      var fixedAdd2 = new Addition();
66      fixedAdd2.Name = "fixed Addition 2";
67      fixedAdd2.Fixed = true;
68
69      var fixedMul = new Multiplication();
70      fixedMul.Name = "fixed Multiplication";
71      fixedMul.Fixed = true;
72
73      ggDru = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
74      ggDru.Name = "fixed Variable GGDru";
75      ggDru.Fixed = true;
76      ggDru.VariableNames = new List<string>() { "GGDRU" };
77      ggDru.WeightManipulatorMu = 1;
78      ggDru.WeightManipulatorSigma = 0;
79      ggDru.WeightMu = 1;
80      ggDru.WeightSigma = 0;
81      ggDru.MultiplicativeWeightManipulatorSigma = 0;
82      ggDru.Changed += new EventHandler(ggDru_Changed);
83
84      flamTmp = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
85      flamTmp.Name = "fixed Variable FlamTmp";
86      flamTmp.Fixed = true;
87      flamTmp.VariableNames = new List<string>() { "FLAMTMP" };
88      flamTmp.WeightManipulatorMu = 1;
89      flamTmp.WeightManipulatorSigma = 0;
90      flamTmp.WeightMu = 1;
91      flamTmp.WeightSigma = 0;
92      flamTmp.MultiplicativeWeightManipulatorSigma = 0;
93      flamTmp.Changed += new EventHandler(flamTmp_Changed);
94
95      var c273 = new Constant();
96      c273.Name = "fixed Constant 273";
97      c273.Fixed = true;
98      c273.MinValue = 273;
99      c273.MaxValue = 273;
100      c273.ManipulatorMu = 0;
101      c273.ManipulatorSigma = 0;
102      c273.MultiplicativeManipulatorSigma = 0;
103
104      var allSymbols = new List<Symbol>()
105        { add, sub, mul, div, log, exp, constant, variableSymbol,
106          fixedAdd1,fixedAdd2,fixedMul,ggDru,flamTmp,c273};
107
108      var unaryFunctionSymbols = new List<Symbol>() { log, exp };
109      var binaryFunctionSymbols = new List<Symbol>() { add, sub, mul, div };
110
111      var terminalSymbols = new List<Symbol>() { variableSymbol, constant };
112      var fixedTerminalSymbols = new List<Symbol>() { ggDru, flamTmp, c273 };
113      var realValuedSymbols = unaryFunctionSymbols.Concat(binaryFunctionSymbols).Concat(terminalSymbols);
114
115      foreach (var symb in allSymbols)
116        AddSymbol(symb);
117      foreach (var terminalSymbol in terminalSymbols) {
118        SetSubtreeCount(terminalSymbol, 0, 0);
119      }
120      foreach (var terminalSymbol in fixedTerminalSymbols) {
121        SetSubtreeCount(terminalSymbol, 0, 0);
122      }
123      foreach (var unaryFun in unaryFunctionSymbols) {
124        SetSubtreeCount(unaryFun, 1, 1);
125      }
126      foreach (var binaryFun in binaryFunctionSymbols) {
127        SetSubtreeCount(binaryFun, 2, 2);
128      }
129
130      SetSubtreeCount(fixedAdd1, 3, 3);
131      SetSubtreeCount(fixedMul, 3, 3);
132      SetSubtreeCount(fixedAdd2, 2, 2);
133
134      AddAllowedChildSymbol(StartSymbol, fixedAdd1);
135
136      AddAllowedChildSymbol(fixedAdd1, fixedMul, 0);
137      AddAllowedChildSymbol(fixedAdd1, ggDru, 1);
138      foreach (var symb in unaryFunctionSymbols.Concat(binaryFunctionSymbols).Concat(terminalSymbols))
139        AddAllowedChildSymbol(fixedAdd1, symb, 2);
140
141      AddAllowedChildSymbol(fixedMul, constant, 0);
142      AddAllowedChildSymbol(fixedMul, fixedAdd2, 1);
143      foreach (var symb in realValuedSymbols)
144        AddAllowedChildSymbol(fixedMul, symb, 2);
145
146      AddAllowedChildSymbol(fixedAdd2, c273, 0);
147      AddAllowedChildSymbol(fixedAdd2, flamTmp, 1);
148
149      foreach (var symb in unaryFunctionSymbols) {
150        foreach (var childSymb in realValuedSymbols) {
151          AddAllowedChildSymbol(symb, childSymb);
152        }
153      }
154
155      foreach (var symb in binaryFunctionSymbols) {
156        foreach (var childSymb in realValuedSymbols) {
157          AddAllowedChildSymbol(symb, childSymb);
158        }
159      }
160    }
161
162    private void flamTmp_Changed(object sender, System.EventArgs e) {
163      if (flamTmp.VariableNames.Count() != 1 || flamTmp.VariableNames.ElementAt(0) != "FLAMTMP")
164        flamTmp.VariableNames = new List<string>() { "FLAMTMP" };
165    }
166
167    private void ggDru_Changed(object sender, System.EventArgs e) {
168      if (ggDru.VariableNames.Count() != 1 || ggDru.VariableNames.ElementAt(0) != "GGDRU")
169        ggDru.VariableNames = new List<string>() { "GGDRU" };
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.