/
Beam.cs
111 lines (94 loc) · 3.25 KB
/
Beam.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TurboTank
{
public class Beam
{
public int CandidateCount = 1;
public TankAction StartAction;
private const int MaxSize = 10;
EvalState[] bestStates = new EvalState[MaxSize];
List<EvalState> candidates = new List<EvalState>();
public Beam(TankOperation operation, Grid grid, SignalWeights weights)
{
StartAction = operation.GetAction();
EvalState startingState = new EvalState(operation.GetAction(), grid);
bestStates[0] = operation.GetScore(startingState, weights);
}
public void Add(EvalState candidateState)
{
CandidateCount++;
if (candidateState.Score >= 0)
{
candidates.Add(candidateState);
}
}
private const int MinSuccessfulScore = 700;
public bool StillSearching()
{
return (bestStates[0].Score > 0 && bestStates[0].Score < MinSuccessfulScore);
}
public bool WasSuccessful()
{
return (bestStates[0].Score >= MinSuccessfulScore);
}
public void Evaluate()
{
foreach (var candidateState in candidates)
{
if (bestStates[MaxSize - 1] == null || candidateState.Score > bestStates[MaxSize - 1].Score)
{
for (int pos = 0; pos < MaxSize; pos++)
{
if (bestStates[pos] == null)
{
bestStates[pos] = candidateState;
break;
}
else if (candidateState.Score > bestStates[pos].Score)
{
for (int movePos = MaxSize - 1; movePos > pos; movePos--)
{
bestStates[movePos] = bestStates[movePos - 1];
}
bestStates[pos] = candidateState;
break;
}
else if (pos > 0 && bestStates[pos].Evaluated)
{
for (int deletePos = pos; deletePos < MaxSize; deletePos++)
{
bestStates[deletePos] = null;
}
bestStates[pos] = candidateState;
break;
}
}
}
}
candidates.Clear();
}
public IEnumerable<EvalState> Iterate()
{
for (int pos = 0; pos < MaxSize; pos++)
{
if (bestStates[pos] != null && !bestStates[pos].Evaluated)
{
bestStates[pos].Evaluated = true;
yield return bestStates[pos];
}
}
}
public EvalState GetBest()
{
return bestStates[0];
}
public override string ToString()
{
return bestStates[0].ToString();
}
}
}