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