1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2017 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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Optimization.Operators;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 | using HeuristicLab.Random;
|
---|
35 | using HEAL.Attic;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Algorithms.GRASP {
|
---|
38 | /// <summary>
|
---|
39 | /// 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 | /// </summary>
|
---|
41 | [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.")]
|
---|
42 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms)]
|
---|
43 | [StorableType("AA509A09-10A7-48B9-AEF1-50997F01A0B0")]
|
---|
44 | public sealed class GRASPWithPathRelinking : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
45 | public string Filename { get; set; }
|
---|
46 |
|
---|
47 | #region Problem Properties
|
---|
48 | public override Type ProblemType {
|
---|
49 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
50 | }
|
---|
51 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
52 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
53 | set { base.Problem = value; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | #region Parameter Properties
|
---|
58 | private IValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
59 | get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
60 | }
|
---|
61 | private IValueParameter<IntValue> SeedParameter {
|
---|
62 | get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
63 | }
|
---|
64 | private IValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
65 | get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
66 | }
|
---|
67 | public IValueParameter<IntValue> MaximumIterationsParameter {
|
---|
68 | get { return (IValueParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
69 | }
|
---|
70 | private IFixedValueParameter<IntValue> EliteSetSizeParameter {
|
---|
71 | get { return (IFixedValueParameter<IntValue>)Parameters["EliteSetSize"]; }
|
---|
72 | }
|
---|
73 | private IFixedValueParameter<IntValue> LocalImprovementMaximumIterationsParameter {
|
---|
74 | get { return (IFixedValueParameter<IntValue>)Parameters["LocalImprovementMaximumIterations"]; }
|
---|
75 | }
|
---|
76 | private ConstrainedValueParameter<ILocalImprovementOperator> LocalImprovementParameter {
|
---|
77 | get { return (ConstrainedValueParameter<ILocalImprovementOperator>)Parameters["LocalImprovement"]; }
|
---|
78 | }
|
---|
79 | public IFixedValueParameter<IntValue> MinimumEliteSetSizeParameter {
|
---|
80 | get { return (IFixedValueParameter<IntValue>)Parameters["MinimumEliteSetSize"]; }
|
---|
81 | }
|
---|
82 | public ConstrainedValueParameter<ICrossover> PathRelinkingParameter {
|
---|
83 | get { return (ConstrainedValueParameter<ICrossover>)Parameters["PathRelinking"]; }
|
---|
84 | }
|
---|
85 | public ConstrainedValueParameter<IPopulationReducer> EliteSetReducerParameter {
|
---|
86 | get { return (ConstrainedValueParameter<IPopulationReducer>)Parameters["EliteSetReducer"]; }
|
---|
87 | }
|
---|
88 | #endregion
|
---|
89 |
|
---|
90 | #region Properties
|
---|
91 | public BoolValue SetSeedRandomly {
|
---|
92 | get { return SetSeedRandomlyParameter.Value; }
|
---|
93 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
94 | }
|
---|
95 | public IntValue Seed {
|
---|
96 | get { return SeedParameter.Value; }
|
---|
97 | set { SeedParameter.Value = value; }
|
---|
98 | }
|
---|
99 | public MultiAnalyzer Analyzer {
|
---|
100 | get { return AnalyzerParameter.Value; }
|
---|
101 | set { AnalyzerParameter.Value = value; }
|
---|
102 | }
|
---|
103 | public IntValue EliteSetSize {
|
---|
104 | get { return EliteSetSizeParameter.Value; }
|
---|
105 | set { EliteSetSizeParameter.Value.Value = value.Value; }
|
---|
106 | }
|
---|
107 | public IntValue LocalImprovementMaximumIterations {
|
---|
108 | get { return LocalImprovementMaximumIterationsParameter.Value; }
|
---|
109 | set { LocalImprovementMaximumIterationsParameter.Value.Value = value.Value; }
|
---|
110 | }
|
---|
111 |
|
---|
112 | private RandomCreator RandomCreator {
|
---|
113 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
114 | }
|
---|
115 | private VariableCreator VariableCreator {
|
---|
116 | get { return (VariableCreator)RandomCreator.Successor; }
|
---|
117 | }
|
---|
118 | private SolutionsCreator SolutionsCreator {
|
---|
119 | get { return (SolutionsCreator)VariableCreator.Successor; }
|
---|
120 | }
|
---|
121 | private SubScopesCounter SubScopesCounter {
|
---|
122 | get { return (SubScopesCounter)SolutionsCreator.Successor; }
|
---|
123 | }
|
---|
124 | private ResultsCollector ResultsCollector {
|
---|
125 | get { return (ResultsCollector)SubScopesCounter.Successor; }
|
---|
126 | }
|
---|
127 | private GRASPWithPathRelinkingMainLoop MainLoop {
|
---|
128 | get { return (GRASPWithPathRelinkingMainLoop)ResultsCollector.Successor; }
|
---|
129 | }
|
---|
130 | #endregion
|
---|
131 |
|
---|
132 | [Storable]
|
---|
133 | private BestAverageWorstQualityAnalyzer analyzer;
|
---|
134 |
|
---|
135 | [StorableConstructor]
|
---|
136 | private GRASPWithPathRelinking(StorableConstructorFlag _) : base(_) { }
|
---|
137 | private GRASPWithPathRelinking(GRASPWithPathRelinking original, Cloner cloner)
|
---|
138 | : base(original, cloner) {
|
---|
139 | analyzer = cloner.Clone(original.analyzer);
|
---|
140 | RegisterEventHandlers();
|
---|
141 | }
|
---|
142 | public GRASPWithPathRelinking()
|
---|
143 | : base() {
|
---|
144 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
145 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator."));
|
---|
146 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each iteration.", new MultiAnalyzer()));
|
---|
147 | Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of iterations that the algorithm should run.", new IntValue(1000)));
|
---|
148 | Parameters.Add(new FixedValueParameter<IntValue>("EliteSetSize", "The elite set stores the best found solutions.", new IntValue(10)));
|
---|
149 | Parameters.Add(new FixedValueParameter<IntValue>("LocalImprovementMaximumIterations", "The maximum number of iterations performed by the local improvement operator.", new IntValue(1000)));
|
---|
150 | Parameters.Add(new ConstrainedValueParameter<ILocalImprovementOperator>("LocalImprovement", "Performs a local search on the solution."));
|
---|
151 | Parameters.Add(new FixedValueParameter<IntValue>("MinimumEliteSetSize", "(ρ) The minimum amount of elites for performing path relinking.", new IntValue(2)));
|
---|
152 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("PathRelinking", "The operator that performs the path relinking."));
|
---|
153 | Parameters.Add(new ConstrainedValueParameter<IPopulationReducer>("EliteSetReducer", "The operator that reduces the old elite set and the new solution(s) to a new elite set."));
|
---|
154 |
|
---|
155 | analyzer = new BestAverageWorstQualityAnalyzer();
|
---|
156 | Analyzer.Operators.Add(analyzer);
|
---|
157 |
|
---|
158 | var randomCreator = new RandomCreator();
|
---|
159 | OperatorGraph.InitialOperator = randomCreator;
|
---|
160 |
|
---|
161 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
162 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
163 | randomCreator.SeedParameter.Value = null;
|
---|
164 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
165 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
166 |
|
---|
167 | var variableCreator = new VariableCreator();
|
---|
168 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
169 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
170 | randomCreator.Successor = variableCreator;
|
---|
171 |
|
---|
172 | var solutionsCreator = new SolutionsCreator();
|
---|
173 | solutionsCreator.NumberOfSolutionsParameter.ActualName = MinimumEliteSetSizeParameter.Name;
|
---|
174 | solutionsCreator.ParallelParameter.Value = new BoolValue(true);
|
---|
175 | variableCreator.Successor = solutionsCreator;
|
---|
176 |
|
---|
177 | var subscopesCounter = new SubScopesCounter();
|
---|
178 | subscopesCounter.Name = "EvaluatedSolutions++";
|
---|
179 | subscopesCounter.AccumulateParameter.Value.Value = true;
|
---|
180 | subscopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
181 | solutionsCreator.Successor = subscopesCounter;
|
---|
182 |
|
---|
183 | var resultsCollector = new ResultsCollector();
|
---|
184 | resultsCollector.CopyValue.Value = false;
|
---|
185 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "Counter for the number of times the evaluation function is called."));
|
---|
186 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations", "The algorithm's current iteration"));
|
---|
187 | subscopesCounter.Successor = resultsCollector;
|
---|
188 |
|
---|
189 | var mainLoop = new GRASPWithPathRelinkingMainLoop();
|
---|
190 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
191 | mainLoop.EliteSetReducerParameter.ActualName = EliteSetReducerParameter.Name;
|
---|
192 | mainLoop.EliteSetSizeParameter.ActualName = EliteSetSizeParameter.Name;
|
---|
193 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
194 | mainLoop.LocalImprovementParameter.ActualName = LocalImprovementParameter.Name;
|
---|
195 | mainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
196 | mainLoop.PathRelinkingParameter.ActualName = PathRelinkingParameter.Name;
|
---|
197 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
198 | resultsCollector.Successor = mainLoop;
|
---|
199 |
|
---|
200 | InitializeOperators();
|
---|
201 | RegisterEventHandlers();
|
---|
202 | }
|
---|
203 |
|
---|
204 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
205 | return new GRASPWithPathRelinking(this, cloner);
|
---|
206 | }
|
---|
207 |
|
---|
208 | public override void Prepare() {
|
---|
209 | if (Problem != null) base.Prepare();
|
---|
210 | }
|
---|
211 |
|
---|
212 | #region Events
|
---|
213 | protected override void OnProblemChanged() {
|
---|
214 | InitializeOperators();
|
---|
215 | base.OnProblemChanged();
|
---|
216 | }
|
---|
217 |
|
---|
218 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
219 | Parameterize();
|
---|
220 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
221 | }
|
---|
222 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
223 | Parameterize();
|
---|
224 | base.Problem_EvaluatorChanged(sender, e);
|
---|
225 | }
|
---|
226 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
227 | InitializeOperators();
|
---|
228 | base.Problem_OperatorsChanged(sender, e);
|
---|
229 | }
|
---|
230 |
|
---|
231 | private void LocalImprovementParameter_ValueChanged(object sender, EventArgs e) {
|
---|
232 | Parameterize();
|
---|
233 | }
|
---|
234 | #endregion
|
---|
235 |
|
---|
236 | #region Helpers
|
---|
237 | [StorableHook(HookType.AfterDeserialization)]
|
---|
238 | private void AfterDeserialization() {
|
---|
239 | if (Problem != null) {
|
---|
240 | }
|
---|
241 | RegisterEventHandlers();
|
---|
242 | }
|
---|
243 |
|
---|
244 | private void RegisterEventHandlers() {
|
---|
245 | LocalImprovementParameter.ValueChanged += new EventHandler(LocalImprovementParameter_ValueChanged);
|
---|
246 | }
|
---|
247 |
|
---|
248 | private void InitializeOperators() {
|
---|
249 | Analyzer.Operators.Clear();
|
---|
250 | if (Problem != null) {
|
---|
251 | foreach (IAnalyzer a in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
252 | foreach (var param in a.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
253 | param.Depth = 1;
|
---|
254 | Analyzer.Operators.Add(a);
|
---|
255 | Analyzer.Operators.SetItemCheckedState(a, a.EnabledByDefault);
|
---|
256 | }
|
---|
257 | InitializeFromInstallation(LocalImprovementParameter, x => x is ILocalImprovementAlgorithmOperator
|
---|
258 | && ((ILocalImprovementAlgorithmOperator)x).ProblemType.IsInstanceOfType(Problem));
|
---|
259 | InitializeFromProblem(PathRelinkingParameter);
|
---|
260 | InitializeFromProblem(EliteSetReducerParameter);
|
---|
261 | } else {
|
---|
262 | LocalImprovementParameter.ValidValues.Clear();
|
---|
263 | PathRelinkingParameter.ValidValues.Clear();
|
---|
264 | EliteSetReducerParameter.ValidValues.Clear();
|
---|
265 | }
|
---|
266 | Analyzer.Operators.Add(analyzer);
|
---|
267 |
|
---|
268 | Parameterize();
|
---|
269 | }
|
---|
270 |
|
---|
271 | private void Parameterize() {
|
---|
272 | if (Problem != null) {
|
---|
273 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
274 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
275 |
|
---|
276 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
277 | MainLoop.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
278 |
|
---|
279 | analyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
280 |
|
---|
281 | if (LocalImprovementParameter.Value != null) {
|
---|
282 | if (LocalImprovementParameter.Value is ILocalImprovementAlgorithmOperator)
|
---|
283 | ((ILocalImprovementAlgorithmOperator)LocalImprovementParameter.Value).Problem = Problem;
|
---|
284 | }
|
---|
285 | }
|
---|
286 | foreach (var localImprovement in LocalImprovementParameter.ValidValues) {
|
---|
287 | localImprovement.MaximumIterationsParameter.ActualName = LocalImprovementMaximumIterationsParameter.Name;
|
---|
288 | localImprovement.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
289 | localImprovement.ResultsParameter.ActualName = "Results";
|
---|
290 | }
|
---|
291 | foreach (var reducer in EliteSetReducerParameter.ValidValues) {
|
---|
292 | reducer.MinimumPopulationSizeParameter.ActualName = MinimumEliteSetSizeParameter.Name;
|
---|
293 | reducer.MaximumPopulationSizeParameter.ActualName = EliteSetSizeParameter.Name;
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | private void InitializeFromProblem<T>(ConstrainedValueParameter<T> parameter) where T : class, INamedItem {
|
---|
298 | InitializeFromProblem<T>(parameter, x => true);
|
---|
299 | }
|
---|
300 | private void InitializeFromProblem<T>(ConstrainedValueParameter<T> parameter, Func<T, bool> condition) where T : class, INamedItem {
|
---|
301 | if (parameter == null) throw new ArgumentNullException("parameter");
|
---|
302 | if (condition == null) throw new ArgumentNullException("condition");
|
---|
303 | string oldName = String.Empty;
|
---|
304 | if (parameter.Value != null) oldName = parameter.Value.Name;
|
---|
305 | parameter.ValidValues.Clear();
|
---|
306 | foreach (var item in Problem.Operators.OfType<T>().Where(condition)) {
|
---|
307 | parameter.ValidValues.Add(item);
|
---|
308 | }
|
---|
309 | var newItem = parameter.ValidValues.FirstOrDefault(x => x.Name == oldName);
|
---|
310 | if (newItem != null) parameter.Value = newItem;
|
---|
311 | }
|
---|
312 | private void InitializeFromInstallation<T>(ConstrainedValueParameter<T> parameter) where T : class, INamedItem {
|
---|
313 | InitializeFromInstallation<T>(parameter, x => true);
|
---|
314 | }
|
---|
315 | private void InitializeFromInstallation<T>(ConstrainedValueParameter<T> parameter, Func<T, bool> condition) where T : class, INamedItem {
|
---|
316 | if (parameter == null) throw new ArgumentNullException("parameter");
|
---|
317 | if (condition == null) throw new ArgumentNullException("condition");
|
---|
318 | string oldName = String.Empty;
|
---|
319 | if (parameter.Value != null) oldName = parameter.Value.Name;
|
---|
320 | parameter.ValidValues.Clear();
|
---|
321 | foreach (var item in ApplicationManager.Manager.GetInstances<T>().Where(condition)) {
|
---|
322 | parameter.ValidValues.Add(item);
|
---|
323 | }
|
---|
324 | var newItem = parameter.ValidValues.FirstOrDefault(x => x.Name == oldName);
|
---|
325 | if (newItem != null) parameter.Value = newItem;
|
---|
326 | }
|
---|
327 | #endregion
|
---|
328 | }
|
---|
329 | }
|
---|