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