Changeset 15562 for branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators
- Timestamp:
- 12/29/17 23:56:43 (7 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/LocalImprovers/OneOptLocalSearch.cs
r15558 r15562 1 #region License Information 1 2 #region License Information 2 3 /* HeuristicLab 3 4 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL) … … 21 22 22 23 using System; 23 using System.Collections.Generic;24 24 using System.Linq; 25 25 using HeuristicLab.Common; … … 34 34 35 35 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { 36 /// <summary> 37 /// 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 38 /// </summary> 39 [Item("ApproximateLocalSearch", "The approximate local search is 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.")] 36 [Item("1-opt LocalSearch", "A simple exhaustive 1-change local search.")] 40 37 [StorableClass] 41 public class ApproximateLocalSearch : SingleSuccessorOperator, IProblemInstanceAwareGQAPOperator,38 public class OneOptLocalSearch : SingleSuccessorOperator, IProblemInstanceAwareGQAPOperator, 42 39 IQualityAwareGQAPOperator, IGQAPLocalImprovementOperator, IAssignmentAwareGQAPOperator, IStochasticOperator { 43 40 public IProblem Problem { get; set; } … … 67 64 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 68 65 } 69 public IValueLookupParameter<IntValue> MaximumCandidateListSizeParameter {70 get { return (IValueLookupParameter<IntValue>)Parameters["MaximumCandidateListSize"]; }71 }72 public IValueLookupParameter<PercentValue> OneMoveProbabilityParameter {73 get { return (IValueLookupParameter<PercentValue>)Parameters["OneMoveProbability"]; }74 }75 66 public ILookupParameter<ResultCollection> ResultsParameter { 76 67 get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; } 77 68 } 78 public IValueLookupParameter<BoolValue> GreedyParameter {79 get { return (IValueLookupParameter<BoolValue>)Parameters["Greedy"]; }80 }81 69 82 70 [StorableConstructor] 83 protected ApproximateLocalSearch(bool deserializing) : base(deserializing) { }84 protected ApproximateLocalSearch(ApproximateLocalSearch original, Cloner cloner) : base(original, cloner) { }85 public ApproximateLocalSearch()71 protected OneOptLocalSearch(bool deserializing) : base(deserializing) { } 72 protected OneOptLocalSearch(OneOptLocalSearch original, Cloner cloner) : base(original, cloner) { } 73 public OneOptLocalSearch() 86 74 : base() { 87 75 Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription)); … … 89 77 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "")); 90 78 Parameters.Add(new LookupParameter<Evaluation>("Evaluation", GQAP.EvaluationDescription)); 91 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "Th e maximum number of iterations that should be performed."));79 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "This parameter is ignored by the operator.") { Hidden = true }); 92 80 Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solution equivalents.")); 93 81 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 94 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumCandidateListSize", "The maximum number of candidates that should be found in each step.", new IntValue(10)));95 Parameters.Add(new ValueLookupParameter<PercentValue>("OneMoveProbability", "The probability for performing a 1-move, which is the opposite of performing a 2-move.", new PercentValue(.5)));96 82 Parameters.Add(new LookupParameter<ResultCollection>("Results", "The result collection that stores the results.")); 97 Parameters.Add(new ValueLookupParameter<BoolValue>("Greedy", "Whether to use a greedy selection strategy or a probabilistic one.", new BoolValue(true)));98 83 } 99 84 100 85 public override IDeepCloneable Clone(Cloner cloner) { 101 return new ApproximateLocalSearch(this, cloner);86 return new OneOptLocalSearch(this, cloner); 102 87 } 103 88 104 public static void Apply(IRandom random, GQAPSolution sol, int maxCLS, 105 double oneMoveProbability, int maximumIterations, 106 GQAPInstance problemInstance, out int evaluatedSolutions, bool greedy = true) { 89 public static void Apply(IRandom random, GQAPSolution sol, 90 GQAPInstance problemInstance, out int evaluatedSolutions) { 107 91 var fit = problemInstance.ToSingleObjective(sol.Evaluation); 108 92 var eval = sol.Evaluation; 109 Apply(random, sol.Assignment, ref fit, ref eval, maxCLS, oneMoveProbability, maximumIterations, problemInstance, 110 out evaluatedSolutions, greedy); 93 Apply(random, sol.Assignment, ref fit, ref eval, problemInstance, out evaluatedSolutions); 111 94 sol.Evaluation = eval; 112 95 } … … 125 108 /// <param name="greedy">Greedy selection performed better in 5 of 8 instances according to the paper</param> 126 109 public static void Apply(IRandom random, IntegerVector assignment, 127 ref double quality, ref Evaluation evaluation, int maxCLS, 128 double oneMoveProbability, int maximumIterations, 129 GQAPInstance problemInstance, out int evaluatedSolutions, bool greedy = true) { 110 ref double quality, ref Evaluation evaluation, 111 GQAPInstance problemInstance, out int evaluatedSolutions) { 130 112 evaluatedSolutions = 0; 131 113 var capacities = problemInstance.Capacities; … … 133 115 var evaluations = 0.0; 134 116 var deltaEvaluationFactor = 1.0 / assignment.Length; 135 while (true) { // line 1 of Algorithm 3 136 var count = 0; // line 2 of Algorithm 3 137 var CLS = new List<Tuple<NMove, double, Evaluation>>(); // line 3 of Algorithm 3 138 do { 139 var move = Move(random, assignment, oneMoveProbability, capacities); // line 4 of Algorithm 3 140 117 var change = true; 118 var moves = ExhaustiveOneMoveGenerator.GenerateAllNxM(problemInstance).ToList(); 119 var slack = evaluation.Slack.ToList(); 120 while (change) { 121 change = false; 122 var feasible = evaluation.IsFeasible; 123 foreach (var move in moves 124 .Where(x => { 125 var equip = x.Indices[0]; 126 var assignTo = x.Reassignments[equip] - 1; 127 if (assignTo == assignment[equip]) return false; 128 var dem = problemInstance.Demands[equip]; 129 if (feasible) return slack[assignTo] >= dem; 130 return slack[assignTo] - dem > slack[assignment[equip]]; 131 }) 132 .Shuffle(random)) { 141 133 var moveEval = GQAPNMoveEvaluator.Evaluate(move, assignment, evaluation, problemInstance); 142 evaluations += move.Indices.Count * deltaEvaluationFactor; 143 double moveQuality = problemInstance.ToSingleObjective(moveEval); 144 145 if (moveEval.ExcessDemand <= 0.0 && moveQuality < quality) { // line 5 of Algorithm 3 146 CLS.Add(Tuple.Create(move, moveQuality, moveEval)); // line 6 of Algorithm 3 134 evaluations += deltaEvaluationFactor; 135 var moveQuality = problemInstance.ToSingleObjective(moveEval); 136 if (moveQuality < quality) { 137 NMoveMaker.Apply(assignment, move); 138 quality = moveQuality; 139 evaluation = moveEval; 140 change = true; 141 break; 147 142 } 148 count++; // line 8 of Algorithm 3149 } while (CLS.Count < maxCLS && count < maximumIterations); // line 9 of Algorithm 3150 151 if (CLS.Count == 0) { // line 10 of Algorithm 3152 evaluatedSolutions += (int)Math.Ceiling(evaluations);153 return; // END154 } else {155 // line 11 of Algorithm 3156 Tuple<NMove, double, Evaluation> selected;157 if (greedy) {158 selected = CLS.MinItems(x => x.Item2).Shuffle(random).First();159 } else {160 selected = CLS.SampleProportional(random, 1, CLS.Select(x => 1.0 / x.Item2), false, false).Single();161 }162 NMoveMaker.Apply(assignment, selected.Item1);163 quality = selected.Item2;164 evaluation = selected.Item3;165 143 } 166 144 } 145 evaluatedSolutions += (int)Math.Ceiling(evaluations); 167 146 } 168 147 … … 183 162 ref fit, 184 163 ref evaluation, 185 MaximumCandidateListSizeParameter.ActualValue.Value,186 OneMoveProbabilityParameter.ActualValue.Value,187 MaximumIterationsParameter.ActualValue.Value,188 164 ProblemInstanceParameter.ActualValue, 189 out evaluatedSolutions, 190 GreedyParameter.ActualValue.Value); 165 out evaluatedSolutions); 191 166 192 167 EvaluationParameter.ActualValue = evaluation;
Note: See TracChangeset
for help on using the changeset viewer.