-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDAstar.go
147 lines (143 loc) · 3.74 KB
/
IDAstar.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
package main
import (
"fmt"
"math/rand"
)
var moves [6]string
var max [3]int
var result [5]string
func (env *Env) idAstar() {
moves = [6]string{"F", "R", "L", "U", "D", "B"}
max = [3]int{13, 13, 15} // max dept limits
var closedList []CubeEnv
closedList = append(closedList, env.currentCube)
phase := 0
threshold := globalHeuristic(env.currentCube, phase)
for {
tmpThres, _ := env.search(threshold, &closedList, &phase, 0)
if tmpThres == -1 {
var finalResult string
for _, res := range result {
finalResult += res
}
finalResult = parseOutput(finalResult)
fmt.Println(finalResult)
return
} else if tmpThres == -2 || tmpThres >= 10000 {
return
}
threshold = tmpThres
}
}
func (env *Env) search(threshold int, closedList *[]CubeEnv, phase *int, depth int) (int, *[]CubeEnv) {
// Handle dept limit
depth++
if *phase < 3 && depth >= max[*phase] {
// reset cube and result
env.currentCube = env.startCube
result = [5]string{}
// generate new random steps
for i := 0; i < 5; i++ {
randIdx := rand.Intn(6)
env.execStep(moves[randIdx])
result[0] += moves[randIdx] + " "
}
if env.debug {
fmt.Println("RESET AND MIX MORE")
debugCube(env.currentCube.cube)
fmt.Println(result)
}
env.idAstar()
return -2, closedList
}
// IDAstar threshold
currCube := (*closedList)[len(*closedList)-1]
if currCube.heuristic+currCube.cost > threshold {
return currCube.heuristic + currCube.cost, closedList
}
// Phases transitions
if *phase == 0 && isInG1(currCube) == 0 {
for _, step := range (*closedList)[1:len(*closedList)] {
result[*phase] += step.internationalMove + " "
}
*phase = 1
currCube.cost = 0
threshold = isInG2(currCube)
if env.debug {
fmt.Println("Phase0 DONE")
debugCube(currCube.cube)
debugPathIDA(*closedList, (*closedList)[len(*closedList)-1])
fmt.Print("\n")
}
*closedList = (*closedList)[len(*closedList)-1 : len(*closedList)]
}
if *phase == 1 && isInG2(currCube) == 0 {
for _, step := range (*closedList)[1:len(*closedList)] {
result[*phase] += step.internationalMove + " "
}
*phase = 2
currCube.cost = 0
threshold = isInG3(currCube)
if env.debug {
fmt.Println("Phase1 DONE")
debugCube(currCube.cube)
debugPathIDA(*closedList, (*closedList)[len(*closedList)-1])
fmt.Print("\n")
}
*closedList = (*closedList)[len(*closedList)-1 : len(*closedList)]
}
if *phase == 2 && isInG3(currCube) == 0 {
for _, step := range (*closedList)[1:len(*closedList)] {
result[*phase] += step.internationalMove + " "
}
*phase = 3
currCube.cost = 0
threshold = isInGc(currCube)
if env.debug {
fmt.Println("Phase2 DONE")
debugCube(currCube.cube)
debugPathIDA(*closedList, (*closedList)[len(*closedList)-1])
fmt.Print("\n")
}
*closedList = (*closedList)[len(*closedList)-1 : len(*closedList)]
}
if isFinished(currCube) {
for i, step := range (*closedList)[1:len(*closedList)] {
result[*phase] += step.internationalMove
if i < len(*closedList)-2 {
result[*phase] += " "
}
}
*phase = 4
if env.debug {
fmt.Println("ALL DONE")
debugCube(currCube.cube)
debugPathIDA(*closedList, (*closedList)[len(*closedList)-1])
if len(*closedList) > 1 {
fmt.Print(" ")
}
fmt.Print("\n")
}
return -1, closedList
}
//IDAstar
min := 100000
for _, child := range getMoves(currCube, *phase) {
if !existInClosedList(child, *closedList) {
*closedList = append(*closedList, child)
result, closedList := env.search(threshold, closedList, phase, depth)
if result == -1 || result == -2 {
return result, closedList
}
if result < min {
min = result
}
if len(*closedList) > 1 {
*closedList = (*closedList)[:len(*closedList)-1]
} else {
return result, closedList
}
}
}
return min, closedList
}