所以我刚刚开始更深入地探索动态规划,我遇到了一个我已经有一段时间无法解决的问题.如果您能帮我,我将不胜感激.

如果一个推销员有len(days_travel)天的空闲时间,他知道他在每个城市每天能获得多少收入,那么他可以找到最大收入;

输入列表收入显示他在特定城市的特定日期将获得的收入(例如revenue[0][1] = 2: day 0 and at city 'b'天),旅行天数是他从一个城市到另一个城市旅行所需的天数(例如days_travel[2][1] = 2 days is required to move from city c to city b天和start天是他出发的城市(第0天)

销售员可以决定他是否会在第101天上班

Ex. (start = 1)
    #      city: a  b  c  # day:
    revenue = [[1, 2, 1],   # 0
               [3, 3, 1],   # 1
               [1, 1, 100]] # 2

    #         city:  a   b   c    # city:
    days_travel  = [[0,  1,  1],  # a
                   [ 1, 0,  1],  # b
                   [ 1,  2, 0]]  # c
max revenue = 102

路径:

  1. 从第0天的城市"b"开始,在那里销售,赚取2美元
  2. 现在在第一天,早上前往城市c
  3. 现在在第二天,早上到达C城,在那里工作,赚取100美元

我的步骤:

对days_travel执行Floyd Warshall算法,找到the shortest days from one city to the other,使用上面的例子,结果是相同的矩阵(但在其他矩阵中肯定会有所不同)

def floyd_warshall(days_travel):
    nV = len(G)
    # Adding vertices individually
    for k in range(nV):
        for i in range(nV):
            for j in range(nV):
                G[i][j] = min(G[i][j], G[i][k] + G[k][j])

这让我->

    #         city:  a   b   c    # city:
    days_travel  = [[0,  1,  1],  # a
                   [ 1, 0,  1],   # b
                   [ 1,  2, 0]]   # c

然后我想循环所有的东西,使用max(),但不起作用

for x in range(len(days_travel)):
   for y in range(len(days_travel)):
      for day in range(len(revenue)):

如果能帮上忙,我将不胜感激.

Edit:

@然而,MarkKI的代码工作正常.如果我们增加一个约束条件,即销售人员不必从start_city开始,但可以旅行呢

Where a -1 indicates that there is no direct path 

#       city:    0  1  2  3   # city:
travel_days = [[-1, -1, 3, 1],  # 0
               [-1, -1, -1, 1],  # 1
               [1, -1, -1, 1],  # 2
               [1, 1, 2, -1]]  # 3

#       city:    0     1  2  3 # day:
revenue = [[1, 2, 3, 4],  # 0
           [3, 6, 1, 5],  # 1
           [1, 8, 4, 1],  # 2
           [1, 10, 4, 5],  # 3
           [10, 4, 5, 9]]  # 4

start = 3
This would mean my route is:
day 0: travel to city 1
day 1: make revenue in city 1 (6)
day 2: make revenue in city 1 (8)
day 3: make revenue in city 1 (10)
day 4: make revenue in city 1 (4)

total revenue: 6+8+10+4 = 28

我认为自上而下的方法是正确的

推荐答案

编辑:

在做了测试之后,我发现代码并没有完美地工作

我的代码是一个充分的折衷方案,效率为O(n*d^2)

最终代码:

        def floyd_warshall_days(days_travel):

    nV = len(days_travel)
    G = days_travel
    # Adding vertices individually
    for k in range(nV):
        for i in range(nV):
            for j in range(nV):
                G[i][j] = min(G[i][j], G[i][k] + G[k][j])
    return G

# The algorithm is similar to Floyd-Warshall
def floyd_warshall_rev(revenue, best_days_travel, city_to_start):

    nV = len(revenue)

    # Set G to array of zeroes by size of nV, When G[0] equals to the starting city revenue
    G = [revenue[0][city_to_start]] + [0]*(nV-1)

    # Set the start city as the best city we can go to in day zero
    StartCityArr = [city_to_start]
    best = city_to_start

    for day in range(1, nV): # For each day
        for city in range(len(revenue[0])): # For each city
            for Previous_Days in range(len(StartCityArr)): # For all previous days

                # The best way from the city we were in a few days ago to the current city
                travel_helper = best_days_travel[city][StartCityArr[Previous_Days]]

                # Normalize zero days travel time to one
                if travel_helper == 0: travel_helper = 1

                # Checks 2 things:
                # 1. Enough days have passed to get to this city
                # 2. The revenue of the current path is greater than the revenue of the best path so far

                if (day - travel_helper) >= Previous_Days and G[day] <= revenue[day][city] + G[Previous_Days]:

                    # The current path is the best one
                    G[day] = revenue[day][city] + G[Previous_Days]
                    # Update the last city visited on the best path
                    best = city

        # Append each day the last city visited on the best path to this day
        StartCityArr.append(best)

    return G


def main():
    #      city: a  b  c  # day:
    revenue = [[1, 2, 1],  # 0
               [3, 3, 1],  # 1
               [1, 1, 100]]  # 2

    #         city:  a   b   c    # city:
    days_travel = [[0, 1, 1],  # a
                   [1, 0, 1],  # b
                   [1, 2, 0]]  # c

    # Finds the best time to travel from each city to another
    best_days_travel = floyd_warshall_days(days_travel)

    # Finds the best travel by revenue from some "start city" for each number of day
    best_revenue_by_day = floyd_warshall_rev(revenue, best_days_travel,1)

    print(best_revenue_by_day)

main()

代码使用动态规划,

所以在第一次运行中,算法在我们只有一天的情况下找到了从B出发的最佳路由

在第二次运行中,算法在前一次运行只有两天的情况下,从B找到最佳路径

等等

因此,对于当前数据,输出将如下所示:

[2, 5, 102]

如果从B开始,2是1天工作的答案

如果从B开始,5是2天工作的答案

102是3天工作的答案,如果从B开始

Python相关问答推荐

这些变量是否相等,因为它们引用相同的实例,尽管它们看起来应该具有不同的值?

在Python中,如何初始化集合列表脚本的输出

Pandas 按照特殊规则保留每n行

Flask:如何在完整路由代码执行之前返回验证

Django序列化器没有验证或保存数据

使用Curses for Python保存和恢复终端窗口内容

telegram 机器人API setMyName不起作用

使用Python Cerberus初始化一个循环数据 struct (例如树)(v1.3.5)

遵循轮廓中对象方向的计算线

Pandas - groupby字符串字段并按时间范围 Select

' osmnx.shortest_track '返回有效源 node 和目标 node 的'无'

如何使用根据其他值相似的列从列表中获取的中间值填充空NaN数据

从groupby执行计算后创建新的子框架

当独立的网络调用不应该互相阻塞时,'

Streamlit应用程序中的Plotly条形图中未正确显示Y轴刻度

Pandas—在数据透视表中占总数的百分比

LocaleError:模块keras._' tf_keras. keras没有属性__internal_'''

找到相对于列表索引的当前最大值列表""

基于另一列的GROUP-BY聚合将列添加到Polars LazyFrame

如何获取Python synsets列表的第一个内容?