Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8938


Ignore:
Timestamp:
11/22/12 16:26:15 (10 years ago)
Author:
mkommend
Message:

##1976: Removed obsolete code in SymbolicRegressionConstantOptimizationEvaluator.

File:
1 edited

Legend:

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

    r8828 r8938  
    4141    private const string UpdateConstantsInTreeParameterName = "UpdateConstantsInSymbolicExpressionTree";
    4242
    43     private const string EvaluatedTreesResultName = "EvaluatedTrees";
    44     private const string EvaluatedTreeNodesResultName = "EvaluatedTreeNodes";
    45 
    46     public ILookupParameter<IntValue> EvaluatedTreesParameter {
    47       get { return (ILookupParameter<IntValue>)Parameters[EvaluatedTreesResultName]; }
    48     }
    49     public ILookupParameter<IntValue> EvaluatedTreeNodesParameter {
    50       get { return (ILookupParameter<IntValue>)Parameters[EvaluatedTreeNodesResultName]; }
    51     }
    52 
    5343    public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter {
    5444      get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; }
     
    9585    public SymbolicRegressionConstantOptimizationEvaluator()
    9686      : base() {
    97       Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the constant of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue(3), true));
     87      Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the constant of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue(10), true));
    9888      Parameters.Add(new FixedValueParameter<DoubleValue>(ConstantOptimizationImprovementParameterName, "Determines the relative improvement which must be achieved in the constant optimization to continue with it (0 indicates other or default stopping criterion).", new DoubleValue(0), true));
    9989      Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationProbabilityParameterName, "Determines the probability that the constants are optimized", new PercentValue(1), true));
    10090      Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for constant optimization", new PercentValue(1), true));
    10191      Parameters.Add(new FixedValueParameter<BoolValue>(UpdateConstantsInTreeParameterName, "Determines if the constants in the tree should be overwritten by the optimized constants.", new BoolValue(true)));
    102 
    103       Parameters.Add(new LookupParameter<IntValue>(EvaluatedTreesResultName));
    104       Parameters.Add(new LookupParameter<IntValue>(EvaluatedTreeNodesResultName));
    10592    }
    10693
     
    116103
    117104    public override IOperation Apply() {
    118       AddResults();
    119105      var solution = SymbolicExpressionTreeParameter.ActualValue;
    120106      double quality;
     
    123109        quality = OptimizeConstants(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, ProblemDataParameter.ActualValue,
    124110           constantOptimizationRows, ApplyLinearScalingParameter.ActualValue.Value, ConstantOptimizationIterations.Value,
    125            EstimationLimitsParameter.ActualValue.Upper, EstimationLimitsParameter.ActualValue.Lower, UpdateConstantsInTree,
    126           EvaluatedTreesParameter.ActualValue, EvaluatedTreeNodesParameter.ActualValue);
     111           EstimationLimitsParameter.ActualValue.Upper, EstimationLimitsParameter.ActualValue.Lower, UpdateConstantsInTree);
     112
    127113        if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) {
    128114          var evaluationRows = GenerateRowsToEvaluate();
     
    134120      }
    135121      QualityParameter.ActualValue = new DoubleValue(quality);
    136       lock (locker) {
    137         EvaluatedTreesParameter.ActualValue.Value += 1;
    138         EvaluatedTreeNodesParameter.ActualValue.Value += solution.Length;
    139       }
    140122
    141123      if (Successor != null)
     
    143125      else
    144126        return null;
    145     }
    146 
    147     private object locker = new object();
    148     private void AddResults() {
    149       lock (locker) {
    150         if (EvaluatedTreesParameter.ActualValue == null) {
    151           var scope = ExecutionContext.Scope;
    152           while (scope.Parent != null)
    153             scope = scope.Parent;
    154           scope.Variables.Add(new Core.Variable(EvaluatedTreesResultName, new IntValue()));
    155         }
    156         if (EvaluatedTreeNodesParameter.ActualValue == null) {
    157           var scope = ExecutionContext.Scope;
    158           while (scope.Parent != null)
    159             scope = scope.Parent;
    160           scope.Variables.Add(new Core.Variable(EvaluatedTreeNodesResultName, new IntValue()));
    161         }
    162       }
    163127    }
    164128
     
    204168
    205169    public static double OptimizeConstants(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree tree, IRegressionProblemData problemData,
    206       IEnumerable<int> rows, bool applyLinearScaling, int maxIterations, double upperEstimationLimit = double.MaxValue, double lowerEstimationLimit = double.MinValue, bool updateConstantsInTree = true, IntValue evaluatedTrees = null, IntValue evaluatedTreeNodes = null) {
     170      IEnumerable<int> rows, bool applyLinearScaling, int maxIterations, double upperEstimationLimit = double.MaxValue, double lowerEstimationLimit = double.MinValue, bool updateConstantsInTree = true) {
    207171
    208172      List<AutoDiff.Variable> variables = new List<AutoDiff.Variable>();
     
    234198        }
    235199      }
     200      double[] originalConstants = (double[])c.Clone();
     201      double originalQuality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
    236202
    237203      alglib.lsfitstate state;
     
    258224      try {
    259225        alglib.lsfitcreatefg(x, y, c, n, m, k, false, out state);
    260         alglib.lsfitsetcond(state, 0, 0, maxIterations);
     226        alglib.lsfitsetcond(state, 0.0, 0.0, maxIterations);
     227        //alglib.lsfitsetgradientcheck(state, 0.001);
    261228        alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, null, null);
    262229        alglib.lsfitresults(state, out info, out c, out rep);
    263 
    264230      }
    265231      catch (ArithmeticException) {
    266         return 0.0;
     232        return SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
    267233      }
    268234      catch (alglib.alglibexception) {
    269         return 0.0;
    270       }
    271       var newTree = tree;
    272       if (!updateConstantsInTree) newTree = (ISymbolicExpressionTree)tree.Clone();
    273       {
    274         // only when no error occurred
    275         // set constants in tree
    276         int i = 2;
    277         foreach (var node in newTree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {
    278           ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
    279           VariableTreeNode variableTreeNode = node as VariableTreeNode;
    280           if (constantTreeNode != null)
    281             constantTreeNode.Value = c[i++];
    282           else if (variableTreeNode != null)
    283             variableTreeNode.Weight = c[i++];
    284         }
    285 
    286       }
    287       return SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, newTree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
     235        return SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
     236      }
     237
     238      //info == -7  => constant optimization failed due to wrong gradient
     239      if (info != -7) UpdateConstants(tree, c.Skip(2).ToArray());
     240      var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
     241
     242      if (!updateConstantsInTree) UpdateConstants(tree, originalConstants.Skip(2).ToArray());
     243      if (originalQuality - quality > 0.001 || double.IsNaN(quality)) {
     244        UpdateConstants(tree, originalConstants.Skip(2).ToArray());
     245        return originalQuality;
     246      }
     247      return quality;
     248    }
     249
     250    private static void UpdateConstants(ISymbolicExpressionTree tree, double[] constants) {
     251      int i = 0;
     252      foreach (var node in tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>()) {
     253        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
     254        VariableTreeNode variableTreeNode = node as VariableTreeNode;
     255        if (constantTreeNode != null)
     256          constantTreeNode.Value = constants[i++];
     257        else if (variableTreeNode != null)
     258          variableTreeNode.Weight = constants[i++];
     259      }
    288260    }
    289261
Note: See TracChangeset for help on using the changeset viewer.