[645] | 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 |
|
---|
[2222] | 22 | using HeuristicLab.GP.Interfaces;
|
---|
[645] | 23 | using System;
|
---|
| 24 |
|
---|
| 25 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
[2222] | 26 | public class ModelAnalyzerExporter : IFunctionTreeSerializer, IFunctionTreeNameGenerator {
|
---|
[654] | 27 | #region IFunctionTreeExporter Members
|
---|
| 28 |
|
---|
| 29 | public string Name {
|
---|
| 30 | get {
|
---|
| 31 | return "HL2 ModelAnalyzer Exporter";
|
---|
| 32 | }
|
---|
[645] | 33 | }
|
---|
[654] | 34 |
|
---|
| 35 | public bool TryExport(IFunctionTree tree, out string exported) {
|
---|
| 36 | try {
|
---|
| 37 | exported = Export(tree);
|
---|
| 38 | return true;
|
---|
| 39 | } catch(UnknownFunctionException) {
|
---|
| 40 | exported = "";
|
---|
| 41 | return false;
|
---|
| 42 | }
|
---|
[645] | 43 | }
|
---|
| 44 |
|
---|
[654] | 45 | public string Export(IFunctionTree tree) {
|
---|
| 46 | string result = ExportFunction(tree.Function, tree);
|
---|
[1227] | 47 | if(tree.SubTrees.Count>0)
|
---|
| 48 | result += "(\n";
|
---|
[654] | 49 | foreach(IFunctionTree subTree in tree.SubTrees) {
|
---|
| 50 | result += Export(subTree);
|
---|
| 51 | result += ";\n";
|
---|
| 52 | }
|
---|
| 53 | result = result.TrimEnd(';', '\n');
|
---|
| 54 | if(tree.SubTrees.Count > 0) result += ")";
|
---|
| 55 | return result;
|
---|
[645] | 56 | }
|
---|
| 57 |
|
---|
[2222] | 58 | public IFunctionTree Import(string tree) {
|
---|
| 59 | throw new UnknownFunctionException(tree);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public bool TryImport(string tree, out IFunctionTree importedTree) {
|
---|
| 63 | try {
|
---|
| 64 | importedTree = Import(tree);
|
---|
| 65 | return true;
|
---|
| 66 | }
|
---|
| 67 | catch (UnknownFunctionException) {
|
---|
| 68 | importedTree = null;
|
---|
| 69 | return false;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[654] | 73 | #endregion
|
---|
[645] | 74 |
|
---|
[654] | 75 | private string ExportFunction(IFunction function, IFunctionTree tree) {
|
---|
| 76 | // this is smelly, if there is a cleaner way to have a 'dynamic' visitor
|
---|
| 77 | // please let me know! (gkronber 14.10.2008)
|
---|
| 78 | if(function is Addition) return ((Addition)function).ExportToHL2(tree);
|
---|
| 79 | if(function is And) return ((And)function).ExportToHL2(tree);
|
---|
| 80 | if(function is Average) return ((Average)function).ExportToHL2(tree);
|
---|
| 81 | if(function is Constant) return ((Constant)function).ExportToHL2(tree);
|
---|
| 82 | if(function is Cosinus) return ((Cosinus)function).ExportToHL2(tree);
|
---|
| 83 | if(function is Differential) return ((Differential)function).ExportToHL2(tree);
|
---|
| 84 | if(function is Division) return ((Division)function).ExportToHL2(tree);
|
---|
| 85 | if(function is Equal) return ((Equal)function).ExportToHL2(tree);
|
---|
| 86 | if(function is Exponential) return ((Exponential)function).ExportToHL2(tree);
|
---|
| 87 | if(function is GreaterThan) return ((GreaterThan)function).ExportToHL2(tree);
|
---|
| 88 | if(function is IfThenElse) return ((IfThenElse)function).ExportToHL2(tree);
|
---|
| 89 | if(function is LessThan) return ((LessThan)function).ExportToHL2(tree);
|
---|
| 90 | if(function is Logarithm) return ((Logarithm)function).ExportToHL2(tree);
|
---|
| 91 | if(function is Multiplication) return ((Multiplication)function).ExportToHL2(tree);
|
---|
| 92 | if(function is Not) return ((Not)function).ExportToHL2(tree);
|
---|
| 93 | if(function is Or) return ((Or)function).ExportToHL2(tree);
|
---|
| 94 | if(function is Power) return ((Power)function).ExportToHL2(tree);
|
---|
| 95 | if(function is Signum) return ((Signum)function).ExportToHL2(tree);
|
---|
| 96 | if(function is Sinus) return ((Sinus)function).ExportToHL2(tree);
|
---|
| 97 | if(function is Sqrt) return ((Sqrt)function).ExportToHL2(tree);
|
---|
| 98 | if(function is Subtraction) return ((Subtraction)function).ExportToHL2(tree);
|
---|
| 99 | if(function is Tangens) return ((Tangens)function).ExportToHL2(tree);
|
---|
| 100 | if(function is Variable) return ((Variable)function).ExportToHL2(tree);
|
---|
| 101 | if(function is Xor) return ((Xor)function).ExportToHL2(tree);
|
---|
| 102 | throw new UnknownFunctionException(function.Name);
|
---|
[645] | 103 | }
|
---|
| 104 |
|
---|
[654] | 105 | #region IFunctionTreeNameGenerator Members
|
---|
| 106 |
|
---|
| 107 | string IFunctionTreeNameGenerator.Name {
|
---|
| 108 | get { return "HL2 Representation"; }
|
---|
[645] | 109 | }
|
---|
| 110 |
|
---|
[654] | 111 | public string GetName(IFunctionTree tree) {
|
---|
| 112 | string name = "";
|
---|
| 113 | try {
|
---|
| 114 | name = ExportFunction(tree.Function, tree);
|
---|
| 115 | } catch(UnknownFunctionException) {
|
---|
| 116 | name = "N/A";
|
---|
| 117 | }
|
---|
| 118 | return name;
|
---|
[645] | 119 | }
|
---|
| 120 |
|
---|
[654] | 121 | #endregion
|
---|
[2222] | 122 |
|
---|
| 123 |
|
---|
[654] | 124 | }
|
---|
| 125 |
|
---|
| 126 | internal static class HL2ExporterExtensions {
|
---|
| 127 | private static string GetHL2FunctionName(string name) {
|
---|
| 128 | return "[F]" + name ;
|
---|
[645] | 129 | }
|
---|
| 130 |
|
---|
[654] | 131 | public static string ExportToHL2(this Addition addition, IFunctionTree tree) {
|
---|
| 132 | return GetHL2FunctionName("Addition[0]");
|
---|
| 133 | }
|
---|
[645] | 134 |
|
---|
[654] | 135 | public static string ExportToHL2(this Constant constant, IFunctionTree tree) {
|
---|
[2222] | 136 | double value = ((ConstantFunctionTree)tree).Value;
|
---|
[654] | 137 | return "[T]Constant(" + value.ToString("r") + ";0;0)";
|
---|
[645] | 138 | }
|
---|
| 139 |
|
---|
[654] | 140 | public static string ExportToHL2(this Cosinus cosinus, IFunctionTree tree) {
|
---|
| 141 | return GetHL2FunctionName("Trigonometrics[1]");
|
---|
[645] | 142 | }
|
---|
| 143 |
|
---|
[654] | 144 | public static string ExportToHL2(this Differential differential, IFunctionTree tree) {
|
---|
[2222] | 145 | var varTree = (VariableFunctionTree)tree;
|
---|
| 146 | return "[T]Differential(" + varTree.Weight.ToString("r") + ";" + varTree.VariableName + ";" + -varTree.SampleOffset + ")";
|
---|
[645] | 147 | }
|
---|
| 148 |
|
---|
[654] | 149 | public static string ExportToHL2(this Division division, IFunctionTree tree) {
|
---|
| 150 | return GetHL2FunctionName("Division[0]");
|
---|
[645] | 151 | }
|
---|
| 152 |
|
---|
[654] | 153 | public static string ExportToHL2(this Exponential exponential, IFunctionTree tree) {
|
---|
| 154 | return GetHL2FunctionName("Exponential[0]");
|
---|
[645] | 155 | }
|
---|
| 156 |
|
---|
[654] | 157 | public static string ExportToHL2(this Logarithm logarithm, IFunctionTree tree) {
|
---|
| 158 | return GetHL2FunctionName("Logarithm[0]");
|
---|
[645] | 159 | }
|
---|
| 160 |
|
---|
[654] | 161 | public static string ExportToHL2(this Multiplication multiplication, IFunctionTree tree) {
|
---|
| 162 | return GetHL2FunctionName("Multiplication[0]");
|
---|
[645] | 163 | }
|
---|
| 164 |
|
---|
[654] | 165 | public static string ExportToHL2(this Power power, IFunctionTree tree) {
|
---|
| 166 | return GetHL2FunctionName("Power[0]");
|
---|
[645] | 167 | }
|
---|
| 168 |
|
---|
[654] | 169 | public static string ExportToHL2(this Signum signum, IFunctionTree tree) {
|
---|
| 170 | return GetHL2FunctionName("Signum[0]");
|
---|
[645] | 171 | }
|
---|
| 172 |
|
---|
[654] | 173 | public static string ExportToHL2(this Sinus sinus, IFunctionTree tree) {
|
---|
| 174 | return GetHL2FunctionName("Trigonometrics[0]");
|
---|
[645] | 175 | }
|
---|
| 176 |
|
---|
[654] | 177 | public static string ExportToHL2(this Sqrt sqrt, IFunctionTree tree) {
|
---|
| 178 | return GetHL2FunctionName("Sqrt[0]");
|
---|
[645] | 179 | }
|
---|
| 180 |
|
---|
[654] | 181 | public static string ExportToHL2(this Subtraction substraction, IFunctionTree tree) {
|
---|
| 182 | return GetHL2FunctionName("Subtraction[0]");
|
---|
| 183 | }
|
---|
[645] | 184 |
|
---|
[654] | 185 | public static string ExportToHL2(this Tangens tangens, IFunctionTree tree) {
|
---|
| 186 | return GetHL2FunctionName("Trigonometrics[2]");
|
---|
[645] | 187 | }
|
---|
| 188 |
|
---|
[654] | 189 | public static string ExportToHL2(this Variable variable, IFunctionTree tree) {
|
---|
[2222] | 190 | var varTree = (VariableFunctionTree)tree;
|
---|
| 191 | return "[T]Variable(" + varTree.Weight.ToString("r") + ";" + varTree.VariableName + ";" + -varTree.SampleOffset + ")";
|
---|
[645] | 192 | }
|
---|
| 193 |
|
---|
[654] | 194 | public static string ExportToHL2(this And and, IFunctionTree tree) {
|
---|
| 195 | return GetHL2FunctionName("Logical[0]");
|
---|
[645] | 196 | }
|
---|
| 197 |
|
---|
[654] | 198 | public static string ExportToHL2(this Average average, IFunctionTree tree) {
|
---|
| 199 | return GetHL2FunctionName("Average[0]");
|
---|
[645] | 200 | }
|
---|
| 201 |
|
---|
[654] | 202 | public static string ExportToHL2(this IfThenElse ifThenElse, IFunctionTree tree) {
|
---|
| 203 | return GetHL2FunctionName("Conditional[0]");
|
---|
[645] | 204 | }
|
---|
| 205 |
|
---|
[654] | 206 | public static string ExportToHL2(this Not not, IFunctionTree tree) {
|
---|
| 207 | return GetHL2FunctionName("Logical[2]");
|
---|
[645] | 208 | }
|
---|
| 209 |
|
---|
[654] | 210 | public static string ExportToHL2(this Or or, IFunctionTree tree) {
|
---|
| 211 | return GetHL2FunctionName("Logical[1]");
|
---|
[645] | 212 | }
|
---|
| 213 |
|
---|
[654] | 214 | public static string ExportToHL2(this Xor xor, IFunctionTree tree) {
|
---|
| 215 | return GetHL2FunctionName("Logical[3]");
|
---|
[645] | 216 | }
|
---|
| 217 |
|
---|
[654] | 218 | public static string ExportToHL2(this Equal equal, IFunctionTree tree) {
|
---|
| 219 | return GetHL2FunctionName("Boolean[2]");
|
---|
[645] | 220 | }
|
---|
| 221 |
|
---|
[654] | 222 | public static string ExportToHL2(this LessThan lessThan, IFunctionTree tree) {
|
---|
| 223 | return GetHL2FunctionName("Boolean[0]");
|
---|
[645] | 224 | }
|
---|
| 225 |
|
---|
[654] | 226 | public static string ExportToHL2(this GreaterThan greaterThan, IFunctionTree tree) {
|
---|
| 227 | return GetHL2FunctionName("Boolean[4]");
|
---|
[645] | 228 | }
|
---|
| 229 | }
|
---|
| 230 | }
|
---|