Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/Manipulators/SwapItemManipulator.cs @ 15936

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

#2640: Updated year of copyrights in license headers

File size: 3.1 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.LinearLinkageEncoding {
30  [Item("Swap Item Manipulator", "Performs N swaps operations of two items each. The same items may be swapped multiple times, at least two groups need to be present.")]
31  [StorableClass]
32  public sealed class SwapItemManipulator : LinearLinkageManipulator {
33
34    public IValueLookupParameter<IntValue> NParameter {
35      get { return (IValueLookupParameter<IntValue>)Parameters["N"]; }
36    }
37
38    [StorableConstructor]
39    private SwapItemManipulator(bool deserializing) : base(deserializing) { }
40    private SwapItemManipulator(SwapItemManipulator original, Cloner cloner) : base(original, cloner) { }
41    public SwapItemManipulator() {
42      Parameters.Add(new ValueLookupParameter<IntValue>("N", "The number of swaps to perform.", new IntValue(1)));
43    }
44    public SwapItemManipulator(int n)
45      : this() {
46      NParameter.Value = new IntValue(n);
47    }
48
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new SwapItemManipulator(this, cloner);
51    }
52
53    public static void Apply(IRandom random, LinearLinkage lle, int n) {
54      var grouping = lle.GetGroups().Select(x => x.ToList()).ToList();
55      if (grouping.Count == 1) return; // nothing can be changed
56
57      var prevGroup = random.Next(grouping.Count);
58      var prevItem = random.Next(grouping[prevGroup].Count);
59      for (var i = 0; i < n; i++) {
60        int nextGroup, nextItem;
61        do {
62          nextGroup = random.Next(grouping.Count);
63          nextItem = random.Next(grouping[nextGroup].Count);
64        } while (nextGroup == prevGroup);
65        var h = grouping[nextGroup][nextItem];
66        grouping[nextGroup][nextItem] = grouping[prevGroup][prevItem];
67        grouping[prevGroup][prevItem] = h;
68        prevGroup = nextGroup;
69        prevItem = nextItem;
70      }
71
72      lle.SetGroups(grouping);
73    }
74
75    protected override void Manipulate(IRandom random, LinearLinkage lle) {
76      var N = NParameter.ActualValue.Value;
77      Apply(random, lle, N);
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.