Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/24/22 20:33:45 (2 years ago)
Author:
gkronber
Message:

#3136: reintegrated structure-template GP branch into trunk

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic

  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs

    r18174 r18220  
    199199        var tanh = (ISymbolicExpressionTreeNode)branch.Clone();
    200200        return Product(fxp, Subtract(CreateNumber(1.0), Square(tanh)));
     201      }
     202      if (branch.Symbol is SubFunctionSymbol) {
     203        return Derive(branch.GetSubtree(0), variableName);
    201204      }
    202205      throw new NotSupportedException(string.Format("Symbol {0} is not supported.", branch.Symbol));
     
    285288          !(n.Symbol is Cosine) &&
    286289          !(n.Symbol is Tangent) &&
    287           !(n.Symbol is StartSymbol)
     290          !(n.Symbol is StartSymbol) &&
     291          !(n.Symbol is SubFunctionSymbol)
    288292        select n).Any();
    289293      return !containsUnknownSymbol;
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs

    r18132 r18220  
    106106      out ParametricFunctionGradient func_grad) {
    107107
     108      return TryConvertToAutoDiff(tree, makeVariableWeightsVariable, addLinearScalingTerms, Enumerable.Empty<ISymbolicExpressionTreeNode>(),
     109        out parameters, out initialParamValues, out func, out func_grad);
     110    }
     111
     112    public static bool TryConvertToAutoDiff(ISymbolicExpressionTree tree, bool makeVariableWeightsVariable, bool addLinearScalingTerms, IEnumerable<ISymbolicExpressionTreeNode> excludedNodes,
     113      out List<DataForVariable> parameters, out double[] initialParamValues,
     114      out ParametricFunction func,
     115      out ParametricFunctionGradient func_grad) {
     116
    108117      // use a transformator object which holds the state (variable list, parameter list, ...) for recursive transformation of the tree
    109       var transformator = new TreeToAutoDiffTermConverter(makeVariableWeightsVariable, addLinearScalingTerms);
     118      var transformator = new TreeToAutoDiffTermConverter(makeVariableWeightsVariable, addLinearScalingTerms, excludedNodes);
    110119      AutoDiff.Term term;
    111120      try {
     
    134143    private readonly bool makeVariableWeightsVariable;
    135144    private readonly bool addLinearScalingTerms;
    136 
    137     private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable, bool addLinearScalingTerms) {
     145    private readonly HashSet<ISymbolicExpressionTreeNode> excludedNodes;
     146
     147    private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable, bool addLinearScalingTerms, IEnumerable<ISymbolicExpressionTreeNode> excludedNodes) {
    138148      this.makeVariableWeightsVariable = makeVariableWeightsVariable;
    139149      this.addLinearScalingTerms = addLinearScalingTerms;
     150      this.excludedNodes = new HashSet<ISymbolicExpressionTreeNode>(excludedNodes);
     151
    140152      this.initialParamValues = new List<double>();
    141153      this.parameters = new Dictionary<DataForVariable, AutoDiff.Variable>();
     
    161173        var par = FindOrCreateParameter(parameters, varNode.VariableName, varValue);
    162174
    163         if (makeVariableWeightsVariable) {
     175        if (makeVariableWeightsVariable && !excludedNodes.Contains(node)) {
    164176          initialParamValues.Add(varNode.Weight);
    165177          var w = new AutoDiff.Variable();
     
    176188          var par = FindOrCreateParameter(parameters, factorVarNode.VariableName, variableValue);
    177189
    178           initialParamValues.Add(factorVarNode.GetValue(variableValue));
    179           var wVar = new AutoDiff.Variable();
    180           variables.Add(wVar);
    181 
    182           products.Add(AutoDiff.TermBuilder.Product(wVar, par));
     190          if (makeVariableWeightsVariable && !excludedNodes.Contains(node)) {
     191            initialParamValues.Add(factorVarNode.GetValue(variableValue));
     192            var wVar = new AutoDiff.Variable();
     193            variables.Add(wVar);
     194
     195            products.Add(AutoDiff.TermBuilder.Product(wVar, par));
     196          } else {
     197            var weight = factorVarNode.GetValue(variableValue);
     198            products.Add(weight * par);
     199          }
     200
    183201        }
    184202        return AutoDiff.TermBuilder.Sum(products);
     
    188206        var par = FindOrCreateParameter(parameters, varNode.VariableName, string.Empty, varNode.Lag);
    189207
    190         if (makeVariableWeightsVariable) {
     208        if (makeVariableWeightsVariable && !excludedNodes.Contains(node)) {
    191209          initialParamValues.Add(varNode.Weight);
    192210          var w = new AutoDiff.Variable();
     
    305323          return t * alpha + beta;
    306324        } else return ConvertToAutoDiff(node.GetSubtree(0));
     325      }
     326      if (node.Symbol is SubFunctionSymbol) {
     327        return ConvertToAutoDiff(node.GetSubtree(0));
    307328      }
    308329      throw new ConversionException();
     
    353374          !(n.Symbol is Cube) &&
    354375          !(n.Symbol is CubeRoot) &&
    355           !(n.Symbol is Power)
     376          !(n.Symbol is Power) &&
     377          !(n.Symbol is SubFunctionSymbol)
    356378        select n).Any();
    357379      return !containsUnknownSymbol;
Note: See TracChangeset for help on using the changeset viewer.