Free cookie consent management tool by TermsFeed Policy Generator

source: branches/dataset-ids-2695/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/Moves/EMSS/ShiftMove.cs @ 15529

Last change on this file since 15529 was 15079, checked in by abeham, 7 years ago

#2666: Added move generators for extract, merge, shift, and split moves and renamed those to EMSS moves, some code cleanup

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Encodings.LinearLinkageEncoding {
28  [Item("Shift Move", "Shifts an item from one group to another.")]
29  [StorableClass]
30  public sealed class ShiftMove : EMSSMove {
31    [Storable]
32    private int previousItemOfOldGroup;
33    public int PreviousItemOfOldGroup { get { return previousItemOfOldGroup; } }
34    [Storable]
35    private int previousItemOfNewGroup;
36    public int PreviousItemOfNewGroup { get { return previousItemOfNewGroup; } }
37    [Storable]
38    private int nextItemOfOldGroup;
39    public int NextItemOfOldGroup { get { return nextItemOfOldGroup; } }
40    [Storable]
41    private int nextItemOfNewGroup;
42    public int NextItemOfNewGroup { get { return nextItemOfNewGroup; } }
43
44    private bool IsFirst { get { return PreviousItemOfOldGroup == Item; } }
45    private bool IsLast { get { return NextItemOfOldGroup == Item; } }
46    private bool NewGroupClosed { get { return PreviousItemOfNewGroup == NextItemOfNewGroup; } }
47
48    [StorableConstructor]
49    private ShiftMove(bool deserializing) : base(deserializing) { }
50    private ShiftMove(ShiftMove original, Cloner cloner)
51      : base(original, cloner) {
52      previousItemOfOldGroup = original.previousItemOfOldGroup;
53      previousItemOfNewGroup = original.previousItemOfNewGroup;
54      nextItemOfOldGroup = original.nextItemOfOldGroup;
55      nextItemOfNewGroup = original.nextItemOfNewGroup;
56    }
57    public ShiftMove(int item, int prevOld, int prevNew, int nextOld, int nextNew)
58      : base(item) {
59      previousItemOfOldGroup = prevOld;
60      previousItemOfNewGroup = prevNew;
61      nextItemOfOldGroup = nextOld;
62      nextItemOfNewGroup = nextNew;
63    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new ShiftMove(this, cloner);
67    }
68
69    public override void Apply(LinearLinkage lle) {
70      if ((!IsFirst && lle[PreviousItemOfOldGroup] != Item)
71        || lle[Item] != NextItemOfOldGroup
72        || lle[PreviousItemOfNewGroup] != NextItemOfNewGroup)
73        throw new ArgumentException("Move conditions have changed!");
74      if (!IsFirst)
75        lle[PreviousItemOfOldGroup] = IsLast ? PreviousItemOfOldGroup : NextItemOfOldGroup;
76      lle[PreviousItemOfNewGroup] = Item;
77      lle[Item] = NewGroupClosed ? Item : NextItemOfNewGroup;
78    }
79
80    public override void Undo(LinearLinkage lle) {
81      if (!IsFirst && lle[PreviousItemOfOldGroup] != (IsLast ? PreviousItemOfOldGroup : NextItemOfOldGroup)
82        || lle[PreviousItemOfNewGroup] != Item
83        || lle[Item] != (NewGroupClosed ? Item : NextItemOfNewGroup))
84        throw new ArgumentException("Move conditions have changed, cannot undo move.");
85
86      if (!IsFirst)
87        lle[PreviousItemOfOldGroup] = Item;
88      lle[PreviousItemOfNewGroup] = NextItemOfNewGroup;
89      lle[Item] = NextItemOfOldGroup;
90    }
91
92    public override void ApplyToLLEb(int[] lleb) {
93      if (!IsLast)
94        lleb[NextItemOfOldGroup] = IsFirst ? NextItemOfOldGroup : PreviousItemOfOldGroup;
95      lleb[Item] = PreviousItemOfNewGroup;
96      if (!NewGroupClosed)
97        lleb[NextItemOfNewGroup] = Item;
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.