Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614

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