Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASPWithPathRelinkingMainLoop.cs @ 7345

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

#1614

  • added first version of GRASP+PR algorithm
File size: 11.4 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.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Optimization.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Selection;
33
34namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms {
35  [Item("GRASP+PR MainLoop", "The main loop that implements the behavior of the GRASP+PR algorithm.")]
36  [StorableClass]
37  public class GRASPWithPathRelinkingMainLoop : AlgorithmOperator {
38    public IValueLookupParameter<IOperator> SolutionCreatorParameter {
39      get { return (IValueLookupParameter<IOperator>)Parameters["SolutionCreator"]; }
40    }
41    public IValueLookupParameter<IOperator> EvaluatorParameter {
42      get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
43    }
44    public IValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
45      get { return (IValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
46    }
47    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
48      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
49    }
50    public IValueLookupParameter<ResultCollection> ResultsParameter {
51      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
52    }
53    public IValueLookupParameter<IntValue> EliteSetSizeParameter {
54      get { return (IValueLookupParameter<IntValue>)Parameters["EliteSetSize"]; }
55    }
56    public IValueLookupParameter<IOperator> LocalImprovementParameter {
57      get { return (IValueLookupParameter<IOperator>)Parameters["LocalImprovement"]; }
58    }
59    public IValueLookupParameter<IOperator> PathRelinkingParameter {
60      get { return (IValueLookupParameter<IOperator>)Parameters["PathRelinking"]; }
61    }
62    public IValueLookupParameter<IOperator> EliteSetReplacerParameter {
63      get { return (IValueLookupParameter<IOperator>)Parameters["EliteSetReplacer"]; }
64    }
65    public IValueLookupParameter<IOperator> AnalyzerParameter {
66      get { return (IValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
67    }
68
69    [StorableConstructor]
70    protected GRASPWithPathRelinkingMainLoop(bool deserializing) : base(deserializing) { }
71    protected GRASPWithPathRelinkingMainLoop(GRASPWithPathRelinkingMainLoop original, Cloner cloner)
72      : base(original, cloner) { }
73    public GRASPWithPathRelinkingMainLoop()
74      : base() {
75      Parameters.Add(new ValueLookupParameter<IOperator>("SolutionCreator", "The solution creation procedure which ideally should be a greedy initialization heuristic."));
76      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The evaluator which calculates the fitness of a solutions."));
77      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions."));
78      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The number of maximum iterations that should be performed."));
79      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "Specifies where the results are stored."));
80      Parameters.Add(new ValueLookupParameter<IntValue>("EliteSetSize", "The size of the elite set."));
81      Parameters.Add(new ValueLookupParameter<IOperator>("LocalImprovement", "The operator which performs the local improvement."));
82      Parameters.Add(new ValueLookupParameter<IOperator>("PathRelinking", "The operator which performs the path relinking."));
83      Parameters.Add(new ValueLookupParameter<IOperator>("EliteSetReplacer", "The operator that replaces elements in the elite set."));
84      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The analyzer that is to be applied."));
85
86      var variableCreator1 = new VariableCreator();
87      variableCreator1.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
88
89      var variableCreator2 = new VariableCreator();
90      variableCreator2.CollectedValues.Add(new ValueParameter<IntValue>("ActualEliteSetSize", new IntValue(0));
91      variableCreator2.Name = "ActualEliteSetSize = 0";
92
93      var subScopesCounter1 = new SubScopesCounter();
94      subScopesCounter1.Name = "ActualEliteSetSize += |SubScopes|";
95      subScopesCounter1.ValueParameter.ActualName = "ActualEliteSetSize";
96
97      var comparator1 = new Comparator();
98      comparator1.Name = "ActualEliteSetSize >= MinimumEliteSetSize";
99      comparator1.Comparison.Value = ComparisonType.GreaterOrEqual;
100      comparator1.LeftSideParameter.ActualName = "ActualEliteSetSize";
101      comparator1.RightSideParameter.ActualName = EliteSetSizeParameter.ActualName;
102      comparator1.ResultParameter.ActualName = "SizeOkay";
103
104      var conditionalBranch1 = new ConditionalBranch();
105      conditionalBranch1.Name = "Elite set has at least ρ elements";
106      conditionalBranch1.ConditionParameter.ActualName = "SizeOkay";
107
108      var selector1 = new RandomSelector();
109      selector1.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
110      selector1.CopySelected.Value = true;
111
112      var selector2 = new RandomSelector();
113      selector2.NumberOfSelectedSubScopesParameter.Value = new IntValue(0);
114     
115      var ssp1 = new SubScopesProcessor();
116
117      var eo1 = new EmptyOperator();
118
119      var solutionsCreator = new SolutionsCreator();
120      solutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
121      solutionsCreator.SolutionCreatorParameter.ActualName = SolutionCreatorParameter.Name;
122      solutionsCreator.NumberOfSolutions = new IntValue(1);
123     
124      var subScopesCounter2 = new SubScopesCounter();
125      subScopesCounter2.Name = "Count subscopes";
126      subScopesCounter2.ValueParameter.ActualName = "SolutionScopes";
127
128      var comparator2 = new Comparator();
129      comparator2.Name = "SolutionScopes == 2";
130      comparator2.Comparison.Value = ComparisonType.Equal;
131      comparator2.LeftSideParameter.ActualName = "SolutionScopes";
132      comparator2.RightSideParameter.Value = new IntValue(2);
133      comparator2.ResultParameter.ActualName = "PerformPR";
134
135      var conditionalBranch2 = new ConditionalBranch();
136      conditionalBranch2.Name = "Path relinking?";
137      conditionalBranch2.ConditionParameter.ActualName = "PerformPR";
138
139      var ssp2 = new SubScopesProcessor();
140
141      var eo2 = new EmptyOperator();
142
143      var placeholder1 = new Placeholder();
144      placeholder1.Name = "(LocalImprovement)";
145      placeholder1.OperatorParameter.ActualName = LocalImprovementParameter.Name;
146
147      var childrenCreator = new ChildrenCreator();
148      childrenCreator.ParentsPerChild = new IntValue(2);
149     
150      var placeholder2 = new Placeholder();
151      placeholder2.Name = "(PathRelinking)";
152      placeholder2.OperatorParameter.ActualName = PathRelinkingParameter.Name;
153
154      var subScopesRemover = new SubScopesRemover();
155      subScopesRemover.RemoveAllSubScopes = true;
156
157      var ssp3 = new SubScopesProcessor();
158
159      var placeholder3 = new Placeholder();
160      placeholder3.Name = "(LocalImprovement)";
161      placeholder3.OperatorParameter.ActualName = LocalImprovementParameter.Name;
162
163      var placeholder4 = new Placeholder();
164      placeholder4.Name = "(Replacer)";
165      placeholder4.OperatorParameter.ActualName = EliteSetReplacerParameter.Name;
166
167      var counter = new IntCounter();
168      counter.Name = "Iterations++";
169      counter.ValueParameter.ActualName = "Iterations";
170      counter.Increment = new IntValue(1);
171
172      var analyzer1 = new Placeholder();
173      analyzer1.Name = "(Analyzer)";
174      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
175
176      var comparator3 = new Comparator();
177      comparator3.Name = "Iterations >= MaximumIterations";
178      comparator3.Comparison.Value = ComparisonType.GreaterOrEqual;
179      comparator3.LeftSideParameter.ActualName = "Iterations";
180      comparator3.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
181      comparator3.ResultParameter.ActualName = "TerminatedByIteration";
182
183      var conditionalBranch3 = new ConditionalBranch();
184      conditionalBranch3.Name = "Terminate by Iterations?";
185      conditionalBranch3.ConditionParameter.ActualName = "TerminatedByIteration";
186
187      OperatorGraph.InitialOperator = variableCreator1;
188      variableCreator1.Successor = variableCreator2;
189      variableCreator2.Successor = subScopesCounter1;
190      subScopesCounter1.Successor = comparator1;
191      comparator1.Successor = conditionalBranch1;
192      conditionalBranch1.TrueBranch = selector1;
193      conditionalBranch1.FalseBranch = selector2;
194      conditionalBranch1.Successor = ssp1;
195      ssp1.Operators.Add(eo1);
196      ssp1.Operators.Add(solutionsCreator);
197      ssp1.Successor = placeholder4;
198      eo1.Successor = null;
199      solutionsCreator.Successor = subScopesCounter2;
200      subScopesCounter2.Successor = comparator2;
201      comparator2.Successor = conditionalBranch2;
202      conditionalBranch2.TrueBranch = ssp2;
203      conditionalBranch2.FalseBranch = null;
204      conditionalBranch2.Successor = ssp3;
205      ssp2.Operators.Add(eo2);
206      ssp2.Operators.Add(placeholder1);
207      ssp2.Successor = null;
208      eo2.Successor = null;
209      placeholder1.Successor = childrenCreator;
210      childrenCreator.Successor = placeholder2;
211      placeholder2.Successor = subScopesRemover;
212      subScopesRemover.Successor = null;
213      ssp3.Operators.Add(placeholder3);
214      ssp3.Successor = null;
215      placeholder4.Successor = counter;
216      counter.Successor = analyzer1;
217      analyzer1.Successor = comparator3;
218      comparator3.Successor = conditionalBranch3;
219      conditionalBranch3.TrueBranch = null;
220      conditionalBranch3.FalseBranch = variableCreator2;
221      conditionalBranch3.Successor = null;
222    }
223
224    public override IDeepCloneable Clone(Cloner cloner) {
225      return new GRASPWithPathRelinkingMainLoop(this, cloner);
226    }
227
228    private void Parameterize() {
229      var operators = OperatorGraph.InitialOperator.Walk().ToArray();
230
231      foreach (var solutionsCreator in operators.OfType<SolutionsCreator>()) {
232        solutionsCreator.SolutionCreatorParameter.ActualName = SolutionCreatorParameter.Name;
233        solutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
234      }
235    }
236
237    public event EventHandler ProblemChanged;
238    protected virtual void OnProblemChanged() {
239      Parameterize();
240      var handler = ProblemChanged;
241      if (handler != null) handler(this, EventArgs.Empty);
242    }
243  }
244}
Note: See TracBrowser for help on using the repository browser.