[7345] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15490] | 3 | * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7345] | 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;
|
---|
[15492] | 23 | using System.Collections.Generic;
|
---|
[7345] | 24 | using System.Linq;
|
---|
[16728] | 25 | using HEAL.Attic;
|
---|
[7345] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Operators;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Optimization.Operators;
|
---|
| 32 | using HeuristicLab.Parameters;
|
---|
| 33 | using HeuristicLab.Selection;
|
---|
| 34 |
|
---|
[15490] | 35 | namespace HeuristicLab.Algorithms.GRASP {
|
---|
[7345] | 36 | [Item("GRASP+PR MainLoop", "The main loop that implements the behavior of the GRASP+PR algorithm.")]
|
---|
[16728] | 37 | [StorableType("14AD672E-ADC2-4506-87A8-5022C2832DF6")]
|
---|
[7345] | 38 | public class GRASPWithPathRelinkingMainLoop : AlgorithmOperator {
|
---|
[15507] | 39 | public IValueLookupParameter<IntValue> IterationsParameter {
|
---|
| 40 | get { return (IValueLookupParameter<IntValue>)Parameters["Iterations"]; }
|
---|
| 41 | }
|
---|
[7345] | 42 | public IValueLookupParameter<IOperator> SolutionCreatorParameter {
|
---|
| 43 | get { return (IValueLookupParameter<IOperator>)Parameters["SolutionCreator"]; }
|
---|
| 44 | }
|
---|
| 45 | public IValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
| 46 | get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
| 47 | }
|
---|
| 48 | public IValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 49 | get { return (IValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
| 50 | }
|
---|
| 51 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
| 52 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
| 53 | }
|
---|
| 54 | public IValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 55 | get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
| 56 | }
|
---|
| 57 | public IValueLookupParameter<IntValue> EliteSetSizeParameter {
|
---|
| 58 | get { return (IValueLookupParameter<IntValue>)Parameters["EliteSetSize"]; }
|
---|
| 59 | }
|
---|
| 60 | public IValueLookupParameter<IOperator> LocalImprovementParameter {
|
---|
| 61 | get { return (IValueLookupParameter<IOperator>)Parameters["LocalImprovement"]; }
|
---|
| 62 | }
|
---|
| 63 | public IValueLookupParameter<IOperator> PathRelinkingParameter {
|
---|
| 64 | get { return (IValueLookupParameter<IOperator>)Parameters["PathRelinking"]; }
|
---|
| 65 | }
|
---|
[7478] | 66 | public IValueLookupParameter<IOperator> EliteSetReducerParameter {
|
---|
| 67 | get { return (IValueLookupParameter<IOperator>)Parameters["EliteSetReducer"]; }
|
---|
[7345] | 68 | }
|
---|
| 69 | public IValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 70 | get { return (IValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | [StorableConstructor]
|
---|
[16728] | 74 | protected GRASPWithPathRelinkingMainLoop(StorableConstructorFlag _) : base(_) { }
|
---|
[7345] | 75 | protected GRASPWithPathRelinkingMainLoop(GRASPWithPathRelinkingMainLoop original, Cloner cloner)
|
---|
| 76 | : base(original, cloner) { }
|
---|
| 77 | public GRASPWithPathRelinkingMainLoop()
|
---|
| 78 | : base() {
|
---|
[15507] | 79 | Parameters.Add(new ValueLookupParameter<IntValue>("Iterations", "The algorithm's current iteration."));
|
---|
[7345] | 80 | Parameters.Add(new ValueLookupParameter<IOperator>("SolutionCreator", "The solution creation procedure which ideally should be a greedy initialization heuristic."));
|
---|
| 81 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The evaluator which calculates the fitness of a solutions."));
|
---|
| 82 | Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions."));
|
---|
| 83 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The number of maximum iterations that should be performed."));
|
---|
| 84 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "Specifies where the results are stored."));
|
---|
| 85 | Parameters.Add(new ValueLookupParameter<IntValue>("EliteSetSize", "The size of the elite set."));
|
---|
| 86 | Parameters.Add(new ValueLookupParameter<IOperator>("LocalImprovement", "The operator which performs the local improvement."));
|
---|
| 87 | Parameters.Add(new ValueLookupParameter<IOperator>("PathRelinking", "The operator which performs the path relinking."));
|
---|
[7478] | 88 | Parameters.Add(new ValueLookupParameter<IOperator>("EliteSetReducer", "The operator that reduces the existing elite set and the new solution to a new elite set."));
|
---|
[7345] | 89 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The analyzer that is to be applied."));
|
---|
| 90 |
|
---|
[15507] | 91 | var analyzer1 = new Placeholder() { Name = "(Analyzer)" };
|
---|
| 92 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[7345] | 93 |
|
---|
| 94 | var selector1 = new RandomSelector();
|
---|
| 95 | selector1.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
|
---|
| 96 | selector1.CopySelected.Value = true;
|
---|
| 97 |
|
---|
| 98 | var ssp1 = new SubScopesProcessor();
|
---|
| 99 |
|
---|
| 100 | var eo1 = new EmptyOperator();
|
---|
| 101 |
|
---|
| 102 | var solutionsCreator = new SolutionsCreator();
|
---|
[7373] | 103 | solutionsCreator.ParallelParameter.Value = new BoolValue(false);
|
---|
[7345] | 104 | solutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
| 105 | solutionsCreator.SolutionCreatorParameter.ActualName = SolutionCreatorParameter.Name;
|
---|
| 106 | solutionsCreator.NumberOfSolutions = new IntValue(1);
|
---|
[7363] | 107 |
|
---|
[7345] | 108 | var ssp2 = new SubScopesProcessor();
|
---|
| 109 |
|
---|
| 110 | var eo2 = new EmptyOperator();
|
---|
| 111 |
|
---|
[15507] | 112 | var placeholder1 = new Placeholder() { Name = "(LocalImprovement)" };
|
---|
[7345] | 113 | placeholder1.OperatorParameter.ActualName = LocalImprovementParameter.Name;
|
---|
| 114 |
|
---|
| 115 | var childrenCreator = new ChildrenCreator();
|
---|
| 116 | childrenCreator.ParentsPerChild = new IntValue(2);
|
---|
[7363] | 117 |
|
---|
[7373] | 118 | var ssp3 = new SubScopesProcessor();
|
---|
| 119 |
|
---|
[15507] | 120 | var placeholder2 = new Placeholder() { Name = "(PathRelinking)" };
|
---|
[7345] | 121 | placeholder2.OperatorParameter.ActualName = PathRelinkingParameter.Name;
|
---|
| 122 |
|
---|
[15507] | 123 | var placeholder3 = new Placeholder() { Name = "(Evaluator)" };
|
---|
[7412] | 124 | placeholder3.OperatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
| 125 |
|
---|
[7345] | 126 | var subScopesRemover = new SubScopesRemover();
|
---|
| 127 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
| 128 |
|
---|
[7373] | 129 | var ssp4 = new SubScopesProcessor();
|
---|
[7345] | 130 |
|
---|
| 131 | var placeholder4 = new Placeholder();
|
---|
[7412] | 132 | placeholder4.Name = "(LocalImprovement)";
|
---|
| 133 | placeholder4.OperatorParameter.ActualName = LocalImprovementParameter.Name;
|
---|
[7345] | 134 |
|
---|
[15507] | 135 | var placeholder5 = new Placeholder() { Name = "(EliteSetReplacer)" };
|
---|
[7478] | 136 | placeholder5.OperatorParameter.ActualName = EliteSetReducerParameter.Name;
|
---|
[7412] | 137 |
|
---|
[15507] | 138 | var counter = new IntCounter() { Name = "Iterations++" };
|
---|
| 139 | counter.ValueParameter.ActualName = IterationsParameter.Name;
|
---|
[7345] | 140 | counter.Increment = new IntValue(1);
|
---|
| 141 |
|
---|
[15507] | 142 | var analyzer2 = new Placeholder() { Name = "(Analyzer)" };
|
---|
| 143 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[7345] | 144 |
|
---|
[15507] | 145 | var comparator3 = new Comparator() { Name = "Iterations >= MaximumIterations" };
|
---|
[7345] | 146 | comparator3.Comparison.Value = ComparisonType.GreaterOrEqual;
|
---|
[15507] | 147 | comparator3.LeftSideParameter.ActualName = IterationsParameter.Name;
|
---|
[7345] | 148 | comparator3.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
| 149 | comparator3.ResultParameter.ActualName = "TerminatedByIteration";
|
---|
| 150 |
|
---|
[15507] | 151 | var conditionalBranch3 = new ConditionalBranch() { Name = "Terminate by Iterations?" };
|
---|
[7345] | 152 | conditionalBranch3.ConditionParameter.ActualName = "TerminatedByIteration";
|
---|
| 153 |
|
---|
[15507] | 154 | OperatorGraph.InitialOperator = analyzer1;
|
---|
| 155 | analyzer1.Successor = selector1;
|
---|
[7478] | 156 | selector1.Successor = ssp1;
|
---|
[7345] | 157 | ssp1.Operators.Add(eo1);
|
---|
| 158 | ssp1.Operators.Add(solutionsCreator);
|
---|
[7412] | 159 | ssp1.Successor = placeholder5;
|
---|
[7345] | 160 | eo1.Successor = null;
|
---|
[7478] | 161 | solutionsCreator.Successor = ssp2;
|
---|
[7345] | 162 | ssp2.Operators.Add(eo2);
|
---|
| 163 | ssp2.Operators.Add(placeholder1);
|
---|
[7373] | 164 | ssp2.Successor = childrenCreator;
|
---|
[7345] | 165 | eo2.Successor = null;
|
---|
[7373] | 166 | placeholder1.Successor = null;
|
---|
| 167 | childrenCreator.Successor = ssp3;
|
---|
| 168 | ssp3.Operators.Add(placeholder2);
|
---|
| 169 | ssp3.Successor = null;
|
---|
[7412] | 170 | placeholder2.Successor = placeholder3;
|
---|
| 171 | placeholder3.Successor = subScopesRemover;
|
---|
[7478] | 172 | subScopesRemover.Successor = placeholder4;
|
---|
| 173 | placeholder4.Successor = null;
|
---|
[7412] | 174 | placeholder5.Successor = counter;
|
---|
[15507] | 175 | counter.Successor = analyzer2;
|
---|
| 176 | analyzer2.Successor = comparator3;
|
---|
[7345] | 177 | comparator3.Successor = conditionalBranch3;
|
---|
| 178 | conditionalBranch3.TrueBranch = null;
|
---|
[7478] | 179 | conditionalBranch3.FalseBranch = selector1;
|
---|
[7345] | 180 | conditionalBranch3.Successor = null;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 184 | return new GRASPWithPathRelinkingMainLoop(this, cloner);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | private void Parameterize() {
|
---|
[15492] | 188 | var operators = Walk(OperatorGraph).ToArray();
|
---|
[7345] | 189 |
|
---|
| 190 | foreach (var solutionsCreator in operators.OfType<SolutionsCreator>()) {
|
---|
| 191 | solutionsCreator.SolutionCreatorParameter.ActualName = SolutionCreatorParameter.Name;
|
---|
| 192 | solutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[15492] | 196 | /// <summary>
|
---|
| 197 | /// Walks an operator graph in that it jumps from one operator to all its operator parameters and yields each operator it touches.
|
---|
| 198 | /// Cycles are detected and not walked twice.
|
---|
| 199 | /// </summary>
|
---|
| 200 | /// <param name="graph">The operator graph that should be walked.</param>
|
---|
| 201 | /// <returns>An enumeration of all the operators that could be found.</returns>
|
---|
| 202 | private IEnumerable<IOperator> Walk(OperatorGraph graph) {
|
---|
| 203 | var open = new Stack<IOperator>();
|
---|
| 204 | var visited = new HashSet<IOperator>();
|
---|
| 205 | open.Push(graph.InitialOperator);
|
---|
| 206 |
|
---|
| 207 | while (open.Any()) {
|
---|
| 208 | IOperator current = open.Pop();
|
---|
| 209 | if (visited.Contains(current)) continue;
|
---|
| 210 | visited.Add(current);
|
---|
| 211 | yield return current;
|
---|
| 212 |
|
---|
| 213 | foreach (var parameter in current.Parameters.OfType<IValueParameter>()) {
|
---|
| 214 | if (typeof(IOperator).IsAssignableFrom(parameter.DataType)) {
|
---|
| 215 | if (parameter.Value != null)
|
---|
| 216 | open.Push((IOperator)parameter.Value);
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | var algOp = current as AlgorithmOperator;
|
---|
| 220 | if (algOp != null && algOp.OperatorGraph.InitialOperator != null) {
|
---|
| 221 | open.Push(algOp.OperatorGraph.InitialOperator);
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[7345] | 226 | public event EventHandler ProblemChanged;
|
---|
| 227 | protected virtual void OnProblemChanged() {
|
---|
| 228 | Parameterize();
|
---|
| 229 | var handler = ProblemChanged;
|
---|
| 230 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 | }
|
---|