[5622] | 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;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Common;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Algorithms.LocalSearch;
|
---|
| 32 | using HeuristicLab.Data;
|
---|
| 33 | using HeuristicLab.Optimization;
|
---|
| 34 | using HeuristicLab.Optimization.Operators;
|
---|
[5735] | 35 | using HeuristicLab.Analysis;
|
---|
[5622] | 36 |
|
---|
| 37 | namespace HeuristicLab.Algorithms.LocalSearch {
|
---|
| 38 | /// <summary>
|
---|
| 39 | /// A local search improvement operator.
|
---|
| 40 | /// </summary>
|
---|
[5735] | 41 | [Item("LocalSearchImprovementOperator", "A local search improvement operator.")]
|
---|
[5622] | 42 | [StorableClass]
|
---|
[5735] | 43 | public class LocalSearchImprovementOperator: SingleSuccessorOperator, ILocalImprovementOperator {
|
---|
[5622] | 44 | [Storable]
|
---|
| 45 | private LocalSearchMainLoop loop;
|
---|
[5735] | 46 |
|
---|
| 47 | [Storable]
|
---|
| 48 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
[5622] | 49 |
|
---|
[5642] | 50 | private ConstrainedValueParameter<IMoveGenerator> MoveGeneratorParameter {
|
---|
| 51 | get { return (ConstrainedValueParameter<IMoveGenerator>)Parameters["MoveGenerator"]; }
|
---|
[5622] | 52 | }
|
---|
[5642] | 53 | private ConstrainedValueParameter<IMoveMaker> MoveMakerParameter {
|
---|
| 54 | get { return (ConstrainedValueParameter<IMoveMaker>)Parameters["MoveMaker"]; }
|
---|
[5622] | 55 | }
|
---|
[5642] | 56 | private ConstrainedValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter {
|
---|
| 57 | get { return (ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>)Parameters["MoveEvaluator"]; }
|
---|
[5622] | 58 | }
|
---|
| 59 | private ValueParameter<IntValue> MaximumIterationsParameter {
|
---|
| 60 | get { return (ValueParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
| 61 | }
|
---|
| 62 | private ValueParameter<IntValue> SampleSizeParameter {
|
---|
| 63 | get { return (ValueParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
| 64 | }
|
---|
| 65 | public LookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 66 | get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
| 67 | }
|
---|
[5735] | 68 | public ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 69 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
[5622] | 70 | }
|
---|
| 71 |
|
---|
| 72 | public IMoveGenerator MoveGenerator {
|
---|
| 73 | get { return MoveGeneratorParameter.Value; }
|
---|
| 74 | set { MoveGeneratorParameter.Value = value; }
|
---|
| 75 | }
|
---|
| 76 | public IMoveMaker MoveMaker {
|
---|
| 77 | get { return MoveMakerParameter.Value; }
|
---|
| 78 | set { MoveMakerParameter.Value = value; }
|
---|
| 79 | }
|
---|
| 80 | public ISingleObjectiveMoveEvaluator MoveEvaluator {
|
---|
| 81 | get { return MoveEvaluatorParameter.Value; }
|
---|
| 82 | set { MoveEvaluatorParameter.Value = value; }
|
---|
| 83 | }
|
---|
[5735] | 84 | public MultiAnalyzer Analyzer {
|
---|
| 85 | get { return AnalyzerParameter.Value; }
|
---|
| 86 | set { AnalyzerParameter.Value = value; }
|
---|
| 87 | }
|
---|
[5622] | 88 |
|
---|
| 89 | [StorableConstructor]
|
---|
[5735] | 90 | protected LocalSearchImprovementOperator(bool deserializing) : base(deserializing) {}
|
---|
[5642] | 91 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 92 | private void AfterDeserialization() {
|
---|
| 93 | Initialize();
|
---|
| 94 | }
|
---|
[5735] | 95 | protected LocalSearchImprovementOperator(LocalSearchImprovementOperator original, Cloner cloner)
|
---|
[5622] | 96 | : base(original, cloner) {
|
---|
| 97 | this.loop = cloner.Clone(original.loop);
|
---|
[5735] | 98 | this.qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
[5642] | 99 | Initialize();
|
---|
[5622] | 100 | }
|
---|
| 101 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[5735] | 102 | return new LocalSearchImprovementOperator(this, cloner);
|
---|
[5622] | 103 | }
|
---|
[5735] | 104 | public LocalSearchImprovementOperator()
|
---|
[5622] | 105 | : base() {
|
---|
[5751] | 106 | loop = new LocalSearchMainLoop();
|
---|
| 107 |
|
---|
| 108 | ResultsCollector rc = (loop.OperatorGraph.InitialOperator as SingleSuccessorOperator).Successor as ResultsCollector;
|
---|
[5735] | 109 | rc.CollectedValues.Remove("BestLocalQuality");
|
---|
| 110 |
|
---|
| 111 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
| 112 |
|
---|
[5642] | 113 | Parameters.Add(new ConstrainedValueParameter<IMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution."));
|
---|
| 114 | Parameters.Add(new ConstrainedValueParameter<IMoveMaker>("MoveMaker", "The operator used to perform a move."));
|
---|
| 115 | Parameters.Add(new ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>("MoveEvaluator", "The operator used to evaluate a move."));
|
---|
[5735] | 116 | Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(150)));
|
---|
| 117 | Parameters.Add(new ValueParameter<IntValue>("SampleSize", "Number of moves that MultiMoveGenerators should create. This is ignored for Exhaustive- and SingleMoveGenerators.", new IntValue(1500)));
|
---|
[5622] | 118 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated moves."));
|
---|
[5735] | 119 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the solution.", new MultiAnalyzer()));
|
---|
[5642] | 120 |
|
---|
| 121 | Initialize();
|
---|
[5622] | 122 | }
|
---|
| 123 |
|
---|
[5642] | 124 | private void Initialize() {
|
---|
| 125 | MoveGeneratorParameter.ValueChanged += new EventHandler(MoveGeneratorParameter_ValueChanged);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[5735] | 128 | public void OnProblemChanged(IProblem problem) {
|
---|
[5622] | 129 | UpdateMoveOperators(problem);
|
---|
[5642] | 130 | ChooseMoveOperators();
|
---|
| 131 |
|
---|
| 132 | ParameterizeMoveGenerators(problem as ISingleObjectiveProblem);
|
---|
| 133 | ParameterizeMoveEvaluators(problem as ISingleObjectiveProblem);
|
---|
| 134 | ParameterizeMoveMakers(problem as ISingleObjectiveProblem);
|
---|
[5735] | 135 |
|
---|
| 136 | ParameterizeAnalyzers(problem as ISingleObjectiveProblem);
|
---|
| 137 | UpdateAnalyzers(problem as ISingleObjectiveProblem);
|
---|
[5622] | 138 | }
|
---|
| 139 |
|
---|
[5735] | 140 | void ParameterizeAnalyzers(ISingleObjectiveProblem problem) {
|
---|
| 141 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 142 | if (problem != null) {
|
---|
| 143 | qualityAnalyzer.MaximizationParameter.ActualName = problem.MaximizationParameter.Name;
|
---|
| 144 | if (MoveEvaluator != null)
|
---|
| 145 | qualityAnalyzer.QualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
| 146 | qualityAnalyzer.BestKnownQualityParameter.ActualName = problem.BestKnownQualityParameter.Name;
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | void UpdateAnalyzers(ISingleObjectiveProblem problem) {
|
---|
| 151 | Analyzer.Operators.Clear();
|
---|
| 152 | if (problem != null) {
|
---|
| 153 | foreach (IAnalyzer analyzer in problem.Operators.OfType<IAnalyzer>()) {
|
---|
| 154 | IAnalyzer clone = analyzer.Clone() as IAnalyzer;
|
---|
| 155 | foreach (IScopeTreeLookupParameter param in clone.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 156 | param.Depth = 0;
|
---|
| 157 | Analyzer.Operators.Add(clone);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[5642] | 163 | void MoveGeneratorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 164 | ChooseMoveOperators();
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[5622] | 167 | private void UpdateMoveOperators(IProblem problem) {
|
---|
[5642] | 168 | IMoveGenerator oldMoveGenerator = MoveGenerator;
|
---|
| 169 | IMoveMaker oldMoveMaker = MoveMaker;
|
---|
| 170 | ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
|
---|
| 171 |
|
---|
| 172 | ClearMoveParameters();
|
---|
| 173 |
|
---|
[5622] | 174 | if (problem != null) {
|
---|
| 175 | foreach (IMoveGenerator generator in problem.Operators.OfType<IMoveGenerator>().OrderBy(x => x.Name))
|
---|
| 176 | MoveGeneratorParameter.ValidValues.Add(generator);
|
---|
| 177 |
|
---|
| 178 | foreach (IMoveMaker maker in problem.Operators.OfType<IMoveMaker>().OrderBy(x => x.Name))
|
---|
| 179 | MoveMakerParameter.ValidValues.Add(maker);
|
---|
| 180 |
|
---|
| 181 | foreach (ISingleObjectiveMoveEvaluator evaluator in problem.Operators.OfType<ISingleObjectiveMoveEvaluator>().OrderBy(x => x.Name))
|
---|
| 182 | MoveEvaluatorParameter.ValidValues.Add(evaluator);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | if (oldMoveGenerator != null) {
|
---|
| 186 | IMoveGenerator newMoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
|
---|
| 187 | if (newMoveGenerator != null) MoveGenerator = newMoveGenerator;
|
---|
| 188 | }
|
---|
| 189 | if (MoveGenerator == null) {
|
---|
| 190 | ClearMoveParameters();
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | if (oldMoveMaker != null) {
|
---|
| 194 | IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
|
---|
| 195 | if (mm != null) MoveMaker = mm;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | if (oldMoveEvaluator != null) {
|
---|
| 199 | ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
|
---|
| 200 | if (me != null) MoveEvaluator = me;
|
---|
[5642] | 201 | }
|
---|
[5622] | 202 | }
|
---|
| 203 |
|
---|
[5642] | 204 | private void ChooseMoveOperators() {
|
---|
| 205 | IMoveMaker oldMoveMaker = MoveMaker;
|
---|
| 206 | ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
|
---|
| 207 |
|
---|
| 208 | if (MoveGenerator != null) {
|
---|
| 209 | List<Type> moveTypes = MoveGenerator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList();
|
---|
| 210 | foreach (Type type in moveTypes.ToList()) {
|
---|
| 211 | if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t)))
|
---|
| 212 | moveTypes.Remove(type);
|
---|
| 213 | }
|
---|
| 214 | List<IMoveMaker> validMoveMakers = new List<IMoveMaker>();
|
---|
| 215 | List<ISingleObjectiveMoveEvaluator> validMoveEvaluators = new List<ISingleObjectiveMoveEvaluator>();
|
---|
| 216 |
|
---|
| 217 | foreach (Type type in moveTypes) {
|
---|
| 218 | var moveMakers = MoveMakerParameter.ValidValues.Where(x => type.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
|
---|
| 219 | foreach (IMoveMaker moveMaker in moveMakers)
|
---|
| 220 | validMoveMakers.Add(moveMaker);
|
---|
| 221 |
|
---|
| 222 | var moveEvaluators = MoveEvaluatorParameter.ValidValues.Where(x => type.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
|
---|
| 223 | foreach (ISingleObjectiveMoveEvaluator moveEvaluator in moveEvaluators)
|
---|
| 224 | validMoveEvaluators.Add(moveEvaluator);
|
---|
| 225 | }
|
---|
| 226 | if (oldMoveMaker != null) {
|
---|
| 227 | IMoveMaker mm = validMoveMakers.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
|
---|
| 228 | if (mm != null) MoveMaker = mm;
|
---|
| 229 | else MoveMaker = validMoveMakers.FirstOrDefault();
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | if (oldMoveEvaluator != null) {
|
---|
| 233 | ISingleObjectiveMoveEvaluator me = validMoveEvaluators.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
|
---|
| 234 | if (me != null) MoveEvaluator = me;
|
---|
| 235 | else MoveEvaluator = validMoveEvaluators.FirstOrDefault();
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[5622] | 240 | private void ClearMoveParameters() {
|
---|
[5642] | 241 | MoveGeneratorParameter.ValidValues.Clear();
|
---|
| 242 | MoveMakerParameter.ValidValues.Clear();
|
---|
| 243 | MoveEvaluatorParameter.ValidValues.Clear();
|
---|
[5622] | 244 | }
|
---|
| 245 |
|
---|
[5642] | 246 | private void ParameterizeMoveGenerators(ISingleObjectiveProblem problem) {
|
---|
| 247 | if (problem != null) {
|
---|
| 248 | foreach (IMultiMoveGenerator generator in problem.Operators.OfType<IMultiMoveGenerator>())
|
---|
| 249 | generator.SampleSizeParameter.ActualName = SampleSizeParameter.Name;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | private void ParameterizeMoveEvaluators(ISingleObjectiveProblem problem) {
|
---|
| 253 | foreach (ISingleObjectiveMoveEvaluator op in problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
|
---|
| 254 | op.QualityParameter.ActualName = problem.Evaluator.QualityParameter.ActualName;
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | private void ParameterizeMoveMakers(ISingleObjectiveProblem problem) {
|
---|
| 258 | foreach (IMoveMaker op in problem.Operators.OfType<IMoveMaker>()) {
|
---|
| 259 | op.QualityParameter.ActualName = problem.Evaluator.QualityParameter.ActualName;
|
---|
| 260 | if (MoveEvaluator != null)
|
---|
| 261 | op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[5622] | 265 | public override IOperation Apply() {
|
---|
| 266 | Scope subScope = new Scope();
|
---|
| 267 | Scope individual = new Scope();
|
---|
| 268 |
|
---|
| 269 | foreach (Variable var in ExecutionContext.Scope.Variables) {
|
---|
| 270 | individual.Variables.Add(var);
|
---|
| 271 | }
|
---|
| 272 | subScope.SubScopes.Add(individual);
|
---|
| 273 |
|
---|
| 274 | ExecutionContext.Scope.SubScopes.Add(subScope);
|
---|
| 275 | int index = subScope.Parent.SubScopes.IndexOf(subScope);
|
---|
| 276 |
|
---|
| 277 | SubScopesProcessor processor = new SubScopesProcessor();
|
---|
| 278 | SubScopesRemover remover = new SubScopesRemover();
|
---|
| 279 |
|
---|
| 280 | remover.RemoveAllSubScopes = false;
|
---|
| 281 | remover.SubScopeIndexParameter.Value = new IntValue(index);
|
---|
| 282 |
|
---|
| 283 | for (int i = 0; i < index; i++) {
|
---|
| 284 | processor.Operators.Add(new EmptyOperator());
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[5751] | 287 | VariableCreator variableCreator = new VariableCreator();
|
---|
| 288 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("LocalIterations", new IntValue(0)));
|
---|
| 289 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestLocalQuality", new DoubleValue(0)));
|
---|
| 290 |
|
---|
| 291 | variableCreator.Successor = loop;
|
---|
| 292 |
|
---|
[5622] | 293 | loop.EvaluatedMovesParameter.ActualName = EvaluatedSolutionsParameter.ActualName;
|
---|
| 294 |
|
---|
[5751] | 295 | processor.Operators.Add(variableCreator);
|
---|
[5622] | 296 | processor.Successor = remover;
|
---|
| 297 |
|
---|
| 298 | IOperation next = base.Apply();
|
---|
| 299 | if (next as ExecutionContext != null) {
|
---|
| 300 | remover.Successor = (next as ExecutionContext).Operator;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[5735] | 303 | return ExecutionContext.CreateChildOperation(processor);
|
---|
[5622] | 304 | }
|
---|
| 305 | }
|
---|
| 306 | }
|
---|