【问题标题】:Basic Greedy Search in PythonPython中的基本贪心搜索
【发布时间】:2017-01-19 18:36:21
【问题描述】:
def basic_greedy():

    start = 1
    mindist = d_dict[(1,2)]
    m = 0

    while len(tour)!=size:
        tour.append(start)
        for k in range(len(cities)):
            if cities[k] not in tour:
                if d_dict[(start,cities[k])]<mindist:
                    mindist = d_dict[(start,cities[k])]
                    m = k
        start = cities[m]

    return tour

这是我在 Python 中进行基本贪心搜索的代码。 start 是起始城市,tour 是一个列表,其中包含按访问顺序排列的城市,cities 是一个包含 1 中所有城市的列表size (1,2,3,4.....12..size) 其中 size 是城市的数量。 d_dict 是一个字典,包含每对可能的城市之间的距离。 mindistm 只是临时变量,用于跟踪最近的邻居等。

我希望代码从城市 1 开始,移动到最近的邻居,然后到最近的邻居,直到每个城市都被覆盖一次。我希望这段代码的输出类似于城市 1 到 8 的 [1,5,3,8,4,6,2,7](访问所有城市恰好一次),但我得到 [1,7,7,7,7,7,7,7]。怎么了?

【问题讨论】:

  • d_dict 是否包括城市与城市之间的距离?看起来你可能会以 7 为结尾,因为 7 最接近它自己。此外,您可能想在下一个循环开始之前重置 mindist;如果我从 city1 到 city2 并且距离为 1 个单位,然后从 city2 到任何地方的距离为 >1 个单位,会发生什么情况。
  • @AlbertRothman 刚刚重置 mindist 有效:D 我刚开始时包括 mindist = 99。休息照原样。非常感谢!
  • 很高兴工作。为了使它更好,您可以搜索字典并找到最大的距离,然后在其中添加一个,并在每个循环开始时将其用作思维器;这样您的大距离就不会被硬编码(因此您可以添加比 99 更远的城市并且仍然可以获得解决方案)。

标签: python loops infinite-loop greedy traveling-salesman


【解决方案1】:

问题: 一般来说,问题是不明确的。代码不完整。但是,让我提供一些基本的指示。希望能帮助到你。来了……

  1. 一旦找到并添加了一个城市,您就需要跳出 for 循环(似乎是这样,检查,仔细检查)。
  2. 代码不完整。例如: (a) 您如何访问列表游览? (b) 尺寸未定义
  3. 确保不要重新考虑或添加旅游列表中已经访问过的城市(好像是这样,检查,重新检查)。
  4. 建议使用图/节点技术来实现这种贪婪搜索。

【讨论】:

  • 我已经保证 2,3 - 我没有提到整个源代码,因为那太长了! :)
  • 这不能回答问题,作为评论会更好......
【解决方案2】:
def basic_greedy():
    # greedy search algorithm
    d_dict = {1: [(1, 2), (2, 15), (3, 30)], 2: [(1, 30), (7, 10)]}  # dict of lists of tuples such that nodei : [ (neighbourj, distancej), .... ]
    currentCity = 1
    tour = []   # list of covered nodes
    tour.append(currentCity)
    distanceTravelled = 0   # distance travelled in tour
    while len(set([neighbourCity for (neighbourCity, distance) in d_dict.get(currentCity, [])]).difference(set(tour))) > 0:  # set(currentCityNeighbours) - set(visitedNodes)
        # way 1 starts
        minDistanceNeighbour = None
        minDistance = None
        for eachNeighbour, eachNeighbourdDistance in d_dict[currentCity]:
            if eachNeighbour != currentCity and eachNeighbour not in tour:
                if minDistance is not None:
                    if minDistance > eachNeighbourdDistance:
                        minDistance = eachNeighbourdDistance
                        minDistanceNeighbour = eachNeighbour
                else:
                    minDistance = eachNeighbourdDistance
                    minDistanceNeighbour = eachNeighbour
        nearestNeigbhourCity = (minDistanceNeighbour, minDistance)
        # way 1 ends
        # way 2 starts
        # nearestNeigbhourCity = min(d_dict[currentCity], key=lambda someList: someList[1] if someList[0] not in tour else 1000000000)  # else part returns some very large number
        # way 2 ends
        tour.append(nearestNeigbhourCity[0])
        currentCity = nearestNeigbhourCity[0]
        distanceTravelled += nearestNeigbhourCity[1]
    print(tour)
    print(distanceTravelled)

这是你要求的吗?我可能也改变了距离字典和其他一些变量的结构,但这不会影响贪婪的方法逻辑,希望这对你有用...... 编辑了我的答案,因为我第一次在currentCity 的所有邻居中找到nearestNeighbourCity,但现在我在当前城市的所有未访问邻居中找到nearestNeighbourCity,因为我已经用两种方式做到了@987654325 @和way 2

【讨论】:

    猜你喜欢
    • 2019-04-09
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    相关资源
    最近更新 更多