Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14576


Ignore:
Timestamp:
01/15/17 14:17:06 (7 years ago)
Author:
bburlacu
Message:

#1772: Merge trunk changes.

Location:
branches/HeuristicLab.EvolutionTracking
Files:
23 edited
3 copied

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding

  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views

    • Property svn:mergeinfo changed (with no actual effect on merging)
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/SymbolicExpressionGrammarBase.cs

    r14312 r14576  
    8282    protected SymbolicExpressionGrammarBase(bool deserializing)
    8383      : base(deserializing) {
    84       cachedMinExpressionLength = new Dictionary<string, int>();
    85       cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>();
    86       cachedMinExpressionDepth = new Dictionary<string, int>();
    87       cachedMaxExpressionDepth = new Dictionary<string, int>();
    88 
    89       cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();
    90       cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();
    9184
    9285      symbols = new Dictionary<string, ISymbol>();
     
    10093    protected SymbolicExpressionGrammarBase(SymbolicExpressionGrammarBase original, Cloner cloner)
    10194      : base(original, cloner) {
    102       cachedMinExpressionLength = new Dictionary<string, int>();
    103       cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>();
    104       cachedMinExpressionDepth = new Dictionary<string, int>();
    105       cachedMaxExpressionDepth = new Dictionary<string, int>();
    106 
    107       cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();
    108       cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();
    10995
    11096      symbols = original.symbols.ToDictionary(x => x.Key, y => cloner.Clone(y.Value));
     
    124110    protected SymbolicExpressionGrammarBase(string name, string description)
    125111      : base(name, description) {
    126       cachedMinExpressionLength = new Dictionary<string, int>();
    127       cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>();
    128       cachedMinExpressionDepth = new Dictionary<string, int>();
    129       cachedMaxExpressionDepth = new Dictionary<string, int>();
    130 
    131       cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();
    132       cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();
    133 
    134112      symbols = new Dictionary<string, ISymbol>();
    135113      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>();
     
    322300    }
    323301
    324     private readonly Dictionary<Tuple<string, string>, bool> cachedIsAllowedChildSymbol;
     302    private readonly Dictionary<Tuple<string, string>, bool> cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();
    325303    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
    326304      if (allowedChildSymbols.Count == 0) return false;
     
    352330    }
    353331
    354     private readonly Dictionary<Tuple<string, string, int>, bool> cachedIsAllowedChildSymbolIndex;
     332    private readonly Dictionary<Tuple<string, string, int>, bool> cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();
    355333    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
    356334      if (!child.Enabled) return false;
     
    412390    }
    413391
    414     private readonly Dictionary<string, int> cachedMinExpressionLength;
     392    private readonly Dictionary<string, int> cachedMinExpressionLength = new Dictionary<string, int>();
    415393    public int GetMinimumExpressionLength(ISymbol symbol) {
    416394      int res;
     
    423401        if (cachedMinExpressionLength.TryGetValue(symbol.Name, out res)) return res;
    424402
    425         res = GetMinimumExpressionLengthRec(symbol);
    426         foreach (var entry in cachedMinExpressionLength.Where(e => e.Value >= int.MaxValue).ToList()) {
    427           if (entry.Key != symbol.Name) cachedMinExpressionLength.Remove(entry.Key);
    428         }
    429         return res;
    430       }
    431     }
    432 
    433     private int GetMinimumExpressionLengthRec(ISymbol symbol) {
    434       int temp;
    435       if (!cachedMinExpressionLength.TryGetValue(symbol.Name, out temp)) {
    436         cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
    437         long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
    438                                               let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
    439                                                                       where s.InitialFrequency > 0.0
    440                                                                       select GetMinimumExpressionLengthRec(s)).DefaultIfEmpty(0).Min()
    441                                               select minForSlot).DefaultIfEmpty(0).Sum();
    442 
    443         cachedMinExpressionLength[symbol.Name] = (int)Math.Min(sumOfMinExpressionLengths, int.MaxValue);
     403        GrammarUtils.CalculateMinimumExpressionLengths(this, cachedMinExpressionLength);
    444404        return cachedMinExpressionLength[symbol.Name];
    445405      }
    446       return temp;
    447     }
    448 
    449     private readonly Dictionary<Tuple<string, int>, int> cachedMaxExpressionLength;
     406    }
     407
     408
     409    private readonly Dictionary<Tuple<string, int>, int> cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>();
    450410    public int GetMaximumExpressionLength(ISymbol symbol, int maxDepth) {
    451411      int temp;
     
    469429    }
    470430
    471     private readonly Dictionary<string, int> cachedMinExpressionDepth;
     431    private readonly Dictionary<string, int> cachedMinExpressionDepth = new Dictionary<string, int>();
    472432    public int GetMinimumExpressionDepth(ISymbol symbol) {
    473433      int res;
     
    480440        if (cachedMinExpressionDepth.TryGetValue(symbol.Name, out res)) return res;
    481441
    482         res = GetMinimumExpressionDepthRec(symbol);
    483         foreach (var entry in cachedMinExpressionDepth.Where(e => e.Value >= int.MaxValue).ToList()) {
    484           if (entry.Key != symbol.Name) cachedMinExpressionDepth.Remove(entry.Key);
    485         }
    486         return res;
    487       }
    488     }
    489     private int GetMinimumExpressionDepthRec(ISymbol symbol) {
    490       int temp;
    491       if (!cachedMinExpressionDepth.TryGetValue(symbol.Name, out temp)) {
    492         cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion
    493         long minDepth = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
    494                              let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
    495                                                      where s.InitialFrequency > 0.0
    496                                                      select GetMinimumExpressionDepthRec(s)).DefaultIfEmpty(0).Min()
    497                              select minForSlot).DefaultIfEmpty(0).Max();
    498         cachedMinExpressionDepth[symbol.Name] = (int)Math.Min(minDepth, int.MaxValue);
     442        GrammarUtils.CalculateMinimumExpressionDepth(this, cachedMinExpressionDepth);
    499443        return cachedMinExpressionDepth[symbol.Name];
    500444      }
    501       return temp;
    502     }
    503 
    504     private readonly Dictionary<string, int> cachedMaxExpressionDepth;
     445    }
     446
     447    private readonly Dictionary<string, int> cachedMaxExpressionDepth = new Dictionary<string, int>();
    505448    public int GetMaximumExpressionDepth(ISymbol symbol) {
    506449      int temp;
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj

    r13482 r14576  
    181181    <Compile Include="ArchitectureManipulators\SubroutineDuplicater.cs" />
    182182    <Compile Include="ArchitectureManipulators\SymbolicExpressionTreeArchitectureManipulator.cs" />
     183    <Compile Include="Grammars\GrammarUtils.cs" />
    183184    <Compile Include="SymbolicExpressionTreeProblem.cs" />
    184185    <Compile Include="Compiler\Instruction.cs" />
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeEncoding.cs

    r14312 r14576  
    164164    public SymbolicExpressionTreeEncoding(string name, ISymbolicExpressionGrammar grammar, int maximumLength, int maximumDepth)
    165165      : base(name) {
    166       treeLengthParameter = new FixedValueParameter<IntValue>("Maximum Tree Length", "Maximal length of the symbolic expression.", new IntValue(maximumLength));
    167       treeDepthParameter = new FixedValueParameter<IntValue>("Maximum Tree Depth", "Maximal depth of the symbolic expression. The minimum depth needed for the algorithm is 3 because two levels are reserved for the ProgramRoot and the Start symbol.", new IntValue(maximumDepth));
    168       grammarParameter = new ValueParameter<ISymbolicExpressionGrammar>("Grammar", "The grammar that should be used for symbolic expression tree.", grammar);
    169       functionDefinitionsParameter = new FixedValueParameter<IntValue>("Function Definitions", "Maximal number of automatically defined functions", new IntValue(0));
    170       functionArgumentsParameter = new FixedValueParameter<IntValue>("Function Arguments", "Maximal number of arguments of automatically defined functions.", new IntValue(0));
     166      treeLengthParameter = new FixedValueParameter<IntValue>(Name + ".Maximum Tree Length", "Maximal length of the symbolic expression.", new IntValue(maximumLength));
     167      treeDepthParameter = new FixedValueParameter<IntValue>(Name + ".Maximum Tree Depth", "Maximal depth of the symbolic expression. The minimum depth needed for the algorithm is 3 because two levels are reserved for the ProgramRoot and the Start symbol.", new IntValue(maximumDepth));
     168      grammarParameter = new ValueParameter<ISymbolicExpressionGrammar>(Name + ".Grammar", "The grammar that should be used for symbolic expression tree.", grammar);
     169      functionDefinitionsParameter = new FixedValueParameter<IntValue>(Name + ".Function Definitions", "Maximal number of automatically defined functions", new IntValue(0));
     170      functionArgumentsParameter = new FixedValueParameter<IntValue>(Name + ".Function Arguments", "Maximal number of arguments of automatically defined functions.", new IntValue(0));
    171171
    172172      Parameters.Add(treeLengthParameter);
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/SimpleSymbol.cs

    r14312 r14576  
    6161    }
    6262
     63    public SimpleSymbol(string name, int arity)
     64      : this(name, string.Empty, arity, arity) {
     65    }
     66
    6367    public SimpleSymbol(string name, string description, int minimumArity, int maximumArity)
    6468      : base(name, description) {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views

  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs

    r14312 r14576  
    112112      treeChart.Tree = tree;
    113113      treeChart.Repaint();
    114       bool valid = !tree.IterateNodesPostfix().Any(node => node.SubtreeCount < GetMinArity(node.Symbol) || node.SubtreeCount > node.Symbol.MaximumArity);
     114      // check if all nodes have a legal arity
     115      var nodes = tree.IterateNodesPostfix().ToList();
     116      bool valid = !nodes.Any(node => node.SubtreeCount < GetMinArity(node.Symbol) || node.SubtreeCount > node.Symbol.MaximumArity);
     117
     118      if (valid) {
     119        // check if all variables are contained in the dataset
     120        var variables = new HashSet<string>(Content.ProblemData.Dataset.DoubleVariables);
     121        valid = nodes.OfType<VariableTreeNode>().All(x => variables.Contains(x.VariableName));
     122      }
     123
    115124      if (valid) {
    116125        btnOptimizeConstants.Enabled = true;
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicExpressionTreeChart.cs

    r14312 r14576  
    102102        if (dialog.DialogResult != DialogResult.OK) return;
    103103
    104         var symbol = dialog.SelectedSymbol();
     104        var symbol = dialog.SelectedSymbol;
    105105        var node = symbol.CreateTreeNode();
    106106        if (node is ConstantTreeNode) {
     
    110110          var variable = node as VariableTreeNode;
    111111          variable.Weight = double.Parse(dialog.variableWeightTextBox.Text);
    112           variable.VariableName = dialog.variableNamesCombo.Text;
     112          variable.VariableName = dialog.SelectedVariableName;
    113113        } else if (node.Symbol.MinimumArity <= parent.SubtreeCount && node.Symbol.MaximumArity >= parent.SubtreeCount) {
    114114          for (int i = parent.SubtreeCount - 1; i >= 0; --i) {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/TreeEditDialogs/SymbolicExpressionTreeNodeInsertDialog.Designer.cs

    r14312 r14576  
    5757      this.okButton = new System.Windows.Forms.Button();
    5858      this.cancelButton = new System.Windows.Forms.Button();
     59      this.variableNameTextBox = new System.Windows.Forms.TextBox();
    5960      ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit();
    6061      this.SuspendLayout();
     
    168169      this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
    169170      //
     171      // variableNameTextBox
     172      //
     173      this.variableNameTextBox.Location = new System.Drawing.Point(101, 63);
     174      this.variableNameTextBox.Name = "variableNameTextBox";
     175      this.variableNameTextBox.Size = new System.Drawing.Size(127, 20);
     176      this.variableNameTextBox.TabIndex = 11;
     177      this.variableNameTextBox.Visible = false;
     178      //
    170179      // InsertNodeDialog
    171180      //
     
    175184      this.ClientSize = new System.Drawing.Size(241, 133);
    176185      this.ControlBox = false;
     186      this.Controls.Add(this.variableNameTextBox);
    177187      this.Controls.Add(this.cancelButton);
    178188      this.Controls.Add(this.okButton);
     
    208218    private System.Windows.Forms.Button cancelButton;
    209219    private System.Windows.Forms.Button okButton;
     220    private System.Windows.Forms.TextBox variableNameTextBox;
    210221  }
    211222}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/TreeEditDialogs/SymbolicExpressionTreeNodeInsertDialog.cs

    r14312 r14576  
    2323using System.Collections.Generic;
    2424using System.ComponentModel;
     25using System.Linq;
    2526using System.Text;
    2627using System.Windows.Forms;
     
    3031namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
    3132  public partial class InsertNodeDialog : Form {
     33    public string SelectedVariableName {
     34      get {
     35        var variable = SelectedSymbol as Variable;
     36        if (variable == null)
     37          return string.Empty;
     38        return variable.VariableNames.Any() ? variableNamesCombo.Text : variableNameTextBox.Text;
     39      }
     40    }
     41
    3242    public InsertNodeDialog() {
    3343      InitializeComponent();
     
    4050    }
    4151
    42     public ISymbol SelectedSymbol() {
    43       return (ISymbol)allowedSymbolsCombo.SelectedItem;
     52    public ISymbol SelectedSymbol {
     53      get { return (ISymbol)allowedSymbolsCombo.SelectedItem; }
    4454    }
    4555
     
    5666        constantValueTextBox.Visible = true;
    5767      } else if (symbol is Variable) {
    58         var variable = (Variable)symbol;
    59         foreach (var name in variable.VariableNames) variableNamesCombo.Items.Add(name);
    60         variableNamesCombo.SelectedIndex = 0;
     68        var variableSymbol = (Variable)symbol;
     69        if (variableSymbol.VariableNames.Any()) {
     70          foreach (var name in variableSymbol.VariableNames)
     71            variableNamesCombo.Items.Add(name);
     72          variableNamesCombo.SelectedIndex = 0;
     73          variableNamesCombo.Visible = true;
     74          variableNameTextBox.Visible = false;
     75        } else {
     76          variableNamesCombo.Visible = false;
     77          variableNameTextBox.Visible = true;
     78        }
    6179        variableNameLabel.Visible = true;
    62         variableNamesCombo.Visible = true;
    6380        variableWeightLabel.Visible = true;
    6481        variableWeightTextBox.Visible = true;
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/TreeEditDialogs/SymbolicExpressionTreeVariableNodeEditDialog.Designer.cs

    r14312 r14576  
    5555      this.okButton = new System.Windows.Forms.Button();
    5656      this.cancelButton = new System.Windows.Forms.Button();
     57      this.variableNameTextBox = new System.Windows.Forms.TextBox();
    5758      ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit();
    5859      this.SuspendLayout();
     
    146147      this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
    147148      //
     149      // variableNameTextBox
     150      //
     151      this.variableNameTextBox.Location = new System.Drawing.Point(101, 12);
     152      this.variableNameTextBox.Name = "variableNameTextBox";
     153      this.variableNameTextBox.Size = new System.Drawing.Size(131, 20);
     154      this.variableNameTextBox.TabIndex = 12;
     155      this.variableNameTextBox.Visible = false;
     156      //
    148157      // VariableNodeEditDialog
    149158      //
     
    154163      this.ClientSize = new System.Drawing.Size(244, 134);
    155164      this.ControlBox = false;
     165      this.Controls.Add(this.variableNameTextBox);
    156166      this.Controls.Add(this.cancelButton);
    157167      this.Controls.Add(this.okButton);
     
    188198    public System.Windows.Forms.TextBox newValueTextBox;
    189199    public System.Windows.Forms.ComboBox variableNamesCombo;
     200    private System.Windows.Forms.TextBox variableNameTextBox;
    190201  }
    191202}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/TreeEditDialogs/SymbolicExpressionTreeVariableNodeEditDialog.cs

    r14312 r14576  
    2222using System;
    2323using System.ComponentModel;
     24using System.Linq;
    2425using System.Text;
    2526using System.Windows.Forms;
     
    4041    }
    4142
     43    public string SelectedVariableName {
     44      get { return variableNamesCombo.Visible ? variableNamesCombo.Text : variableNameTextBox.Text; }
     45    }
     46
    4247    public VariableNodeEditDialog(ISymbolicExpressionTreeNode node) {
    4348      InitializeComponent();
    4449      oldValueTextBox.TabStop = false; // cannot receive focus using tab key
    45 
    4650      NewNode = (VariableTreeNode)node; // will throw an invalid cast exception if node is not of the correct type
    4751      InitializeFields();
     
    5761        variableNameLabel.Visible = true;
    5862        variableNamesCombo.Visible = true;
    59         foreach (var name in variableTreeNode.Symbol.VariableNames) variableNamesCombo.Items.Add(name);
    60         variableNamesCombo.SelectedIndex = variableNamesCombo.Items.IndexOf(variableTreeNode.VariableName);
     63        if (variableTreeNode.Symbol.VariableNames.Any()) {
     64          foreach (var name in variableTreeNode.Symbol.VariableNames)
     65            variableNamesCombo.Items.Add(name);
     66          variableNamesCombo.SelectedIndex = variableNamesCombo.Items.IndexOf(variableTreeNode.VariableName);
     67          variableNamesCombo.Visible = true;
     68          variableNameTextBox.Visible = false;
     69        } else {
     70          variableNamesCombo.Visible = false;
     71          variableNameTextBox.Visible = true;
     72        }
    6173      }
    6274    }
     
    93105    #region combo box validation and events
    94106    private void variableNamesCombo_Validating(object sender, CancelEventArgs e) {
     107      if (variableNamesCombo.Items.Count == 0) return;
    95108      if (variableNamesCombo.Items.Contains(variableNamesCombo.SelectedItem)) return;
    96109      e.Cancel = true;
     
    119132    private void OnDialogValidated(object sender, EventArgs e) {
    120133      double weight = double.Parse(newValueTextBox.Text);
    121       var variableName = (string)variableNamesCombo.SelectedItem;
    122134      // we impose an extra validation condition: that the weight/value be different than the original ones
     135      var variableName = SelectedVariableName;
    123136      if (variableTreeNode.Weight.Equals(weight) && variableTreeNode.VariableName.Equals(variableName)) return;
    124137      variableTreeNode.Weight = weight;
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs

    r14307 r14576  
    8181          }
    8282          strBuilder.Append(")");
     83        } else {
     84          // function with multiple arguments
     85          strBuilder.Append(token).Append("(");
     86          FormatRecursively(node.Subtrees.First(), strBuilder);
     87          foreach (var subtree in node.Subtrees.Skip(1)) {
     88            strBuilder.Append(", ");
     89            FormatRecursively(subtree, strBuilder);
     90          }
     91          strBuilder.Append(")");
    8392        }
    8493      } else if (node.SubtreeCount == 1) {
     
    94103          FormatRecursively(node.GetSubtree(0), strBuilder);
    95104        } else {
    96           // function
     105          // function with only one argument
    97106          strBuilder.Append(token).Append("(");
    98107          FormatRecursively(node.GetSubtree(0), strBuilder);
     
    101110      } else {
    102111        // no subtrees
    103         if (node.Symbol is Variable) {
     112        if (node.Symbol is LaggedVariable) {
     113          var varNode = node as LaggedVariableTreeNode;
     114          if (!varNode.Weight.IsAlmost(1.0)) {
     115            strBuilder.Append("(");
     116            strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight);
     117            strBuilder.Append("*");
     118          }
     119          strBuilder.Append("LAG(");
     120          if (varNode.VariableName.Contains("'")) {
     121            strBuilder.AppendFormat("\"{0}\"", varNode.VariableName);
     122          } else {
     123            strBuilder.AppendFormat("'{0}'", varNode.VariableName);
     124          }
     125          strBuilder.Append(", ")
     126            .AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Lag)
     127            .Append(")");
     128        } else if (node.Symbol is Variable) {
    104129          var varNode = node as VariableTreeNode;
    105130          if (!varNode.Weight.IsAlmost(1.0)) {
     
    121146            strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constNode.Value);
    122147          else
    123             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "({0})", constNode.Value);     // (-1)
     148            strBuilder.AppendFormat(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1
    124149        }
    125150      }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs

    r14312 r14576  
    6767        strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root));
    6868        return strBuilder.ToString();
    69       }
    70       catch (NotImplementedException ex) {
     69      } catch (NotImplementedException ex) {
    7170        return ex.Message + Environment.NewLine + ex.StackTrace;
    7271      }
     
    109108      } else if (node.Symbol is Division) {
    110109        if (node.SubtreeCount == 1) {
    111           strBuilder.Append(@" \cfrac{1");
     110          strBuilder.Append(@" \cfrac{1}{");
    112111        } else {
    113112          strBuilder.Append(@" \cfrac{ ");
     
    176175        strBuilder.Append(@" \operatorname{if}  \left( ");
    177176      } else if (node.Symbol is Constant) {
    178         strBuilder.Append("c_{" + constants.Count + "} ");
    179177        var constNode = node as ConstantTreeNode;
    180         constants.Add(constNode.Value);
     178        if (constNode.Value.IsAlmost(1.0)) {
     179          strBuilder.Append("1 ");
     180        } else {
     181          strBuilder.Append("c_{" + constants.Count + "} ");
     182          constants.Add(constNode.Value);
     183        }
    181184      } else if (node.Symbol is LaggedVariable) {
    182185        var laggedVarNode = node as LaggedVariableTreeNode;
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs

    r14307 r14576  
    2626using System.Text;
    2727using HeuristicLab.Collections;
     28using HeuristicLab.Common;
    2829using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2930
     
    3738  /// </summary>
    3839  public sealed class InfixExpressionParser {
    39     private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, End, NA };
     40    private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, Comma, End, NA };
    4041    private class Token {
    4142      internal double doubleVal;
     
    102103        { "MEAN", new Average()},
    103104        { "IF", new IfThenElse()},
    104         { ">", new GreaterThan()},
    105         { "<", new LessThan()},
     105        { "GT", new GreaterThan()},
     106        { "LT", new LessThan()},
    106107        { "AND", new And()},
    107108        { "OR", new Or()},
     
    109110        { "XOR", new Xor()},
    110111        { "DIFF", new Derivative()},
     112        { "LAG", new LaggedVariable() },
    111113      };
    112114
     
    138140        }
    139141        if (char.IsDigit(str[pos])) {
    140           // read number (=> read until white space or operator)
     142          // read number (=> read until white space or operator or comma)
    141143          var sb = new StringBuilder();
    142144          sb.Append(str[pos]);
    143145          pos++;
    144146          while (pos < str.Length && !char.IsWhiteSpace(str[pos])
    145             && (str[pos] != '+' || str[pos-1] == 'e' || str[pos-1] == 'E')     // continue reading exponents
     147            && (str[pos] != '+' || str[pos - 1] == 'e' || str[pos - 1] == 'E')     // continue reading exponents
    146148            && (str[pos] != '-' || str[pos - 1] == 'e' || str[pos - 1] == 'E')
    147             && str[pos] != '*'           
     149            && str[pos] != '*'
    148150            && str[pos] != '/'
    149             && str[pos] != ')') {
     151            && str[pos] != ')'
     152            && str[pos] != ',') {
    150153            sb.Append(str[pos]);
    151154            pos++;
     
    211214          pos++;
    212215          yield return new Token { TokenType = TokenType.RightPar, strVal = ")" };
    213         }
    214       }
    215     }
    216 
    217     // S = Expr EOF
    218     // Expr = ['-' | '+'] Term { '+' Term | '-' Term }
    219     // Term = Fact { '*' Fact | '/' Fact }
    220     // Fact = '(' Expr ')' | funcId '(' Expr ')' | varId | number
     216        } else if (str[pos] == ',') {
     217          pos++;
     218          yield return new Token { TokenType = TokenType.Comma, strVal = "," };
     219        } else {
     220          throw new ArgumentException("Invalid character: " + str[pos]);
     221        }
     222      }
     223    }
     224
     225    // S       = Expr EOF
     226    // Expr    = ['-' | '+'] Term { '+' Term | '-' Term }
     227    // Term    = Fact { '*' Fact | '/' Fact }
     228    // Fact    = '(' Expr ')' | funcId '(' ArgList ')' | varId | number
     229    // ArgList = Expr { ',' Expr }
    221230    private ISymbolicExpressionTreeNode ParseS(Queue<Token> tokens) {
    222231      var expr = ParseExpr(tokens);
     
    326335    }
    327336
    328     // Fact = '(' Expr ')' | funcId '(' Expr ')' | varId | number
     337    // Fact = '(' Expr ')' | 'LAG' '(' varId ',' ['+' | '-'] number ')' | funcId '(' ArgList ')' | varId | number
    329338    private ISymbolicExpressionTreeNode ParseFact(Queue<Token> tokens) {
    330339      var next = tokens.Peek();
     
    346355          if (lPar.TokenType != TokenType.LeftPar)
    347356            throw new ArgumentException("expected (");
    348           var expr = ParseExpr(tokens);
     357
     358          // handle 'lag' specifically
     359          if (funcNode.Symbol is LaggedVariable) {
     360            var varId = tokens.Dequeue();
     361            if (varId.TokenType != TokenType.Identifier) throw new ArgumentException("Identifier expected. Format for lagged variables: \"lag(x, -1)\"");
     362            var comma = tokens.Dequeue();
     363            if (comma.TokenType != TokenType.Comma) throw new ArgumentException("',' expected, Format for lagged variables: \"lag(x, -1)\"");
     364            double sign = 1.0;
     365            if (tokens.Peek().strVal == "+" || tokens.Peek().strVal == "-") {
     366              // read sign
     367              var signTok = tokens.Dequeue();
     368              if (signTok.strVal == "-") sign = -1.0;
     369            }
     370            var lagToken = tokens.Dequeue();
     371            if (lagToken.TokenType != TokenType.Number) throw new ArgumentException("Number expected, Format for lagged variables: \"lag(x, -1)\"");
     372            if (!lagToken.doubleVal.IsAlmost(Math.Round(lagToken.doubleVal)))
     373              throw new ArgumentException("Time lags must be integer values");
     374            var laggedVarNode = funcNode as LaggedVariableTreeNode;
     375            laggedVarNode.VariableName = varId.strVal;
     376            laggedVarNode.Lag = (int)Math.Round(sign * lagToken.doubleVal);
     377            laggedVarNode.Weight = 1.0;
     378          } else {
     379            // functions
     380            var args = ParseArgList(tokens);
     381            // check number of arguments
     382            if (funcNode.Symbol.MinimumArity > args.Length || funcNode.Symbol.MaximumArity < args.Length) {
     383              throw new ArgumentException(string.Format("Symbol {0} requires between {1} and  {2} arguments.", funcId,
     384                funcNode.Symbol.MinimumArity, funcNode.Symbol.MaximumArity));
     385            }
     386            foreach (var arg in args) funcNode.AddSubtree(arg);
     387          }
     388
    349389          var rPar = tokens.Dequeue();
    350390          if (rPar.TokenType != TokenType.RightPar)
    351391            throw new ArgumentException("expected )");
    352392
    353           funcNode.AddSubtree(expr);
    354393          return funcNode;
    355394        } else {
     
    369408      }
    370409    }
     410
     411    // ArgList = Expr { ',' Expr }
     412    private ISymbolicExpressionTreeNode[] ParseArgList(Queue<Token> tokens) {
     413      var exprList = new List<ISymbolicExpressionTreeNode>();
     414      exprList.Add(ParseExpr(tokens));
     415      while (tokens.Peek().TokenType != TokenType.RightPar) {
     416        var comma = tokens.Dequeue();
     417        if (comma.TokenType != TokenType.Comma) throw new ArgumentException("expected ',' ");
     418        exprList.Add(ParseExpr(tokens));
     419      }
     420      return exprList.ToArray();
     421    }
    371422  }
    372423}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs

    r14312 r14576  
    3535    private const string INVOKESTART = "CALL";
    3636    private const string TIMELAGSTART = "LAG";
    37     private Dictionary<string, Symbol> knownSymbols = new Dictionary<string, Symbol>() 
     37    private Dictionary<string, Symbol> knownSymbols = new Dictionary<string, Symbol>()
    3838      {
    3939        {"+", new Addition()},
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs

    r14312 r14576  
    614614        case OpCodes.VariableCondition: {
    615615            var variableConditionTreeNode = (VariableConditionTreeNode)node;
     616            if (variableConditionTreeNode.Symbol.IgnoreSlope) throw new NotSupportedException("Strict variable conditionals are not supported");
    616617            var variableName = variableConditionTreeNode.VariableName;
    617618            var indexExpr = Expression.Constant(variableIndices[variableName]);
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs

    r14312 r14576  
    471471            if (row < 0 || row >= dataset.Rows) return double.NaN;
    472472            var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode;
    473             double variableValue = ((IList<double>)currentInstr.data)[row];
    474             double x = variableValue - variableConditionTreeNode.Threshold;
    475             double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
    476 
    477             double trueBranch = Evaluate(dataset, ref row, state);
    478             double falseBranch = Evaluate(dataset, ref row, state);
    479 
    480             return trueBranch * p + falseBranch * (1 - p);
     473            if (!variableConditionTreeNode.Symbol.IgnoreSlope) {
     474              double variableValue = ((IList<double>)currentInstr.data)[row];
     475              double x = variableValue - variableConditionTreeNode.Threshold;
     476              double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
     477
     478              double trueBranch = Evaluate(dataset, ref row, state);
     479              double falseBranch = Evaluate(dataset, ref row, state);
     480
     481              return trueBranch * p + falseBranch * (1 - p);
     482            } else {
     483              // strict threshold
     484              double variableValue = ((IList<double>)currentInstr.data)[row];
     485              if (variableValue <= variableConditionTreeNode.Threshold) {
     486                var left = Evaluate(dataset, ref row, state);
     487                state.SkipInstructions();
     488                return left;
     489              } else {
     490                state.SkipInstructions();
     491                return Evaluate(dataset, ref row, state);
     492              }
     493            }
    481494          }
    482495        default:
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs

    r14312 r14576  
    126126    private readonly object syncRoot = new object();
    127127    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
     128      if (!rows.Any()) return Enumerable.Empty<double>();
    128129      if (CheckExpressionsWithIntervalArithmetic)
    129130        throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
     
    166167          if (row < 0 || row >= dataset.Rows) instr.value = double.NaN;
    167168          var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
    168           double variableValue = ((IList<double>)instr.data)[row];
    169           double x = variableValue - variableConditionTreeNode.Threshold;
    170           double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
    171 
    172           double trueBranch = code[instr.childIndex].value;
    173           double falseBranch = code[instr.childIndex + 1].value;
    174 
    175           instr.value = trueBranch * p + falseBranch * (1 - p);
     169          if (!variableConditionTreeNode.Symbol.IgnoreSlope) {
     170            double variableValue = ((IList<double>)instr.data)[row];
     171            double x = variableValue - variableConditionTreeNode.Threshold;
     172            double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
     173
     174            double trueBranch = code[instr.childIndex].value;
     175            double falseBranch = code[instr.childIndex + 1].value;
     176
     177            instr.value = trueBranch * p + falseBranch * (1 - p);
     178          } else {
     179            double variableValue = ((IList<double>)instr.data)[row];
     180            if (variableValue <= variableConditionTreeNode.Threshold) {
     181              instr.value = code[instr.childIndex].value;
     182            } else {
     183              instr.value = code[instr.childIndex + 1].value;
     184            }
     185          }
    176186        } else if (instr.opCode == OpCodes.Add) {
    177187          double s = code[instr.childIndex].value;
     
    420430              for (int j = 1; j != seq.Length; ++j)
    421431                seq[j].skip = true;
    422             }
    423             break;
     432              break;
     433            }
    424434        }
    425435        #endregion
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VariableCondition.cs

    r14312 r14576  
    149149        }
    150150      }
     151    }
     152
     153    /// <summary>
     154    /// Flag to indicate if the interpreter should ignore the slope parameter (introduced for representation of expression trees)
     155    /// </summary>
     156    [Storable]
     157    private bool ignoreSlope;
     158    public bool IgnoreSlope {
     159      get { return ignoreSlope; }
     160      set { ignoreSlope = value; }
    151161    }
    152162
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VariableConditionTreeNode.cs

    r14312 r14576  
    9696
    9797    public override string ToString() {
    98       if (slope.IsAlmost(0.0))
     98      if (slope.IsAlmost(0.0) || Symbol.IgnoreSlope) {
     99        return variableName + " < " + threshold.ToString("E4");
     100      } else {
    99101        return variableName + " > " + threshold.ToString("E4") + Environment.NewLine +
    100           "slope: " + slope.ToString("E4");
    101       else
    102         return variableName + " > " + threshold.ToString("E4");
     102               "slope: " + slope.ToString("E4");
     103      }
    103104    }
    104105  }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.TravelingSalesman

    • Property svn:mergeinfo changed (with no actual effect on merging)
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.TravelingSalesman.Views

    • Property svn:mergeinfo changed (with no actual effect on merging)
Note: See TracChangeset for help on using the changeset viewer.