Changeset 18151


Ignore:
Timestamp:
12/16/21 16:47:07 (7 months ago)
Author:
dpiringe
Message:

#3136

  • fixed eventhandler reregister after deserialisazion/cloning
  • added a test case for StructuredSymbolicRegressionSingleObjectiveProblem
  • changed the usage of a Dictionary to List
Location:
branches/3136_Structural_GP
Files:
1 added
4 edited

Legend:

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

    r18146 r18151  
    8080      var shapeConstraintProblemData = new ShapeConstrainedRegressionProblemData(problemData);
    8181
    82 
    8382      var targetInterval = shapeConstraintProblemData.VariableRanges.GetInterval(shapeConstraintProblemData.TargetVariable);
    8483      var estimationWidth = targetInterval.Width * 10;
    8584
    86 
    8785      var structureTemplate = new StructureTemplate();
    88       structureTemplate.Changed += OnTemplateChanged;
    8986
    9087      var evaluators = new ItemSet<SymbolicRegressionSingleObjectiveEvaluator>(
     
    10097        ProblemDataParameterName,
    10198        shapeConstraintProblemData));
    102       ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
    10399
    104100      Parameters.Add(new FixedValueParameter<StructureTemplate>(
     
    123119      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
    124120
     121      RegisterEventHandlers();
    125122      StructureTemplate.Template =
    126123        "(" +
     
    132129
    133130    public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original,
    134       Cloner cloner) : base(original, cloner) { }
     131      Cloner cloner) : base(original, cloner) {
     132      ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
     133      RegisterEventHandlers();
     134    }
    135135
    136136    [StorableConstructor]
    137137    protected StructuredSymbolicRegressionSingleObjectiveProblem(StorableConstructorFlag _) : base(_) { }
     138
     139
     140    [StorableHook(HookType.AfterDeserialization)]
     141    private void AfterDeserialization() {
     142      RegisterEventHandlers();
     143    }
     144
    138145    #endregion
    139146
     
    148155    }
    149156
     157    private void RegisterEventHandlers() {
     158      if (StructureTemplate != null) {
     159        StructureTemplate.Changed += OnTemplateChanged;
     160      }
     161
     162      ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
     163    }
     164
    150165    private void OnTemplateChanged(object sender, EventArgs args) {
    151166      SetupStructureTemplate();
     
    156171        Encoding.Remove(e);
    157172
    158       foreach (var f in StructureTemplate.SubFunctions.Values) {
     173      foreach (var f in StructureTemplate.SubFunctions) {
    159174        SetupVariables(f);
    160175        if (!Encoding.Encodings.Any(x => x.Name == f.Name)) // to prevent the same encoding twice
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/StructureTemplate/StructureTemplateView.cs

    r18134 r18151  
    4444      if(visualTreeNode != null) {
    4545        var subFunctionTreeNode = visualTreeNode.Content as SubFunctionTreeNode;
    46         if(subFunctionTreeNode != null && Content.SubFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction subFunction))
    47           viewHost.Content = subFunction;
     46        var selectedSubFunction = Content.SubFunctions.Where(x => x.Name == subFunctionTreeNode.Name).FirstOrDefault();
     47        if(subFunctionTreeNode != null && selectedSubFunction != null)
     48          viewHost.Content = selectedSubFunction;
    4849      }
    4950    }
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs

    r18146 r18151  
    3333        treeWithLinearScaling = AddLinearScalingTerms(value);
    3434        treeWithoutLinearScaling = value;
    35         SubFunctions = GetSubFunctions();
     35        subFunctions = GetSubFunctions();
    3636      }
    3737    }
    3838
    3939    [Storable]
    40     public IReadOnlyDictionary<string, SubFunction> SubFunctions { get; private set; }
     40    private IList<SubFunction> subFunctions = new List<SubFunction>();
     41    public IEnumerable<SubFunction> SubFunctions => subFunctions;
    4142
    4243    [Storable]
     
    7273      this.Template = original.Template;
    7374      this.ApplyLinearScaling = original.ApplyLinearScaling;
    74       this.SubFunctions = original.SubFunctions;
     75      this.subFunctions = original.subFunctions.Select(cloner.Clone).ToList();
     76      RegisterEventHandlers();
     77    }
     78
     79
     80    [StorableHook(HookType.AfterDeserialization)]
     81    private void AfterDeserialization() {
     82      RegisterEventHandlers();
    7583    }
    7684    #endregion
     
    8290
    8391    public void Reset() {
    84       SubFunctions = new Dictionary<string, SubFunction>();
     92      subFunctions = new List<SubFunction>();
    8593      treeWithoutLinearScaling = null;
    8694      treeWithLinearScaling = null;
     
    9098    }
    9199
    92     private Dictionary<string, SubFunction> GetSubFunctions() {
    93       var subFunctions = new Dictionary<string, SubFunction>();
     100    private IList<SubFunction> GetSubFunctions() {
     101      var subFunctions = new List<SubFunction>();
    94102      foreach (var node in Tree.IterateNodesPrefix())
    95103        if (node is SubFunctionTreeNode subFunctionTreeNode) {
     
    97105            throw new ArgumentException($"The sub-function '{subFunctionTreeNode}' requires inputs (e.g. {subFunctionTreeNode.Name}(var1, var2)).");
    98106
    99           if (subFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction v)) {
    100             if(!v.Arguments.SequenceEqual(subFunctionTreeNode.Arguments))
     107          var existingSubFunction = subFunctions.Where(x => x.Name == subFunctionTreeNode.Name).FirstOrDefault();
     108          if (existingSubFunction != null) {
     109            // an existing subFunction needs the same signature
     110            if(!existingSubFunction.Arguments.SequenceEqual(subFunctionTreeNode.Arguments))
    101111              throw new ArgumentException(
    102                 $"The sub-function '{v.Name}' has (at least two) different signatures " +
    103                 $"({v.Name}({string.Join(",", v.Arguments)}) <> {subFunctionTreeNode.Name}({string.Join(",", subFunctionTreeNode.Arguments)})).");
     112                $"The sub-function '{existingSubFunction.Name}' has (at least two) different signatures " +
     113                $"({existingSubFunction.Name}({string.Join(",", existingSubFunction.Arguments)}) <> " +
     114                $"{subFunctionTreeNode.Name}({string.Join(",", subFunctionTreeNode.Arguments)})).");
    104115          } else {
    105116            var subFunction = new SubFunction() {
     
    108119            };
    109120            subFunction.Changed += OnSubFunctionChanged;
    110             subFunctions.Add(subFunction.Name, subFunction);
     121            subFunctions.Add(subFunction);
    111122          }
    112123        }
    113124      return subFunctions;
     125    }
     126
     127    private void RegisterEventHandlers() {
     128      foreach(var sf in SubFunctions) {
     129        sf.Changed += OnSubFunctionChanged;
     130      }
    114131    }
    115132
  • branches/3136_Structural_GP/HeuristicLab.Tests/HeuristicLab.Tests.csproj

    r17958 r18151  
    471471    <Compile Include="HeuristicLab-3.3\Samples\GAGroupingProblemSampleTest.cs" />
    472472    <Compile Include="HeuristicLab-3.3\Samples\ShapeConstrainedRegressionSampleTest.cs" />
     473    <Compile Include="HeuristicLab-3.3\Samples\StructureTemplateRegressionSampleTest.cs" />
    473474    <Compile Include="HeuristicLab-3.3\Samples\VnsOpSampleTest.cs" />
    474475    <Compile Include="HeuristicLab-3.3\Samples\EsGriewankSampleTest.cs" />
Note: See TracChangeset for help on using the changeset viewer.