同一个具有相同名称的函数内的python3 var是不同的

时间:2017-05-20 03:02:39

标签: python python-3.x python-3.4

让它缩短

这里是代码,我有WorldState和Action类,这里使用的是Action类属性NAME,还使用了WorldState discontentment和next_action方法

def choose_action_plan(max_depth):
# storage for world stage and action
states = [[WorldState(goals, actions, costs), Action('base')]]

# keep track of current best actions
best_action = None
best_value = 10000
best_plan = []

verbose = True

if verbose:
    print('Searching...')

changed = True

while states:
    current_value = states[-1][0].discontentment()

    if verbose and changed:
        print(states[-1][1].name + ' [' + str(current_value) + ']')

    if len(states) >= max_depth:
        # if current value is best (low) keep it!
        if current_value < best_value:
            best_action = states[1][1]
            best_value = current_value
            best_plan = [state[1].name for state in states if state[1]] + [best_value]
        states.pop()
        continue

    next_action = states[-1][0].next_action()
    if next_action:
        new_state = deepcopy(states[-1][0])
        states.append([new_state, None])
        states[-1][1] = Action(next_action)
        # apply action
        new_state.apply_action(next_action)
        changed = True
    else:
        # drop back down a level
        states.pop()


# Return the "best action"
return best_action.name

我正在为AI创建面向目标的行动计划, 在上面的代码中best_action总是以none形式返回,我已经尝试调试,它已经在已填充best_action的循环内部传递,并且best_value + best_plan也未被修改,它就像在循环外和循环内有2个不同的变量。我不明白代码中发生了什么,我错过了什么?

1 个答案:

答案 0 :(得分:0)

事实证明,当能量耗尽时,我最好的动作在最后一个循环中没有返回(显然我无法一直找到最佳动作)。我只需要在退回之前检查BestAction是否为None