Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on VNS main loop (#1425)

File size: 3.0 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: CheckedMultiOperator<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 original, Cloner cloner) : base(original, cloner) { }
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new ShakingOperator(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 override IOperation Apply() {
62      int index = IndexParameter.ActualValue.Value;   
63      var operators = base.Operators.CheckedItems.ToList();
64
65      IOperator successor = null;
66
67      if (index >= operators.Count - 1) {
68        ContinueParameter.ActualValue = new BoolValue(false);
69      } else {
70        ContinueParameter.ActualValue = new BoolValue(true);
71      }
72
73      if (index >= 0 && index < operators.Count) {
74        successor = operators[index].Value;
75      }
76
77      OperationCollection next = new OperationCollection(base.Apply());
78      if (successor != null)
79        next.Insert(0, ExecutionContext.CreateOperation(successor));
80
81      return next;
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.