Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

  • added first version of GRASP+PR algorithm
File size: 10.7 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.Random;
32
33namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms {
34  /// <summary>
35  /// A genetic algorithm.
36  /// </summary>
37  [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.")]
38  [Creatable("Algorithms")]
39  [StorableClass]
40  public sealed class GRASPWithPathRelinking : HeuristicOptimizationEngineAlgorithm {
41    #region Problem Properties
42    public override Type ProblemType {
43      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
44    }
45    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
46      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
47      set { base.Problem = value; }
48    }
49    #endregion
50
51    #region Parameter Properties
52    private IValueParameter<BoolValue> SetSeedRandomlyParameter {
53      get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
54    }
55    private IValueParameter<IntValue> SeedParameter {
56      get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
57    }
58    private IValueParameter<MultiAnalyzer> AnalyzerParameter {
59      get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
60    }
61    private IValueParameter<IntValue> EliteSetSizeParameter {
62      get { return (IValueParameter<IntValue>)Parameters["EliteSetSize"]; }
63    }
64    private IValueParameter<IntValue> LocalImprovementMaximumIterationsParameter {
65      get { return (IValueParameter<IntValue>)Parameters["LocalImprovementMaximumIterations"]; }
66    }
67    private ConstrainedValueParameter<ILocalImprovementOperator> LocalImprovementParameter {
68      get { return (ConstrainedValueParameter<ILocalImprovementOperator>)Parameters["LocalImprovement"]; }
69    }
70    public IFixedValueParameter<IntValue> MinimumEliteSetSizeParameter {
71      get { return (IFixedValueParameter<IntValue>)Parameters["MinimumEliteSetSize"]; }
72    }
73    public ConstrainedValueParameter<ICrossover> PathRelinkingParameter {
74      get { return (ConstrainedValueParameter<ICrossover>)Parameters["PathRelinking"]; }
75    }
76    public ConstrainedValueParameter<IReplacer> EliteSetReplacerParameter {
77      get { return (ConstrainedValueParameter<IReplacer>)Parameters["EliteSetReplacer"]; }
78    }
79    #endregion
80
81    #region Properties
82    public BoolValue SetSeedRandomly {
83      get { return SetSeedRandomlyParameter.Value; }
84      set { SetSeedRandomlyParameter.Value = value; }
85    }
86    public IntValue Seed {
87      get { return SeedParameter.Value; }
88      set { SeedParameter.Value = value; }
89    }
90    public MultiAnalyzer Analyzer {
91      get { return AnalyzerParameter.Value; }
92      set { AnalyzerParameter.Value = value; }
93    }
94    public IntValue EliteSetSize {
95      get { return EliteSetSizeParameter.Value; }
96      set { EliteSetSizeParameter.Value = value; }
97    }
98    public IntValue LocalImprovementMaximumIterations {
99      get { return LocalImprovementMaximumIterationsParameter.Value; }
100      set { LocalImprovementMaximumIterationsParameter.Value = value; }
101    }
102
103    private RandomCreator RandomCreator {
104      get { return (RandomCreator)OperatorGraph.InitialOperator; }
105    }
106    #endregion
107
108    [StorableConstructor]
109    private GRASPWithPathRelinking(bool deserializing) : base(deserializing) { }
110    private GRASPWithPathRelinking(GRASPWithPathRelinking original, Cloner cloner)
111      : base(original, cloner) {
112      // TODO: clone your private fields here
113      RegisterEventHandlers();
114    }
115    public GRASPWithPathRelinking()
116      : base() {
117      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
118      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator."));
119      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));
123      Parameters.Add(new FixedValueParameter<IntValue>("MinimumEliteSetSize", "(ρ) The minimum amount of elites for performing path relinking.", new IntValue(1)));
124      Parameters.Add(new ConstrainedValueParameter<ICrossover>("PathRelinking", "The operator that performs the path relinking."));
125      Parameters.Add(new ConstrainedValueParameter<IReplacer>("EliteSetReplacer", "The operator that replaces solutions in the elite set."));
126
127      RandomCreator randomCreator = new RandomCreator();
128      OperatorGraph.InitialOperator = randomCreator;
129
130      randomCreator.RandomParameter.ActualName = "Random";
131      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
132      randomCreator.SeedParameter.Value = null;
133      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
134      randomCreator.SetSeedRandomlyParameter.Value = null;
135
136      var mainLoop = new GRASPWithPathRelinkingMainLoop();
137      randomCreator.Successor = mainLoop;
138
139      InitializeOperators();
140      RegisterEventHandlers();
141    }
142
143    public override IDeepCloneable Clone(Cloner cloner) {
144      return new GRASPWithPathRelinking(this, cloner);
145    }
146
147    public override void Prepare() {
148      if (Problem != null) base.Prepare();
149    }
150
151    #region Events
152    protected override void OnProblemChanged() {
153      // TODO: Initialize and parameterize operators
154      InitializeOperators();
155      base.OnProblemChanged();
156    }
157
158    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
159      // TODO: Parameterize operators
160      base.Problem_SolutionCreatorChanged(sender, e);
161    }
162    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
163      // TODO: Parameterize operators
164      base.Problem_EvaluatorChanged(sender, e);
165    }
166    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
167      // TODO: Parameterize operators
168      InitializeOperators();
169      base.Problem_OperatorsChanged(sender, e);
170    }
171    #endregion
172
173    #region Helpers
174    [StorableHook(HookType.AfterDeserialization)]
175    private void AfterDeserialization() {
176      if (Problem != null) {
177      }
178    }
179
180    private void RegisterEventHandlers() {
181
182    }
183
184    private void InitializeOperators() {
185      Analyzer.Operators.Clear();
186      if (Problem != null) {
187        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
188          foreach (var param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
189            param.Depth = 1;
190          Analyzer.Operators.Add(analyzer);
191        }
192
193        string oldLocalImprovement = String.Empty;
194        if (LocalImprovementParameter.Value != null)
195          oldLocalImprovement = LocalImprovementParameter.Value.Name;
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        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        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      }
223
224      Parameterize();
225    }
226
227    private void Parameterize() {
228      foreach (var localImprovement in LocalImprovementParameter.ValidValues) {
229        localImprovement.MaximumIterationsParameter.ActualName = LocalImprovementMaximumIterationsParameter.Name;
230        localImprovement.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
231        localImprovement.ResultsParameter.ActualName = "Results";
232        if (Problem != null && localImprovement.ProblemType.IsAssignableFrom(Problem.GetType()))
233          localImprovement.Problem = Problem;
234      }
235    }
236    #endregion
237  }
238}
Note: See TracBrowser for help on using the repository browser.