-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_test.go
158 lines (134 loc) · 2.76 KB
/
agent_test.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package goap
import (
"testing"
)
func Test_agent_possibleactions(t *testing.T) {
armedState := State{
"armed": true,
}
woundedState := State{
"wounded": true,
}
shootAction := &DefaultAction{
conditions: armedState,
effects: State{
"fired": true,
},
}
healAction := &DefaultAction{
conditions: woundedState,
effects: State{
"wounded": false,
},
}
agent := Agent{
Actions: []Action{shootAction, healAction},
WorldState: State{
"armed": true,
},
}
if len(agent.possibleActions()) != 1 {
t.Error("expected single valid action")
}
selectedAction := agent.possibleActions()[0]
if selectedAction != shootAction {
t.Errorf("Expected %v, got %v", shootAction, selectedAction)
}
// Check we get two actions
agent.WorldState.Update(State{
"armed": true,
"wounded": true,
})
if len(agent.possibleActions()) != 2 {
t.Errorf("expected two valid actions, got %d", len(agent.possibleActions()))
}
}
func Test_goalsMet(t *testing.T) {
agent := Agent{
WorldState: State{},
Goals: State{},
}
if !agent.goalsMet() {
t.Error("empty goals are always met")
}
agent.Goals = State{
"armed": true,
}
if agent.goalsMet() {
t.Error("unmet goals are not met")
}
agent.WorldState = State{
"armed": true,
}
if !agent.goalsMet() {
t.Error("met goals are met")
}
}
func Test_GetPlans(t *testing.T) {
armedState := State{
"armed": true,
}
armAction := &DefaultAction{
effects: armedState,
}
waveArmsAction := &DefaultAction{
effects: State{
"wavingArms": true,
},
}
agent := Agent{
Actions: []Action{
armAction,
waveArmsAction,
},
WorldState: State{},
Goals: State{
"armed": true,
},
}
if len(agent.GetPlans(Plan{})) != 2 {
t.Errorf("expected two plans, got %d", len(agent.GetPlans(Plan{})))
}
chosenAction := agent.GetPlans(Plan{})[0][0]
if chosenAction != armAction {
t.Errorf("expected plan to be %v, got %v", armAction, chosenAction)
}
if len(agent.GetPlans(Plan{})[0]) != 1 {
t.Errorf("expected first plan to have 1 action")
}
if len(agent.GetPlans(Plan{})[1]) != 2 {
t.Errorf("expected second plan to have 2 actions")
}
}
func Test_GetBestPlan(t *testing.T) {
armedState := State{
"armed": true,
}
armAction := &DefaultAction{
cost: 2,
effects: armedState,
}
waveArmsAction := &DefaultAction{
cost: 1,
effects: State{
"wavingArms": true,
},
}
agent := Agent{
Actions: []Action{
armAction,
waveArmsAction,
},
WorldState: State{},
Goals: State{
"armed": true,
},
}
bestPlan, cost := agent.GetBestPlan()
if cost != armAction.cost {
t.Errorf("Expected best cost to be %d, got %d", armAction.cost, cost)
}
if bestPlan[0] != armAction || len(bestPlan) != 1 {
t.Errorf("Expected armAction to be best plan, got %v", bestPlan[0])
}
}