[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 |
|
---|
| 46 | private /*Constrained*/ValueParameter<IMoveGenerator> MoveGeneratorParameter {
|
---|
| 47 | get { return (/*Constrained*/ValueParameter<IMoveGenerator>)Parameters["MoveGenerator"]; }
|
---|
| 48 | }
|
---|
| 49 | private /*Constrained*/ValueParameter<IMoveMaker> MoveMakerParameter {
|
---|
| 50 | get { return (/*Constrained*/ValueParameter<IMoveMaker>)Parameters["MoveMaker"]; }
|
---|
| 51 | }
|
---|
| 52 | private /*Constrained*/ValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter {
|
---|
| 53 | get { return (/*Constrained*/ValueParameter<ISingleObjectiveMoveEvaluator>)Parameters["MoveEvaluator"]; }
|
---|
| 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]
|
---|
| 82 | protected LocalSearchImprovement(bool deserializing) : base(deserializing) { }
|
---|
| 83 | protected LocalSearchImprovement(LocalSearchImprovement original, Cloner cloner)
|
---|
| 84 | : base(original, cloner) {
|
---|
| 85 | this.loop = cloner.Clone(original.loop);
|
---|
| 86 | }
|
---|
| 87 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 88 | return new LocalSearchImprovement(this, cloner);
|
---|
| 89 | }
|
---|
| 90 | public LocalSearchImprovement()
|
---|
| 91 | : base() {
|
---|
| 92 | loop = new LocalSearchMainLoop(new BoolValue(true));
|
---|
| 93 |
|
---|
| 94 | Parameters.Add(new /*Constrained*/ValueParameter<IMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution."));
|
---|
| 95 | Parameters.Add(new /*Constrained*/ValueParameter<IMoveMaker>("MoveMaker", "The operator used to perform a move."));
|
---|
| 96 | Parameters.Add(new /*Constrained*/ValueParameter<ISingleObjectiveMoveEvaluator>("MoveEvaluator", "The operator used to evaluate a move."));
|
---|
| 97 | Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
| 98 | Parameters.Add(new ValueParameter<IntValue>("SampleSize", "Number of moves that MultiMoveGenerators should create. This is ignored for Exhaustive- and SingleMoveGenerators.", new IntValue(100)));
|
---|
| 99 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated moves."));
|
---|
| 100 | Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution."));
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public void Parameterize(IProblem problem) {
|
---|
| 104 | UpdateMoveOperators(problem);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | private void UpdateMoveOperators(IProblem problem) {
|
---|
| 108 | /* IMoveGenerator oldMoveGenerator = MoveGenerator;
|
---|
| 109 | MoveGeneratorParameter.ValidValues.Clear();
|
---|
| 110 | if (problem != null) {
|
---|
| 111 | foreach (IMoveGenerator generator in problem.Operators.OfType<IMoveGenerator>().OrderBy(x => x.Name))
|
---|
| 112 | MoveGeneratorParameter.ValidValues.Add(generator);
|
---|
| 113 |
|
---|
| 114 | foreach (IMoveMaker maker in problem.Operators.OfType<IMoveMaker>().OrderBy(x => x.Name))
|
---|
| 115 | MoveMakerParameter.ValidValues.Add(maker);
|
---|
| 116 |
|
---|
| 117 | foreach (ISingleObjectiveMoveEvaluator evaluator in problem.Operators.OfType<ISingleObjectiveMoveEvaluator>().OrderBy(x => x.Name))
|
---|
| 118 | MoveEvaluatorParameter.ValidValues.Add(evaluator);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | if (oldMoveGenerator != null) {
|
---|
| 122 | IMoveGenerator newMoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
|
---|
| 123 | if (newMoveGenerator != null) MoveGenerator = newMoveGenerator;
|
---|
| 124 | }
|
---|
| 125 | if (MoveGenerator == null) {
|
---|
| 126 | ClearMoveParameters();
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | IMoveMaker oldMoveMaker = MoveMaker;
|
---|
| 130 | if (oldMoveMaker != null) {
|
---|
| 131 | IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
|
---|
| 132 | if (mm != null) MoveMaker = mm;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
|
---|
| 136 | if (oldMoveEvaluator != null) {
|
---|
| 137 | ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
|
---|
| 138 | if (me != null) MoveEvaluator = me;
|
---|
| 139 | }*/
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | private void ClearMoveParameters() {
|
---|
| 143 | /*MoveMakerParameter.ValidValues.Clear();
|
---|
| 144 | MoveEvaluatorParameter.ValidValues.Clear();*/
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | public override IOperation Apply() {
|
---|
| 148 | Scope subScope = new Scope();
|
---|
| 149 | Scope individual = new Scope();
|
---|
| 150 |
|
---|
| 151 | foreach (Variable var in ExecutionContext.Scope.Variables) {
|
---|
| 152 | individual.Variables.Add(var);
|
---|
| 153 | }
|
---|
| 154 | subScope.SubScopes.Add(individual);
|
---|
| 155 |
|
---|
| 156 | ExecutionContext.Scope.SubScopes.Add(subScope);
|
---|
| 157 | int index = subScope.Parent.SubScopes.IndexOf(subScope);
|
---|
| 158 |
|
---|
| 159 | SubScopesProcessor processor = new SubScopesProcessor();
|
---|
| 160 | SubScopesRemover remover = new SubScopesRemover();
|
---|
| 161 |
|
---|
| 162 | remover.RemoveAllSubScopes = false;
|
---|
| 163 | remover.SubScopeIndexParameter.Value = new IntValue(index);
|
---|
| 164 |
|
---|
| 165 | for (int i = 0; i < index; i++) {
|
---|
| 166 | processor.Operators.Add(new EmptyOperator());
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | loop.MoveGeneratorParameter.Value = MoveGeneratorParameter.Value;
|
---|
| 170 | if (loop.MoveGeneratorParameter.Value != null && loop.MoveGeneratorParameter.Value.Parameters.ContainsKey("SampleSize")) {
|
---|
| 171 | IParameter parameter = loop.MoveGeneratorParameter.Value.Parameters["SampleSize"];
|
---|
| 172 | if(parameter is IValueParameter)
|
---|
| 173 | (parameter as IValueParameter).Value = SampleSizeParameter.Value;
|
---|
| 174 | }
|
---|
| 175 | loop.MoveEvaluatorParameter.Value = MoveEvaluatorParameter.Value;
|
---|
| 176 | loop.MoveMakerParameter.Value = MoveMakerParameter.Value;
|
---|
| 177 | loop.MaximumIterationsParameter.Value = MaximumIterationsParameter.Value;
|
---|
| 178 | loop.EvaluatedMovesParameter.ActualName = EvaluatedSolutionsParameter.ActualName;
|
---|
| 179 | if (AnalyzerParameter.ActualValue != null) {
|
---|
| 180 | loop.AnalyzerParameter.Value = AnalyzerParameter.ActualValue.Clone() as IAnalyzer;
|
---|
| 181 |
|
---|
| 182 | foreach (IScopeTreeLookupParameter param in loop.AnalyzerParameter.Value.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 183 | param.Depth = 0;
|
---|
| 184 |
|
---|
| 185 | if (loop.AnalyzerParameter.Value is IMultiAnalyzer) {
|
---|
| 186 | foreach (IAnalyzer analyzer in (loop.AnalyzerParameter.Value as IMultiAnalyzer).Operators) {
|
---|
| 187 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 188 | param.Depth = 0;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | } else {
|
---|
| 192 | loop.AnalyzerParameter.Value = null;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | processor.Operators.Add(loop);
|
---|
| 196 | processor.Successor = remover;
|
---|
| 197 |
|
---|
| 198 | IOperation next = base.Apply();
|
---|
| 199 | if (next as ExecutionContext != null) {
|
---|
| 200 | remover.Successor = (next as ExecutionContext).Operator;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | return ExecutionContext.CreateOperation(processor);
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 | }
|
---|