Changeset 7363 for branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASPWithPathRelinking.cs
- Timestamp:
- 01/18/12 16:55:08 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASPWithPathRelinking.cs
r7345 r7363 29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.PluginInfrastructure; 32 using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common; 31 33 using HeuristicLab.Random; 32 34 33 35 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms { 34 36 /// <summary> 35 /// A genetic algorithm.37 /// The algorithm combines the Greedy Randomized Adaptive Search Procedure (GRASP) with Path Relinking and 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 38 /// </summary> 37 39 [Item("GRASP+PR", "The algorithm combines the Greedy Randomized Adaptive Search Procedure (GRASP) with Path Relinking and 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.")] … … 59 61 get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; } 60 62 } 61 private I ValueParameter<IntValue> EliteSetSizeParameter {62 get { return (I ValueParameter<IntValue>)Parameters["EliteSetSize"]; }63 } 64 private I ValueParameter<IntValue> LocalImprovementMaximumIterationsParameter {65 get { return (I ValueParameter<IntValue>)Parameters["LocalImprovementMaximumIterations"]; }63 private IFixedValueParameter<IntValue> EliteSetSizeParameter { 64 get { return (IFixedValueParameter<IntValue>)Parameters["EliteSetSize"]; } 65 } 66 private IFixedValueParameter<IntValue> LocalImprovementMaximumIterationsParameter { 67 get { return (IFixedValueParameter<IntValue>)Parameters["LocalImprovementMaximumIterations"]; } 66 68 } 67 69 private ConstrainedValueParameter<ILocalImprovementOperator> LocalImprovementParameter { … … 74 76 get { return (ConstrainedValueParameter<ICrossover>)Parameters["PathRelinking"]; } 75 77 } 76 public ConstrainedValueParameter<I Replacer> EliteSetReplacerParameter {77 get { return (ConstrainedValueParameter<I Replacer>)Parameters["EliteSetReplacer"]; }78 public ConstrainedValueParameter<IMerger> EliteSetReplacerParameter { 79 get { return (ConstrainedValueParameter<IMerger>)Parameters["EliteSetMerger"]; } 78 80 } 79 81 #endregion … … 94 96 public IntValue EliteSetSize { 95 97 get { return EliteSetSizeParameter.Value; } 96 set { EliteSetSizeParameter.Value = value; }98 set { EliteSetSizeParameter.Value.Value = value.Value; } 97 99 } 98 100 public IntValue LocalImprovementMaximumIterations { 99 101 get { return LocalImprovementMaximumIterationsParameter.Value; } 100 set { LocalImprovementMaximumIterationsParameter.Value = value; }102 set { LocalImprovementMaximumIterationsParameter.Value.Value = value.Value; } 101 103 } 102 104 … … 110 112 private GRASPWithPathRelinking(GRASPWithPathRelinking original, Cloner cloner) 111 113 : base(original, cloner) { 112 // TODO: clone your private fields here113 114 RegisterEventHandlers(); 114 115 } … … 118 119 Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.")); 119 120 Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each iteration.", new MultiAnalyzer())); 120 Parameters.Add(new ValueParameter<IntValue>("EliteSetSize", "The elite set stores the best found solutions.", new IntValue(10)));121 Parameters.Add(new ValueParameter<IntValue>("LocalImprovementMaximumIterations", "The maximum number of iterations performed by the local improvement operator.", new IntValue(100)));122 Parameters.Add(new ConstrainedValueParameter<ILocalImprovementOperator>("LocalImprovement", "Performs a local search on the solution." , null));121 Parameters.Add(new FixedValueParameter<IntValue>("EliteSetSize", "The elite set stores the best found solutions.", new IntValue(10))); 122 Parameters.Add(new FixedValueParameter<IntValue>("LocalImprovementMaximumIterations", "The maximum number of iterations performed by the local improvement operator.", new IntValue(100))); 123 Parameters.Add(new ConstrainedValueParameter<ILocalImprovementOperator>("LocalImprovement", "Performs a local search on the solution.")); 123 124 Parameters.Add(new FixedValueParameter<IntValue>("MinimumEliteSetSize", "(ρ) The minimum amount of elites for performing path relinking.", new IntValue(1))); 124 125 Parameters.Add(new ConstrainedValueParameter<ICrossover>("PathRelinking", "The operator that performs the path relinking.")); 125 Parameters.Add(new ConstrainedValueParameter<I Replacer>("EliteSetReplacer", "The operator that replaces solutions inthe elite set."));126 Parameters.Add(new ConstrainedValueParameter<IMerger>("EliteSetMerger", "The operator that merges new solutions into the elite set.")); 126 127 127 128 RandomCreator randomCreator = new RandomCreator(); … … 151 152 #region Events 152 153 protected override void OnProblemChanged() { 153 // TODO: Initialize and parameterize operators154 154 InitializeOperators(); 155 155 base.OnProblemChanged(); … … 157 157 158 158 protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) { 159 // TODO: Parameterize operators159 Parameterize(); 160 160 base.Problem_SolutionCreatorChanged(sender, e); 161 161 } 162 162 protected override void Problem_EvaluatorChanged(object sender, EventArgs e) { 163 // TODO: Parameterize operators163 Parameterize(); 164 164 base.Problem_EvaluatorChanged(sender, e); 165 165 } 166 166 protected override void Problem_OperatorsChanged(object sender, EventArgs e) { 167 // TODO: Parameterize operators168 167 InitializeOperators(); 169 168 base.Problem_OperatorsChanged(sender, e); … … 176 175 if (Problem != null) { 177 176 } 177 RegisterEventHandlers(); 178 178 } 179 179 … … 190 190 Analyzer.Operators.Add(analyzer); 191 191 } 192 193 string oldLocalImprovement = String.Empty;194 if (LocalImprovementParameter.Value != null)195 oldLocalImprovement = LocalImprovementParameter.Value.Name;192 InitializeFromInstallation(LocalImprovementParameter, x => x.ProblemType.IsAssignableFrom(Problem.GetType())); 193 InitializeFromProblem(PathRelinkingParameter); 194 InitializeFromProblem(EliteSetReplacerParameter); 195 } else { 196 196 LocalImprovementParameter.ValidValues.Clear(); 197 foreach (ILocalImprovementOperator localImprovement in Problem.Operators.OfType<ILocalImprovementOperator>()) {198 LocalImprovementParameter.ValidValues.Add(localImprovement);199 }200 var newLocalImprovement = LocalImprovementParameter.ValidValues.FirstOrDefault(x => x.Name == oldLocalImprovement);201 if (newLocalImprovement != null) LocalImprovementParameter.Value = newLocalImprovement;202 203 string oldPathRelinking = String.Empty;204 if (PathRelinkingParameter.Value != null)205 oldPathRelinking = PathRelinkingParameter.Value.Name;206 197 PathRelinkingParameter.ValidValues.Clear(); 207 foreach (ICrossover pathRelinking in Problem.Operators.OfType<ICrossover>()) {208 PathRelinkingParameter.ValidValues.Add(pathRelinking);209 }210 var newPathRelinking = PathRelinkingParameter.ValidValues.FirstOrDefault(x => x.Name == oldPathRelinking);211 if (newPathRelinking != null) PathRelinkingParameter.Value = newPathRelinking;212 213 string oldReplacer = String.Empty;214 if (EliteSetReplacerParameter.Value != null)215 oldReplacer = EliteSetReplacerParameter.Value.Name;216 198 EliteSetReplacerParameter.ValidValues.Clear(); 217 foreach (IReplacer replacer in Problem.Operators.OfType<IReplacer>()) {218 EliteSetReplacerParameter.ValidValues.Add(replacer);219 }220 var newReplacer = EliteSetReplacerParameter.ValidValues.FirstOrDefault(x => x.Name == oldReplacer);221 if (newReplacer != null) EliteSetReplacerParameter.Value = newReplacer;222 199 } 223 200 … … 234 211 } 235 212 } 213 214 private void InitializeFromProblem<T>(ConstrainedValueParameter<T> parameter) where T : class, INamedItem { 215 InitializeFromProblem(parameter, x => true); 216 } 217 private void InitializeFromProblem<T>(ConstrainedValueParameter<T> parameter, Func<T, bool> condition) where T : class, INamedItem { 218 if (parameter == null) throw new ArgumentNullException("parameter"); 219 if (condition == null) throw new ArgumentNullException("condition"); 220 string oldName = String.Empty; 221 if (parameter.Value != null) oldName = parameter.Value.Name; 222 parameter.ValidValues.Clear(); 223 foreach (var item in Problem.Operators.OfType<T>().Where(condition)) { 224 parameter.ValidValues.Add(item); 225 } 226 var newItem = parameter.ValidValues.FirstOrDefault(x => x.Name == oldName); 227 if (newItem != null) parameter.Value = newItem; 228 } 229 private void InitializeFromInstallation<T>(ConstrainedValueParameter<T> parameter) where T : class, INamedItem { 230 InitializeFromInstallation(parameter, x => true); 231 } 232 private void InitializeFromInstallation<T>(ConstrainedValueParameter<T> parameter, Func<T, bool> condition) where T : class, INamedItem { 233 if (parameter == null) throw new ArgumentNullException("parameter"); 234 if (condition == null) throw new ArgumentNullException("condition"); 235 string oldName = String.Empty; 236 if (parameter.Value != null) oldName = parameter.Value.Name; 237 parameter.ValidValues.Clear(); 238 foreach (var item in ApplicationManager.Manager.GetInstances<T>().Where(condition)) { 239 parameter.ValidValues.Add(item); 240 } 241 var newItem = parameter.ValidValues.FirstOrDefault(x => x.Name == oldName); 242 if (newItem != null) parameter.Value = newItem; 243 } 236 244 #endregion 237 245 }
Note: See TracChangeset
for help on using the changeset viewer.