Changeset 11615 for trunk/sources/HeuristicLab.Analysis
- Timestamp:
- 12/02/14 10:50:44 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Analysis/3.3/BestScopeSolutionAnalyzer.cs
r11171 r11615 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 37 38 [StorableClass] 38 39 public class BestScopeSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer { 40 private const string BestSolutionResultName = "Best Solution"; 41 39 42 public virtual bool EnabledByDefault { 40 43 get { return true; } 41 44 } 42 43 45 public LookupParameter<BoolValue> MaximizationParameter { 44 46 get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; } … … 46 48 public ScopeTreeLookupParameter<DoubleValue> QualityParameter { 47 49 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"]; }54 50 } 55 51 public ILookupParameter<DoubleValue> BestKnownQualityParameter { … … 72 68 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem.")); 73 69 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."));76 70 Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution.")); 77 71 Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the solution should be stored.")); … … 84 78 DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue; 85 79 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 86 84 int i = -1; 87 85 if (!max) … … 91 89 IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope }; 92 90 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); 94 92 IScope currentBestScope = scopes.ToList()[i]; 95 93 … … 98 96 || !max && qualities[i].Value < bestKnownQuality.Value) { 99 97 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value); 100 BestKnownSolutionParameter.ActualValue = (IScope)currentBestScope.Clone();101 98 } 102 99 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)); 108 107 } else { 108 var bestSolution = (IScope)results[BestSolutionResultName].Value; 109 109 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; 117 120 } 118 121 } 119 122 } 120 121 123 return base.Apply(); 122 124 }
Note: See TracChangeset
for help on using the changeset viewer.