Changeset 18190


Ignore:
Timestamp:
01/13/22 11:35:38 (8 days ago)
Author:
mkommend
Message:

#3136:
Added parameters for paramter optimization / linear scaling in StructeredSymRegProblem.
Added license headers to StructureTemplate and StructeredSymRegProblem.
Fixed type in NMSEConstraint evaluator.

Location:
branches/3136_Structural_GP
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/NMSESingleObjectiveConstraintsEvaluator.cs

    r18183 r18190  
    287287      var parameterEntries = parameters.ToArray(); // order of entries must be the same for x
    288288
    289       // extract inital parameters
     289      // extract initial parameters
    290290      double[] c = (double[])initialParameters.Clone();
    291291
     
    380380        if (numberTreeNode != null) {
    381381          if (numberTreeNode.Parent.Symbol is Power
    382               && numberTreeNode.Parent.GetSubtree(1) == numberTreeNode) continue; // exponents in powers are not optimizated (see TreeToAutoDiffTermConverter)
     382              && numberTreeNode.Parent.GetSubtree(1) == numberTreeNode) continue; // exponents in powers are not optimized (see TreeToAutoDiffTermConverter)
    383383          numberTreeNode.Value = parameters[i++];
    384384        } else if (updateVariableWeights && variableTreeNodeBase != null)
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs

    r18184 r18190  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Linq;
    324using HEAL.Attic;
    425using HeuristicLab.Common;
    526using HeuristicLab.Core;
     27using HeuristicLab.Data;
    628using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    729using HeuristicLab.Optimization;
     
    2446    private const string EstimationLimitsParameterName = "EstimationLimits";
    2547    private const string BestTrainingSolutionParameterName = "Best Training Solution";
     48    private const string ApplyLinearScalingParameterName = "Apply Linear Scaling";
     49    private const string OptimizeParametersParameterName = "Optimize Parameters";
    2650
    2751    private const string SymbolicExpressionTreeName = "SymbolicExpressionTree";
     
    4266    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter => (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName];
    4367    public IResultParameter<ISymbolicRegressionSolution> BestTrainingSolutionParameter => (IResultParameter<ISymbolicRegressionSolution>)Parameters[BestTrainingSolutionParameterName];
     68
     69    public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter => (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName];
     70    public IFixedValueParameter<BoolValue> OptimizeParametersParameter => (IFixedValueParameter<BoolValue>)Parameters[OptimizeParametersParameterName];
    4471    #endregion
    4572
     
    6491
    6592    public DoubleLimit EstimationLimits => EstimationLimitsParameter.Value;
     93
     94    public bool ApplyLinearScaling {
     95      get => ApplyLinearScalingParameter.Value.Value;
     96      set => ApplyLinearScalingParameter.Value.Value = value;
     97    }
     98
     99    public bool OptimizeParameters {
     100      get => OptimizeParametersParameter.Value.Value;
     101      set => OptimizeParametersParameter.Value.Value = value;
     102    }
    66103
    67104    public override bool Maximization => false;
     
    99136        structureTemplate));
    100137
     138      Parameters.Add(new FixedValueParameter<BoolValue>(
     139        ApplyLinearScalingParameterName, new BoolValue(true)
     140        ));
     141
     142      Parameters.Add(new FixedValueParameter<BoolValue>(
     143        OptimizeParametersParameterName, new BoolValue(true)
     144        ));
     145
    101146      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(
    102147        InterpreterParameterName,
    103148        new SymbolicDataAnalysisExpressionTreeBatchInterpreter()) { Hidden = true });
    104 
    105149      Parameters.Add(new FixedValueParameter<DoubleLimit>(
    106150        EstimationLimitsParameterName,
    107151        new DoubleLimit(double.NegativeInfinity, double.PositiveInfinity)) { Hidden = true });
    108 
    109152      Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestTrainingSolutionParameterName, "") { Hidden = true });
    110153
     
    116159
    117160      RegisterEventHandlers();
     161
     162      StructureTemplate.ApplyLinearScaling = ApplyLinearScaling;
    118163      StructureTemplate.Template =
    119164        "(" +
     
    137182    [StorableHook(HookType.AfterDeserialization)]
    138183    private void AfterDeserialization() {
     184      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
     185        Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, new BoolValue(StructureTemplate.ApplyLinearScaling)));
     186      }
     187
     188      if (!Parameters.ContainsKey(OptimizeParametersParameterName)) {
     189        Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeParametersParameterName, new BoolValue(false)));
     190      }
     191
    139192      RegisterEventHandlers();
    140193    }
     
    148201
    149202      ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
     203      ApplyLinearScalingParameter.Value.ValueChanged += (o, e) => StructureTemplate.ApplyLinearScaling = ApplyLinearScaling;
     204      OptimizeParametersParameter.Value.ValueChanged += (o, e) => {
     205        if (OptimizeParameters) ApplyLinearScaling = true;
     206      };
     207
    150208    }
    151209
     
    156214
    157215    private void OnTemplateChanged(object sender, EventArgs args) {
     216      ApplyLinearScaling = StructureTemplate.ApplyLinearScaling;
    158217      SetupEncoding();
    159218    }
     
    165224      foreach (var subFunction in StructureTemplate.SubFunctions) {
    166225        subFunction.SetupVariables(ProblemData.AllowedInputVariables);
    167         // to prevent the same encoding twice
    168         if (Encoding.Encodings.Any(x => x.Name == subFunction.Name)) continue; // duplicate subfunction
     226        // prevent the same encoding twice
     227        if (Encoding.Encodings.Any(x => x.Name == subFunction.Name)) continue;
    169228
    170229        var encoding = new SymbolicExpressionTreeEncoding(
     
    196255
    197256      var tree = (ISymbolicExpressionTree)best[SymbolicExpressionTreeName];
    198 
    199257      var model = new SymbolicRegressionModel(ProblemData.TargetVariable, tree, Interpreter);
    200258      var solution = model.CreateRegressionSolution(ProblemData);
     
    227285    }
    228286
    229     private static void AdjustLinearScalingParams(IRegressionProblemData problemData, ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter) {
    230       var offsetNode = tree.Root.GetSubtree(0).GetSubtree(0);
    231       var scalingNode = offsetNode.Subtrees.Where(x => !(x is NumberTreeNode)).First();
    232 
    233       var offsetNumberNode = (NumberTreeNode)offsetNode.Subtrees.Where(x => x is NumberTreeNode).First();
    234       var scalingNumberNode = (NumberTreeNode)scalingNode.Subtrees.Where(x => x is NumberTreeNode).First();
    235 
    236       var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices);
    237       var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
    238 
    239       OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out double a, out double b, out OnlineCalculatorError error);
    240       if (error == OnlineCalculatorError.None) {
    241         offsetNumberNode.Value = a;
    242         scalingNumberNode.Value = b;
    243       }
    244     }
    245 
    246287    private static ISymbolicExpressionTree BuildTree(ISymbolicExpressionTree template, Individual individual) {
    247288      var resolvedTree = (ISymbolicExpressionTree)template.Clone();
    248 
    249289      // build main tree
    250290      foreach (var subFunctionTreeNode in resolvedTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) {
     
    260300    }
    261301
     302    private static void AdjustLinearScalingParams(IRegressionProblemData problemData, ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter) {
     303      var offsetNode = tree.Root.GetSubtree(0).GetSubtree(0);
     304      var scalingNode = offsetNode.Subtrees.Where(x => !(x is NumberTreeNode)).First();
     305
     306      var offsetNumberNode = (NumberTreeNode)offsetNode.Subtrees.Where(x => x is NumberTreeNode).First();
     307      var scalingNumberNode = (NumberTreeNode)scalingNode.Subtrees.Where(x => x is NumberTreeNode).First();
     308
     309      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices);
     310      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
     311
     312      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out double a, out double b, out OnlineCalculatorError error);
     313      if (error == OnlineCalculatorError.None) {
     314        offsetNumberNode.Value = a;
     315        scalingNumberNode.Value = b;
     316      }
     317    }
     318
     319
     320
     321
     322
    262323
    263324    public void Load(IRegressionProblemData data) {
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs

    r18188 r18190  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Collections.Generic;
    324using System.Linq;
Note: See TracChangeset for help on using the changeset viewer.