Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614: simplified GRASP+PR operator graph, replaced IMerger with an IPopulationReducer

File size: 9.8 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> EliteSetReducerParameter {
64      get { return (IValueLookupParameter<IOperator>)Parameters["EliteSetReducer"]; }
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>("EliteSetReducer", "The operator that reduces the existing elite set and the new solution to a new elite set."));
85      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The analyzer that is to be applied."));
86
87      var variableCreator1 = new VariableCreator();
88      variableCreator1.Name = "Iterations = 0";
89      variableCreator1.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
90
91      var resultsCollector = new ResultsCollector();
92      resultsCollector.CopyValue = new BoolValue(false);
93      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
94
95      var selector1 = new RandomSelector();
96      selector1.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
97      selector1.CopySelected.Value = true;
98
99      var ssp1 = new SubScopesProcessor();
100
101      var eo1 = new EmptyOperator();
102
103      var solutionsCreator = new SolutionsCreator();
104      solutionsCreator.ParallelParameter.Value = new BoolValue(false);
105      solutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
106      solutionsCreator.SolutionCreatorParameter.ActualName = SolutionCreatorParameter.Name;
107      solutionsCreator.NumberOfSolutions = new IntValue(1);
108
109      var ssp2 = new SubScopesProcessor();
110
111      var eo2 = new EmptyOperator();
112
113      var placeholder1 = new Placeholder();
114      placeholder1.Name = "(LocalImprovement)";
115      placeholder1.OperatorParameter.ActualName = LocalImprovementParameter.Name;
116
117      var childrenCreator = new ChildrenCreator();
118      childrenCreator.ParentsPerChild = new IntValue(2);
119
120      var ssp3 = new SubScopesProcessor();
121
122      var placeholder2 = new Placeholder();
123      placeholder2.Name = "(PathRelinking)";
124      placeholder2.OperatorParameter.ActualName = PathRelinkingParameter.Name;
125
126      var placeholder3 = new Placeholder();
127      placeholder3.Name = "(Evaluator)";
128      placeholder3.OperatorParameter.ActualName = EvaluatorParameter.Name;
129
130      var subScopesRemover = new SubScopesRemover();
131      subScopesRemover.RemoveAllSubScopes = true;
132
133      var ssp4 = new SubScopesProcessor();
134
135      var placeholder4 = new Placeholder();
136      placeholder4.Name = "(LocalImprovement)";
137      placeholder4.OperatorParameter.ActualName = LocalImprovementParameter.Name;
138
139      var placeholder5 = new Placeholder();
140      placeholder5.Name = "(EliteSetReplacer)";
141      placeholder5.OperatorParameter.ActualName = EliteSetReducerParameter.Name;
142
143      var counter = new IntCounter();
144      counter.Name = "Iterations++";
145      counter.ValueParameter.ActualName = "Iterations";
146      counter.Increment = new IntValue(1);
147
148      var analyzer1 = new Placeholder();
149      analyzer1.Name = "(Analyzer)";
150      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
151
152      var comparator3 = new Comparator();
153      comparator3.Name = "Iterations >= MaximumIterations";
154      comparator3.Comparison.Value = ComparisonType.GreaterOrEqual;
155      comparator3.LeftSideParameter.ActualName = "Iterations";
156      comparator3.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
157      comparator3.ResultParameter.ActualName = "TerminatedByIteration";
158
159      var conditionalBranch3 = new ConditionalBranch();
160      conditionalBranch3.Name = "Terminate by Iterations?";
161      conditionalBranch3.ConditionParameter.ActualName = "TerminatedByIteration";
162
163      OperatorGraph.InitialOperator = variableCreator1;
164      variableCreator1.Successor = resultsCollector;
165      resultsCollector.Successor = selector1;
166      selector1.Successor = ssp1;
167      ssp1.Operators.Add(eo1);
168      ssp1.Operators.Add(solutionsCreator);
169      ssp1.Successor = placeholder5;
170      eo1.Successor = null;
171      solutionsCreator.Successor = ssp2;
172      ssp2.Operators.Add(eo2);
173      ssp2.Operators.Add(placeholder1);
174      ssp2.Successor = childrenCreator;
175      eo2.Successor = null;
176      placeholder1.Successor = null;
177      childrenCreator.Successor = ssp3;
178      ssp3.Operators.Add(placeholder2);
179      ssp3.Successor = null;
180      placeholder2.Successor = placeholder3;
181      placeholder3.Successor = subScopesRemover;
182      subScopesRemover.Successor = placeholder4;
183      placeholder4.Successor = null;
184      placeholder5.Successor = counter;
185      counter.Successor = analyzer1;
186      analyzer1.Successor = comparator3;
187      comparator3.Successor = conditionalBranch3;
188      conditionalBranch3.TrueBranch = null;
189      conditionalBranch3.FalseBranch = selector1;
190      conditionalBranch3.Successor = null;
191    }
192
193    public override IDeepCloneable Clone(Cloner cloner) {
194      return new GRASPWithPathRelinkingMainLoop(this, cloner);
195    }
196
197    private void Parameterize() {
198      var operators = OperatorGraph.InitialOperator.Walk().ToArray();
199
200      foreach (var solutionsCreator in operators.OfType<SolutionsCreator>()) {
201        solutionsCreator.SolutionCreatorParameter.ActualName = SolutionCreatorParameter.Name;
202        solutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
203      }
204    }
205
206    public event EventHandler ProblemChanged;
207    protected virtual void OnProblemChanged() {
208      Parameterize();
209      var handler = ProblemChanged;
210      if (handler != null) handler(this, EventArgs.Empty);
211    }
212  }
213}
Note: See TracBrowser for help on using the repository browser.