Changeset 16949


Ignore:
Timestamp:
05/13/19 16:20:11 (2 weeks ago)
Author:
abeham
Message:

#2521: Adapted test function problems to new real vector problem

  • Made encoding readonly in symbolic expression tree problem
Location:
branches/2521_ProblemRefactoring
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3/HeuristicLab.Encodings.RealVectorEncoding-3.3.csproj

    r16723 r16949  
    115115  <ItemGroup>
    116116    <Compile Include="Creators\NormalDistributedRealVectorCreator.cs" />
     117    <Compile Include="RealVectorMultiObjectiveProblem.cs" />
     118    <Compile Include="RealVectorProblem.cs" />
    117119    <Compile Include="Interfaces\IRealVectorAdditiveMoveQualityOperator.cs" />
    118120    <Compile Include="Interfaces\IRealVectorBoundedOperator.cs" />
     
    205207  </ItemGroup>
    206208  <ItemGroup>
     209    <ProjectReference Include="..\..\HeuristicLab.Analysis\3.3\HeuristicLab.Analysis-3.3.csproj">
     210      <Project>{887425b4-4348-49ed-a457-b7d2c26ddbf9}</Project>
     211      <Name>HeuristicLab.Analysis-3.3</Name>
     212      <Private>False</Private>
     213    </ProjectReference>
    207214    <ProjectReference Include="..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj">
    208215      <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project>
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeProblem.cs

    r16813 r16949  
    4646    }
    4747
    48     protected SymbolicExpressionTreeProblem(SymbolicExpressionTreeEncoding encoding) : base(encoding) { }
     48    protected SymbolicExpressionTreeProblem(SymbolicExpressionTreeEncoding encoding)
     49      : base(encoding) {
     50      EncodingParameter.ReadOnly = true;
     51    }
    4952
    5053    public override void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results,
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/MultiObjectiveTestFunctionProblem.cs

    r16807 r16949  
    3535  [Creatable(CreatableAttribute.Categories.Problems, Priority = 95)]
    3636  [Item("Test Function (multi-objective)", "Test functions with real valued inputs and multiple objectives.")]
    37   public class MultiObjectiveTestFunctionProblem : MultiObjectiveProblem<RealVectorEncoding, RealVector>, IProblemInstanceConsumer<MOTFData> {
     37  public class MultiObjectiveTestFunctionProblem : RealVectorMultiObjectiveProblem,
     38    IProblemInstanceConsumer<MOTFData> {
    3839
    3940    #region Parameter Properties
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions/3.3/Analyzers/BestSingleObjectiveTestFunctionSolutionAnalyzer.cs

    r16749 r16949  
    2020#endregion
    2121
     22using System;
    2223using System.Linq;
     24using HEAL.Attic;
    2325using HeuristicLab.Common;
    2426using HeuristicLab.Core;
     
    2830using HeuristicLab.Optimization;
    2931using HeuristicLab.Parameters;
    30 using HEAL.Attic;
     32using HeuristicLab.PluginInfrastructure;
    3133
    3234namespace HeuristicLab.Problems.TestFunctions {
     35  // BackwardsCompatibility3.3
     36  #region Backwards compatible code, remove with 3.4
    3337  /// <summary>
    3438  /// An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.
     
    3640  [Item("BestSingleObjectiveTestFunctionSolutionAnalyzer", "An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.")]
    3741  [StorableType("A0F04F9F-DD27-44D8-A1F6-B289F1F40DE2")]
     42  [NonDiscoverableType]
     43  [Obsolete("Use the Analyze method of the test function problem class")]
    3844  public class BestSingleObjectiveTestFunctionSolutionAnalyzer : SingleSuccessorOperator, IBestSingleObjectiveTestFunctionSolutionAnalyzer {
    3945    public virtual bool EnabledByDefault {
     
    145151    }
    146152  }
     153  #endregion
    147154}
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs

    r16723 r16949  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HEAL.Attic;
    2526using HeuristicLab.Analysis;
    2627using HeuristicLab.Common;
     
    2930using HeuristicLab.Encodings.RealVectorEncoding;
    3031using HeuristicLab.Optimization;
    31 using HeuristicLab.Optimization.Operators;
    3232using HeuristicLab.Parameters;
    33 using HEAL.Attic;
    3433using HeuristicLab.Problems.Instances;
    3534
     
    3837  [StorableType("F0AB7236-2C9B-49DC-9D4F-A3558FD9E992")]
    3938  [Creatable(CreatableAttribute.Categories.Problems, Priority = 90)]
    40   public sealed class SingleObjectiveTestFunctionProblem :
    41     SingleObjectiveProblem<RealVectorEncoding, RealVector>,
     39  public sealed class SingleObjectiveTestFunctionProblem : RealVectorProblem,
    4240    IProblemInstanceConsumer<SOTFData> {
    4341
     
    7371      get { return TestFunctionParameter.Value; }
    7472      set { TestFunctionParameter.Value = value; }
    75     }
    76 
    77     private BestSingleObjectiveTestFunctionSolutionAnalyzer BestSingleObjectiveTestFunctionSolutionAnalyzer {
    78       get { return Operators.OfType<BestSingleObjectiveTestFunctionSolutionAnalyzer>().FirstOrDefault(); }
    7973    }
    8074    #endregion
     
    121115    }
    122116
     117    public override void Analyze(RealVector[] realVectors, double[] qualities, ResultCollection results, IRandom random) {
     118
     119      bool max = Maximization;
     120      DoubleValue bestKnownQuality = BestKnownQualityParameter.Value;
     121      SingleObjectiveTestFunctionSolution solution = null;
     122      if (results.TryGetValue("Best Solution", out var res)) {
     123        solution = (SingleObjectiveTestFunctionSolution)res.Value;
     124      }
     125
     126      int i = -1;
     127      if (!max) i = qualities.Select((x, index) => new { index, quality = x }).OrderBy(x => x.quality).First().index;
     128      else i = qualities.Select((x, index) => new { index, quality = x }).OrderByDescending(x => x.quality).First().index;
     129
     130      if (bestKnownQuality == null ||
     131          max && qualities[i] > bestKnownQuality.Value
     132          || !max && qualities[i] < bestKnownQuality.Value) {
     133        BestKnownQualityParameter.Value = new DoubleValue(qualities[i]);
     134        BestKnownSolutionParameter.Value = (RealVector)realVectors[i].Clone();
     135        if (solution != null)
     136          solution.BestKnownRealVector = BestKnownSolutionParameter.Value;
     137      }
     138
     139      if (solution == null) {
     140        solution = new SingleObjectiveTestFunctionSolution((RealVector)realVectors[i].Clone(),
     141                                                           new DoubleValue(qualities[i]),
     142                                                           TestFunctionParameter.Value);
     143        solution.Population = realVectors[i].Length == 2
     144          ? new ItemArray<RealVector>(realVectors.Select(x => x.Clone()).Cast<RealVector>())
     145          : null;
     146        solution.BestKnownRealVector = BestKnownSolutionParameter.Value;
     147        solution.Bounds = BoundsParameter.Value;
     148        results.Add(new Result("Best Solution", solution));
     149      } else {
     150        if (max && qualities[i] > solution.BestQuality.Value
     151          || !max && qualities[i] < solution.BestQuality.Value) {
     152          solution.BestRealVector = (RealVector)realVectors[i].Clone();
     153          solution.BestQuality = new DoubleValue(qualities[i]);
     154        }
     155        solution.Population = realVectors[i].Length == 2
     156          ? new ItemArray<RealVector>(realVectors.Select(x => x.Clone()).Cast<RealVector>())
     157          : null;
     158      }
     159    }
     160
    123161    #region Events
    124162    protected override void OnEncodingChanged() {
     
    162200      Operators.Add(new SingleObjectiveTestFunctionPathRelinker());
    163201      Operators.Add(new SingleObjectiveTestFunctionSimilarityCalculator());
    164       Operators.Add(new HammingSimilarityCalculator());
    165202      Operators.Add(new EuclideanSimilarityCalculator());
    166       Operators.Add(new QualitySimilarityCalculator());
    167203      Operators.Add(new AdditiveMoveEvaluator());
    168204
    169       Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
    170       Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
    171205      Parameterize();
    172206    }
     
    174208    private void Parameterize() {
    175209      var operators = new List<IItem>();
    176       if (BestSingleObjectiveTestFunctionSolutionAnalyzer != null) {
    177         operators.Add(BestSingleObjectiveTestFunctionSolutionAnalyzer);
    178         BestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
    179         BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
    180         BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
    181         BestSingleObjectiveTestFunctionSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
    182         BestSingleObjectiveTestFunctionSolutionAnalyzer.TestFunctionParameter.ActualName = TestFunctionParameter.Name;
     210      foreach (var op in Operators.OfType<PopulationSimilarityAnalyzer>()) {
     211        var calcs = Operators.OfType<ISolutionSimilarityCalculator>().ToArray();
     212        op.SimilarityCalculatorParameter.ValidValues.Clear();
     213        foreach (var c in calcs) {
     214          // TODO: unified encoding parameters
     215          c.SolutionVariableName = ((IRealVectorSolutionOperator)Encoding.SolutionCreator).RealVectorParameter.ActualName;
     216          c.QualityVariableName = Evaluator.QualityParameter.ActualName;
     217          op.SimilarityCalculatorParameter.ValidValues.Add(c);
     218        }
    183219      }
    184220      foreach (var op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
Note: See TracChangeset for help on using the changeset viewer.