Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/27/15 15:37:46 (9 years ago)
Author:
gkronber
Message:

#2472: update best solution only if a solution with a better best quality is found

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeProblem.cs

    r12904 r12921  
    2626using HeuristicLab.Common;
    2727using HeuristicLab.Core;
     28using HeuristicLab.Data;
    2829using HeuristicLab.Optimization;
    2930using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    5657    }
    5758
    58     public virtual void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results, IRandom random) {
    59       var bestQuality = Maximization ? qualities.Max() : qualities.Min();
    60       var bestIdx = Array.IndexOf(qualities, bestQuality);
    61       var best = trees[bestIdx];
     59    public virtual void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results,
     60      IRandom random) {
     61      if (!results.ContainsKey("Best Solution Quality")) {
     62        results.Add(new Result("Best Solution Quality", typeof(DoubleValue)));
     63      }
    6264      if (!results.ContainsKey("Best Solution")) {
    6365        results.Add(new Result("Best Solution", typeof(ISymbolicExpressionTree)));
    6466      }
    65       results["Best Solution"].Value = (IItem)best.Clone();
     67
     68      var bestQuality = Maximization ? qualities.Max() : qualities.Min();
     69
     70      if (results["Best Solution Quality"].Value == null ||
     71          IsBetter(bestQuality, ((DoubleValue)results["Best Solution Quality"].Value).Value)) {
     72        var bestIdx = Array.IndexOf(qualities, bestQuality);
     73        var bestClone = (IItem)trees[bestIdx].Clone();
     74        results["Best Solution"].Value = bestClone;
     75        results["Best Solution Quality"].Value = new DoubleValue(bestQuality);
     76      }
    6677    }
     78
    6779    public sealed override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    6880      Analyze(individuals.Select(ind => ind.SymbolicExpressionTree()).ToArray(), qualities, results, random);
Note: See TracChangeset for help on using the changeset viewer.