Free cookie consent management tool by TermsFeed Policy Generator

source: branches/StackingProblems/HeuristicLab.Data.MoveVectorData/3.3/Moves/OutgoingMove.cs @ 14278

Last change on this file since 14278 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: 3.3 KB
Line 
1using HeuristicLab.Core;
2using HeuristicLab.Data.MoveVectorData.Interfaces;
3using System;
4using System.Collections.Generic;
5using System.Text;
6
7namespace HeuristicLab.Data.MoveVectorData.Moves
8{
9    public struct OutgoingMove : IMove
10    {
11        public bool Enabled { get; set; }
12
13        public override string ToString()
14        {
15            StringBuilder sb = new StringBuilder();
16            if (Enabled) sb.Append("(o"); else sb.Append("<o");
17            if (Enabled) sb.Append(")"); else sb.Append(">");
18            return sb.ToString();
19        }
20
21        public bool TryParse(string s)
22        {
23            bool enable = false;
24            if (s.StartsWith("(o") && s.EndsWith(")"))
25            {
26                enable = true;
27            }
28            else if (s.StartsWith("<o") && s.EndsWith(">"))
29            {
30                enable = false;
31            }
32            else {
33                return false;
34            }
35            Enabled = enable;
36            return true;
37        }
38
39        public void Randomize(IRandom random, int min, int max, int step = 1)
40        {
41            Enabled = true;
42        }
43
44        public int Apply(ref StackingArea area, ref Stack inputStack, out int realMoves)
45        {
46            if (Enabled)
47            {
48                var minValue = Int32.MaxValue;
49                var minStack = Int32.MaxValue;
50                var minRow = Int32.MaxValue;
51                for (int stack = 0; stack < area.Size; stack++)
52                {
53                    for (int row = area.MaxHeight - 1; row >= 0; row--)
54                    {
55                        if(area[row, stack] != 0 && area[row, stack] < minValue)
56                        {
57                            minValue = area[row, stack];
58                            minStack = stack;
59                            minRow = row;
60                        }
61                    }
62                }
63
64                for (int stack = 0; stack < inputStack.Capacity; stack++)
65                {
66                    if (inputStack[stack] != 0 && inputStack[stack] < minValue)
67                    {
68                        realMoves = 0;
69                        return 1;
70                    }
71                }
72
73                if (minValue == Int32.MaxValue)
74                {
75                    realMoves = 0;
76                    return 1;
77                }
78
79                area[minRow, minStack] = 0;
80                int atop = 0;
81                for (int row = minRow; row < area.MaxHeight; row++)
82                {
83                    if (row < area.MaxHeight - 1)
84                    {
85                        area[row, minStack] = area[row + 1, minStack];
86                    } else
87                    {
88                        area[row, minStack] = 0;
89                    }
90                    atop = atop + 1;
91                }
92                realMoves = atop * 2 + 1;
93                return atop * 2 + 1;
94            }
95            realMoves = 0;
96            return 0;
97        }
98
99        public IEnumerable<IMove> GenerateNeighbours(IRandom random, int min, int max, int step = 1)
100        {
101            var moves = new List<IMove>();
102            moves.Add(new OutgoingMove() { Enabled = !this.Enabled, });
103            return moves;
104        }
105    }
106}
Note: See TracBrowser for help on using the repository browser.