Changeset 18194


Ignore:
Timestamp:
01/14/22 08:33:46 (7 days ago)
Author:
mkommend
Message:

#3136:

  • Added handling of numeric parameters in structur GP problem by using a real vector encoding.
  • Configured grammar in sub function.
  • Added property for numeric parameters in structure template.
Location:
branches/3136_Structural_GP
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj

    r18192 r18194  
    220220      <Private>False</Private>
    221221    </ProjectReference>
     222    <ProjectReference Include="..\..\HeuristicLab.Encodings.RealVectorEncoding\3.3\HeuristicLab.Encodings.RealVectorEncoding-3.3.csproj">
     223      <Project>{BB6D334A-4BB6-4674-9883-31A6EBB32CAB}</Project>
     224      <Name>HeuristicLab.Encodings.RealVectorEncoding-3.3</Name>
     225    </ProjectReference>
    222226    <ProjectReference Include="..\..\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding\3.4\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj">
    223227      <Project>{06D4A186-9319-48A0-BADE-A2058D462EEA}</Project>
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/Plugin.cs.frame

    r18158 r18194  
    3434  [PluginDependency("HeuristicLab.Core", "3.3")]
    3535  [PluginDependency("HeuristicLab.Data", "3.3")]
    36   [PluginDependency("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding", "3.4")]
     36  [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")]
     37  [PluginDependency("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding", "3.4")] 
    3738  [PluginDependency("HeuristicLab.Operators", "3.3")]
    3839  [PluginDependency("HeuristicLab.Optimization", "3.3")]
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs

    r18192 r18194  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
     28using HeuristicLab.Encodings.RealVectorEncoding;
    2829using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2930using HeuristicLab.Optimization;
     
    4849
    4950    private const string SymbolicExpressionTreeName = "SymbolicExpressionTree";
     51    private const string NumericParametersEncoding = "Numeric Parameters";
    5052
    5153    private const string StructureTemplateDescriptionText =
     
    188190      ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
    189191      ApplyLinearScalingParameter.Value.ValueChanged += (o, e) => StructureTemplate.ApplyLinearScaling = ApplyLinearScaling;
    190       //OptimizeParametersParameter.Value.ValueChanged += (o, e) => {
    191       //  if (OptimizeParameters) ApplyLinearScaling = true;
    192       //};
    193 
    194192    }
    195193
     
    207205      foreach (var e in Encoding.Encodings.ToArray())
    208206        Encoding.Remove(e);
     207
     208
     209      var templateNumberTreeNodes = StructureTemplate.Tree.IterateNodesPrefix().OfType<NumberTreeNode>();
     210      if (templateNumberTreeNodes.Any()) {
     211        var templateParameterValues = templateNumberTreeNodes.Select(n => n.Value).ToArray();
     212        var encoding = new RealVectorEncoding(NumericParametersEncoding, templateParameterValues.Length);
     213
     214        var creator = encoding.Operators.OfType<NormalDistributedRealVectorCreator>().First();
     215        creator.MeanParameter.Value = new RealVector(templateParameterValues);
     216        creator.SigmaParameter.Value = new DoubleArray(templateParameterValues.Length);
     217        encoding.SolutionCreator = creator;
     218
     219        Encoding.Add(encoding);
     220      }
    209221
    210222      foreach (var subFunction in StructureTemplate.SubFunctions) {
     
    221233      }
    222234
    223       //set multi manipulator as default manipulator for all encoding parts
    224       var manipulator = (IParameterizedItem)Encoding.Operators.OfType<MultiEncodingManipulator>().FirstOrDefault();
    225       if (manipulator != null) {
    226         foreach (var param in manipulator.Parameters.OfType<ConstrainedValueParameter<IManipulator>>()) {
    227           var m = param.ValidValues.OfType<MultiSymbolicExpressionTreeManipulator>().FirstOrDefault();
    228           param.Value = m == null ? param.ValidValues.First() : m;
     235      //set single point crossover for numeric parameters
     236      var multiCrossover = (IParameterizedItem)Encoding.Operators.OfType<MultiEncodingCrossover>().First();
     237      foreach (var param in multiCrossover.Parameters.OfType<ConstrainedValueParameter<ICrossover>>()) {
     238        var singlePointCrossover = param.ValidValues.OfType<SinglePointCrossover>().FirstOrDefault();
     239        param.Value = singlePointCrossover ?? param.ValidValues.First();
     240      }
     241
     242      //adapt crossover probability for subtree crossover
     243      foreach (var param in multiCrossover.Parameters.OfType<ConstrainedValueParameter<ICrossover>>()) {
     244        var subtreeCrossover = param.ValidValues.OfType<SubtreeCrossover>().FirstOrDefault();
     245        if (subtreeCrossover != null) {
     246          subtreeCrossover.CrossoverProbability = 1.0 / Encoding.Encodings.OfType<SymbolicExpressionTreeEncoding>().Count();
     247          param.Value = subtreeCrossover;
    229248        }
     249      }
     250
     251      //set multi manipulator as default manipulator for all symbolic expression tree encoding parts
     252      var manipulator = (IParameterizedItem)Encoding.Operators.OfType<MultiEncodingManipulator>().First();
     253      foreach (var param in manipulator.Parameters.OfType<ConstrainedValueParameter<IManipulator>>()) {
     254        var m = param.ValidValues.OfType<MultiSymbolicExpressionTreeManipulator>().FirstOrDefault();
     255        param.Value = m ?? param.ValidValues.First();
    230256      }
    231257    }
     
    253279        throw new ArgumentException("No structure template defined!");
    254280
    255       //create tree where all functions have been resolved (integrated)
    256       var tree = BuildTree(templateTree, individual);
     281      var tree = BuildTreeFromIndividual(templateTree, individual, updateNumericParameters: StructureTemplate.ContainsNumericParameters);
    257282      individual[SymbolicExpressionTreeName] = tree;
    258283
     
    262287        LinearScaling.AdjustLinearScalingParams(ProblemData, tree, Interpreter);
    263288      }
     289
     290      UpdateIndividualFromTree(tree, individual, updateNumericParameters: StructureTemplate.ContainsNumericParameters);
    264291
    265292      //calculate NMSE
     
    290317    }
    291318
    292     private static ISymbolicExpressionTree BuildTree(ISymbolicExpressionTree template, Individual individual) {
     319    private static ISymbolicExpressionTree BuildTreeFromIndividual(ISymbolicExpressionTree template, Individual individual, bool updateNumericParameters) {
    293320      var resolvedTree = (ISymbolicExpressionTree)template.Clone();
     321
     322      //set numeric parameter values
     323      if (updateNumericParameters) {
     324        var realVector = individual.RealVector(NumericParametersEncoding);
     325        var numberTreeNodes = resolvedTree.IterateNodesPrefix().OfType<NumberTreeNode>().ToArray();
     326
     327        if (realVector.Length != numberTreeNodes.Length)
     328          throw new InvalidOperationException("The number of numeric parameters in the tree does not match the provided numerical values.");
     329
     330        for (int i = 0; i < numberTreeNodes.Length; i++)
     331          numberTreeNodes[i].Value = realVector[i];
     332      }
     333
    294334      // build main tree
    295335      foreach (var subFunctionTreeNode in resolvedTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) {
     
    305345    }
    306346
     347    private static void UpdateIndividualFromTree(ISymbolicExpressionTree tree, Individual individual, bool updateNumericParameters) {
     348      var clonedTree = (ISymbolicExpressionTree)tree.Clone();
     349
     350      foreach (var subFunctionTreeNode in clonedTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) {
     351        var grammar = ((ISymbolicExpressionTree)individual[subFunctionTreeNode.Name]).Root.Grammar;
     352        var functionTreeNode = subFunctionTreeNode.GetSubtree(0);
     353        //remove function code to make numeric parameters extraction easier
     354        subFunctionTreeNode.RemoveSubtree(0);
     355
     356
     357        var rootNode = (SymbolicExpressionTreeTopLevelNode)new ProgramRootSymbol().CreateTreeNode();
     358        rootNode.SetGrammar(grammar);
     359
     360        var startNode = (SymbolicExpressionTreeTopLevelNode)new StartSymbol().CreateTreeNode();
     361        startNode.SetGrammar(grammar);
     362
     363        rootNode.AddSubtree(startNode);
     364        startNode.AddSubtree(functionTreeNode);
     365        var functionTree = new SymbolicExpressionTree(rootNode);
     366        individual[subFunctionTreeNode.Name] = functionTree;
     367      }
     368
     369      //set numeric parameter values
     370      if (updateNumericParameters) {
     371        var realVector = individual.RealVector(NumericParametersEncoding);
     372        var numberTreeNodes = clonedTree.IterateNodesPrefix().OfType<NumberTreeNode>().ToArray();
     373
     374        if (realVector.Length != numberTreeNodes.Length)
     375          throw new InvalidOperationException("The number of numeric parameters in the tree does not match the provided numerical values.");
     376
     377        for (int i = 0; i < numberTreeNodes.Length; i++)
     378          realVector[i] = numberTreeNodes[i].Value;
     379      }
     380    }
     381
    307382    public void Load(IRegressionProblemData data) {
    308383      ProblemData = data;
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs

    r18191 r18194  
    6161      get => tree;
    6262      private set {
     63        containsNumericParameters = null;
    6364        tree = value;
    6465
     
    6869        // adds new functions and keeps the old ones (if they match)
    6970        subFunctions = newFunctions.Except(oldFunctions).Concat(oldFunctions).ToList();
     71      }
     72    }
     73
     74    private bool? containsNumericParameters;
     75    public bool ContainsNumericParameters {
     76      get {
     77        if (!containsNumericParameters.HasValue)
     78          containsNumericParameters = Tree.IterateNodesPrefix().OfType<NumberTreeNode>().Any();
     79
     80        return containsNumericParameters.Value;
    7081      }
    7182    }
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/SubFunction.cs

    r18184 r18194  
    7575    #region Event Handling
    7676    private void RegisterEventHandlers() {
    77       GrammarParameter.ValueChanged += OnParameterValueChanged;
     77      GrammarParameter.ValueChanged += (o, e) => {
     78        if (Grammar is TypeCoherentExpressionGrammar tceg) tceg.ConfigureAsDefaultRegressionGrammar();
     79        OnParameterValueChanged(o, e);
     80      };
    7881      MaximumSymbolicExpressionTreeDepthParameter.Value.ValueChanged += OnParameterValueChanged;
    7982      MaximumSymbolicExpressionTreeLengthParameter.Value.ValueChanged += OnParameterValueChanged;
Note: See TracChangeset for help on using the changeset viewer.