Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Manipulators/MoveItemManipulator.cs @ 16659

Last change on this file since 16659 was 14711, checked in by gkronber, 8 years ago

#2520

  • renamed StorableClass -> StorableType
  • changed persistence to use GUIDs instead of type names
File size: 3.2 KB
RevLine 
[13368]1#region License Information
[12286]2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.LinearLinkageEncoding {
[12396]31  [Item("Move Item Manipulator", "Performs a maximum of N move operations between groups or to new groups. An already moved item may be moved again.")]
[14711]32  [StorableType("D3152965-D6B9-4615-8492-6CB753431163")]
[12286]33  public sealed class MoveItemManipulator : LinearLinkageManipulator {
34
35    public IValueLookupParameter<IntValue> NParameter {
36      get { return (IValueLookupParameter<IntValue>)Parameters["N"]; }
37    }
38
39    [StorableConstructor]
40    private MoveItemManipulator(bool deserializing) : base(deserializing) { }
41    private MoveItemManipulator(MoveItemManipulator original, Cloner cloner) : base(original, cloner) { }
42    public MoveItemManipulator() {
43      Parameters.Add(new ValueLookupParameter<IntValue>("N", "The number of items to move.", new IntValue(1)));
44    }
45    public MoveItemManipulator(int n)
46      : this() {
47      NParameter.Value = new IntValue(n);
48    }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new MoveItemManipulator(this, cloner);
52    }
53
54    public static void Apply(IRandom random, LinearLinkage lle, int n) {
55      var grouping = lle.GetGroups().ToList();
56      grouping.Add(new List<int>()); // add an empty group
57
58      for (var i = 0; i < n; i++) {
59        var src = random.Next(grouping.Count - 1); // only select from non-empty groups
60        var dest = random.Next(grouping.Count);
61        while (src == dest || grouping[src].Count == 1 && dest == grouping.Count - 1) {
62          src = random.Next(grouping.Count - 1);
63          dest = random.Next(grouping.Count);
64        }
65        if (dest == grouping.Count - 1) grouping.Add(new List<int>());
66
67        var e = random.Next(grouping[src].Count);
68        grouping[dest].Add(grouping[src][e]);
69        grouping[src].RemoveAt(e);
70        if (grouping[src].Count == 0)
71          grouping.RemoveAt(src);
72      }
73
74      lle.SetGroups(grouping.Where(x => x.Count > 0));
75    }
76
77    protected override void Manipulate(IRandom random, LinearLinkage lle) {
78      var N = NParameter.ActualValue.Value;
79      Apply(random, lle, N);
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.