Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASPWithPathRelinking.cs @ 7412

Last change on this file since 7412 was 7412, checked in by abeham, 13 years ago

#1614: worked on GQAP and GRASP+PR

File size: 13.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
33using HeuristicLab.Random;
34
35namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms {
36  /// <summary>
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.
38  /// </summary>
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.")]
40  [Creatable("Algorithms")]
41  [StorableClass]
42  public sealed class GRASPWithPathRelinking : HeuristicOptimizationEngineAlgorithm {
43    #region Problem Properties
44    public override Type ProblemType {
45      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
46    }
47    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
48      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
49      set { base.Problem = value; }
50    }
51    #endregion
52
53    #region Parameter Properties
54    private IValueParameter<BoolValue> SetSeedRandomlyParameter {
55      get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
56    }
57    private IValueParameter<IntValue> SeedParameter {
58      get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
59    }
60    private IValueParameter<MultiAnalyzer> AnalyzerParameter {
61      get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
62    }
63    public IValueParameter<IntValue> MaximumIterationsParameter {
64      get { return (IValueParameter<IntValue>)Parameters["MaximumIterations"]; }
65    }
66    private IFixedValueParameter<IntValue> EliteSetSizeParameter {
67      get { return (IFixedValueParameter<IntValue>)Parameters["EliteSetSize"]; }
68    }
69    private IFixedValueParameter<IntValue> LocalImprovementMaximumIterationsParameter {
70      get { return (IFixedValueParameter<IntValue>)Parameters["LocalImprovementMaximumIterations"]; }
71    }
72    private ConstrainedValueParameter<ILocalImprovementOperator> LocalImprovementParameter {
73      get { return (ConstrainedValueParameter<ILocalImprovementOperator>)Parameters["LocalImprovement"]; }
74    }
75    public IFixedValueParameter<IntValue> MinimumEliteSetSizeParameter {
76      get { return (IFixedValueParameter<IntValue>)Parameters["MinimumEliteSetSize"]; }
77    }
78    public ConstrainedValueParameter<ICrossover> PathRelinkingParameter {
79      get { return (ConstrainedValueParameter<ICrossover>)Parameters["PathRelinking"]; }
80    }
81    public ConstrainedValueParameter<IMerger> EliteSetMergerParameter {
82      get { return (ConstrainedValueParameter<IMerger>)Parameters["EliteSetMerger"]; }
83    }
84    #endregion
85
86    #region Properties
87    public BoolValue SetSeedRandomly {
88      get { return SetSeedRandomlyParameter.Value; }
89      set { SetSeedRandomlyParameter.Value = value; }
90    }
91    public IntValue Seed {
92      get { return SeedParameter.Value; }
93      set { SeedParameter.Value = value; }
94    }
95    public MultiAnalyzer Analyzer {
96      get { return AnalyzerParameter.Value; }
97      set { AnalyzerParameter.Value = value; }
98    }
99    public IntValue EliteSetSize {
100      get { return EliteSetSizeParameter.Value; }
101      set { EliteSetSizeParameter.Value.Value = value.Value; }
102    }
103    public IntValue LocalImprovementMaximumIterations {
104      get { return LocalImprovementMaximumIterationsParameter.Value; }
105      set { LocalImprovementMaximumIterationsParameter.Value.Value = value.Value; }
106    }
107
108    private RandomCreator RandomCreator {
109      get { return (RandomCreator)OperatorGraph.InitialOperator; }
110    }
111    private GRASPWithPathRelinkingMainLoop MainLoop {
112      get { return RandomCreator.Successor as GRASPWithPathRelinkingMainLoop; }
113    }
114    #endregion
115
116    [Storable]
117    private BestAverageWorstQualityAnalyzer analyzer;
118
119    [StorableConstructor]
120    private GRASPWithPathRelinking(bool deserializing) : base(deserializing) { }
121    private GRASPWithPathRelinking(GRASPWithPathRelinking original, Cloner cloner)
122      : base(original, cloner) {
123      analyzer = cloner.Clone(original.analyzer);
124      RegisterEventHandlers();
125    }
126    public GRASPWithPathRelinking()
127      : base() {
128      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
129      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator."));
130      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each iteration.", new MultiAnalyzer()));
131      Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of iterations that the algorithm should run.", new IntValue(1000)));
132      Parameters.Add(new FixedValueParameter<IntValue>("EliteSetSize", "The elite set stores the best found solutions.", new IntValue(10)));
133      Parameters.Add(new FixedValueParameter<IntValue>("LocalImprovementMaximumIterations", "The maximum number of iterations performed by the local improvement operator.", new IntValue(1000)));
134      Parameters.Add(new ConstrainedValueParameter<ILocalImprovementOperator>("LocalImprovement", "Performs a local search on the solution."));
135      Parameters.Add(new FixedValueParameter<IntValue>("MinimumEliteSetSize", "(ρ) The minimum amount of elites for performing path relinking.", new IntValue(2)));
136      Parameters.Add(new ConstrainedValueParameter<ICrossover>("PathRelinking", "The operator that performs the path relinking."));
137      Parameters.Add(new ConstrainedValueParameter<IMerger>("EliteSetMerger", "The operator that merges new solutions into the elite set."));
138
139      analyzer = new BestAverageWorstQualityAnalyzer();
140      Analyzer.Operators.Add(analyzer);
141
142      RandomCreator randomCreator = new RandomCreator();
143      OperatorGraph.InitialOperator = randomCreator;
144
145      randomCreator.RandomParameter.ActualName = "Random";
146      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
147      randomCreator.SeedParameter.Value = null;
148      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
149      randomCreator.SetSeedRandomlyParameter.Value = null;
150
151      var mainLoop = new GRASPWithPathRelinkingMainLoop();
152      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
153      mainLoop.EliteSetMergerParameter.ActualName = EliteSetMergerParameter.Name;
154      mainLoop.EliteSetSizeParameter.ActualName = EliteSetSizeParameter.Name;
155      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
156      mainLoop.LocalImprovementParameter.ActualName = LocalImprovementParameter.Name;
157      mainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
158      mainLoop.PathRelinkingParameter.ActualName = PathRelinkingParameter.Name;
159      mainLoop.ResultsParameter.ActualName = "Results";
160      randomCreator.Successor = mainLoop;
161
162      InitializeOperators();
163      RegisterEventHandlers();
164    }
165
166    public override IDeepCloneable Clone(Cloner cloner) {
167      return new GRASPWithPathRelinking(this, cloner);
168    }
169
170    public override void Prepare() {
171      if (Problem != null) base.Prepare();
172    }
173
174    #region Events
175    protected override void OnProblemChanged() {
176      InitializeOperators();
177      base.OnProblemChanged();
178    }
179
180    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
181      Parameterize();
182      base.Problem_SolutionCreatorChanged(sender, e);
183    }
184    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
185      Parameterize();
186      base.Problem_EvaluatorChanged(sender, e);
187    }
188    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
189      InitializeOperators();
190      base.Problem_OperatorsChanged(sender, e);
191    }
192    #endregion
193
194    #region Helpers
195    [StorableHook(HookType.AfterDeserialization)]
196    private void AfterDeserialization() {
197      if (Problem != null) {
198      }
199      RegisterEventHandlers();
200    }
201
202    private void RegisterEventHandlers() {
203
204    }
205
206    private void InitializeOperators() {
207      Analyzer.Operators.Clear();
208      if (Problem != null) {
209        foreach (IAnalyzer a in Problem.Operators.OfType<IAnalyzer>()) {
210          foreach (var param in a.Parameters.OfType<IScopeTreeLookupParameter>())
211            param.Depth = 1;
212          Analyzer.Operators.Add(a);
213        }
214        InitializeFromInstallation(LocalImprovementParameter, x => x.ProblemType.IsAssignableFrom(Problem.GetType()));
215        InitializeFromProblem(PathRelinkingParameter);
216        InitializeFromProblem(EliteSetMergerParameter);
217      } else {
218        LocalImprovementParameter.ValidValues.Clear();
219        PathRelinkingParameter.ValidValues.Clear();
220        EliteSetMergerParameter.ValidValues.Clear();
221      }
222      Analyzer.Operators.Add(analyzer);
223
224      Parameterize();
225    }
226
227    private void Parameterize() {
228      if (Problem != null) {
229        MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
230        MainLoop.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
231
232        analyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
233      }
234      foreach (var localImprovement in LocalImprovementParameter.ValidValues) {
235        localImprovement.MaximumIterationsParameter.ActualName = LocalImprovementMaximumIterationsParameter.Name;
236        localImprovement.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
237        localImprovement.ResultsParameter.ActualName = "Results";
238        if (Problem != null && localImprovement.ProblemType.IsAssignableFrom(Problem.GetType()))
239          localImprovement.Problem = Problem;
240      }
241      foreach (var merger in EliteSetMergerParameter.ValidValues) {
242        merger.PopulationSizeParameter.ActualName = EliteSetSizeParameter.Name;
243      }
244    }
245
246    private void InitializeFromProblem<T>(ConstrainedValueParameter<T> parameter) where T : class, INamedItem {
247      InitializeFromProblem<T>(parameter, x => true);
248    }
249    private void InitializeFromProblem<T>(ConstrainedValueParameter<T> parameter, Func<T, bool> condition) where T : class, INamedItem {
250      if (parameter == null) throw new ArgumentNullException("parameter");
251      if (condition == null) throw new ArgumentNullException("condition");
252      string oldName = String.Empty;
253      if (parameter.Value != null) oldName = parameter.Value.Name;
254      parameter.ValidValues.Clear();
255      foreach (var item in Problem.Operators.OfType<T>().Where(condition)) {
256        parameter.ValidValues.Add(item);
257      }
258      var newItem = parameter.ValidValues.FirstOrDefault(x => x.Name == oldName);
259      if (newItem != null) parameter.Value = newItem;
260    }
261    private void InitializeFromInstallation<T>(ConstrainedValueParameter<T> parameter) where T : class, INamedItem {
262      InitializeFromInstallation<T>(parameter, x => true);
263    }
264    private void InitializeFromInstallation<T>(ConstrainedValueParameter<T> parameter, Func<T, bool> condition) where T : class, INamedItem {
265      if (parameter == null) throw new ArgumentNullException("parameter");
266      if (condition == null) throw new ArgumentNullException("condition");
267      string oldName = String.Empty;
268      if (parameter.Value != null) oldName = parameter.Value.Name;
269      parameter.ValidValues.Clear();
270      foreach (var item in ApplicationManager.Manager.GetInstances<T>().Where(condition)) {
271        parameter.ValidValues.Add(item);
272      }
273      var newItem = parameter.ValidValues.FirstOrDefault(x => x.Name == oldName);
274      if (newItem != null) parameter.Value = newItem;
275    }
276    #endregion
277  }
278}
Note: See TracBrowser for help on using the repository browser.