Free cookie consent management tool by TermsFeed Policy Generator

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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.