1 | using HeuristicLab.Core;
|
---|
2 | using HeuristicLab.Data.MoveVectorData.Interfaces;
|
---|
3 | using System;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Text;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Data.MoveVectorData.Moves
|
---|
8 | {
|
---|
9 | public struct TwoPointMove : IMove
|
---|
10 | {
|
---|
11 | public bool Enabled { get; set; }
|
---|
12 | public int pickup;
|
---|
13 | public int putdown;
|
---|
14 |
|
---|
15 | public override string ToString()
|
---|
16 | {
|
---|
17 | StringBuilder sb = new StringBuilder();
|
---|
18 | if (Enabled) sb.Append("("); else sb.Append("<");
|
---|
19 | sb.Append(pickup.ToString() + "," + putdown.ToString());
|
---|
20 | if (Enabled) sb.Append(")"); else sb.Append(">");
|
---|
21 | return sb.ToString();
|
---|
22 | }
|
---|
23 |
|
---|
24 | public bool TryParse(string s)
|
---|
25 | {
|
---|
26 | bool enable = false;
|
---|
27 | if (s.StartsWith("(") && s.EndsWith(")"))
|
---|
28 | {
|
---|
29 | enable = true;
|
---|
30 | }
|
---|
31 | else if (s.StartsWith("<") && s.EndsWith(">"))
|
---|
32 | {
|
---|
33 | enable = false;
|
---|
34 | }
|
---|
35 | else {
|
---|
36 | return false;
|
---|
37 | }
|
---|
38 | string s1 = s.Substring(1, s.Length - 1);
|
---|
39 | string[] valueStrings = s1.Split(',');
|
---|
40 | if (valueStrings.Length != 2)
|
---|
41 | {
|
---|
42 | return false;
|
---|
43 | }
|
---|
44 | int valPickup;
|
---|
45 | if (!int.TryParse(valueStrings[0], out valPickup))
|
---|
46 | {
|
---|
47 | return false;
|
---|
48 | }
|
---|
49 | int valPutdown;
|
---|
50 | if (!int.TryParse(valueStrings[1], out valPutdown))
|
---|
51 | {
|
---|
52 | return false;
|
---|
53 | }
|
---|
54 | this.Enabled = enable;
|
---|
55 | this.pickup = valPickup;
|
---|
56 | this.putdown = valPutdown;
|
---|
57 | return true;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void Randomize(IRandom random, int min, int max, int step = 1)
|
---|
61 | {
|
---|
62 | int numbers = (int)Math.Floor((max - min) / (double)step);
|
---|
63 | do
|
---|
64 | {
|
---|
65 | pickup = random.Next(numbers) * step + min;
|
---|
66 | putdown = random.Next(numbers) * step + min;
|
---|
67 | } while (pickup == putdown);
|
---|
68 | Enabled = true;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public int Apply(ref StackingArea area, ref Stack inputStack, out int realMoves)
|
---|
72 | {
|
---|
73 | if (!Enabled)
|
---|
74 | {
|
---|
75 | realMoves = 0;
|
---|
76 | return 0;
|
---|
77 | }
|
---|
78 | if (area[putdown].CurrentHeight < area[putdown].Length &&
|
---|
79 | area[pickup].CurrentHeight > 0)
|
---|
80 | {
|
---|
81 | int pick = area[pickup].Pop();
|
---|
82 | area[putdown].Push(pick);
|
---|
83 | realMoves = 1;
|
---|
84 | return 2;
|
---|
85 | }
|
---|
86 | realMoves = 0;
|
---|
87 | return 1;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public IEnumerable<IMove> GenerateNeighbours(IRandom random, int min, int max, int step = 1)
|
---|
91 | {
|
---|
92 | var moves = new List<IMove>();
|
---|
93 | moves.Add(new TwoPointMove() { Enabled = !this.Enabled, putdown = this.putdown, pickup = this.pickup });
|
---|
94 |
|
---|
95 | /*
|
---|
96 | int numbers = (int)Math.Floor((max - min) / (double)step);
|
---|
97 | int newPickup;
|
---|
98 | int newPutdown;
|
---|
99 | do
|
---|
100 | {
|
---|
101 | newPickup = random.Next(numbers) * step + min;
|
---|
102 | putdown = random.Next(numbers) * step + min;
|
---|
103 | } while (newPickup == putdown);
|
---|
104 | do
|
---|
105 | {
|
---|
106 | pickup = random.Next(numbers) * step + min;
|
---|
107 | newPutdown = random.Next(numbers) * step + min;
|
---|
108 | } while (pickup == newPutdown);
|
---|
109 |
|
---|
110 | moves.Add(new TwoPointMove() { Enabled = true, putdown = newPutdown, pickup = this.pickup });
|
---|
111 | moves.Add(new TwoPointMove() { Enabled = true, putdown = this.putdown, pickup = newPickup });
|
---|
112 | */
|
---|
113 |
|
---|
114 | for(int i = min; i < max; i++)
|
---|
115 | {
|
---|
116 | moves.Add(new TwoPointMove() { Enabled = true, putdown = i, pickup = this.pickup });
|
---|
117 | moves.Add(new TwoPointMove() { Enabled = true, putdown = this.putdown, pickup = i });
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*
|
---|
121 | if (putdown + step <= max)
|
---|
122 | moves.Add(new TwoPointMove() { Enabled = true, putdown = this.putdown + step, pickup = this.pickup });
|
---|
123 | if (putdown - step >= min)
|
---|
124 | moves.Add(new TwoPointMove() { Enabled = true, putdown = this.putdown - step, pickup = this.pickup });
|
---|
125 | if (pickup + step <= max)
|
---|
126 | moves.Add(new TwoPointMove() { Enabled = true, putdown = this.putdown, pickup = this.pickup + step });
|
---|
127 | if (pickup - step >= min)
|
---|
128 | moves.Add(new TwoPointMove() { Enabled = true, putdown = this.putdown, pickup = this.pickup - step });*/
|
---|
129 | return moves;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|