-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.go
49 lines (44 loc) · 997 Bytes
/
agent.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
package goap
type Agent struct {
Actions []Action
WorldState State
Goals State
}
func (a *Agent) possibleActions() []Action {
validActions := []Action{}
for _, action := range a.Actions {
if action.CanRun(*a) {
validActions = append(validActions, action)
}
}
return validActions
}
func (a *Agent) goalsMet() bool {
return a.WorldState.Contains(a.Goals)
}
func (a *Agent) GetPlans(currentPlan Plan) []Plan {
results := []Plan{}
for _, action := range a.possibleActions() {
newPlan := append(currentPlan, action)
newAgent, _ := action.Run(*a)
if newAgent.goalsMet() {
results = append(results, newPlan)
} else {
results = append(results, newAgent.GetPlans(newPlan)...)
}
}
return results
}
func (a *Agent) GetBestPlan() (Plan, int) {
plans := a.GetPlans(Plan{})
bestPlan := Plan{}
bestCost := 99999
for _, plan := range plans {
cost := plan.Cost()
if cost < bestCost {
bestPlan = plan
bestCost = cost
}
}
return bestPlan, bestCost
}