Changeset 15572 for branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASP/GRASP.cs
- Timestamp:
- 01/03/18 00:28:51 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASP/GRASP.cs
r15564 r15572 36 36 /// This is an implementation of the algorithm described in Mateus, G.R., Resende, M.G.C. & Silva, R.M.A. J Heuristics (2011) 17: 527. https://doi.org/10.1007/s10732-010-9144-0 37 37 /// </summary> 38 /// <remarks> 39 /// A fine distinction to the described algorithm is that newly found best solutions are always accepted into the elite set. This is reasonable, but not mentioned in the paper by Mateus et al. 40 /// </remarks> 38 41 [Item("GRASP+PR (GQAP)", "The algorithm implements the Greedy Randomized Adaptive Search Procedure (GRASP) with Path Relinking as described in Mateus, G., Resende, M., and Silva, R. 2011. GRASP with path-relinking for the generalized quadratic assignment problem. Journal of Heuristics 17, Springer Netherlands, pp. 527-565.")] 39 42 [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms)] 40 43 [StorableClass] 41 public sealed class GRASP : StochasticAlgorithm<GRASPContext > {44 public sealed class GRASP : StochasticAlgorithm<GRASPContext, IntegerVectorEncoding> { 42 45 43 46 public override bool SupportsPause { … … 175 178 GQAPSolution pi_prime; 176 179 if (!Problem.ProblemInstance.IsFeasible(pi_prime_vec)) // line 5 in Algorithm 1 177 pi_prime = Context.AtPopulation(Context.Random.Next(Context.PopulationCount)).Solution; // line 6 in Algorithm 1180 pi_prime = (GQAPSolution)Context.AtPopulation(Context.Random.Next(Context.PopulationCount)).Solution.Clone(); // line 6 in Algorithm 1 178 181 else { 179 182 // This is necessary, because pi_prime has not been evaluated yet and such details are not covered in Algorithm 1 … … 188 191 var fitness = Problem.ProblemInstance.ToSingleObjective(pi_prime.Evaluation); 189 192 // Book-keeping 190 if (Context.BestQuality > fitness) { 193 var newBest = Context.BestQuality > fitness; 194 if (newBest) { 191 195 Context.BestQuality = fitness; 192 196 Context.BestSolution = (GQAPSolution)pi_prime.Clone(); … … 194 198 195 199 if (Context.PopulationCount == EliteSetSize) { // line 12 in Algorithm 1 196 var fit = Problem.ProblemInstance.ToSingleObjective(pi_prime.Evaluation);197 200 double[] similarities = Context.Population.Select(x => HammingSimilarityCalculator.CalculateSimilarity(x.Solution.Assignment, pi_prime.Assignment)).ToArray(); 198 if (similarities.Max() <= 1.0 - (MinimumDifference / (double)pi_prime.Assignment.Length)) { // cond. 2 of line 13 in Algorithm 1 199 var replacement = Context.Population.Select((v, i) => new { V = v, Index = i }) 200 .Where(x => x.V.Fitness >= fit).ToArray(); 201 if (replacement.Length > 0) { // cond. 1 of line 13 in Algorithm 1 202 // next two lines: line 14 in Algorithm 1 203 replacement = replacement.OrderBy(x => similarities[x.Index]).ToArray(); 204 Context.ReplaceAtPopulation(replacement.Last().Index, Context.ToScope(pi_prime, fit)); 201 // NOTE: In the original paper it is not described that new best solutions are accepted regardless of difference 202 // in this implementation a new best solution is always accepted into the elite set 203 if (newBest || similarities.Max() <= 1.0 - (MinimumDifference / (double)pi_prime.Assignment.Length)) { // cond. 2 of line 13 in Algorithm 1 204 var replacement = Context.Population.Select((v, i) => new { Scope = v, Index = i }) 205 .Where(x => x.Scope.Fitness >= fitness) // cond. 1 of line 13 in Algorithm 1 206 .OrderByDescending(x => similarities[x.Index]) // line 14 in Algorithm 1 207 .FirstOrDefault(); 208 if (replacement != null) { 209 Context.ReplaceAtPopulation(replacement.Index, Context.ToScope(pi_prime, fitness)); // line 14 in Algorithm 1 205 210 } 206 211 } 207 } else if (IsSufficientlyDifferent(pi_prime.Assignment)) { // line 17 in Algorithm 1 212 // NOTE: In the original paper it is not described that new best solutions are accepted regardless of difference 213 // in this implementation a new best solution is always accepted into the elite set 214 } else if (newBest || IsSufficientlyDifferent(pi_prime.Assignment)) { // line 17 in Algorithm 1 208 215 Context.AddToPopulation(Context.ToScope(pi_prime, Problem.ProblemInstance.ToSingleObjective(pi_prime.Evaluation))); // line 18 in Algorithm 1 209 216 }
Note: See TracChangeset
for help on using the changeset viewer.