Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5603 was 5603, checked in by svonolfe, 13 years ago

Added first version of VNS main loop (WIP) #1425

File size: 6.8 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        loop.ResultsParameter.Value = new VariableCollection();
90
91        Parameters.Add(new /*Constrained*/ValueParameter<IMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution."));
92        Parameters.Add(new /*Constrained*/ValueParameter<IMoveMaker>("MoveMaker", "The operator used to perform a move."));
93        Parameters.Add(new /*Constrained*/ValueParameter<ISingleObjectiveMoveEvaluator>("MoveEvaluator", "The operator used to evaluate a move."));
94        Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(1000)));
95        Parameters.Add(new ValueParameter<IntValue>("SampleSize", "Number of moves that MultiMoveGenerators should create. This is ignored for Exhaustive- and SingleMoveGenerators.", new IntValue(100)));
96        Parameters.Add(new LookupParameter<IntValue>("EvaluatedMoves", "The number of evaluated moves."));
97    }
98
99    public void Parameterize(IProblem problem) {
100      UpdateMoveOperators(problem);
101    }
102
103    private void UpdateMoveOperators(IProblem problem) {
104     /* IMoveGenerator oldMoveGenerator = MoveGenerator;
105      MoveGeneratorParameter.ValidValues.Clear();
106      if (problem != null) {
107        foreach (IMoveGenerator generator in problem.Operators.OfType<IMoveGenerator>().OrderBy(x => x.Name))
108          MoveGeneratorParameter.ValidValues.Add(generator);
109
110        foreach (IMoveMaker maker in problem.Operators.OfType<IMoveMaker>().OrderBy(x => x.Name))
111          MoveMakerParameter.ValidValues.Add(maker);
112
113        foreach (ISingleObjectiveMoveEvaluator evaluator in problem.Operators.OfType<ISingleObjectiveMoveEvaluator>().OrderBy(x => x.Name))
114          MoveEvaluatorParameter.ValidValues.Add(evaluator);
115      }
116
117      if (oldMoveGenerator != null) {
118        IMoveGenerator newMoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
119        if (newMoveGenerator != null) MoveGenerator = newMoveGenerator;
120      }
121      if (MoveGenerator == null) {
122        ClearMoveParameters();
123      }
124
125      IMoveMaker oldMoveMaker = MoveMaker;
126      if (oldMoveMaker != null) {
127        IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
128        if (mm != null) MoveMaker = mm;
129      }
130
131      ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
132      if (oldMoveEvaluator != null) {
133        ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
134        if (me != null) MoveEvaluator = me;
135      }*/
136    }
137
138    private void ClearMoveParameters() {
139      /*MoveMakerParameter.ValidValues.Clear();
140      MoveEvaluatorParameter.ValidValues.Clear();*/
141    }
142
143    public override IOperation Apply() {
144      Scope subScope = new Scope();
145      subScope.Parent = ExecutionContext.Scope;
146
147      IAtomicOperation op = ExecutionContext.CreateChildOperation(loop, subScope);
148      op.Operator.Execute((IExecutionContext)op, CancellationToken);
149
150      ExecutionContext.Scope.SubScopes.Remove(subScope);
151
152      return base.Apply();
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.