- Timestamp:
- 06/07/11 12:49:03 (13 years ago)
- Location:
- branches/GP.Grammar.Editor/HeuristicLab.Problems.QuadraticAssignment/3.3
- Files:
-
- 5 edited
- 6 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/GP.Grammar.Editor/HeuristicLab.Problems.QuadraticAssignment/3.3/Analyzers/BestQAPSolutionAnalyzer.cs
r5933 r6377 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 (there may be multiple) 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("BestKnownSolutions")) { 95 Parameters.Add(new LookupParameter<ItemList<Permutation>>("BestKnownSolutions", "The best known solutions of this QAP instance.")); 96 } 97 #endregion 84 98 } 85 99 … … 93 107 DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue; 94 108 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; 109 var sorted = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).ToArray(); 110 if (max) sorted = sorted.Reverse().ToArray(); 111 int i = sorted.First().index; 99 112 100 if (bestKnownQuality == null || 101 max && qualities[i].Value > bestKnownQuality.Value || 102 !max && qualities[i].Value < bestKnownQuality.Value) { 113 if (bestKnownQuality == null 114 || max && qualities[i].Value > bestKnownQuality.Value 115 || !max && qualities[i].Value < bestKnownQuality.Value) { 116 // if there isn't a best-known quality or we improved the best-known quality we'll add the current solution as best-known 103 117 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value); 104 118 BestKnownSolutionParameter.ActualValue = (Permutation)permutations[i].Clone(); 119 BestKnownSolutionsParameter.ActualValue = new ItemList<Permutation>(); 120 BestKnownSolutionsParameter.ActualValue.Add((Permutation)permutations[i].Clone()); 121 } else if (bestKnownQuality.Value == qualities[i].Value) { 122 // if we matched the best-known quality we'll try to set the best-known solution if it isn't null 123 // and try to add it to the pool of best solutions if it is different 124 if (BestKnownSolutionParameter.ActualValue == null) 125 BestKnownSolutionParameter.ActualValue = (Permutation)permutations[i].Clone(); 126 if (BestKnownSolutionsParameter.ActualValue == null) 127 BestKnownSolutionsParameter.ActualValue = new ItemList<Permutation>(); 128 PermutationEqualityComparer comparer = new PermutationEqualityComparer(); 129 foreach (var k in sorted) { // for each solution that we found check if it is in the pool of best-knowns 130 if (!max && k.Value > qualities[i].Value 131 || max && k.Value < qualities[i].Value) break; // stop when we reached a solution worse than the best-known quality 132 Permutation p = permutations[k.index]; 133 bool alreadyPresent = false; 134 foreach (Permutation p2 in BestKnownSolutionsParameter.ActualValue) { 135 if (comparer.Equals(p, p2)) { 136 alreadyPresent = true; 137 break; 138 } 139 } 140 if (!alreadyPresent) 141 BestKnownSolutionsParameter.ActualValue.Add((Permutation)permutations[k.index].Clone()); 142 } 105 143 } 106 144 -
branches/GP.Grammar.Editor/HeuristicLab.Problems.QuadraticAssignment/3.3/Evaluators/QAPEvaluator.cs
r5838 r6377 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/GP.Grammar.Editor/HeuristicLab.Problems.QuadraticAssignment/3.3/HeuristicLab.Problems.QuadraticAssignment-3.3.csproj
r5950 r6377 109 109 <ItemGroup> 110 110 <Compile Include="Analyzers\BestQAPSolutionAnalyzer.cs" /> 111 <Compile Include="Analyzers\QAPAlleleFrequencyAnalyzer.cs" /> 112 <Compile Include="Analyzers\QAPPopulationDiversityAnalyzer.cs" /> 111 113 <Compile Include="Evaluators\QAPSwap2MoveEvaluator.cs" /> 112 114 <Compile Include="Evaluators\QAPEvaluator.cs" /> … … 118 120 <Compile Include="Interfaces\IQAPEvaluator.cs" /> 119 121 <Compile Include="Interfaces\IQAPMoveEvaluator.cs" /> 122 <Compile Include="LocalImprovement\QAPExhaustiveSwap2LocalImprovement.cs" /> 120 123 <Compile Include="Parsers\QAPLIBSolutionParser.cs" /> 121 124 <Compile Include="Parsers\QAPLIBParser.cs" /> … … 386 389 </ItemGroup> 387 390 <ItemGroup> 391 <ProjectReference Include="..\..\HeuristicLab.Analysis\3.3\HeuristicLab.Analysis-3.3.csproj"> 392 <Project>{887425B4-4348-49ED-A457-B7D2C26DDBF9}</Project> 393 <Name>HeuristicLab.Analysis-3.3</Name> 394 </ProjectReference> 388 395 <ProjectReference Include="..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj"> 389 396 <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project> … … 432 439 </ItemGroup> 433 440 <ItemGroup> 434 <Content Include="Data\tai100a.sln" /> 435 <Content Include="Data\tai25a.sln" /> 436 <Content Include="Data\tai50a.sln" /> 441 <EmbeddedResource Include="Data\tai100a.sln" /> 442 <EmbeddedResource Include="Data\tai25a.sln" /> 443 <EmbeddedResource Include="Data\tai50a.sln" /> 444 </ItemGroup> 445 <ItemGroup> 446 <EmbeddedResource Include="Data\esc32a.sln" /> 447 </ItemGroup> 448 <ItemGroup> 449 <EmbeddedResource Include="Data\sko49.sln" /> 437 450 </ItemGroup> 438 451 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
branches/GP.Grammar.Editor/HeuristicLab.Problems.QuadraticAssignment/3.3/Plugin.cs.frame
r6099 r6377 25 25 [Plugin("HeuristicLab.Problems.QuadraticAssignment", "3.3.4.$WCREV$")] 26 26 [PluginFile("HeuristicLab.Problems.QuadraticAssignment-3.3.dll", PluginFileType.Assembly)] 27 [PluginDependency("HeuristicLab.Analysis", "3.3")] 27 28 [PluginDependency("HeuristicLab.Collections", "3.3")] 28 29 [PluginDependency("HeuristicLab.Common", "3.3")] -
branches/GP.Grammar.Editor/HeuristicLab.Problems.QuadraticAssignment/3.3/QuadraticAssignmentProblem.cs
r6088 r6377 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; } … … 88 95 get { return Operators.OfType<BestQAPSolutionAnalyzer>().FirstOrDefault(); } 89 96 } 97 98 private QAPAlleleFrequencyAnalyzer QAPAlleleFrequencyAnalyzer { 99 get { return Operators.OfType<QAPAlleleFrequencyAnalyzer>().FirstOrDefault(); } 100 } 101 102 private QAPPopulationDiversityAnalyzer QAPPopulationDiversityAnalyzer { 103 get { return Operators.OfType<QAPPopulationDiversityAnalyzer>().FirstOrDefault(); } 104 } 90 105 #endregion 91 106 … … 98 113 public QuadraticAssignmentProblem() 99 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)); 100 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)); 101 117 Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The strength of the connection between the facilities.", new DoubleMatrix(5, 5))); … … 130 146 public override IDeepCloneable Clone(Cloner cloner) { 131 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 if (Parameters.ContainsKey("DistanceMatrix")) { 167 DoubleMatrix bla = ((ValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]).Value; 168 Parameters.Remove("DistanceMatrix"); 169 Parameters.Add(new ValueParameter<DoubleMatrix>("Distances", "bla", bla)); 170 } 171 AttachEventHandlers(); 172 #endregion 132 173 } 133 174 … … 217 258 218 259 #region Helpers 219 [StorableHook(HookType.AfterDeserialization)]220 private void AfterDeserializationHook() {221 AttachEventHandlers();222 }223 224 260 private void AttachEventHandlers() { 225 261 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged); … … 238 274 Operators.AddRange(ApplicationManager.Manager.GetInstances<IQAPMoveEvaluator>()); 239 275 Operators.Add(new BestQAPSolutionAnalyzer()); 276 Operators.Add(new QAPAlleleFrequencyAnalyzer()); 277 Operators.Add(new QAPPopulationDiversityAnalyzer()); 278 Operators.Add(new QAPExhaustiveSwap2LocalImprovement()); 240 279 ParameterizeAnalyzers(); 241 280 ParameterizeOperators(); … … 262 301 BestQAPSolutionAnalyzer.ResultsParameter.ActualName = "Results"; 263 302 BestQAPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name; 264 BestQAPSolutionAnalyzer.BestKnownSolution Parameter.ActualName = BestKnownSolutionParameter.Name;303 BestQAPSolutionAnalyzer.BestKnownSolutionsParameter.ActualName = BestKnownSolutionsParameter.Name; 265 304 BestQAPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name; 305 } 306 if (QAPAlleleFrequencyAnalyzer != null) { 307 QAPAlleleFrequencyAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 308 QAPAlleleFrequencyAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name; 309 QAPAlleleFrequencyAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name; 310 QAPAlleleFrequencyAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name; 311 QAPAlleleFrequencyAnalyzer.ResultsParameter.ActualName = "Results"; 312 QAPAlleleFrequencyAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 313 QAPAlleleFrequencyAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name; 314 } 315 if (QAPPopulationDiversityAnalyzer != null) { 316 QAPPopulationDiversityAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name; 317 QAPPopulationDiversityAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 318 QAPPopulationDiversityAnalyzer.ResultsParameter.ActualName = "Results"; 319 QAPPopulationDiversityAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 266 320 } 267 321 } … … 291 345 foreach (var op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) 292 346 op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 347 348 QAPExhaustiveSwap2LocalImprovement localOpt = Operators.OfType<QAPExhaustiveSwap2LocalImprovement>().SingleOrDefault(); 349 if (localOpt != null) { 350 localOpt.AssignmentParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 351 localOpt.DistancesParameter.ActualName = DistancesParameter.Name; 352 localOpt.MaximizationParameter.ActualName = MaximizationParameter.Name; 353 localOpt.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 354 localOpt.WeightsParameter.ActualName = WeightsParameter.Name; 355 } 293 356 } 294 357 … … 315 378 Description = "Imported problem data using QAPLIBParser " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().FirstOrDefault().Version + "."; 316 379 BestKnownQuality = null; 317 BestKnownSolution = null;380 BestKnownSolutions = null; 318 381 OnReset(); 319 382 } … … 348 411 if (solParser.Quality.IsAlmost(QAPEvaluator.Apply(new Permutation(PermutationTypes.Absolute, solParser.Assignment), Weights, Distances))) { 349 412 BestKnownQuality = new DoubleValue(solParser.Quality); 413 BestKnownSolutions = new ItemList<Permutation>(new Permutation[] { new Permutation(PermutationTypes.Absolute, solParser.Assignment) }); 350 414 BestKnownSolution = new Permutation(PermutationTypes.Absolute, solParser.Assignment); 351 415 } else { 352 416 BestKnownQuality = new DoubleValue(solParser.Quality); 417 BestKnownSolutions = null; 353 418 BestKnownSolution = null; 354 419 } 355 420 } else { 356 421 BestKnownQuality = new DoubleValue(solParser.Quality); 422 BestKnownSolutions = new ItemList<Permutation>(new Permutation[] { new Permutation(PermutationTypes.Absolute, solParser.Assignment) }); 357 423 BestKnownSolution = new Permutation(PermutationTypes.Absolute, solParser.Assignment); 358 424 } … … 360 426 } else { 361 427 BestKnownQuality = null; 428 BestKnownSolutions = null; 362 429 BestKnownSolution = null; 363 430 }
Note: See TracChangeset
for help on using the changeset viewer.