Skip to content

Commit

Permalink
fix depth
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Nov 12, 2023
1 parent 901040c commit 3caf9b5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 30 deletions.
17 changes: 14 additions & 3 deletions planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"sync"
)

const maxDepth = 100

// Action represents an action that can be performed.
type Action interface {

Expand All @@ -33,6 +35,14 @@ func Plan(start, goal *State, actions []Action) ([]Action, error) {
for heap.Len() > 0 {
current, _ := heap.Pop()

/*fmt.Printf("- (%d) %s, cost=%v, heuristic=%v, total=%v\n",
current.depth, current.action,
current.stateCost, current.heuristic, current.totalCost)*/

if current.depth >= maxDepth {
return reconstructPlan(current), nil
}

// If we reached the goal, reconstruct the path.
done, err := current.Match(goal)
switch {
Expand Down Expand Up @@ -68,14 +78,15 @@ func Plan(start, goal *State, actions []Action) ([]Action, error) {
newState.action = action
newState.heuristic = heuristic
newState.stateCost = newCost
newState.totalCost = newCost * heuristic
newState.totalCost = newCost + heuristic
newState.depth = current.depth + 1
heap.Push(newState)

// In any of those cases, we need to release the new state
case found && !node.visited && newCost < node.stateCost:
node.parent = current
node.stateCost = newCost
node.totalCost = newCost * node.heuristic
node.totalCost = newCost + node.heuristic
heap.Fix(node) // Update the node's position in the heap
fallthrough
default: // The new state is already visited or the newCost is higher
Expand All @@ -89,7 +100,7 @@ func Plan(start, goal *State, actions []Action) ([]Action, error) {

// reconstructPlan reconstructs the plan from the goal node to the start node.
func reconstructPlan(goalNode *State) []Action {
plan := make([]Action, 0, int(goalNode.index))
plan := make([]Action, 0, int(goalNode.depth))
for n := goalNode; n != nil; n = n.parent {
if n.action != nil { // The start node has no action
plan = append(plan, n.action)
Expand Down
3 changes: 3 additions & 0 deletions planner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func TestNumericPlan(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, []string{"Forage", "Forage", "Forage", "Sleep", "Forage", "Forage", "Sleep", "Forage", "Forage", "Forage", "Sleep", "Forage"},
planOf(plan))

//assert.Fail(t, "xxx")
}

func TestMaze(t *testing.T) {
Expand All @@ -113,6 +115,7 @@ func TestMaze(t *testing.T) {
"J->K", "K->L", "L->M", "M->N", "N->O", "O->P", "P->Q", "Q->R", "R->S", "S->T", "T->U", "U->V",
"V->W", "W->X", "X->Y", "Y->Z"},
planOf(plan))
//assert.Fail(t, "xxx")
}

func TestWeightedPlan(t *testing.T) {
Expand Down
16 changes: 6 additions & 10 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type node struct {
stateCost float32 // Cost from the start state to this state
totalCost float32 // Sum of cost and heuristic
index int // Index of the state in the heap
depth int // Depth of the state in the tree
visited bool // Whether the state was visited
}

Expand Down Expand Up @@ -248,30 +249,25 @@ func (state *State) Distance(goal *State) (diff float32) {
case opEqual:
switch {
case v < x:
diff += (x - v) / x
diff += (x - v)
case v > x:
diff += (v - x) / (100 - x)
diff += (v - x)
default: // v == x
}

case opLess:
if v > x {
diff += (v - x) / (100 - x)
diff += (v - x)
}

case opGreater:
if v < x {
diff += (x - v) / x
diff += (x - v)
}
}
}

if diff == 0 || len(goal.vx) == 0 {
return 0
}

// Normalize the difference by the number of elements
return diff / float32(len(goal.vx))
return diff
}

// Equals returns true if the state is equal to the other state.
Expand Down
34 changes: 17 additions & 17 deletions state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,27 @@ func TestDistance(t *testing.T) {
expect float32
}{
{[]string{"A"}, []string{"A"}, 0},
{[]string{"A=100"}, []string{"A=10"}, 1.0},
{[]string{"A=100"}, []string{"A=90"}, 1.0},
{[]string{"A=25"}, []string{"A=50"}, 0.50},
{[]string{"A=0"}, []string{"A=50"}, 1.0},
{[]string{"A=75"}, []string{"A=50"}, 0.5},
{[]string{"A"}, []string{"B"}, 1.0},
{[]string{"A"}, []string{"A", "B"}, .50},
{[]string{"A=100"}, []string{"A=10"}, 90},
{[]string{"A=100"}, []string{"A=90"}, 10},
{[]string{"A=25"}, []string{"A=50"}, 25},
{[]string{"A=0"}, []string{"A=50"}, 50},
{[]string{"A=75"}, []string{"A=50"}, 25},
{[]string{"A"}, []string{"B"}, 100},
{[]string{"A"}, []string{"A", "B"}, 100},
{[]string{"A", "B"}, []string{"A"}, 0},
{[]string{"A", "B"}, []string{"C", "D"}, 1.0},
{[]string{"A", "B"}, []string{"C", "D"}, 200},
{[]string{"A", "B"}, []string{"A", "B"}, 0},
{[]string{"A", "B"}, []string{"A", "B", "C"}, .33},
{[]string{"A", "B", "C"}, []string{"D", "B"}, .50},
{[]string{"A=20"}, []string{"B=10"}, 1.0},
{[]string{"A=20"}, []string{"B=70"}, 1.0},
{[]string{"A=20", "C=40"}, []string{"B=5"}, 1.0},
{[]string{"A=5", "C=40"}, []string{"A=10", "E=40"}, 0.75},
{[]string{"A", "B"}, []string{"A", "B", "C"}, 100},
{[]string{"A", "B", "C"}, []string{"D", "B"}, 100},
{[]string{"A=20"}, []string{"B=10"}, 10},
{[]string{"A=20"}, []string{"B=70"}, 70},
{[]string{"A=20", "C=40"}, []string{"B=5"}, 5},
{[]string{"A=5", "C=40"}, []string{"A=10", "E=40"}, 45},
{[]string{"A=10"}, []string{}, 0},
{[]string{}, []string{"A=10"}, 1.0},
{[]string{}, []string{"A=10"}, 10},
{[]string{"A=10"}, []string{"A<50"}, 0},
{[]string{"A=75"}, []string{"A<50"}, .50},
{[]string{"A=10"}, []string{"A>50"}, .80},
{[]string{"A=75"}, []string{"A<50"}, 25},
{[]string{"A=10"}, []string{"A>50"}, 40},
{[]string{"A=70"}, []string{"A>50"}, 0},
}

Expand Down

0 comments on commit 3caf9b5

Please sign in to comment.