Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2605_StackingProblems/HeuristicLab.Encodings.MoveVectorEncoding/3.3/Manipulators/SingleMoveShiftManipulator.cs @ 17800

Last change on this file since 17800 was 14278, checked in by mzehetho, 8 years ago

Initial Commit for ticket #2605

Implemented:

  • Encoding
  • Moves
  • Views
  • Blocks World Problem
  • Blocks Relocation Problem
  • Stacking Problem
File size: 2.0 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Core;
3using HeuristicLab.Data.MoveVectorData;
4using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5
6namespace 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}
Note: See TracBrowser for help on using the repository browser.