Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/29/16 10:29:10 (8 years ago)
Author:
gkronber
Message:

#2650: improved handling of factors in ConstantOptimizationEvaluator (create binary indicators only once)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs

    r14251 r14266  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Diagnostics.Contracts;
    2425using System.Linq;
    2526using AutoDiff;
     
    322323        var factorVarNode = node as BinaryFactorVariableTreeNode;
    323324        // factor variable values are only 0 or 1 and set in x accordingly
    324         var par = new AutoDiff.Variable();
    325         parameters.Add(par);
    326         variableNames.Add(varNode.VariableName);
    327         categoricalVariableValues.Add(factorVarNode != null ? factorVarNode.VariableValue : string.Empty);
     325        var varValue = factorVarNode != null ? factorVarNode.VariableValue : string.Empty;
     326        var par = FindOrCreateParameter(varNode.VariableName, varValue, parameters, variableNames, categoricalVariableValues);
    328327
    329328        if (updateVariableWeights) {
     
    340339        var products = new List<Term>();
    341340        foreach (var variableValue in factorVarNode.Symbol.GetVariableValues(factorVarNode.VariableName)) {
    342           var par = new AutoDiff.Variable();
    343           parameters.Add(par);
    344           variableNames.Add(factorVarNode.VariableName);
    345           categoricalVariableValues.Add(variableValue);
     341          var par = FindOrCreateParameter(factorVarNode.VariableName, variableValue, parameters, variableNames, categoricalVariableValues);
    346342
    347343          var wVar = new AutoDiff.Variable();
     
    516512      term = null;
    517513      return false;
     514    }
     515
     516    // for each factor variable value we need a parameter which represents a binary indicator for that variable & value combination
     517    // each binary indicator is only necessary once. So we only create a parameter if this combination is not yet available
     518    private static Term FindOrCreateParameter(string varName, string varValue,
     519      List<AutoDiff.Variable> parameters, List<string> variableNames, List<string> variableValues) {
     520      Contract.Assert(variableNames.Count == variableValues.Count);
     521      int idx = -1;
     522      for (int i = 0; i < variableNames.Count; i++) {
     523        if (variableNames[i] == varName && variableValues[i] == varValue) {
     524          idx = i;
     525          break;
     526        }
     527      }
     528
     529      AutoDiff.Variable par = null;
     530      if (idx == -1) {
     531        // not found -> create new parameter and entries in names and values lists
     532        par = new AutoDiff.Variable();
     533        parameters.Add(par);
     534        variableNames.Add(varName);
     535        variableValues.Add(varValue);
     536      } else {
     537        par = parameters[idx];
     538      }
     539      return par;
    518540    }
    519541
Note: See TracChangeset for help on using the changeset viewer.