Changeset 16690


Ignore:
Timestamp:
03/16/19 12:51:33 (3 months ago)
Author:
gkronber
Message:

#2974: added code in the unit tests to make behaviour of new and old ConstOpt consistent to allow direct comparison of results (+fixed a small bug in the unit tests)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2974_Constants_Optimization/UnitTests/ConstantsOptimizationTests.cs

    r16507 r16690  
    4747      var new_result = LMConstantsOptimizer.OptimizeConstants(new_optimizedTree, problemData.Dataset, problemData.TargetVariable, problemData.TrainingIndices, applyLinearScaling, maxIterations: 10);
    4848
    49       //check R² values
    50       //Assert.AreEqual(old_result, new_result);
    51 
    52       //check numeric values of constants
     49      // extract constants
    5350      var old_constants = Util.ExtractConstants(old_optimizedTree, applyLinearScaling);
    5451      var new_constants = Util.ExtractConstants(new_optimizedTree, applyLinearScaling);
    55       //Assert.IsTrue(old_constants.SequenceEqual(new_constants));
    56 
     52
     53      {
     54        // consistency with old ConstOpt style
     55        var initalR2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
     56          new SymbolicDataAnalysisExpressionTreeLinearInterpreter(),
     57          tree, double.MinValue, double.MaxValue, problemData, problemData.TrainingIndices, applyLinearScaling);
     58
     59        // LMConstantsOptimizer returns 0 if optimization was not successful
     60        if (initalR2 - new_result > 0.001) {
     61          new_result = initalR2;
     62          new_constants = old_constants;
     63        }
     64      }
     65
     66      //check R² values
     67      Assert.AreEqual(old_result, new_result, 1E-6);
     68
     69      //check numeric values of constants
    5770      for (int i = 0; i < old_constants.Length; i++) {
    58         Assert.AreEqual(old_constants[i], new_constants[i], 0.00000001);
     71        if (old_constants[i] == 0) {
     72          Assert.AreEqual(old_constants[i], new_constants[i], 1E-8); // check absolute error
     73        } else {
     74          Assert.AreEqual(0.0, 1.0 - new_constants[i] / old_constants[i], 1E-6); // check percentual error
     75        }
    5976      }
    6077    }
     
    147164    [TestProperty("Time", "short")]
    148165    public void ConstantsOptimizationTest_Nguyen07() {
    149       var problemData = new NguyenFunctionFive(seed).GenerateRegressionData();
     166      var problemData = new NguyenFunctionSeven(seed).GenerateRegressionData();
    150167      var modelTemplate = "({0}) * LOG(({1})*X + ({2})) + ({3}) * LOG(({4})*X*X + ({5})) + ({6})";
    151168
     
    162179      modelString = string.Format(modelTemplate, constants);
    163180      modelTree = new InfixExpressionParser().Parse(modelString);
     181      // This test fails not because of a problem in ConstOpt but because of the code in
     182      // OnlinePearsonsRCalculator
     183      //         double xVar = sxCalculator.PopulationVariance;
     184      //         double yVar = syCalculator.PopulationVariance;
     185      //         if (xVar.IsAlmost(0.0) || yVar.IsAlmost(0.0)) {
     186      //           return 0.0;
     187      //         } else { ...
     188      // PopulationVariance might become close to zero, but the result of cov / (sqrt(xvar) * sqrt(yvar) might still be correct.
     189      // Currently, we just return 0. Which means that we reset optimized constants to initial values
    164190      CompareConstantsOptimizationResults(problemData, modelTree);
    165191
     
    223249      CompareConstantsOptimizationResults(problemData, modelTree);
    224250    }
     251
     252    [TestInitialize]
     253    public void InitEnvironment() {
     254      System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
     255    }
    225256  }
    226257}
Note: See TracChangeset for help on using the changeset viewer.