同一个函数中同名的python3 var是differen

2024-04-25 02:17:35 发布

您现在位置:Python中文网/ 问答频道 /正文

让我们简短一点

下面是代码,我有WorldState和Action的类,这里使用Action类属性名,WorldState不满和next\u 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

我正在为人工智能制定一个面向目标的行动计划, 在上面的代码中,best_action总是以none返回,我已经尝试过调试,它通过循环内部传递出去,best_action已经被填充,并且best_value + best_plan也没有被修改,就像循环内外有两个不同的变量一样。我不明白代码里发生了什么,我错过了什么?你知道吗


Tags: 代码trueverboseifvalueactioncurrentnext
1条回答
网友
1楼 · 发布于 2024-04-25 02:17:35

结果发现,当能量耗尽时,我的最佳动作在最后一个循环中没有返回任何结果(显然我不能一直找到最佳动作)。在返回之前,我只需要检查BestAction是否是None。你知道吗

相关问题 更多 >