Ejemplo n.º 1
0
def randomized_maxvalue(problem, limit=100, callback=None):
    seed(42)
    current = LSNode(problem, problem.initial, 0)
    best = current
    for _ in range(limit):
        if callback is not None:
            callback(current)
        successors = list(current.expand())
        list_of_best = []
        i = 0
        while (i < 5):
            ind = successors.index(max(successors, key=lambda node: node.state))
            list_of_best.append(successors[ind])
            successors.pop(ind)
            i += 1
        current = choice(list_of_best)
        if current.state > best.state:
            best = current
    return best
Ejemplo n.º 2
0
def maxvalue(problem, limit=100, callback=None):
    """Perform a random walk in the search space and return the best solution
    found. The returned value is a Node.
    If callback is not None, it must be a one-argument function that will be
    called at each step with the current node.
    """
    current = LSNode(problem, problem.initial, 0)
    best_of_best = current
    #best.v = current.state.value
    for _ in range(limit):
        #best.v = -float('inf')
        if callback is not None:
            callback(current)
        successors = list(current.expand())
        ind = successors.index(max(successors, key=lambda node: node.state))
        current = successors[ind]
        if current.state > best_of_best.state:
            best_of_best = current
    #return current
    return best_of_best
Ejemplo n.º 3
0
def randomized_maxvalue(problem, limit=100, callback=None):
    seed(42)
    current = LSNode(problem, problem.initial, 0)
    best = current
    for _ in range(limit):
        if callback is not None:
            callback(current)
        successors = list(current.expand())
        list_of_best = []
        i = 0
        while (i < 5):
            ind = successors.index(max(successors,
                                       key=lambda node: node.state))
            list_of_best.append(successors[ind])
            successors.pop(ind)
            i += 1
        current = choice(list_of_best)
        if current.state > best.state:
            best = current
    return best
Ejemplo n.º 4
0
def maxvalue(problem, limit=100, callback=None):
    """Perform a random walk in the search space and return the best solution
    found. The returned value is a Node.
    If callback is not None, it must be a one-argument function that will be
    called at each step with the current node.
    """
    current = LSNode(problem, problem.initial, 0)
    best_of_best = current
    #best.v = current.state.value
    for _ in range(limit):
        #best.v = -float('inf')
        if callback is not None:
            callback(current)
        successors = list(current.expand())
        ind = successors.index(max(successors, key=lambda node: node.state))
        current = successors[ind]
        if current.state > best_of_best.state:
            best_of_best = current
    #return current
    return best_of_best