Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/02/14 10:50:44 (10 years ago)
Author:
mkommend
Message:

#2249: Refactored best scope solution analyzer.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Analysis/3.3/BestScopeSolutionAnalyzer.cs

    r11171 r11615  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    3738  [StorableClass]
    3839  public class BestScopeSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
     40    private const string BestSolutionResultName = "Best Solution";
     41
    3942    public virtual bool EnabledByDefault {
    4043      get { return true; }
    4144    }
    42 
    4345    public LookupParameter<BoolValue> MaximizationParameter {
    4446      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
     
    4648    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
    4749      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
    48     }
    49     public ILookupParameter<IScope> BestSolutionParameter {
    50       get { return (ILookupParameter<IScope>)Parameters["BestSolution"]; }
    51     }
    52     public ILookupParameter<IScope> BestKnownSolutionParameter {
    53       get { return (ILookupParameter<IScope>)Parameters["BestKnownSolution"]; }
    5450    }
    5551    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
     
    7268      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
    7369      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the solutions."));
    74       Parameters.Add(new LookupParameter<IScope>("BestSolution", "The best solution."));
    75       Parameters.Add(new LookupParameter<IScope>("BestKnownSolution", "The best known solution."));
    7670      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution."));
    7771      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the solution should be stored."));
     
    8478      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
    8579
     80      if (results.ContainsKey(BestSolutionResultName) && !typeof(IScope).IsAssignableFrom(results[BestSolutionResultName].DataType)) {
     81        throw new InvalidOperationException(string.Format("Could not add best solution result, because there is already a result with the name \"{0}\" present in the result collecdtion.", BestSolutionResultName));
     82      }
     83
    8684      int i = -1;
    8785      if (!max)
     
    9189      IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
    9290      for (int j = 0; j < QualityParameter.Depth; j++)
    93         scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
     91        scopes = scopes.SelectMany(x => x.SubScopes);
    9492      IScope currentBestScope = scopes.ToList()[i];
    9593
     
    9896          || !max && qualities[i].Value < bestKnownQuality.Value) {
    9997        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
    100         BestKnownSolutionParameter.ActualValue = (IScope)currentBestScope.Clone();
    10198      }
    10299
    103       IScope solution = BestSolutionParameter.ActualValue;
    104       if (solution == null) {
    105         solution = (IScope)currentBestScope.Clone();
    106         BestSolutionParameter.ActualValue = solution;
    107         results.Add(new Result("Best Solution", solution));
     100      if (!results.ContainsKey(BestSolutionResultName)) {
     101        var cloner = new Cloner();
     102        //avoid cloning of subscopes
     103        cloner.RegisterClonedObject(currentBestScope.SubScopes, new ScopeList());
     104        var solution = cloner.Clone(currentBestScope);
     105
     106        results.Add(new Result(BestSolutionResultName, solution));
    108107      } else {
     108        var bestSolution = (IScope)results[BestSolutionResultName].Value;
    109109        string qualityName = QualityParameter.TranslatedName;
    110         if (solution.Variables.ContainsKey(qualityName)) {
    111           double bestSoFarQuality = (solution.Variables[qualityName].Value as DoubleValue).Value;
    112           if (max && qualities[i].Value > bestSoFarQuality
    113             || !max && qualities[i].Value < bestSoFarQuality) {
    114             solution = (IScope)currentBestScope.Clone();
    115             BestSolutionParameter.ActualValue = solution;
    116             results["Best Solution"].Value = solution;
     110        if (bestSolution.Variables.ContainsKey(qualityName)) {
     111          double bestQuality = ((DoubleValue)bestSolution.Variables[qualityName].Value).Value;
     112          if (max && qualities[i].Value > bestQuality
     113              || !max && qualities[i].Value < bestQuality) {
     114            var cloner = new Cloner();
     115            //avoid cloning of subscopes
     116            cloner.RegisterClonedObject(currentBestScope.SubScopes, new ScopeList());
     117            var solution = cloner.Clone(currentBestScope);
     118
     119            results[BestSolutionResultName].Value = solution;
    117120          }
    118121        }
    119122      }
    120 
    121123      return base.Apply();
    122124    }
Note: See TracChangeset for help on using the changeset viewer.