Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/02/16 13:02:03 (8 years ago)
Author:
gkronber
Message:

#2657:

  • added switch to determine if numeric parameters should be initialized randomly.
  • fixed a bug in the infix parser
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/NonlinearRegression/NonlinearRegression.cs

    r14316 r14319  
    2222using System;
    2323using System.Linq;
     24using HeuristicLab.Analysis;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    4748    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
    4849    private const string SeedParameterName = "Seed";
     50    private const string InitParamsRandomlyParameterName = "InitializeParametersRandomly";
    4951
    5052    public IFixedValueParameter<StringValue> ModelStructureParameter {
     
    6567    public IFixedValueParameter<IntValue> RestartsParameter {
    6668      get { return (IFixedValueParameter<IntValue>)Parameters[RestartsParameterName]; }
     69    }
     70
     71    public IFixedValueParameter<BoolValue> InitParametersRandomlyParameter {
     72      get { return (IFixedValueParameter<BoolValue>)Parameters[InitParamsRandomlyParameterName]; }
    6773    }
    6874
     
    9096      get { return SetSeedRandomlyParameter.Value.Value; }
    9197      set { SetSeedRandomlyParameter.Value.Value = value; }
     98    }
     99
     100    public bool InitializeParametersRandomly {
     101      get { return InitParametersRandomlyParameter.Value.Value; }
     102      set { InitParametersRandomlyParameter.Value.Value = value; }
    92103    }
    93104
     
    102113      Parameters.Add(new FixedValueParameter<StringValue>(ModelStructureParameterName, "The function for which the parameters must be fit (only numeric constants are tuned).", new StringValue("1.0 * x*x + 0.0")));
    103114      Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "The maximum number of iterations for constants optimization.", new IntValue(200)));
    104       Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "The number of independent random restarts", new IntValue(10)));
     115      Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "The number of independent random restarts (>0)", new IntValue(10)));
    105116      Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The PRNG seed value.", new IntValue()));
    106117      Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "Switch to determine if the random number seed should be initialized randomly.", new BoolValue(true)));
     118      Parameters.Add(new FixedValueParameter<BoolValue>(InitParamsRandomlyParameterName, "Switch to determine if the real-valued model parameters should be initialized randomly in each restart.", new BoolValue(false)));
     119
     120      SetParameterHiddenState();
     121
     122      InitParametersRandomlyParameter.Value.ValueChanged += (sender, args) => {
     123        SetParameterHiddenState();
     124      };
     125    }
     126
     127    private void SetParameterHiddenState() {
     128      var hide = !InitializeParametersRandomly;
     129      RestartsParameter.Hidden = hide;
     130      SeedParameter.Hidden = hide;
     131      SetSeedRandomlyParameter.Hidden = hide;
    107132    }
    108133
     
    117142      if (!Parameters.ContainsKey(SetSeedRandomlyParameterName))
    118143        Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "Switch to determine if the random number seed should be initialized randomly.", new BoolValue(true)));
     144      if (!Parameters.ContainsKey(InitParamsRandomlyParameterName))
     145        Parameters.Add(new FixedValueParameter<BoolValue>(InitParamsRandomlyParameterName, "Switch to determine if the numeric parameters of the model should be initialized randomly.", new BoolValue(false)));
     146
     147      SetParameterHiddenState();
     148      InitParametersRandomlyParameter.Value.ValueChanged += (sender, args) => {
     149        SetParameterHiddenState();
     150      };
    119151      #endregion
    120152    }
     
    126158    #region nonlinear regression
    127159    protected override void Run() {
    128       if (SetSeedRandomly) Seed = (new System.Random()).Next();
    129       var rand = new MersenneTwister((uint)Seed);
    130 
    131       var bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand);
    132       for (int r = 0; r < Restarts; r++) {
    133         var solution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand);
    134         if (solution.TrainingRootMeanSquaredError < bestSolution.TrainingRootMeanSquaredError) {
    135           bestSolution = solution;
     160      IRegressionSolution bestSolution = null;
     161      if (InitializeParametersRandomly) {
     162        var qualityTable = new DataTable("RMSE table");
     163        qualityTable.VisualProperties.YAxisLogScale = true;
     164        var trainRMSERow = new DataRow("RMSE (train)");
     165        trainRMSERow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
     166        var testRMSERow = new DataRow("RMSE test");
     167        testRMSERow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
     168
     169        qualityTable.Rows.Add(trainRMSERow);
     170        qualityTable.Rows.Add(testRMSERow);
     171        Results.Add(new Result(qualityTable.Name, qualityTable.Name + " for all restarts", qualityTable));
     172        if (SetSeedRandomly) Seed = (new System.Random()).Next();
     173        var rand = new MersenneTwister((uint)Seed);
     174        bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand);
     175        trainRMSERow.Values.Add(bestSolution.TrainingRootMeanSquaredError);
     176        testRMSERow.Values.Add(bestSolution.TestRootMeanSquaredError);
     177        for (int r = 0; r < Restarts; r++) {
     178          var solution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, rand);
     179          trainRMSERow.Values.Add(solution.TrainingRootMeanSquaredError);
     180          testRMSERow.Values.Add(solution.TestRootMeanSquaredError);
     181          if (solution.TrainingRootMeanSquaredError < bestSolution.TrainingRootMeanSquaredError) {
     182            bestSolution = solution;
     183          }
    136184        }
     185      } else {
     186        bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations);
    137187      }
    138188
     
    154204    /// <param name="random">Optional random number generator for random initialization of numeric constants.</param>
    155205    /// <returns></returns>
    156     public static ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData, string modelStructure, int maxIterations, IRandom random = null) {
     206    public static ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData, string modelStructure, int maxIterations, IRandom rand = null) {
    157207      var parser = new InfixExpressionParser();
    158208      var tree = parser.Parse(modelStructure);
     
    161211
    162212      // initialize constants randomly
    163       if (random != null) {
     213      if (rand != null) {
    164214        foreach (var node in tree.IterateNodesPrefix().OfType<ConstantTreeNode>()) {
    165           node.Value = NormalDistributedRandom.NextDouble(random, 0, 1);
     215          double f = Math.Exp(NormalDistributedRandom.NextDouble(rand, 0, 1));
     216          double s = rand.NextDouble() < 0.5 ? -1 : 1;
     217          node.Value = s * node.Value * f;
    166218        }
    167219      }
Note: See TracChangeset for help on using the changeset viewer.