1 | using HeuristicLab.Core;
|
---|
2 | using HeuristicLab.Data.MoveVectorData.Interfaces;
|
---|
3 | using System;
|
---|
4 | using System.Text;
|
---|
5 | using System.Collections.Generic;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Data.MoveVectorData.Moves
|
---|
8 | {
|
---|
9 | public struct IngoingMove : IMove
|
---|
10 | {
|
---|
11 | public bool Enabled { get; set; }
|
---|
12 | public int putdown;
|
---|
13 |
|
---|
14 | public override string ToString()
|
---|
15 | {
|
---|
16 | StringBuilder sb = new StringBuilder();
|
---|
17 | if (Enabled) sb.Append("(i"); else sb.Append("<i");
|
---|
18 | sb.Append(putdown.ToString());
|
---|
19 | if (Enabled) sb.Append(")"); else sb.Append(">");
|
---|
20 | return sb.ToString();
|
---|
21 | }
|
---|
22 |
|
---|
23 | public bool TryParse(string s)
|
---|
24 | {
|
---|
25 | bool enable = false;
|
---|
26 | if (s.StartsWith("(i") && s.EndsWith(")"))
|
---|
27 | {
|
---|
28 | enable = true;
|
---|
29 | }
|
---|
30 | else if (s.StartsWith("<i") && s.EndsWith(">"))
|
---|
31 | {
|
---|
32 | enable = false;
|
---|
33 | }
|
---|
34 | else {
|
---|
35 | return false;
|
---|
36 | }
|
---|
37 | string[] valueStrings = s.Substring(2, s.Length - 1).Split(' ');
|
---|
38 | int val;
|
---|
39 | if (!int.TryParse(valueStrings[0], out val))
|
---|
40 | {
|
---|
41 | return false;
|
---|
42 | }
|
---|
43 | putdown = val;
|
---|
44 | if (!int.TryParse(valueStrings[1], out val))
|
---|
45 | {
|
---|
46 | return false;
|
---|
47 | }
|
---|
48 | Enabled = enable;
|
---|
49 | return true;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void Randomize(IRandom random, int min, int max, int step = 1)
|
---|
53 | {
|
---|
54 | int numbers = (int)Math.Floor((max - min) / (double)step);
|
---|
55 | putdown = random.Next(numbers) * step + min;
|
---|
56 | Enabled = true;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public int Apply(ref StackingArea area, ref Stack inputStack, out int realMoves)
|
---|
60 | {
|
---|
61 | if (Enabled && area[putdown].CurrentHeight < area[putdown].Length)
|
---|
62 | {
|
---|
63 | area[putdown].Push(inputStack.Pop());
|
---|
64 | realMoves = 1;
|
---|
65 | return 1;
|
---|
66 | }
|
---|
67 | realMoves = 0;
|
---|
68 | return 0;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public IEnumerable<IMove> GenerateNeighbours(IRandom random, int min, int max, int step = 1)
|
---|
72 | {
|
---|
73 | var moves = new List<IMove>();
|
---|
74 | moves.Add(new IngoingMove() { Enabled = !this.Enabled, putdown = this.putdown });
|
---|
75 | if(putdown + step <= max)
|
---|
76 | moves.Add(new IngoingMove() { Enabled = true, putdown = this.putdown + step });
|
---|
77 | if (putdown - step >= min)
|
---|
78 | moves.Add(new IngoingMove() { Enabled = true, putdown = this.putdown - step });
|
---|
79 | return moves;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|