[6042] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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;
|
---|
[5603] | 23 | using System.Linq;
|
---|
[5809] | 24 | using HeuristicLab.Analysis;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
[5603] | 26 | using HeuristicLab.Core;
|
---|
[5809] | 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
[5603] | 29 | using HeuristicLab.Optimization;
|
---|
[5809] | 30 | using HeuristicLab.Optimization.Operators;
|
---|
[5603] | 31 | using HeuristicLab.Parameters;
|
---|
[5809] | 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[6042] | 33 | using HeuristicLab.PluginInfrastructure;
|
---|
[5603] | 34 | using HeuristicLab.Random;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Algorithms.VariableNeighborhoodSearch {
|
---|
[6057] | 37 | [Item("Variable Neighborhood Search", "A variable neighborhood search algorithm based on the description in Mladenovic, N. and Hansen, P. (1997). Variable neighborhood search. Computers & Operations Research Volume 24, Issue 11, pp. 1097-1100.")]
|
---|
[5603] | 38 | [Creatable("Algorithms")]
|
---|
| 39 | [StorableClass]
|
---|
[5809] | 40 | public sealed class VariableNeighborhoodSearch : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
[5603] | 41 | public string Filename { get; set; }
|
---|
| 42 |
|
---|
| 43 | #region Problem Properties
|
---|
| 44 | public override Type ProblemType {
|
---|
[5809] | 45 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
[5603] | 46 | }
|
---|
[5809] | 47 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
| 48 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
[5603] | 49 | set { base.Problem = value; }
|
---|
| 50 | }
|
---|
| 51 | #endregion
|
---|
| 52 |
|
---|
| 53 | #region Parameter Properties
|
---|
[6042] | 54 | private FixedValueParameter<IntValue> SeedParameter {
|
---|
| 55 | get { return (FixedValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
[5603] | 56 | }
|
---|
[6042] | 57 | private FixedValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
| 58 | get { return (FixedValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
[5603] | 59 | }
|
---|
[6042] | 60 | private ConstrainedValueParameter<ILocalImprovementOperator> LocalImprovementParameter {
|
---|
| 61 | get { return (ConstrainedValueParameter<ILocalImprovementOperator>)Parameters["LocalImprovement"]; }
|
---|
[5603] | 62 | }
|
---|
[6042] | 63 | private ConstrainedValueParameter<IMultiNeighborhoodShakingOperator> ShakingOperatorParameter {
|
---|
| 64 | get { return (ConstrainedValueParameter<IMultiNeighborhoodShakingOperator>)Parameters["ShakingOperator"]; }
|
---|
[5603] | 65 | }
|
---|
[6042] | 66 | private FixedValueParameter<IntValue> MaximumIterationsParameter {
|
---|
| 67 | get { return (FixedValueParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
[5603] | 68 | }
|
---|
| 69 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 70 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
| 71 | }
|
---|
[6042] | 72 | public FixedValueParameter<IntValue> LocalImprovementMaximumIterationsParameter {
|
---|
| 73 | get { return (FixedValueParameter<IntValue>)Parameters["LocalImprovementMaximumIterations"]; }
|
---|
[5642] | 74 | }
|
---|
[5603] | 75 | #endregion
|
---|
| 76 |
|
---|
| 77 | #region Properties
|
---|
| 78 | private RandomCreator RandomCreator {
|
---|
| 79 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 80 | }
|
---|
[5642] | 81 | public MultiAnalyzer Analyzer {
|
---|
| 82 | get { return AnalyzerParameter.Value; }
|
---|
| 83 | set { AnalyzerParameter.Value = value; }
|
---|
| 84 | }
|
---|
| 85 | private SolutionsCreator SolutionsCreator {
|
---|
| 86 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
| 87 | }
|
---|
[6042] | 88 | private VariableNeighborhoodSearchMainLoop MainLoop {
|
---|
| 89 | get { return FindMainLoop(SolutionsCreator.Successor); }
|
---|
| 90 | }
|
---|
[5603] | 91 | #endregion
|
---|
| 92 |
|
---|
[5642] | 93 | [Storable]
|
---|
| 94 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
| 95 |
|
---|
[5603] | 96 | [StorableConstructor]
|
---|
| 97 | private VariableNeighborhoodSearch(bool deserializing) : base(deserializing) { }
|
---|
| 98 | private VariableNeighborhoodSearch(VariableNeighborhoodSearch original, Cloner cloner)
|
---|
| 99 | : base(original, cloner) {
|
---|
[5642] | 100 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
[6042] | 101 | RegisterEventHandlers();
|
---|
[5603] | 102 | }
|
---|
| 103 | public VariableNeighborhoodSearch()
|
---|
| 104 | : base() {
|
---|
[6042] | 105 | Parameters.Add(new FixedValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 106 | Parameters.Add(new FixedValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
| 107 | Parameters.Add(new ConstrainedValueParameter<ILocalImprovementOperator>("LocalImprovement", "The local improvement operation"));
|
---|
| 108 | Parameters.Add(new ConstrainedValueParameter<IMultiNeighborhoodShakingOperator>("ShakingOperator", "The operator that performs the shaking of solutions."));
|
---|
| 109 | Parameters.Add(new FixedValueParameter<IntValue>("MaximumIterations", "The maximum number of iterations which should be processed.", new IntValue(50)));
|
---|
| 110 | Parameters.Add(new FixedValueParameter<IntValue>("LocalImprovementMaximumIterations", "The maximum number of iterations which should be performed in the local improvement phase.", new IntValue(50)));
|
---|
[5603] | 111 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the solution and moves.", new MultiAnalyzer()));
|
---|
| 112 |
|
---|
| 113 | RandomCreator randomCreator = new RandomCreator();
|
---|
| 114 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
| 115 | VariableCreator variableCreator = new VariableCreator();
|
---|
| 116 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
| 117 | VariableNeighborhoodSearchMainLoop mainLoop = new VariableNeighborhoodSearchMainLoop();
|
---|
| 118 | OperatorGraph.InitialOperator = randomCreator;
|
---|
| 119 |
|
---|
| 120 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
| 121 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
| 122 | randomCreator.SeedParameter.Value = null;
|
---|
| 123 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
| 124 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
| 125 | randomCreator.Successor = solutionsCreator;
|
---|
| 126 |
|
---|
| 127 | solutionsCreator.NumberOfSolutions = new IntValue(1);
|
---|
| 128 | solutionsCreator.Successor = variableCreator;
|
---|
| 129 |
|
---|
| 130 | variableCreator.Name = "Initialize Evaluated Solutions";
|
---|
[6042] | 131 |
|
---|
| 132 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
| 133 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
| 134 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentNeighborhoodIndex", new IntValue(0)));
|
---|
| 135 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NeighborhoodCount", new IntValue(0)));
|
---|
[5603] | 136 | variableCreator.Successor = resultsCollector;
|
---|
| 137 |
|
---|
| 138 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
[6042] | 139 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
|
---|
[5603] | 140 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
| 141 | resultsCollector.Successor = mainLoop;
|
---|
| 142 |
|
---|
[6042] | 143 | mainLoop.IterationsParameter.ActualName = "Iterations";
|
---|
| 144 | mainLoop.CurrentNeighborhoodIndexParameter.ActualName = "CurrentNeighborhoodIndex";
|
---|
| 145 | mainLoop.NeighborhoodCountParameter.ActualName = "NeighborhoodCount";
|
---|
[5603] | 146 | mainLoop.LocalImprovementParameter.ActualName = LocalImprovementParameter.Name;
|
---|
[6042] | 147 | mainLoop.ShakingOperatorParameter.ActualName = ShakingOperatorParameter.Name;
|
---|
[5603] | 148 | mainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
| 149 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 150 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
| 151 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 152 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
| 153 |
|
---|
[6042] | 154 | InitializeLocalImprovementOperators();
|
---|
[5642] | 155 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
| 156 | ParameterizeAnalyzers();
|
---|
| 157 | UpdateAnalyzers();
|
---|
| 158 |
|
---|
[6042] | 159 | RegisterEventHandlers();
|
---|
[5603] | 160 | }
|
---|
[5642] | 161 |
|
---|
[6042] | 162 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 163 | return new VariableNeighborhoodSearch(this, cloner);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 167 | private void AfterDeserialization() {
|
---|
| 168 | RegisterEventHandlers();
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[5642] | 171 | public override void Prepare() {
|
---|
| 172 | if (Problem != null) base.Prepare();
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[6042] | 175 | private void RegisterEventHandlers() {
|
---|
| 176 | LocalImprovementParameter.ValueChanged += new EventHandler(LocalImprovementParameter_ValueChanged);
|
---|
[5642] | 177 | if (Problem != null) {
|
---|
| 178 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | #region Events
|
---|
| 183 | protected override void OnProblemChanged() {
|
---|
[6042] | 184 | InitializeLocalImprovementOperators();
|
---|
| 185 | UpdateShakingOperators();
|
---|
| 186 | UpdateAnalyzers();
|
---|
| 187 |
|
---|
[5642] | 188 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 189 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 190 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
| 191 | ParameterizeSolutionsCreator();
|
---|
[6042] | 192 | ParameterizeMainLoop();
|
---|
[5642] | 193 | ParameterizeAnalyzers();
|
---|
| 194 | ParameterizeIterationBasedOperators();
|
---|
[6042] | 195 | ParameterizeLocalImprovementOperators();
|
---|
[5642] | 196 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 197 | base.OnProblemChanged();
|
---|
| 198 | }
|
---|
| 199 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 200 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 201 | ParameterizeSolutionsCreator();
|
---|
| 202 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
| 203 | }
|
---|
| 204 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 205 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 206 | ParameterizeSolutionsCreator();
|
---|
[6042] | 207 | ParameterizeMainLoop();
|
---|
[5642] | 208 | ParameterizeAnalyzers();
|
---|
| 209 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 210 | base.Problem_EvaluatorChanged(sender, e);
|
---|
| 211 | }
|
---|
| 212 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
[6042] | 213 | UpdateShakingOperators();
|
---|
| 214 | UpdateAnalyzers();
|
---|
[5642] | 215 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
| 216 | ParameterizeIterationBasedOperators();
|
---|
| 217 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 218 | }
|
---|
| 219 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[6042] | 220 | ParameterizeMainLoop();
|
---|
[5642] | 221 | ParameterizeAnalyzers();
|
---|
| 222 | }
|
---|
[6042] | 223 | private void LocalImprovementParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 224 | ParameterizeLocalImprovementOperators();
|
---|
[5752] | 225 | }
|
---|
[5642] | 226 | #endregion
|
---|
| 227 |
|
---|
| 228 | #region Helpers
|
---|
| 229 | private void ParameterizeSolutionsCreator() {
|
---|
| 230 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 231 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
| 232 | }
|
---|
| 233 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
| 234 | if (op is IStochasticOperator)
|
---|
| 235 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 236 | }
|
---|
[6042] | 237 | private void ParameterizeMainLoop() {
|
---|
| 238 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 239 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 240 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[5642] | 241 | }
|
---|
| 242 | private void ParameterizeAnalyzers() {
|
---|
| 243 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 244 | if (Problem != null) {
|
---|
| 245 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 246 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 247 | qualityAnalyzer.QualityParameter.Depth = 1;
|
---|
| 248 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 | private void ParameterizeIterationBasedOperators() {
|
---|
| 252 | if (Problem != null) {
|
---|
| 253 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
[5735] | 254 | op.IterationsParameter.ActualName = "Iterations";
|
---|
[6042] | 255 | op.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
[5642] | 256 | }
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
[6042] | 259 | private void ParameterizeLocalImprovementOperators() {
|
---|
| 260 | foreach (ILocalImprovementOperator op in LocalImprovementParameter.ValidValues) {
|
---|
| 261 | if (op != LocalImprovementParameter.Value) op.Problem = null;
|
---|
| 262 | op.MaximumIterationsParameter.Value = null;
|
---|
| 263 | op.MaximumIterationsParameter.ActualName = LocalImprovementMaximumIterationsParameter.Name;
|
---|
[5642] | 264 | }
|
---|
[6042] | 265 | if (LocalImprovementParameter.Value != null)
|
---|
| 266 | LocalImprovementParameter.Value.Problem = Problem;
|
---|
| 267 | }
|
---|
| 268 | private void InitializeLocalImprovementOperators() {
|
---|
| 269 | if (Problem == null) {
|
---|
| 270 | LocalImprovementParameter.ValidValues.Clear();
|
---|
| 271 | } else {
|
---|
| 272 | LocalImprovementParameter.ValidValues.RemoveWhere(x => !x.ProblemType.IsAssignableFrom(Problem.GetType()));
|
---|
| 273 | foreach (ILocalImprovementOperator op in ApplicationManager.Manager.GetInstances<ILocalImprovementOperator>().Where(x => x.ProblemType.IsAssignableFrom(Problem.GetType()))) {
|
---|
| 274 | if (!LocalImprovementParameter.ValidValues.Any(x => x.GetType() == op.GetType()))
|
---|
| 275 | LocalImprovementParameter.ValidValues.Add(op);
|
---|
[5642] | 276 | }
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
[6042] | 279 | private void UpdateShakingOperators() {
|
---|
| 280 | ShakingOperatorParameter.ValidValues.Clear();
|
---|
| 281 | foreach (IMultiNeighborhoodShakingOperator op in Problem.Operators.OfType<IMultiNeighborhoodShakingOperator>()) {
|
---|
| 282 | ShakingOperatorParameter.ValidValues.Add(op);
|
---|
| 283 | op.CurrentNeighborhoodIndexParameter.ActualName = "CurrentNeighborhoodIndex";
|
---|
| 284 | op.NeighborhoodCountParameter.ActualName = "NeighborhoodCount";
|
---|
| 285 | }
|
---|
[5642] | 286 | }
|
---|
| 287 | private void UpdateAnalyzers() {
|
---|
| 288 | Analyzer.Operators.Clear();
|
---|
| 289 | if (Problem != null) {
|
---|
| 290 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
| 291 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 292 | param.Depth = 1;
|
---|
| 293 | Analyzer.Operators.Add(analyzer);
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
| 297 | }
|
---|
| 298 | private VariableNeighborhoodSearchMainLoop FindMainLoop(IOperator start) {
|
---|
| 299 | IOperator mainLoop = start;
|
---|
| 300 | while (mainLoop != null && !(mainLoop is VariableNeighborhoodSearchMainLoop))
|
---|
| 301 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
| 302 | if (mainLoop == null) return null;
|
---|
| 303 | else return (VariableNeighborhoodSearchMainLoop)mainLoop;
|
---|
| 304 | }
|
---|
| 305 | #endregion
|
---|
[5603] | 306 | }
|
---|
| 307 | }
|
---|