Changeset 13900


Ignore:
Timestamp:
06/16/16 16:42:37 (3 years ago)
Author:
mkommend
Message:

#2609: Refactored multiplication and division handling during transformationo to AutoDiff terms in the CoOp-Evaluator.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs

    r13869 r13900  
    336336      }
    337337      if (node.Symbol is Multiplication) {
    338         AutoDiff.Term a, b;
    339         if (node.SubtreeCount == 1) {
    340           a = AutoDiff.TermBuilder.Constant(1);
    341           if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out b)) {
     338        List<AutoDiff.Term> terms = new List<Term>();
     339        foreach (var subTree in node.Subtrees) {
     340          AutoDiff.Term t;
     341          if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, updateVariableWeights, out t)) {
    342342            term = null;
    343343            return false;
    344344          }
    345         } else {
    346           if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out a) ||
    347               !TryTransformToAutoDiff(node.GetSubtree(1), variables, parameters, variableNames, updateVariableWeights, out b)) {
     345          terms.Add(t);
     346        }
     347        if (terms.Count == 1) term = terms[0];
     348        else term = terms.Aggregate((a, b) => new AutoDiff.Product(a, b));
     349        return true;
     350
     351      }
     352      if (node.Symbol is Division) {
     353        List<AutoDiff.Term> terms = new List<Term>();
     354        foreach (var subTree in node.Subtrees) {
     355          AutoDiff.Term t;
     356          if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, updateVariableWeights, out t)) {
    348357            term = null;
    349358            return false;
    350359          }
    351         }
    352         List<AutoDiff.Term> factors = new List<Term>();
    353         foreach (var subTree in node.Subtrees.Skip(2)) {
    354           AutoDiff.Term f;
    355           if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, updateVariableWeights, out f)) {
    356             term = null;
    357             return false;
    358           }
    359           factors.Add(f);
    360         }
    361         term = AutoDiff.TermBuilder.Product(a, b, factors.ToArray());
    362         return true;
    363       }
    364       if (node.Symbol is Division) {
    365         AutoDiff.Term a, b;
    366         if (node.SubtreeCount == 1) {
    367           a = AutoDiff.TermBuilder.Constant(1);
    368           if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out b)) {
    369             term = null;
    370             return false;
    371           }
    372         } else {
    373           if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out a) ||
    374               !TryTransformToAutoDiff(node.GetSubtree(1), variables, parameters, variableNames, updateVariableWeights, out b)) {
    375             term = null;
    376             return false;
    377           }
    378         }
    379         List<AutoDiff.Term> factors = new List<Term>();
    380         foreach (var subTree in node.Subtrees.Skip(2)) {
    381           AutoDiff.Term f;
    382           if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, updateVariableWeights, out f)) {
    383             term = null;
    384             return false;
    385           }
    386           factors.Add(1.0 / f);
    387         }
    388         term = AutoDiff.TermBuilder.Product(a, 1.0 / b, factors.ToArray());
     360          terms.Add(t);
     361        }
     362        if (terms.Count == 1) term = 1.0 / terms[0];
     363        else term = terms.Aggregate((a, b) => new AutoDiff.Product(a, 1.0 / b));
    389364        return true;
    390365      }
Note: See TracChangeset for help on using the changeset viewer.