Beispiel #1
0
func New(initialState, finalState State) *graph.Problem {
	return &graph.Problem{
		Actions,
		heuristic,
		graph.NewCost(1<<31 - 1),
		initialState,
		func(s graph.State) bool { return s.(State) == finalState },
		Result,
		StepCost,
		graph.NewCost(0)}
}
Beispiel #2
0
func New(from City) *graph.Problem {
	return &graph.Problem{
		Actions: func(s graph.State) []graph.Action {
			rv := []graph.Action{}
			for k := range edge[s.(City)] {
				rv = append(rv, k)
			}
			return rv
		},
		Heuristic:    func(s graph.State) graph.Cost { return graph.NewCost(toBucharest[s.(City)]) },
		Infinity:     graph.NewCost(1<<31 - 1),
		InitialState: from,
		IsGoal:       func(s graph.State) bool { return s.(City) == Bucharest },
		Result:       func(s graph.State, a graph.Action) graph.State { return a.(City) },
		StepCost: func(s graph.State, a graph.Action) graph.Cost {
			return graph.NewCost(edge[s.(City)][a.(City)])
		},
		Zero: graph.NewCost(0),
	}
}
Beispiel #3
0
// Function h returns a lower bound on cost of solving the puzzle.
// Each piece must move at least the manhatten distance to its correct position.
func heuristic(_s graph.State) graph.Cost {
	manhatten := [9][9]int{
		{0, 1, 2, 1, 2, 3, 2, 3, 4}, {1, 0, 1, 2, 1, 2, 3, 2, 3}, {2, 1, 0, 3, 2, 1, 4, 3, 2},
		{1, 2, 3, 0, 1, 2, 1, 2, 3}, {2, 1, 2, 1, 0, 1, 2, 1, 2}, {3, 2, 1, 2, 1, 0, 3, 2, 1},
		{2, 3, 4, 1, 2, 3, 0, 1, 2}, {3, 2, 3, 2, 1, 2, 1, 0, 1}, {4, 3, 2, 3, 2, 1, 2, 1, 0}}
	ts := _s.(State)
	c := 0
	for i := uint(0); i < 9; i++ {
		tile := ts.Get(i)
		if tile != 0 {
			c += manhatten[i][tile]
		}
	}
	return graph.NewCost(c)
}
Beispiel #4
0
func StepCost(s graph.State, a graph.Action) graph.Cost {
	return graph.NewCost(1)
}