-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (68 loc) · 1.56 KB
/
main.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
package main
import (
"fmt"
mapset "github.com/deckarep/golang-set/v2"
"log"
)
var state = mapset.NewSet(
on('c', 'a'),
on('a', '1'),
on('b', '3'),
clear('c'),
clear('b'),
clear('2'),
clear('4'),
)
func main() {
goals := mapset.NewSet(
on('a', 'b'),
on('b', 'c'),
)
//printState(state)
//run(goals)
NewGoalRegression(state, goals).Run()
//printState(state)
}
func run(goals mapset.Set[Predicate]) {
if goals.IsSubset(state) {
return
}
// 1. select goal
unsolvedGoals := goals.Difference(state)
goal := unsolvedGoals.ToSlice()[0]
// 2. select best action
actions := goal.GenerateActions()
maxCompleted := 0
var bestAction Action
for action := range actions.Iter() {
completed := action.Can().Intersect(state).Cardinality()
if bestAction == nil || completed > maxCompleted {
maxCompleted = completed
bestAction = action
}
}
if bestAction == nil {
log.Fatal("no actions left")
}
// 3. make action possible
run(bestAction.Can())
// 4. execute action
state = state.Union(bestAction.Adds())
state = state.Difference(bestAction.Deletes())
// 5. return to step 1. if non completed goals exist
run(goals)
}
func printState(state mapset.Set[Predicate]) {
world := DisplayWorld{}
world.Apply(state)
fmt.Println()
world.Print()
fmt.Println()
}
func validateState(state mapset.Set[Predicate]) {
for predicate := range state.Iter() {
if !state.Intersect(predicate.InvalidPredicates()).IsEmpty() {
log.Fatalf("invalid state: %v in not possible because of: %v\n", predicate, state.Intersect(predicate.InvalidPredicates()))
}
}
}