Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16950


Ignore:
Timestamp:
05/13/19 22:56:49 (6 years ago)
Author:
abeham
Message:

#2521: Refactor Analyze method of encoding-specific problems, simplify Analyze of SO-TF

Location:
branches/2521_ProblemRefactoring
Files:
8 edited

Legend:

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

    r16948 r16950  
    6363    }
    6464
    65     public override void Analyze(BinaryVector[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    66       base.Analyze(individuals, qualities, results, random);
    67       var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
    68       var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
     65    public override void Analyze(BinaryVector[] vectors, double[] qualities, ResultCollection results, IRandom random) {
     66      base.Analyze(vectors, qualities, results, random);
     67      var best = GetBestSolution(vectors, qualities);
    6968
    70       results.AddOrUpdateResult("Best Solution", (IItem)best.Clone());
     69      results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
    7170    }
    7271
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVectorProblem.cs

    r16948 r16950  
    6363    }
    6464
    65     public override void Analyze(IntegerVector[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    66       base.Analyze(individuals, qualities, results, random);
    67       var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
    68       var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
     65    public override void Analyze(IntegerVector[] vectors, double[] qualities, ResultCollection results, IRandom random) {
     66      base.Analyze(vectors, qualities, results, random);
     67      var best = GetBestSolution(vectors, qualities);
    6968
    70       results.AddOrUpdateResult("Best Solution", (IItem)best.Clone());
     69      results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
    7170    }
    7271
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/LinearLinkageProblem.cs

    r16948 r16950  
    6363    }
    6464
    65     public override void Analyze(LinearLinkage[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    66       base.Analyze(individuals, qualities, results, random);
    67       var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
    68       var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
     65    public override void Analyze(LinearLinkage[] vectors, double[] qualities, ResultCollection results, IRandom random) {
     66      base.Analyze(vectors, qualities, results, random);
     67      var best = GetBestSolution(vectors, qualities);
    6968
    70       results.AddOrUpdateResult("Best Solution", (Item)best.Clone());
     69      results.AddOrUpdateResult("Best Solution", (Item)best.Item1.Clone());
    7170    }
    7271
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.PermutationEncoding/3.3/PermutationProblem.cs

    r16948 r16950  
    6868    }
    6969
    70     public override void Analyze(Permutation[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    71       base.Analyze(individuals, qualities, results, random);
    72       var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
    73       var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
    74 
    75       results.AddOrUpdateResult("Best Solution", (IItem)best.Clone());
     70    public override void Analyze(Permutation[] permutations, double[] qualities, ResultCollection results, IRandom random) {
     71      base.Analyze(permutations, qualities, results, random);
     72      var best = GetBestSolution(permutations, qualities);
     73      results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
    7674    }
    7775
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorProblem.cs

    r16949 r16950  
    6363    }
    6464
    65     public override void Analyze(RealVector[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    66       base.Analyze(individuals, qualities, results, random);
    67       var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
    68       var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
     65    public override void Analyze(RealVector[] vectors, double[] qualities, ResultCollection results, IRandom random) {
     66      base.Analyze(vectors, qualities, results, random);
     67      var best = GetBestSolution(vectors, qualities);
    6968
    70       results.AddOrUpdateResult("Best Solution", (IItem)best.Clone());
     69      results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
    7170    }
    7271
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/MultiObjectiveTestFunctionProblem.cs

    r16949 r16950  
    7373      set { ProblemSizeParameter.Value.Value = value; }
    7474    }
    75     public int Objectives {
     75    public new int Objectives {
    7676      get { return ObjectivesParameter.Value.Value; }
    7777      set { ObjectivesParameter.Value.Value = value; }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions.MultiObjective/3.3/TestFunctions/MultiObjectiveTestFunction.cs

    r16723 r16950  
    2222using System;
    2323using System.Collections.Generic;
     24using HEAL.Attic;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    2728using HeuristicLab.Encodings.RealVectorEncoding;
    2829using HeuristicLab.Parameters;
    29 using HEAL.Attic;
    3030
    3131namespace HeuristicLab.Problems.TestFunctions.MultiObjective {
     
    7171    /// </summary>
    7272    public bool[] Maximization(int objectives) {
    73       CheckObjectives(objectives);
     73      ThrowIfObjectivesOutOfRange(objectives);
    7474      return GetMaximization(objectives);
    7575    }
     
    7979    /// </summary>
    8080    public double[,] Bounds(int objectives) {
    81       CheckObjectives(objectives);
     81      ThrowIfObjectivesOutOfRange(objectives);
    8282      return GetBounds(objectives);
    8383    }
     
    8888    /// </summary>
    8989    public IEnumerable<double[]> OptimalParetoFront(int objectives) {
    90       CheckObjectives(objectives);
     90      ThrowIfObjectivesOutOfRange(objectives);
    9191      return GetOptimalParetoFront(objectives);
    9292    }
     
    9797    /// </summary>
    9898    public double[] ReferencePoint(int objectives) {
    99       CheckObjectives(objectives);
     99      ThrowIfObjectivesOutOfRange(objectives);
    100100      return GetReferencePoint(objectives);
    101101    }
     
    106106    /// </summary>     
    107107    public virtual double OptimalHypervolume(int objectives) {
    108       CheckObjectives(objectives);
     108      ThrowIfObjectivesOutOfRange(objectives);
    109109      return GetBestKnownHypervolume(objectives);
    110110    }
     
    114114    }
    115115
    116     protected void CheckObjectives(int objectives) {
     116    protected void ThrowIfObjectivesOutOfRange(int objectives) {
    117117      if (objectives < MinimumObjectives) throw new ArgumentException(string.Format("There must be at least {0} objectives", MinimumObjectives));
    118118      if (objectives > MaximumObjectives) throw new ArgumentException(string.Format("There must be at most {0} objectives", MaximumObjectives));
     
    121121    [StorableConstructor]
    122122    protected MultiObjectiveTestFunction(StorableConstructorFlag _) : base(_) { }
    123 
    124123    protected MultiObjectiveTestFunction(MultiObjectiveTestFunction original, Cloner cloner)
    125124      : base(original, cloner) {
     
    129128      MaximumSolutionLength = original.MaximumSolutionLength;
    130129    }
    131 
    132130    protected MultiObjectiveTestFunction(int minimumObjectives, int maximumObjectives, int minimumSolutionLength, int maximumSolutionLength)
    133131      : base() {
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs

    r16949 r16950  
    116116
    117117    public override void Analyze(RealVector[] realVectors, double[] qualities, ResultCollection results, IRandom random) {
    118 
    119       bool max = Maximization;
     118      var best = GetBestSolution(realVectors, qualities);
     119
    120120      DoubleValue bestKnownQuality = BestKnownQualityParameter.Value;
     121      RealVector bestKnownSolution = null;
     122      var bestKnownUpdate = bestKnownQuality == null || IsBetter(best.Item2, bestKnownQuality.Value);
     123      if (bestKnownUpdate) {
     124        if (bestKnownQuality != null) bestKnownQuality.Value = best.Item2;
     125        else BestKnownQualityParameter.Value = bestKnownQuality = new DoubleValue(best.Item2);
     126        BestKnownSolutionParameter.Value = bestKnownSolution = (RealVector)best.Item1.Clone();
     127      }
     128
    121129      SingleObjectiveTestFunctionSolution solution = null;
    122130      if (results.TryGetValue("Best Solution", out var res)) {
    123131        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;
     132        if (IsBetter(best.Item2, solution.BestQuality.Value)) {
     133          solution.BestRealVector = (RealVector)best.Item1.Clone();
     134          solution.BestQuality = new DoubleValue(best.Item2);
     135        }
     136      } else {
     137        solution = new SingleObjectiveTestFunctionSolution((RealVector)best.Item1.Clone(),
     138                                                           new DoubleValue(best.Item2),
     139                                                           TestFunctionParameter.Value) {
     140          BestKnownRealVector = bestKnownSolution,
     141          Bounds = BoundsParameter.Value
     142        };
    148143        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       }
     144      }
     145      if (best.Item1.Length == 2) solution.Population = new ItemArray<RealVector>(realVectors.Select(x => (RealVector)x.Clone()));
     146      if (bestKnownUpdate) solution.BestKnownRealVector = bestKnownSolution;
    159147    }
    160148
Note: See TracChangeset for help on using the changeset viewer.