Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis SolutionEnsembles/HeuristicLab.Algorithms.VariableNeighborhoodSearch/3.3/ShakingOperator.cs @ 5867

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

Integrated VNS into trunk (#1425)

File size: 3.3 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.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Common;
31using HeuristicLab.Data;
32using HeuristicLab.Parameters;
33
34namespace HeuristicLab.Algorithms.VariableNeighborhoodSearch {
35  /// <summary>
36  /// A shaking operator for VNS.
37  /// </summary>
38  [Item("ShakingOperator", "A shaking operator for VNS.")]
39  [StorableClass]
40  public class ShakingOperator<T> : CheckedMultiOperator<T>, IShakingOperator where T : class, IManipulator {
41    public IValueLookupParameter<IntValue> IndexParameter {
42      get { return (ValueLookupParameter<IntValue>)Parameters["Index"]; }
43    }
44   
45    public ILookupParameter<BoolValue> ContinueParameter {
46      get { return (LookupParameter<BoolValue>)Parameters["Continue"]; }
47    }
48       
49    [StorableConstructor]
50    protected ShakingOperator(bool deserializing) : base(deserializing) { }
51    protected ShakingOperator(ShakingOperator<T> original, Cloner cloner) : base(original, cloner) { }
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new ShakingOperator<T>(this, cloner);
54    }
55    public ShakingOperator()
56      : base() {
57      Parameters.Add(new ValueLookupParameter<IntValue>("Index", "The index of the operator that should be applied (k)."));
58      Parameters.Add(new LookupParameter<BoolValue>("Continue", "True if k <= available operators."));
59    }
60
61    public void OnProblemChanged(IProblem problem) {
62      Operators.Clear();
63
64      if (problem != null) {
65        foreach (T manipulator in problem.Operators.OfType<T>().OrderBy(x => x.Name))
66          Operators.Add(manipulator);
67      }
68    }
69
70    public override IOperation Apply() {
71      int index = IndexParameter.ActualValue.Value;   
72      var operators = base.Operators.CheckedItems.ToList();
73
74      IOperator successor = null;
75
76      if (index >= operators.Count - 1) {
77        ContinueParameter.ActualValue = new BoolValue(false);
78      } else {
79        ContinueParameter.ActualValue = new BoolValue(true);
80      }
81
82      if (index >= 0 && index < operators.Count) {
83        successor = operators[index].Value;
84      }
85
86      OperationCollection next = new OperationCollection(base.Apply());
87      if (successor != null)
88        next.Insert(0, ExecutionContext.CreateOperation(successor));
89
90      return next;
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.