def h1(n, goalState): state = n.state currentPosition = {} goalPosition = {} # Get current position of all elements on puzzle. for i in range(len(state)): for j in range(len(state[i])): if state[i][j] != 0: currentPosition[state[i][j]] = [i, j] # Get goal position of all elements on puzzle. for i in range(len(goalState)): for j in range(len(goalState[i])): if goalState[i][j] != 0: goalPosition[goalState[i][j]] = [i, j] # Calculate manhattan distances. manhattanDistanceSum = 0 for i in range(1, getSizeFromState(state) + 1): # will put N instead of 8 x = currentPosition[i] y = goalPosition[i] manhattanDistanceSum += abs(x[0] - y[0]) + abs(x[1] - y[1]) # Return heuristic estimation value. return manhattanDistanceSum
def __init__(self, size, wantTextInput, inputFile, wantDifficulty, steps): """Sets initial state and goal. States are representated as a 2D matrix filled with N-1 numbers and 1""" if wantTextInput: self.startState = getInitialStateFromText(open(inputFile, 'r')) self.n = getSizeFromState(self.startState) gen = puzzleGenerator(self.n) elif wantDifficulty: gen = puzzleGenerator(size) self.n = size self.startState = gen.generateNStepsState(steps) else: gen = puzzleGenerator(size) self.n = size self.startState = gen.initial self.goalState = gen.goal super(puzzle, self).__init__(self.startState, self.goalState)
def __init__(self, size, wantTextInput, inputFile, wantDifficulty, steps): """Sets initial state and goal. States are representated as a 2D matrix filled with N-1 numbers and 1""" if wantTextInput: self.startState = getInitialStateFromText(open(inputFile, "r")) self.n = getSizeFromState(self.startState) gen = puzzleGenerator(self.n) elif wantDifficulty: gen = puzzleGenerator(size) self.n = size self.startState = gen.generateNStepsState(steps) else: gen = puzzleGenerator(size) self.n = size self.startState = gen.initial self.goalState = gen.goal super(puzzle, self).__init__(self.startState, self.goalState)