Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/05/15 10:42:38 (10 years ago)
Author:
abeham
Message:

#2249: merged to stable

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Analysis

  • stable/HeuristicLab.Analysis/3.3/BestScopeSolutionAnalyzer.cs

    r11170 r11906  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    3738  [StorableClass]
    3839  public class BestScopeSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
     40
    3941    public virtual bool EnabledByDefault {
    4042      get { return true; }
    4143    }
    42 
    4344    public LookupParameter<BoolValue> MaximizationParameter {
    4445      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
     
    4748      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
    4849    }
    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"]; }
     50    public IFixedValueParameter<StringValue> BestSolutionResultNameParameter {
     51      get { return (IFixedValueParameter<StringValue>)Parameters["BestSolution ResultName"]; }
    5452    }
    5553    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
     
    5856    public IValueLookupParameter<ResultCollection> ResultsParameter {
    5957      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
     58    }
     59
     60    public string BestSolutionResultName {
     61      get { return BestSolutionResultNameParameter.Value.Value; }
     62      set { BestSolutionResultNameParameter.Value.Value = value; }
    6063    }
    6164
     
    6770      return new BestScopeSolutionAnalyzer(this, cloner);
    6871    }
     72
     73    [StorableHook(HookType.AfterDeserialization)]
     74    private void AfterDeserialization() {
     75      // BackwardsCompatibility3.3
     76      #region Backwards compatible code, remove with 3.4
     77      if (!Parameters.ContainsKey("BestSolution ResultName"))
     78        Parameters.Add(new FixedValueParameter<StringValue>("BestSolution ResultName", "The name of the result for storing the best solution.", new StringValue("Best Solution")));
     79      if (Parameters.ContainsKey("BestSolution")) Parameters.Remove("BestSolution");
     80      if (Parameters.ContainsKey("BestKnownSolution")) Parameters.Remove("BestKnownSolution");
     81      #endregion
     82    }
    6983    #endregion
    7084    public BestScopeSolutionAnalyzer()
     
    7286      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
    7387      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."));
     88      Parameters.Add(new FixedValueParameter<StringValue>("BestSolution ResultName", "The name of the result for storing the best solution.", new StringValue("Best Solution")));
    7689      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution."));
    7790      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the solution should be stored."));
     
    8497      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
    8598
     99      if (results.ContainsKey(BestSolutionResultName) && !typeof(IScope).IsAssignableFrom(results[BestSolutionResultName].DataType)) {
     100        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 collection.", BestSolutionResultName));
     101      }
     102
    86103      int i = -1;
    87104      if (!max)
     
    91108      IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope };
    92109      for (int j = 0; j < QualityParameter.Depth; j++)
    93         scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
     110        scopes = scopes.SelectMany(x => x.SubScopes);
    94111      IScope currentBestScope = scopes.ToList()[i];
    95112
     
    98115          || !max && qualities[i].Value < bestKnownQuality.Value) {
    99116        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
    100         BestKnownSolutionParameter.ActualValue = (IScope)currentBestScope.Clone();
    101117      }
    102118
    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));
     119      if (!results.ContainsKey(BestSolutionResultName)) {
     120        var cloner = new Cloner();
     121        //avoid cloning of subscopes and the results collection that the solution is put in
     122        cloner.RegisterClonedObject(results, new ResultCollection());
     123        cloner.RegisterClonedObject(currentBestScope.SubScopes, new ScopeList());
     124        var solution = cloner.Clone(currentBestScope);
     125
     126        results.Add(new Result(BestSolutionResultName, solution));
    108127      } else {
     128        var bestSolution = (IScope)results[BestSolutionResultName].Value;
    109129        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;
     130        if (bestSolution.Variables.ContainsKey(qualityName)) {
     131          double bestQuality = ((DoubleValue)bestSolution.Variables[qualityName].Value).Value;
     132          if (max && qualities[i].Value > bestQuality
     133              || !max && qualities[i].Value < bestQuality) {
     134            var cloner = new Cloner();
     135            //avoid cloning of subscopes and the results collection that the solution is put in
     136            cloner.RegisterClonedObject(results, new ResultCollection());
     137            cloner.RegisterClonedObject(currentBestScope.SubScopes, new ScopeList());
     138            var solution = cloner.Clone(currentBestScope);
     139
     140            results[BestSolutionResultName].Value = solution;
    117141          }
    118142        }
    119143      }
    120 
    121144      return base.Apply();
    122145    }
Note: See TracChangeset for help on using the changeset viewer.