Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ContextAlgorithms/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/Moves/Swap/StochasticSwap2SingleMoveGenerator.cs @ 15624

Last change on this file since 15624 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.LinearLinkageEncoding {
31  [Item("StochasticSwap2SingleMoveGenerator", "Randomly samples a single from all possible swap-2 moves from a given lle grouping.")]
32  [StorableClass]
33  public class StochasticSwap2SingleMoveGenerator : Swap2MoveGenerator, IStochasticOperator, ISingleMoveGenerator {
34    public ILookupParameter<IRandom> RandomParameter {
35      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
36    }
37
38    [StorableConstructor]
39    protected StochasticSwap2SingleMoveGenerator(bool deserializing) : base(deserializing) { }
40    protected StochasticSwap2SingleMoveGenerator(StochasticSwap2SingleMoveGenerator original, Cloner cloner) : base(original, cloner) { }
41    public StochasticSwap2SingleMoveGenerator()
42      : base() {
43      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
44    }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new StochasticSwap2SingleMoveGenerator(this, cloner);
48    }
49
50    public static Swap2Move Apply(LinearLinkage lle, IRandom random) {
51      int length = lle.Length;
52      if (length < 2) throw new ArgumentException("StochasticSwap2SingleMoveGenerator: There cannot be a swap-2 move given only one item.", "lle");
53
54      var groups = lle.GetGroups().ToList();
55      if (groups.Count == 1) throw new InvalidOperationException("StochasticSwap2SingleMoveGenerator: Swap moves cannot be applied when there is only one group.");
56
57      int index1 = random.Next(groups.Count), index2 = 0;
58      do {
59        index2 = random.Next(length);
60      } while (index1 == index2);
61
62      var item1 = random.Next(groups[index1].Count);
63      var item2 = random.Next(groups[index2].Count);
64
65      return new Swap2Move(groups[index1][item1], groups[index2][item2]);
66    }
67
68    protected override Swap2Move[] GenerateMoves(LinearLinkage lle) {
69      IRandom random = RandomParameter.ActualValue;
70      return new[] { Apply(lle, random) };
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.