Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17522


Ignore:
Timestamp:
04/27/20 18:12:39 (4 years ago)
Author:
abeham
Message:

#2521 WIP refactoring:

  1. Introduce nicer type IResultDefinition for API users to avoid complex IParameter interface (hide ActualValue)
  2. Change result parameter to contexts (need quality and solution): only implemented for BinaryVectorProblem
Location:
branches/2521_ProblemRefactoring
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorMultiObjectiveProblem.cs

    r17230 r17522  
    3131  [StorableType("b64caac0-a23a-401a-bb7e-ffa3e22b80ea")]
    3232  public abstract class BinaryVectorMultiObjectiveProblem : MultiObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
     33    [Storable] protected IResultParameter<ParetoFrontScatterPlot<BinaryVector>> BestResultParameter { get; private set; }
     34    public IResultDefinition<ParetoFrontScatterPlot<BinaryVector>> BestResult { get { return BestResultParameter; } }
     35
    3336    public int Length {
    3437      get { return Encoding.Length; }
     
    4548    protected BinaryVectorMultiObjectiveProblem(BinaryVectorMultiObjectiveProblem original, Cloner cloner)
    4649      : base(original, cloner) {
     50      BestResultParameter = cloner.Clone(original.BestResultParameter);
    4751      RegisterEventHandlers();
    4852    }
     
    5155    protected BinaryVectorMultiObjectiveProblem(BinaryVectorEncoding encoding) : base(encoding) {
    5256      EncodingParameter.ReadOnly = true;
     57      Parameters.Add(BestResultParameter = new ResultParameter<ParetoFrontScatterPlot<BinaryVector>>("Best Pareto Front", "The best Pareto front found."));
    5358
    5459      Operators.Add(new HammingSimilarityCalculator());
     
    6469      var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(individuals, qualities, Maximization);
    6570      var plot = new ParetoFrontScatterPlot<BinaryVector>(fronts, individuals, qualities, Objectives, BestKnownFront);
    66       results.AddOrUpdateResult("Pareto Front Scatter Plot", plot);
     71     
     72      BestResultParameter.ActualValue = plot;
    6773    }
    6874
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorProblem.cs

    r17517 r17522  
    3333  [StorableType("2F6FEB34-BD19-47AF-9484-7F48565C0C43")]
    3434  public abstract class BinaryVectorProblem : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
    35     [Storable] public IResultParameter<BinaryVector> BestSolutionParameter { get; private set; }
     35    [Storable] protected IResultParameter<ISingleObjectiveSolutionContext<BinaryVector>> BestResultParameter { get; private set; }
     36    public IResultDefinition<ISingleObjectiveSolutionContext<BinaryVector>> BestResult { get { return BestResultParameter; } }
    3637
    3738    public int Length {
     
    4950    protected BinaryVectorProblem(BinaryVectorProblem original, Cloner cloner)
    5051      : base(original, cloner) {
    51       BestSolutionParameter = cloner.Clone(original.BestSolutionParameter);
     52      BestResultParameter = cloner.Clone(original.BestResultParameter);
    5253      RegisterEventHandlers();
    5354    }
     
    5657    protected BinaryVectorProblem(BinaryVectorEncoding encoding) : base(encoding) {
    5758      EncodingParameter.ReadOnly = true;
    58       BestSolutionParameter = new ResultParameter<BinaryVector>("Best Solution", "The best solution.");
    59       Parameters.Add(BestSolutionParameter);
     59      Parameters.Add(BestResultParameter = new ResultParameter<ISingleObjectiveSolutionContext<BinaryVector>>("Best Solution", "The best solution."));
    6060
    6161      Operators.Add(new HammingSimilarityCalculator());
     
    6767    }
    6868
    69     public override void Analyze(BinaryVector[] vectors, double[] qualities, ResultCollection results, IRandom random) {
    70       base.Analyze(vectors, qualities, results, random);
    71       var best = GetBestSolution(vectors, qualities);
    72       BestSolutionParameter.ActualValue = (BinaryVector)best.Item1.Clone();
     69    public override void Analyze(ISingleObjectiveSolutionContext<BinaryVector>[] solutionContexts, ResultCollection results, IRandom random) {
     70      var best = GetBest(solutionContexts);
     71      var currentBest = BestResultParameter.ActualValue;
     72      if (currentBest == null || IsBetter(best.EvaluationResult.Quality, currentBest.EvaluationResult.Quality))
     73        BestResultParameter.ActualValue = (ISingleObjectiveSolutionContext<BinaryVector>)best.Clone();
    7374    }
    7475
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVectorMultiObjectiveProblem.cs

    r17230 r17522  
    3333  [StorableType("11916b0f-4c34-4ece-acae-e28d11211b43")]
    3434  public abstract class IntegerVectorMultiObjectiveProblem : MultiObjectiveProblem<IntegerVectorEncoding, IntegerVector> {
     35    [Storable] protected IResultParameter<ParetoFrontScatterPlot<IntegerVector>> BestResultParameter { get; private set; }
     36    public IResultDefinition<ParetoFrontScatterPlot<IntegerVector>> BestResult { get { return BestResultParameter; } }
     37
    3538    public int Length {
    3639      get { return Encoding.Length; }
     
    4750    protected IntegerVectorMultiObjectiveProblem(IntegerVectorMultiObjectiveProblem original, Cloner cloner)
    4851      : base(original, cloner) {
     52      BestResultParameter = cloner.Clone(original.BestResultParameter);
    4953      RegisterEventHandlers();
    5054    }
     
    5357    protected IntegerVectorMultiObjectiveProblem(IntegerVectorEncoding encoding) : base(encoding) {
    5458      EncodingParameter.ReadOnly = true;
     59      Parameters.Add(BestResultParameter = new ResultParameter<ParetoFrontScatterPlot<IntegerVector>>("Best Pareto Front", "The best Pareto front found."));
    5560
    5661      Operators.Add(new HammingSimilarityCalculator());
     
    6671      var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(individuals, qualities, Maximization);
    6772      var plot = new ParetoFrontScatterPlot<IntegerVector>(fronts, individuals, qualities, Objectives, BestKnownFront);
    68       results.AddOrUpdateResult("Pareto Front Scatter Plot", plot);
     73
     74      BestResultParameter.ActualValue = plot;
    6975    }
    7076
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVectorProblem.cs

    r16950 r17522  
    3434  [StorableType("c6081457-a3de-45ce-9f47-e0eb1c851bd2")]
    3535  public abstract class IntegerVectorProblem : SingleObjectiveProblem<IntegerVectorEncoding, IntegerVector> {
     36    [Storable] protected IResultParameter<IntegerVector> BestResultParameter { get; private set; }
     37    public IResultDefinition<IntegerVector> BestResult { get => BestResultParameter; }
     38
    3639    public int Length {
    3740      get { return Encoding.Length; }
     
    4851    protected IntegerVectorProblem(IntegerVectorProblem original, Cloner cloner)
    4952      : base(original, cloner) {
     53      BestResultParameter = cloner.Clone(original.BestResultParameter);
    5054      RegisterEventHandlers();
    5155    }
     
    5458    protected IntegerVectorProblem(IntegerVectorEncoding encoding) : base(encoding) {
    5559      EncodingParameter.ReadOnly = true;
     60      Parameters.Add(BestResultParameter = new ResultParameter<IntegerVector>("Best Solution", "The best solution."));
    5661
    5762      Operators.Add(new HammingSimilarityCalculator());
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs

    r17382 r17522  
    149149
    150150    //TODO refactor to solution contexts
     151    protected ISingleObjectiveSolutionContext<TEncodedSolution> GetBest(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts) {
     152      return Maximization ? solutionContexts.MaxItems(x => x.EvaluationResult.Quality).First()
     153        : solutionContexts.MinItems(x => x.EvaluationResult.Quality).First();
     154    }
    151155    protected Tuple<TEncodedSolution, double> GetBestSolution(TEncodedSolution[] solutions, double[] qualities) {
    152156      return GetBestSolution(solutions, qualities, Maximization);
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Results/IResultParameter.cs

    r17517 r17522  
    2121
    2222using System;
     23using HEAL.Attic;
    2324using HeuristicLab.Core;
    24 using HEAL.Attic;
    2525
    2626namespace HeuristicLab.Optimization {
     27  [StorableType("986fa3d0-38f8-43aa-820e-e67d09a29025")]
     28  public interface IResultDefinition {
     29    string Name { get; set; }
     30  }
     31
     32  [StorableType("4c0c854b-676d-4ccd-96c4-b06a3d7f2fa1")]
     33  public interface IResultDefinition<T> : IResultDefinition where T : class, IItem {
     34    T Get(ResultCollection results);
     35  }
     36
    2737  [StorableType("af5d3f60-6f3a-4a44-a906-688ac8296fe3")]
    28   public interface IResultParameter : ILookupParameter {
     38  public interface IResultParameter : ILookupParameter, IResultDefinition {
    2939    string ResultCollectionName { get; set; }
    3040    ResultCollection ResultCollection { get; set; }
     
    3444
    3545  [StorableType("803e6ad6-dd9d-497a-ad1c-7cd3dc5b0d3c")]
    36   public interface IResultParameter<T> : ILookupParameter<T>, IResultParameter where T : class, IItem {
     46  public interface IResultParameter<T> : ILookupParameter<T>, IResultParameter, IResultDefinition<T> where T : class, IItem {
    3747    T DefaultValue { get; set; }
    3848    event EventHandler DefaultValueChanged;
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Results/ResultParameter.cs

    r17517 r17522  
    6262    [Storable]
    6363    public ResultCollection ResultCollection { get; set; }
     64
     65    string IResultDefinition.Name { get => ActualName; set => ActualName = value; }
     66    T IResultDefinition<T>.Get(ResultCollection results) => results[ActualName].Value as T;
    6467
    6568    [StorableConstructor]
Note: See TracChangeset for help on using the changeset viewer.