我在给定状态中是否遗漏了对动作的检查?

问题:三个传统但嫉妒的夫妻需要过河。每对夫妻由一个丈夫和一个妻子组成。他们找到了一艘小船,船上最多只能容纳两人。找出最简单的过河安排,使所有六个人都能过河,且在没有丈夫在场的情况下,任何女人都不会与任何男人单独相处。假设每次过河前所有乘客都已上船,且每次过河至少要有一人在船上。

应教授要求,编辑删除了代码。

我已经在这个问题上花了6个小时,仍然没有头绪。我的教授很忙,无法提供帮助。


回答:

我仔细查看了你的代码。这确实是一个非常有趣且复杂的问题。经过一段时间后,我意识到可能导致你问题的原因是你是在过河前而不是过河后检查条件。我看到了你提供的模板,我猜我们可以尝试坚持以下逻辑:1- 让动作方法返回所有可能的过河方式(暂时不检查状态);2- 对于每个动作,获取相应的新状态并检查该状态是否有效;3- 让value()方法检查我们是否在优化方面取得进展。

class Problem:    def __init__(self, initial_state, goal):        self.goal = goal        self.record = [[0, initial_state, "LEFT", []]]            # list of results [score][state][boat_side][listActions]    def actions(self, state, boat_side):        side = 0 if boat_side == 'LEFT' else 1        boat_dir = 'RIGHT' if boat_side == 'LEFT' else 'LEFT'        group = [i for i, v in enumerate(state) if v == side]        onboard_2 = [[boat_dir, a, b] for a in group for b in group if             a < b and (     # not the same person and unique group             (a%2==0 and b - a == 1) or (        # wife and husband            a%2==0 and b%2==0) or (             # two wife's            a%2==1 and b%2==1)                  # two husbands        )]        onboard_1 = [[boat_dir, a] for a in group]        return onboard_1 + onboard_2    def result(self, state, action):        new_boat_side = action[0]        new_state = []        for i, v in enumerate(state):            if i in action[1:]:                new_state.append(1 if v == 0 else 0)            else:                new_state.append(v)        # check if invalid        for p, side, in enumerate(new_state):            if p%2 == 0: # is woman                if side != new_state[p+1]: # not with husband                    if any(men == side for men in new_state[1::2]):                        new_state = False                        break        return new_state, new_boat_side    def goal_test(self, state):        return state == self.goal    def value(self, state):        # how many people already crossed        return state.count(1)# optimization processinitial_state = [0]*6goal = [1]*6task = Problem(initial_state, goal)while True:    batch_result = []    for score, state, side, l_a in task.record:        possible_actions = task.actions(state, side)        for a in possible_actions:            new_state, new_boat_side = task.result(state, a)            if new_state: # is a valid state                batch_result.append([                    task.value(new_state),                    new_state,                    new_boat_side,                    l_a + a,                ])    batch_result.sort(key= lambda x: x[0], reverse= True)        # sort the results with the most people crossed    task.record = batch_result[:5]        # I am only sticking with the best 5 results but        #   any number should be fine on this problem    if task.goal_test(task.record[0][1]):        break#   for i in task.record[:5]: # uncomment these lines to see full progress#       print(i)#   x = input() # press any key to continueprint(task.record[0][3])

希望这对你有帮助,如果还有什么不清楚的地方,请随时告诉我。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注