Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5729


Ignore:
Timestamp:
03/17/11 12:25:30 (13 years ago)
Author:
gkronber
Message:

#1418 moved liner scaling method into symbolic regression model and fixed bug in interactive solution simplifier

Location:
branches/DataAnalysis Refactoring
Files:
1 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs

    r5717 r5729  
    5454    protected override void UpdateModel(ISymbolicExpressionTree tree) {
    5555      Content.Model = new SymbolicRegressionModel(tree, Content.Model.Interpreter);
     56      Content.ScaleModel();
    5657    }
    5758
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj

    r5722 r5729  
    129129    <Compile Include="SingleObjective\SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs" />
    130130    <Compile Include="SymbolicRegressionSolution.cs" />
    131     <Compile Include="SymbolicRegressionSolutionLinearScaler.cs" />
    132131    <None Include="HeuristicLab.snk" />
    133132    <None Include="HeuristicLabProblemsDataAnalysisSymbolicRegressionPlugin.cs.frame" />
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer.cs

    r5722 r5729  
    9090      Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit for the estimated values produced by the symbolic regression model."));
    9191      Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit for the estimated values produced by the symbolic regression model."));
    92       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(false)));
     92      Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));
    9393    }
    9494
     
    9999    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) {
    100100      var model = new SymbolicRegressionModel(bestTree, SymbolicDataAnalysisTreeInterpreter, LowerEstimationLimit.Value, UpperEstimationLimit.Value);
    101       return new SymbolicRegressionSolution(model, ProblemData);
     101      var solution = new SymbolicRegressionSolution(model, ProblemData);
     102      if (ApplyLinearScaling.Value)
     103        solution.ScaleModel();
     104      return solution;
    102105    }
    103106  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer.cs

    r5722 r5729  
    7474      Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit for the estimated values produced by the symbolic regression model."));
    7575      Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit for the estimated values produced by the symbolic regression model."));
    76       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(false)));
     76      Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));
    7777    }
    7878    public override IDeepCloneable Clone(Cloner cloner) {
     
    8282    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) {
    8383      var model = new SymbolicRegressionModel(bestTree, SymbolicDataAnalysisTreeInterpreter, LowerEstimationLimit.Value, UpperEstimationLimit.Value);
    84       return new SymbolicRegressionSolution(model, ProblemData);
     84      var solution = new SymbolicRegressionSolution(model, ProblemData);
     85      if (ApplyLinearScaling.Value)
     86        solution.ScaleModel();
     87      return solution;
    8588    }
    8689  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer.cs

    r5722 r5729  
    8989      Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit for the estimated values produced by the symbolic regression model."));
    9090      Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit for the estimated values produced by the symbolic regression model."));
    91       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(false)));
     91      Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));
    9292    }
    9393    public override IDeepCloneable Clone(Cloner cloner) {
     
    9797    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
    9898      var model = new SymbolicRegressionModel(bestTree, SymbolicDataAnalysisTreeInterpreter, LowerEstimationLimit.Value, UpperEstimationLimit.Value);
    99       return new SymbolicRegressionSolution(model, ProblemData);
     99      var solution = new SymbolicRegressionSolution(model, ProblemData);
     100      if (ApplyLinearScaling.Value)
     101        solution.ScaleModel();
     102      return solution;
    100103    }
    101104  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer.cs

    r5722 r5729  
    7474      Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit for the estimated values produced by the symbolic regression model."));
    7575      Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit for the estimated values produced by the symbolic regression model."));
    76       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(false)));
     76      Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));
    7777    }
    7878
     
    8585      var solution = new SymbolicRegressionSolution(model, ProblemData);
    8686      if (ApplyLinearScaling.Value)
    87         SymbolicRegressionSolutionLinearScaler.Scale(solution);
     87        solution.ScaleModel();
    8888      return solution;
    8989    }
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs

    r5720 r5729  
    5959      return new SymbolicRegressionSolution(this, cloner);
    6060    }
     61
     62    public void ScaleModel() {
     63      var dataset = ProblemData.Dataset;
     64      var targetVariable = ProblemData.TargetVariable;
     65      var rows = ProblemData.TrainingIndizes;
     66      var estimatedValues = GetEstimatedValues(rows);
     67      var targetValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
     68      double alpha;
     69      double beta;
     70      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta);
     71
     72      ConstantTreeNode alphaTreeNode = null;
     73      ConstantTreeNode betaTreeNode = null;
     74      // check if model has been scaled previously by analyzing the structure of the tree
     75      var startNode = Model.SymbolicExpressionTree.Root.GetSubTree(0);
     76      if (startNode.GetSubTree(0).Symbol is Addition) {
     77        var addNode = startNode.GetSubTree(0);
     78        if (addNode.SubtreesCount == 2 && addNode.GetSubTree(0).Symbol is Multiplication && addNode.GetSubTree(1).Symbol is Constant) {
     79          alphaTreeNode = addNode.GetSubTree(1) as ConstantTreeNode;
     80          var mulNode = addNode.GetSubTree(0);
     81          if (mulNode.SubtreesCount == 2 && mulNode.GetSubTree(1).Symbol is Constant) {
     82            betaTreeNode = mulNode.GetSubTree(1) as ConstantTreeNode;
     83          }
     84        }
     85      }
     86      // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
     87      if (alphaTreeNode != null && betaTreeNode != null) {
     88        betaTreeNode.Value *= beta;
     89        alphaTreeNode.Value *= beta;
     90        alphaTreeNode.Value += alpha;
     91      } else {
     92        var mainBranch = startNode.GetSubTree(0);
     93        startNode.RemoveSubTree(0);
     94        var scaledMainBranch = MakeSum(MakeProduct(beta, mainBranch), alpha);
     95        startNode.AddSubTree(scaledMainBranch);
     96      }
     97
     98      OnModelChanged(EventArgs.Empty);
     99    }
     100
     101    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
     102      if (alpha.IsAlmost(0.0)) {
     103        return treeNode;
     104      } else {
     105        var node = (new Addition()).CreateTreeNode();
     106        var alphaConst = MakeConstant(alpha);
     107        node.AddSubTree(treeNode);
     108        node.AddSubTree(alphaConst);
     109        return node;
     110      }
     111    }
     112
     113    private static ISymbolicExpressionTreeNode MakeProduct(double beta, ISymbolicExpressionTreeNode treeNode) {
     114      if (beta.IsAlmost(1.0)) {
     115        return treeNode;
     116      } else {
     117        var node = (new Multiplication()).CreateTreeNode();
     118        var betaConst = MakeConstant(beta);
     119        node.AddSubTree(treeNode);
     120        node.AddSubTree(betaConst);
     121        return node;
     122      }
     123    }
     124
     125    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
     126      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
     127      node.Value = c;
     128      return node;
     129    }
    61130  }
    62131}
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs

    r5722 r5729  
    3333namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
    3434  public abstract partial class InteractiveSymbolicDataAnalysisSolutionSimplifierView : AsynchronousContentView {
    35     private Dictionary<ISymbolicExpressionTreeNode, ConstantTreeNode> replacementNodes;
     35    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> replacementNodes;
    3636    private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
    3737
    3838    public InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
    3939      InitializeComponent();
    40       this.replacementNodes = new Dictionary<ISymbolicExpressionTreeNode, ConstantTreeNode>();
     40      this.replacementNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
    4141      this.nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
    4242      this.Caption = "Interactive Solution Simplifier";
     
    8181
    8282    private void CalculateReplacementNodesAndNodeImpacts() {
    83       replacementNodes.Clear();
    8483      if (Content != null && Content.Model != null && Content.ProblemData != null) {
    8584        var tree = Content.Model.SymbolicExpressionTree;
    8685        var replacementValues = CalculateReplacementValues(tree);
    8786        foreach (var pair in replacementValues) {
    88           replacementNodes.Add(pair.Key, MakeConstantTreeNode(pair.Value));
     87          if (!(pair.Key is ConstantTreeNode)) {
     88            replacementNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
     89          }
    8990        }
    9091        nodeImpacts = CalculateImpactValues(Content.Model.SymbolicExpressionTree);
     
    9697            var child = parent.GetSubTree(subTreeIndex);
    9798            if (!(child.Symbol is Constant) && nodeImpacts[child].IsAlmost(1.0)) {
    98               ReplaceNodeWithConstant(parent, subTreeIndex);
     99              SwitchNodeWithReplacementNode(parent, subTreeIndex);
    99100            }
    100101          }
     
    126127          ISymbolicExpressionTreeNode subTree = treeNode.GetSubTree(i);
    127128          if (subTree == visualTreeNode.SymbolicExpressionTreeNode) {
    128             ReplaceNodeWithConstant(treeNode, i);
     129            SwitchNodeWithReplacementNode(treeNode, i);
    129130          }
    130131        }
     
    138139    }
    139140
    140     private void ReplaceNodeWithConstant(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
     141    private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
    141142      ISymbolicExpressionTreeNode subTree = parent.GetSubTree(subTreeIndex);
    142143      parent.RemoveSubTree(subTreeIndex);
    143       if (replacementNodes.ContainsKey(subTree))
    144         parent.InsertSubTree(subTreeIndex, replacementNodes[subTree]);
    145       else if (subTree is ConstantTreeNode && replacementNodes.ContainsValue((ConstantTreeNode)subTree))
    146         parent.InsertSubTree(subTreeIndex, replacementNodes.Where(v => v.Value == subTree).Single().Key);
    147       else if (!(subTree is ConstantTreeNode))
     144      if (replacementNodes.ContainsKey(subTree)) {
     145        var replacementNode = replacementNodes[subTree];
     146        parent.InsertSubTree(subTreeIndex, replacementNode);
     147        // exchange key and value
     148        replacementNodes.Remove(subTree);
     149        replacementNodes.Add(replacementNode, subTree);
     150      } else {
    148151        throw new InvalidOperationException("Could not find replacement value.");
     152      }
    149153    }
    150154
     
    155159      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
    156160        if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
    157           double impact = this.nodeImpacts[treeNode];
    158           double replacementValue = this.replacementNodes[treeNode].Value;
     161          double impact = nodeImpacts[treeNode];
    159162          VisualSymbolicExpressionTreeNode visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
    160163
     
    163166          // impact > 0 if new solution is worse
    164167          if (impact < 0.0) {
     168            // min is guaranteed to be < 0
    165169            visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
     170          } else if (impact.IsAlmost(0.0)) {
     171            visualTree.FillColor = Color.White;
    166172          } else {
     173            // max is guaranteed to be > 0
    167174            visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
    168175          }
    169176          visualTree.ToolTip += Environment.NewLine + "Node impact: " + impact;
    170           visualTree.ToolTip += Environment.NewLine + "Replacement value: " + replacementValue;
     177          var constantReplacementNode = replacementNodes[treeNode] as ConstantTreeNode;
     178          if (constantReplacementNode != null) {
     179            visualTree.ToolTip += Environment.NewLine + "Replacement value: " + constantReplacementNode.Value;
     180          }
    171181        }
    172182      }
     
    177187    private void PaintCollapsedNodes() {
    178188      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPostfix()) {
    179         if (treeNode is ConstantTreeNode && replacementNodes.ContainsValue((ConstantTreeNode)treeNode))
     189        if (treeNode is ConstantTreeNode && replacementNodes.ContainsKey(treeNode))
    180190          this.treeChart.GetVisualSymbolicExpressionTreeNode(treeNode).LineColor = Color.DarkOrange;
    181191        else {
Note: See TracChangeset for help on using the changeset viewer.