Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VNS/HeuristicLab.Algorithms.VariableNeighborhoodSearch/3.3/LocalSearchImprovement.cs @ 5609

Last change on this file since 5609 was 5609, checked in by svonolfe, 14 years ago

Worked on VNS main loop (#1425)

File size: 7.6 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Operators;
29using HeuristicLab.Common;
30using HeuristicLab.Parameters;
31using HeuristicLab.Algorithms.LocalSearch;
32using HeuristicLab.Data;
33using HeuristicLab.Optimization;
34
35namespace HeuristicLab.Algorithms.VariableNeighborhoodSearch {
36  /// <summary>
37  /// A local search improvement operator.
38  /// </summary>
39  [Item("LocalSearchImprovement", "A local search improvement operator.")]
40  [StorableClass]
41  public class LocalSearchImprovement: SingleSuccessorOperator, ILocalImprovement {
42    [Storable]
43    private LocalSearchMainLoop loop;
44   
45    private /*Constrained*/ValueParameter<IMoveGenerator> MoveGeneratorParameter {
46      get { return (/*Constrained*/ValueParameter<IMoveGenerator>)Parameters["MoveGenerator"]; }
47    }
48    private /*Constrained*/ValueParameter<IMoveMaker> MoveMakerParameter {
49      get { return (/*Constrained*/ValueParameter<IMoveMaker>)Parameters["MoveMaker"]; }
50    }
51    private /*Constrained*/ValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter {
52      get { return (/*Constrained*/ValueParameter<ISingleObjectiveMoveEvaluator>)Parameters["MoveEvaluator"]; }
53    }
54    private ValueParameter<IntValue> MaximumIterationsParameter {
55      get { return (ValueParameter<IntValue>)Parameters["MaximumIterations"]; }
56    }
57    private ValueParameter<IntValue> SampleSizeParameter {
58      get { return (ValueParameter<IntValue>)Parameters["SampleSize"]; }
59    }
60    public LookupParameter<IntValue> EvaluatedMovesParameter {
61      get { return (LookupParameter<IntValue>)Parameters["EvaluatedMoves"]; }
62    }
63
64    public IMoveGenerator MoveGenerator {
65      get { return MoveGeneratorParameter.Value; }
66      set { MoveGeneratorParameter.Value = value; }
67    }
68    public IMoveMaker MoveMaker {
69      get { return MoveMakerParameter.Value; }
70      set { MoveMakerParameter.Value = value; }
71    }
72    public ISingleObjectiveMoveEvaluator MoveEvaluator {
73      get { return MoveEvaluatorParameter.Value; }
74      set { MoveEvaluatorParameter.Value = value; }
75    }
76
77    [StorableConstructor]
78    protected LocalSearchImprovement(bool deserializing) : base(deserializing) { }
79    protected LocalSearchImprovement(LocalSearchImprovement original, Cloner cloner)
80      : base(original, cloner) {
81        this.loop = cloner.Clone(original.loop);
82    }
83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new LocalSearchImprovement(this, cloner);
85    }
86    public LocalSearchImprovement()
87      : base() {
88        loop = new LocalSearchMainLoop();
89
90        Parameters.Add(new /*Constrained*/ValueParameter<IMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution."));
91        Parameters.Add(new /*Constrained*/ValueParameter<IMoveMaker>("MoveMaker", "The operator used to perform a move."));
92        Parameters.Add(new /*Constrained*/ValueParameter<ISingleObjectiveMoveEvaluator>("MoveEvaluator", "The operator used to evaluate a move."));
93        Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(1000)));
94        Parameters.Add(new ValueParameter<IntValue>("SampleSize", "Number of moves that MultiMoveGenerators should create. This is ignored for Exhaustive- and SingleMoveGenerators.", new IntValue(100)));
95        Parameters.Add(new LookupParameter<IntValue>("EvaluatedMoves", "The number of evaluated moves."));
96    }
97
98    public void Parameterize(IProblem problem) {
99      UpdateMoveOperators(problem);
100    }
101
102    private void UpdateMoveOperators(IProblem problem) {
103     /* IMoveGenerator oldMoveGenerator = MoveGenerator;
104      MoveGeneratorParameter.ValidValues.Clear();
105      if (problem != null) {
106        foreach (IMoveGenerator generator in problem.Operators.OfType<IMoveGenerator>().OrderBy(x => x.Name))
107          MoveGeneratorParameter.ValidValues.Add(generator);
108
109        foreach (IMoveMaker maker in problem.Operators.OfType<IMoveMaker>().OrderBy(x => x.Name))
110          MoveMakerParameter.ValidValues.Add(maker);
111
112        foreach (ISingleObjectiveMoveEvaluator evaluator in problem.Operators.OfType<ISingleObjectiveMoveEvaluator>().OrderBy(x => x.Name))
113          MoveEvaluatorParameter.ValidValues.Add(evaluator);
114      }
115
116      if (oldMoveGenerator != null) {
117        IMoveGenerator newMoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
118        if (newMoveGenerator != null) MoveGenerator = newMoveGenerator;
119      }
120      if (MoveGenerator == null) {
121        ClearMoveParameters();
122      }
123
124      IMoveMaker oldMoveMaker = MoveMaker;
125      if (oldMoveMaker != null) {
126        IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
127        if (mm != null) MoveMaker = mm;
128      }
129
130      ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
131      if (oldMoveEvaluator != null) {
132        ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
133        if (me != null) MoveEvaluator = me;
134      }*/
135    }
136
137    private void ClearMoveParameters() {
138      /*MoveMakerParameter.ValidValues.Clear();
139      MoveEvaluatorParameter.ValidValues.Clear();*/
140    }
141
142    public override IOperation Apply() {
143      Scope subScope = new Scope();
144      Scope individual = new Scope();
145
146      foreach (Variable var in ExecutionContext.Scope.Variables) {
147        individual.Variables.Add(var);
148      }
149      subScope.SubScopes.Add(individual);
150
151      ExecutionContext.Scope.SubScopes.Add(subScope);
152      int index = subScope.Parent.SubScopes.IndexOf(subScope);
153
154      SubScopesProcessor processor = new SubScopesProcessor();
155      SubScopesRemover remover = new SubScopesRemover();
156
157      remover.RemoveAllSubScopes = false;
158      remover.SubScopeIndexParameter.Value = new IntValue(index);
159
160      for (int i = 0; i < index; i++) {
161        processor.Operators.Add(new EmptyOperator());
162      }
163
164      loop.MoveGeneratorParameter.Value = MoveGeneratorParameter.Value;
165      loop.MoveEvaluatorParameter.Value = MoveEvaluatorParameter.Value;
166      loop.MoveMakerParameter.Value = MoveMakerParameter.Value;
167      loop.MaximumIterationsParameter.Value = MaximumIterationsParameter.Value;
168
169      processor.Operators.Add(loop);
170      processor.Successor = remover;
171
172      IOperation next = base.Apply();
173      if (next as ExecutionContext != null) {
174        remover.Successor = (next as ExecutionContext).Operator;
175      }
176
177      return ExecutionContext.CreateOperation(processor);
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.