- Timestamp:
- 04/30/11 15:40:40 (14 years ago)
- Location:
- branches/histogram/HeuristicLab.Problems.QuadraticAssignment/3.3
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/histogram/HeuristicLab.Problems.QuadraticAssignment/3.3/Analyzers/BestQAPSolutionAnalyzer.cs
r5933 r6086 61 61 get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; } 62 62 } 63 public LookupParameter<ItemList<Permutation>> BestKnownSolutionsParameter { 64 get { return (LookupParameter<ItemList<Permutation>>)Parameters["BestKnownSolutions"]; } 65 } 63 66 public LookupParameter<Permutation> BestKnownSolutionParameter { 64 67 get { return (LookupParameter<Permutation>)Parameters["BestKnownSolution"]; } … … 81 84 Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best QAP solution should be stored.")); 82 85 Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this QAP instance.")); 86 Parameters.Add(new LookupParameter<ItemList<Permutation>>("BestKnownSolutions", "The best known solutions of this QAP instance.")); 83 87 Parameters.Add(new LookupParameter<Permutation>("BestKnownSolution", "The best known solution of this QAP instance.")); 88 } 89 90 [StorableHook(HookType.AfterDeserialization)] 91 private void AfterDeserialization() { 92 // BackwardsCompatibility3.3 93 #region Backwards compatible code, remove with 3.4 94 /*if (Parameters.ContainsKey("BestKnownSolution")) { 95 Parameters.Remove("BestKnownSolution"); 96 Parameters.Add(new LookupParameter<ItemList<Permutation>>("BestKnownSolutions", "The best known solutions of this QAP instance.")); 97 }*/ 98 if (!Parameters.ContainsKey("BestKnownSolutions")) { 99 Parameters.Add(new LookupParameter<ItemList<Permutation>>("BestKnownSolutions", "The best known solutions of this QAP instance.")); 100 } 101 #endregion 84 102 } 85 103 … … 93 111 DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue; 94 112 95 int i = -1; 96 if (!max) 97 i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; 98 else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index; 113 var sorted = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).ToArray(); 114 if (max) sorted = sorted.Reverse().ToArray(); 115 int i = sorted.First().index; 99 116 100 if (bestKnownQuality == null || 101 max && qualities[i].Value > bestKnownQuality.Value || 102 !max && qualities[i].Value < bestKnownQuality.Value) { 117 if (bestKnownQuality == null 118 || max && qualities[i].Value > bestKnownQuality.Value 119 || !max && qualities[i].Value < bestKnownQuality.Value 120 || bestKnownQuality.Value == qualities[i].Value 121 && (BestKnownSolutionsParameter.ActualValue == null || BestKnownSolutionsParameter.ActualValue.Count == 0)) { 103 122 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value); 104 123 BestKnownSolutionParameter.ActualValue = (Permutation)permutations[i].Clone(); 124 BestKnownSolutionsParameter.ActualValue = new ItemList<Permutation>(); 125 BestKnownSolutionsParameter.ActualValue.Add((Permutation)permutations[i].Clone()); 126 } else if (bestKnownQuality != null && qualities[i].Value == bestKnownQuality.Value) { 127 PermutationEqualityComparer comparer = new PermutationEqualityComparer(); 128 foreach (var k in sorted) { 129 if (!max && k.Value > qualities[i].Value 130 || max && k.Value < qualities[i].Value) break; 131 Permutation p = permutations[k.index]; 132 bool alreadyPresent = false; 133 foreach (Permutation p2 in BestKnownSolutionsParameter.ActualValue) { 134 if (comparer.Equals(p, p2)) { 135 alreadyPresent = true; 136 break; 137 } 138 } 139 if (!alreadyPresent) 140 BestKnownSolutionsParameter.ActualValue.Add((Permutation)permutations[k.index].Clone()); 141 } 105 142 } 106 143 -
branches/histogram/HeuristicLab.Problems.QuadraticAssignment/3.3/Evaluators/QAPEvaluator.cs
r5838 r6086 69 69 } 70 70 71 public static double Impact(int facility, Permutation assignment, DoubleMatrix weights, DoubleMatrix distances) { 72 double impact = 0; 73 for (int i = 0; i < assignment.Length; i++) { 74 impact += weights[facility, i] * distances[assignment[facility], assignment[i]]; 75 impact += weights[i, facility] * distances[assignment[i], assignment[facility]]; 76 } 77 return impact; 78 } 79 71 80 public override IOperation Apply() { 72 81 Permutation assignment = PermutationParameter.ActualValue; -
branches/histogram/HeuristicLab.Problems.QuadraticAssignment/3.3/HeuristicLab.Problems.QuadraticAssignment-3.3.csproj
r5996 r6086 120 120 <Compile Include="Interfaces\IQAPEvaluator.cs" /> 121 121 <Compile Include="Interfaces\IQAPMoveEvaluator.cs" /> 122 <Compile Include="LocalImprovement\QAPExhaustiveSwap2LocalImprovement.cs" /> 122 123 <Compile Include="Parsers\QAPLIBSolutionParser.cs" /> 123 124 <Compile Include="Parsers\QAPLIBParser.cs" /> -
branches/histogram/HeuristicLab.Problems.QuadraticAssignment/3.3/QuadraticAssignmentProblem.cs
r6046 r6086 49 49 50 50 #region Parameter Properties 51 public IValueParameter<ItemList<Permutation>> BestKnownSolutionsParameter { 52 get { return (IValueParameter<ItemList<Permutation>>)Parameters["BestKnownSolutions"]; } 53 } 51 54 public IValueParameter<Permutation> BestKnownSolutionParameter { 52 55 get { return (IValueParameter<Permutation>)Parameters["BestKnownSolution"]; } … … 61 64 62 65 #region Properties 66 public ItemList<Permutation> BestKnownSolutions { 67 get { return BestKnownSolutionsParameter.Value; } 68 set { BestKnownSolutionsParameter.Value = value; } 69 } 63 70 public Permutation BestKnownSolution { 64 71 get { return BestKnownSolutionParameter.Value; } … … 106 113 public QuadraticAssignmentProblem() 107 114 : base(new QAPEvaluator(), new RandomPermutationCreator()) { 115 Parameters.Add(new OptionalValueParameter<ItemList<Permutation>>("BestKnownSolutions", "The list of best known solutions which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null)); 108 116 Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null)); 109 117 Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The strength of the connection between the facilities.", new DoubleMatrix(5, 5))); … … 138 146 public override IDeepCloneable Clone(Cloner cloner) { 139 147 return new QuadraticAssignmentProblem(this, cloner); 148 } 149 150 [StorableHook(HookType.AfterDeserialization)] 151 private void AfterDeserialization() { 152 // BackwardsCompatibility3.3 153 #region Backwards compatible code, remove with 3.4 154 /*if (Parameters.ContainsKey("BestKnownSolution")) { 155 Permutation solution = ((IValueParameter<Permutation>)Parameters["BestKnownSolution"]).Value; 156 Parameters.Remove("BestKnownSolution"); 157 Parameters.Add(new OptionalValueParameter<ItemList<Permutation>>("BestKnownSolutions", "The list of best known solutions which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null)); 158 if (solution != null) { 159 BestKnownSolutions = new ItemList<Permutation>(); 160 BestKnownSolutions.Add(solution); 161 } 162 }*/ 163 if (!Parameters.ContainsKey("BestKnownSolutions")) { 164 Parameters.Add(new OptionalValueParameter<ItemList<Permutation>>("BestKnownSolutions", "The list of best known solutions which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null)); 165 } 166 #endregion 140 167 } 141 168 … … 246 273 Operators.Add(new QAPAlleleFrequencyAnalyzer()); 247 274 Operators.Add(new QAPPopulationDiversityAnalyzer()); 275 Operators.Add(new QAPExhaustiveSwap2LocalImprovement()); 248 276 ParameterizeAnalyzers(); 249 277 ParameterizeOperators(); … … 270 298 BestQAPSolutionAnalyzer.ResultsParameter.ActualName = "Results"; 271 299 BestQAPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name; 272 BestQAPSolutionAnalyzer.BestKnownSolution Parameter.ActualName = BestKnownSolutionParameter.Name;300 BestQAPSolutionAnalyzer.BestKnownSolutionsParameter.ActualName = BestKnownSolutionsParameter.Name; 273 301 BestQAPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name; 274 302 } … … 314 342 foreach (var op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) 315 343 op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 344 345 QAPExhaustiveSwap2LocalImprovement localOpt = Operators.OfType<QAPExhaustiveSwap2LocalImprovement>().SingleOrDefault(); 346 if (localOpt != null) { 347 localOpt.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 348 localOpt.DistancesParameter.ActualName = DistancesParameter.Name; 349 localOpt.MaximizationParameter.ActualName = MaximizationParameter.Name; 350 localOpt.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 351 localOpt.WeightsParameter.ActualName = WeightsParameter.Name; 352 } 316 353 } 317 354 … … 338 375 Description = "Imported problem data using QAPLIBParser " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().FirstOrDefault().Version + "."; 339 376 BestKnownQuality = null; 340 BestKnownSolution = null;377 BestKnownSolutions = null; 341 378 OnReset(); 342 379 } … … 371 408 if (solParser.Quality.IsAlmost(QAPEvaluator.Apply(new Permutation(PermutationTypes.Absolute, solParser.Assignment), Weights, Distances))) { 372 409 BestKnownQuality = new DoubleValue(solParser.Quality); 410 BestKnownSolutions = new ItemList<Permutation>(new Permutation[] { new Permutation(PermutationTypes.Absolute, solParser.Assignment) }); 373 411 BestKnownSolution = new Permutation(PermutationTypes.Absolute, solParser.Assignment); 374 412 } else { 375 413 BestKnownQuality = new DoubleValue(solParser.Quality); 414 BestKnownSolutions = null; 376 415 BestKnownSolution = null; 377 416 } 378 417 } else { 379 418 BestKnownQuality = new DoubleValue(solParser.Quality); 419 BestKnownSolutions = new ItemList<Permutation>(new Permutation[] { new Permutation(PermutationTypes.Absolute, solParser.Assignment) }); 380 420 BestKnownSolution = new Permutation(PermutationTypes.Absolute, solParser.Assignment); 381 421 } … … 383 423 } else { 384 424 BestKnownQuality = null; 425 BestKnownSolutions = null; 385 426 BestKnownSolution = null; 386 427 }
Note: See TracChangeset
for help on using the changeset viewer.