Changeset 11906 for stable/HeuristicLab.Analysis
- Timestamp:
- 02/05/15 10:42:38 (10 years ago)
- Location:
- stable
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 11615-11616,11618,11854,11856
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Analysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Analysis merged: 11615,11618,11854,11856
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Analysis/3.3/BestScopeSolutionAnalyzer.cs
r11170 r11906 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 39 41 public virtual bool EnabledByDefault { 40 42 get { return true; } 41 43 } 42 43 44 public LookupParameter<BoolValue> MaximizationParameter { 44 45 get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; } … … 47 48 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; } 48 49 } 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"]; } 54 52 } 55 53 public ILookupParameter<DoubleValue> BestKnownQualityParameter { … … 58 56 public IValueLookupParameter<ResultCollection> ResultsParameter { 59 57 get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; } 58 } 59 60 public string BestSolutionResultName { 61 get { return BestSolutionResultNameParameter.Value.Value; } 62 set { BestSolutionResultNameParameter.Value.Value = value; } 60 63 } 61 64 … … 67 70 return new BestScopeSolutionAnalyzer(this, cloner); 68 71 } 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 } 69 83 #endregion 70 84 public BestScopeSolutionAnalyzer() … … 72 86 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem.")); 73 87 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"))); 76 89 Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution.")); 77 90 Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the solution should be stored.")); … … 84 97 DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue; 85 98 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 86 103 int i = -1; 87 104 if (!max) … … 91 108 IEnumerable<IScope> scopes = new IScope[] { ExecutionContext.Scope }; 92 109 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); 94 111 IScope currentBestScope = scopes.ToList()[i]; 95 112 … … 98 115 || !max && qualities[i].Value < bestKnownQuality.Value) { 99 116 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value); 100 BestKnownSolutionParameter.ActualValue = (IScope)currentBestScope.Clone();101 117 } 102 118 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)); 108 127 } else { 128 var bestSolution = (IScope)results[BestSolutionResultName].Value; 109 129 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; 117 141 } 118 142 } 119 143 } 120 121 144 return base.Apply(); 122 145 }
Note: See TracChangeset
for help on using the changeset viewer.