- Timestamp:
- 05/11/10 21:16:12 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.Knapsack/3.3/Analyzers/BestKnapsackSolutionAnalyzer.cs
r3667 r3787 39 39 [StorableClass] 40 40 class BestKnapsackSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer { 41 41 public LookupParameter<BoolValue> MaximizationParameter { 42 get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; } 43 } 42 44 public ScopeTreeLookupParameter<BinaryVector> BinaryVectorParameter { 43 45 get { return (ScopeTreeLookupParameter<BinaryVector>)Parameters["BinaryVector"]; } … … 61 63 get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; } 62 64 } 65 public LookupParameter<DoubleValue> BestKnownQualityParameter { 66 get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; } 67 } 68 public LookupParameter<BinaryVector> BestKnownSolutionParameter { 69 get { return (LookupParameter<BinaryVector>)Parameters["BestKnownSolution"]; } 70 } 63 71 64 72 public BestKnapsackSolutionAnalyzer() 65 73 : base() { 74 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem.")); 66 75 Parameters.Add(new ScopeTreeLookupParameter<BinaryVector>("BinaryVector", "The knapsack solutions from which the best solution should be visualized.")); 67 76 Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.")); … … 72 81 Parameters.Add(new LookupParameter<KnapsackSolution>("BestSolution", "The best knapsack solution.")); 73 82 Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the knapsack solution should be stored.")); 83 Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution.")); 84 Parameters.Add(new LookupParameter<BinaryVector>("BestKnownSolution", "The best known solution.")); 74 85 } 75 86 … … 78 89 ItemArray<DoubleValue> qualities = QualityParameter.ActualValue; 79 90 ResultCollection results = ResultsParameter.ActualValue; 91 bool max = MaximizationParameter.ActualValue.Value; 92 DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue; 80 93 81 int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; 94 int i = -1; 95 if (!max) 96 i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; 97 else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index; 98 99 if (bestKnownQuality == null || 100 max && qualities[i].Value > bestKnownQuality.Value || 101 !max && qualities[i].Value < bestKnownQuality.Value) { 102 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value); 103 BestKnownSolutionParameter.ActualValue = (BinaryVector)binaryVectors[i].Clone(); 104 } 82 105 83 106 KnapsackSolution solution = BestSolutionParameter.ActualValue; 84 107 if (solution == null) { 85 solution = new KnapsackSolution( binaryVectors[i], qualities[i],108 solution = new KnapsackSolution((BinaryVector)binaryVectors[i].Clone(), new DoubleValue(qualities[i].Value), 86 109 KnapsackCapacityParameter.ActualValue, WeightsParameter.ActualValue, ValuesParameter.ActualValue); 87 110 BestSolutionParameter.ActualValue = solution; 88 111 results.Add(new Result("Best Knapsack Solution", solution)); 89 112 } else { 90 solution.BinaryVector = binaryVectors[i]; 91 solution.Quality = qualities[i]; 92 solution.Capacity = KnapsackCapacityParameter.ActualValue; 93 solution.Weights = WeightsParameter.ActualValue; 94 solution.Values = ValuesParameter.ActualValue; 95 96 results["Best Knapsack Solution"].Value = solution; 113 if (max && qualities[i].Value > solution.Quality.Value || 114 !max && qualities[i].Value < solution.Quality.Value) { 115 solution.BinaryVector = (BinaryVector)binaryVectors[i].Clone(); 116 solution.Quality = new DoubleValue(qualities[i].Value); 117 solution.Capacity = KnapsackCapacityParameter.ActualValue; 118 solution.Weights = WeightsParameter.ActualValue; 119 solution.Values = ValuesParameter.ActualValue; 120 } 97 121 } 98 122
Note: See TracChangeset
for help on using the changeset viewer.