Ejemplo n.º 1
0
def hill_climbing(problem, h, rand_cur=None):
    """From the initial node, keep choosing the neighbor with highest value,
    stopping when no neighbor is better. """
    if rand_cur is None:
        current = Node(problem.initial)
    else:
        current = rand_cur
    goal = problem.goal['index']  # goal index
    # path plus hueristic function
    h_lam = lambda node: h(node.state['index'], goal, problem.dim_val)
    while True:
        if h_lam(current) == h_lam(Node(problem.goal)):
            break
        neighbors = current.expand(problem)
        if not neighbors:
            break
        neighbor = argmin_random_tie(neighbors, key=h_lam)
        if h_lam(neighbor) <= h_lam(current):
            break
        current = neighbor
    if not problem.goal_test(current.state):
        rand_i = choice(list(range(problem.dim_val * problem.dim_val)))
        rand_state = dict(index=rand_i,
                          char_val=problem.maze_char_list[rand_i])
        hill_climbing(problem, h, rand_cur=Node(rand_state))
    return current.depth, None, current.solution(), current.path_cost
Ejemplo n.º 2
0
def hill_climbing(problem):
    """From the initial node, keep choosing the neighbor with highest value,
    stopping when no neighbor is better. [Figure 4.2]"""
    current = Node(problem.initial)
    while True:
        neighbors = current.expand(problem)
        if not neighbors:
            break
        """***************changed neighbor = argmax_random_tie(neighbors,
                                     key=lambda node: problem.value(node.state))****************************"""
        neighbor = argmin_random_tie(neighbors,
                                     key=lambda node: problem.value(node.state))
        if problem.value(neighbor.state) >= problem.value(current.state):
            break
	   # print (current)
        current = neighbor
        print (current)
    return current.state
Ejemplo n.º 3
0
def min_conflicts_value(csp, var, current):
	"""Return the value that will give var the least number of conflicts.
	If there is a tie, choose at random."""
	return argmin_random_tie(csp.domains[var],
							 key=lambda val: csp.nconflicts(var, val, current))
Ejemplo n.º 4
0
def mrv(assignment, csp):
	"Minimum-remaining-values heuristic."
	return argmin_random_tie(
		[v for v in csp.variables if v not in assignment],
		key=lambda var: num_legal_values(csp, var, assignment))
Ejemplo n.º 5
0
def min_conflicts_value(csp, var, current):
    """Return the value that will give var the least number of conflicts.
    If there is a tie, choose at random."""
    return argmin_random_tie(csp.domains[var],
                             key=lambda val: csp.nconflicts(var, val, current))
Ejemplo n.º 6
0
def mrv(assignment, csp):
    "Minimum-remaining-values heuristic."
    return argmin_random_tie(
        [v for v in csp.variables if v not in assignment],
        key=lambda var: num_legal_values(csp, var, assignment))
Ejemplo n.º 7
0
def hill_climbing_search(problem):
    """
    Search for a local minimum following a certain optimization function
    The function used for this problem was the distance function from the
    actual point to the goal point. Which is the same used for our second heuristics informed search.
    """
    # we use these two variables at the time of visualisations
    iterations = 0
    all_node_colors = []
    node_colors = {
        k: 'white'
        for k in set(problem.reachable_positions(problem.initial))
    }

    frontier = [(Node(problem.initial))]
    explored = set()

    # modify the color of frontier nodes to orange
    node_colors[Node(problem.initial).state] = "orange"
    iterations += 1
    all_node_colors.append(dict(node_colors))

    while True:
        # Popping first node of stack
        node = frontier.pop()

        # modify the currently searching node to red
        node_colors[node.state] = "red"
        iterations += 1
        all_node_colors.append(dict(node_colors))

        neighbors = node.expand(problem)
        if not neighbors:
            break
        # find the minimum between the neighbors
        neighbor = argmin_random_tie(neighbors,
                                     key=lambda node: problem.h2(node))

        # trying to find minimum
        if problem.h2(neighbor) > problem.h2(node):
            # node is already the local minimum
            node_colors[node.state] = "green"
            all_node_colors.append(dict(node_colors))
            return iterations, all_node_colors, node

        # expand only for painting
        frontier.extend(
            child for child in node.expand(problem)
            if child.state not in explored and child not in frontier)

        for n in frontier:
            # modify the color of frontier nodes to orange
            node_colors[n.state] = "orange"
            all_node_colors.append(dict(node_colors))

        frontier.clear()
        frontier.append(neighbor)  # append the last

        explored.add(node.state)

        # modify the color of explored nodes to gray
        node_colors[node.state] = "gray"

    return None
Ejemplo n.º 8
0
 def mrv(self, assignment):
     return argmin_random_tie(
         [v for v in self.X if v not in assignment],
         key=lambda var: self.num_legal_values(var, assignment))
Ejemplo n.º 9
0
 def min_conflict_value(self, var, current):
     return argmin_random_tie(
         self.D[var], key=lambda val: self.nconflicts(var, val, current))
Ejemplo n.º 10
0
def mrv(assignment, csp):
    return argmin_random_tie(
        [v for v in csp.variables if v not in assignment],
        key=lambda var: num_legal_val(csp, var, assignment))