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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.GP.Interfaces;
|
---|
29 | using HeuristicLab.Modeling;
|
---|
30 | using HeuristicLab.DataAnalysis;
|
---|
31 | using System.Diagnostics;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.GP.StructureIdentification.Networks {
|
---|
34 | public class NetworkToFunctionTransformer : OperatorBase {
|
---|
35 | public NetworkToFunctionTransformer()
|
---|
36 | : base() {
|
---|
37 | AddVariableInfo(new VariableInfo("Network", "The network (open expression)", typeof(IGeneticProgrammingModel), VariableKind.In));
|
---|
38 | AddVariableInfo(new VariableInfo("TargetVariables", "Name of the target variables", typeof(ItemList<StringData>), VariableKind.In));
|
---|
39 | AddVariableInfo(new VariableInfo("FunctionTree", "The function tree with all targetvaribales", typeof(IGeneticProgrammingModel), VariableKind.New));
|
---|
40 | }
|
---|
41 |
|
---|
42 | public override string Description {
|
---|
43 | get { return "Extracts the network (function tree with unbound parameters) and creates a closed form function tree for each target variable."; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override IOperation Apply(IScope scope) {
|
---|
47 | IGeneticProgrammingModel model = GetVariableValue<IGeneticProgrammingModel>("Network", scope, true);
|
---|
48 | ItemList<StringData> targetVariables = GetVariableValue<ItemList<StringData>>("TargetVariables", scope, true);
|
---|
49 | // clear old sub-scopes
|
---|
50 | while (scope.SubScopes.Count > 0) scope.RemoveSubScope(scope.SubScopes[0]);
|
---|
51 |
|
---|
52 | // create a new sub-scope for each target variable with the transformed expression
|
---|
53 | foreach (IFunctionTree transformedTree in Transform(model.FunctionTree, targetVariables.Select(x => x.Data))) {
|
---|
54 | Scope exprScope = new Scope();
|
---|
55 | scope.AddSubScope(exprScope);
|
---|
56 | exprScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), new GeneticProgrammingModel(transformedTree)));
|
---|
57 | }
|
---|
58 |
|
---|
59 | return null;
|
---|
60 | }
|
---|
61 |
|
---|
62 | private static IEnumerable<IFunctionTree> Transform(IFunctionTree networkDescription, IEnumerable<string> targetVariables) {
|
---|
63 | // bind open parameters of network to target variables
|
---|
64 | //IFunctionTree openExpression = RemoveOpenParameters(networkDescription);
|
---|
65 | IFunctionTree paritallyEvaluatedOpenExpression = ApplyMetaFunctions((IFunctionTree)networkDescription.Clone());
|
---|
66 | IFunctionTree boundExpression = BindVariables(paritallyEvaluatedOpenExpression, targetVariables);
|
---|
67 |
|
---|
68 | // create a new sub-scope for each target variable with the transformed expression
|
---|
69 | foreach (var targetVariable in targetVariables) {
|
---|
70 | yield return TransformExpression(boundExpression, targetVariable);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | private static IFunctionTree ApplyMetaFunctions(IFunctionTree tree) {
|
---|
75 | IFunctionTree root = ApplyCycles(tree);
|
---|
76 | List<IFunctionTree> subTrees = new List<IFunctionTree>(root.SubTrees);
|
---|
77 | while (tree.SubTrees.Count > 0) tree.RemoveSubTree(0);
|
---|
78 |
|
---|
79 | foreach (IFunctionTree subTree in subTrees) {
|
---|
80 | root.AddSubTree(ApplyFlips(subTree));
|
---|
81 | }
|
---|
82 | return root;
|
---|
83 | }
|
---|
84 |
|
---|
85 | private static IFunctionTree ApplyFlips(IFunctionTree tree) {
|
---|
86 | if (tree.SubTrees.Count == 0) {
|
---|
87 | return tree;
|
---|
88 | } else if (tree.Function is Flip) {
|
---|
89 | return InvertFunction(tree.SubTrees[0]);
|
---|
90 | } else {
|
---|
91 | IFunctionTree tmp = ApplyFlips(tree.SubTrees[0]);
|
---|
92 | tree.RemoveSubTree(0); tree.AddSubTree(tmp);
|
---|
93 | return tree;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | private static IFunctionTree ApplyCycles(IFunctionTree tree) {
|
---|
98 | int nRotations = 0;
|
---|
99 | while (tree.Function is Cycle) {
|
---|
100 | nRotations++;
|
---|
101 | tree = tree.SubTrees[0];
|
---|
102 | }
|
---|
103 | if (nRotations > 0 && nRotations % tree.SubTrees.Count > 0) {
|
---|
104 | IFunctionTree[] subTrees = tree.SubTrees.ToArray();
|
---|
105 | while (tree.SubTrees.Count > 0) tree.RemoveSubTree(0);
|
---|
106 |
|
---|
107 | nRotations = nRotations % subTrees.Length;
|
---|
108 | Array.Reverse(subTrees, 0, nRotations);
|
---|
109 | Array.Reverse(subTrees, nRotations, subTrees.Length - nRotations);
|
---|
110 | Array.Reverse(subTrees, 0, subTrees.Length);
|
---|
111 |
|
---|
112 | for (int i = 0; i < subTrees.Length; i++) {
|
---|
113 | tree.AddSubTree(subTrees[i]);
|
---|
114 | }
|
---|
115 | }
|
---|
116 | return tree;
|
---|
117 | }
|
---|
118 |
|
---|
119 | private static IFunctionTree InvertFunction(IFunctionTree tree) {
|
---|
120 | IFunctionTree invertedNode = null;
|
---|
121 | if (tree.Function is OpenParameter) {
|
---|
122 | return tree;
|
---|
123 | } else if (tree.Function is AdditionF1) {
|
---|
124 | invertedNode = (new SubtractionF1()).GetTreeNode();
|
---|
125 | invertedNode.AddSubTree(tree.SubTrees[1]);
|
---|
126 | } else if (tree.Function is DivisionF1) {
|
---|
127 | invertedNode = (new MultiplicationF1()).GetTreeNode();
|
---|
128 | invertedNode.AddSubTree(tree.SubTrees[1]);
|
---|
129 | } else if (tree.Function is MultiplicationF1) {
|
---|
130 | invertedNode = (new DivisionF1()).GetTreeNode();
|
---|
131 | invertedNode.AddSubTree(tree.SubTrees[1]);
|
---|
132 | } else if (tree.Function is SubtractionF1) {
|
---|
133 | invertedNode = (new AdditionF1()).GetTreeNode();
|
---|
134 | invertedNode.AddSubTree(tree.SubTrees[1]);
|
---|
135 | } else if (tree.Function is OpenExp) {
|
---|
136 | invertedNode = (new OpenLog()).GetTreeNode();
|
---|
137 | } else if (tree.Function is OpenLog) {
|
---|
138 | invertedNode = (new OpenLog()).GetTreeNode();
|
---|
139 | } else if (tree.Function is OpenSqrt) {
|
---|
140 | invertedNode = (new OpenSqr()).GetTreeNode();
|
---|
141 | } else {
|
---|
142 | throw new ArgumentException();
|
---|
143 | }
|
---|
144 | IFunctionTree invertedTail = ApplyFlips(tree.SubTrees[0]);
|
---|
145 | if (invertedTail.Function is OpenParameter) {
|
---|
146 | invertedNode.InsertSubTree(0, invertedTail);
|
---|
147 | return invertedNode;
|
---|
148 | } else {
|
---|
149 | return AppendLeft(invertedTail, invertedNode);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | private static IFunctionTree AppendLeft(IFunctionTree tree, IFunctionTree node) {
|
---|
154 | IFunctionTree originalTree = tree;
|
---|
155 | while (tree.SubTrees[0].SubTrees.Count > 0) tree = tree.SubTrees[0];
|
---|
156 | tree.InsertSubTree(0, node);
|
---|
157 | return originalTree;
|
---|
158 | }
|
---|
159 |
|
---|
160 | private static IFunctionTree TransformExpression(IFunctionTree tree, string targetVariable) {
|
---|
161 | if (tree.SubTrees.Count >= 3) {
|
---|
162 | int targetIndex = -1;
|
---|
163 | IFunctionTree combinator;
|
---|
164 | List<IFunctionTree> subTrees = new List<IFunctionTree>(tree.SubTrees);
|
---|
165 | //while (tree.SubTrees.Count > 0) tree.RemoveSubTree(0);
|
---|
166 | if (HasTargetVariable(subTrees[0], targetVariable)) {
|
---|
167 | targetIndex = 0;
|
---|
168 | combinator = FunctionFromCombinator(tree);
|
---|
169 | } else {
|
---|
170 | for (int i = 1; i < subTrees.Count; i++) {
|
---|
171 | if (HasTargetVariable(subTrees[i], targetVariable)) {
|
---|
172 | targetIndex = i;
|
---|
173 | break;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | combinator = FunctionFromCombinator(InvertCombinator(tree));
|
---|
177 | }
|
---|
178 | // not found
|
---|
179 | if (targetIndex == -1) throw new InvalidOperationException();
|
---|
180 | IFunctionTree targetChain = InvertFunction(subTrees[targetIndex]);
|
---|
181 | for (int i = 0; i < subTrees.Count; i++) {
|
---|
182 | if (i != targetIndex)
|
---|
183 | combinator.AddSubTree(subTrees[i]);
|
---|
184 | }
|
---|
185 | if (targetChain.Function is OpenParameter) return combinator;
|
---|
186 | else {
|
---|
187 | AppendLeft(targetChain, combinator);
|
---|
188 | return targetChain;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | throw new NotImplementedException();
|
---|
192 | }
|
---|
193 |
|
---|
194 | private static IFunctionTree InvertCombinator(IFunctionTree tree) {
|
---|
195 | if (tree.Function is OpenAddition) {
|
---|
196 | return (new OpenSubtraction()).GetTreeNode();
|
---|
197 | } else if (tree.Function is OpenSubtraction) {
|
---|
198 | return (new OpenAddition()).GetTreeNode();
|
---|
199 | } else if (tree.Function is OpenMultiplication) {
|
---|
200 | return (new OpenDivision()).GetTreeNode();
|
---|
201 | } else if (tree.Function is OpenDivision) {
|
---|
202 | return (new OpenMultiplication()).GetTreeNode();
|
---|
203 | } else throw new InvalidOperationException();
|
---|
204 | }
|
---|
205 |
|
---|
206 | private static IFunctionTree FunctionFromCombinator(IFunctionTree tree) {
|
---|
207 | if (tree.Function is OpenAddition) {
|
---|
208 | return (new Addition()).GetTreeNode();
|
---|
209 | } else if (tree.Function is OpenSubtraction) {
|
---|
210 | return (new Subtraction()).GetTreeNode();
|
---|
211 | } else if (tree.Function is OpenMultiplication) {
|
---|
212 | return (new Multiplication()).GetTreeNode();
|
---|
213 | } else if (tree.Function is OpenDivision) {
|
---|
214 | return (new Division()).GetTreeNode();
|
---|
215 | } else throw new InvalidOperationException();
|
---|
216 | }
|
---|
217 |
|
---|
218 | private static bool HasTargetVariable(IFunctionTree tree, string targetVariable) {
|
---|
219 | if (tree.SubTrees.Count == 0) {
|
---|
220 | return ((OpenParameterFunctionTree)tree).VariableName == targetVariable;
|
---|
221 | } else return HasTargetVariable(tree.SubTrees[0], targetVariable);
|
---|
222 | }
|
---|
223 |
|
---|
224 | private static IFunctionTree BindVariables(IFunctionTree tree, IEnumerable<string> targetVariables) {
|
---|
225 | IEnumerator<string> targetVariablesEnumerator = targetVariables.GetEnumerator();
|
---|
226 | foreach (IFunctionTree node in FunctionTreeIterator.IteratePrefix(tree)) {
|
---|
227 | if (node.Function is OpenParameter && targetVariablesEnumerator.MoveNext()) {
|
---|
228 | var varTreeNode = node as OpenParameterFunctionTree;
|
---|
229 | varTreeNode.VariableName = targetVariablesEnumerator.Current;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | return tree;
|
---|
233 | }
|
---|
234 | }
|
---|
235 | }
|
---|