1 | using HeuristicLab.Common;
|
---|
2 | using HeuristicLab.Core;
|
---|
3 | using HeuristicLab.Data.MoveVectorData;
|
---|
4 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Encodings.MoveVectorEncoding.Manipulators
|
---|
7 | {
|
---|
8 | [Item("SingleMoveShiftManipulator", " Shifts one move to a different position.")]
|
---|
9 | [StorableClass]
|
---|
10 | public class SingleMoveShiftManipulator : MoveVectorManipulator
|
---|
11 | {
|
---|
12 |
|
---|
13 | [StorableConstructor]
|
---|
14 | protected SingleMoveShiftManipulator(bool deserializing) : base(deserializing) { }
|
---|
15 | protected SingleMoveShiftManipulator(SingleMoveShiftManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
16 |
|
---|
17 | public SingleMoveShiftManipulator() : base() { }
|
---|
18 |
|
---|
19 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
20 | {
|
---|
21 | return new SingleMoveShiftManipulator(this, cloner);
|
---|
22 | }
|
---|
23 |
|
---|
24 | public static void Apply(IRandom random, MoveVector moves)
|
---|
25 | {
|
---|
26 | Manipulate(random, moves, random.Next(moves.Length));
|
---|
27 | }
|
---|
28 |
|
---|
29 | public static void Manipulate(IRandom random, MoveVector moves, int index)
|
---|
30 | {
|
---|
31 | moves[index].Enabled = true;
|
---|
32 | int shift = 0;
|
---|
33 | do
|
---|
34 | {
|
---|
35 | shift = random.Next(-index, moves.Length - index + 1);
|
---|
36 | } while (shift == 0);
|
---|
37 |
|
---|
38 | var save = moves[index];
|
---|
39 | if (shift > 0)
|
---|
40 | {
|
---|
41 | for(int i = index; i < index + shift; i++)
|
---|
42 | {
|
---|
43 | if (i + 1 == index + shift) moves[index] = save;
|
---|
44 | else moves[i] = moves[i + 1];
|
---|
45 | }
|
---|
46 | } else {
|
---|
47 | for (int i = index; i >= index + shift; i--)
|
---|
48 | {
|
---|
49 | if (i == index + shift) moves[index] = save;
|
---|
50 | else moves[i] = moves[i - 1];
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected override void Manipulate(IRandom random, MoveVector moves)
|
---|
56 | {
|
---|
57 | Apply(random, moves);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|