Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 一个有约束的航班入槽分配问题?_Python_Python 3.x_Python 2.7 - Fatal编程技术网

Python 一个有约束的航班入槽分配问题?

Python 一个有约束的航班入槽分配问题?,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我在为到达机场的航班分配航班时遇到问题(分配问题) 我有以下资料: time_slots = ['4:45' , '5:00', '5:15', '5:30', '5:45', '6:00'] (这意味着间隔,例如插槽1:“4:45”到“4:59”) (我们可以为每个时段分配多少航班) 我计算过: Indices_for_arrivals = [0, 1, 1, 1, 1, 3, 4, 4, 4] (这意味着每个航班属于哪个时段)` 因此,我需要在容量限制下将每个航班分配到插槽。例如,

我在为到达机场的航班分配航班时遇到问题(分配问题)

我有以下资料:

time_slots = ['4:45' , '5:00', '5:15', '5:30', '5:45', '6:00']  
(这意味着间隔,例如插槽1:“4:45”到“4:59”)

(我们可以为每个时段分配多少航班)

我计算过:

Indices_for_arrivals = [0, 1, 1, 1, 1, 3, 4, 4, 4] 
(这意味着每个航班属于哪个时段)`

因此,我需要在容量限制下将每个航班分配到插槽。例如,插槽1(4:45)不能乘坐多个航班,插槽2(5:00)不能乘坐多个航班……等等

在我刚才使用以下代码解释的约束条件下,我成功地为每个航班分配了机位:

def ration_by_schedule(flight_times, capacity, T):
    num_sched_flights = [0 for i in range(T)] # list to keep track of number of flights scheduled in each interval
    new_flight_schedule = [] # list to store new flight times 
    for t in flight_times:
        for s in range(t, T):
             if num_sched_flights[s] < capacity[s]:
                new_flight_schedule.append(s)
                num_sched_flights[s] += 1
                break
        else:
            raise RuntimeError ("Not enough capacity to schedule all flights")
    return new_flight_schedule

schedule = ration_by_schedule(Indices_for_arrivals, Capacity_each_slot, 9)
print(schedule)
在这种情况下,我的结果是:

新航班=[0,2,1,3,1,3,4,4]


如何在“我的代码”中添加持续时间约束?

这不是解决您的问题的方法,但这是一种使您的代码更清晰的方法(用于应用其他约束)

使用字典,而不是(计算)索引-创建它们非常简单:

time_slots = ['4:45', '5:00', '5:15', '5:30', '5:45', '6:00']  
capacity_each_slot = [1, 2, 1, 1, 2, 3] 

capacities = dict(zip(time_slots, capacity_each_slot))

flights_arrivals = ['4:47', '5:02', '5:10', '5:12', '5:14', '5:33', '5:48', '5:50', '5:58']
duration_flights = [500, 400, 1200, 350, 1000, 350, 1000, 250, 300]

durations = dict(zip(flights_arrivals, duration_flights))
(我将您的大写姓名改为所有小写字母-请参阅。)

所以你会得到字典

此外,最好使用内置模块
datetime
,而不是用字符串表示时间,这样您就可以比较时间:

import datetime

slot0 = datetime.time(4, 45)              # 4:45
slot1 = datetime.time(5)                  # 5:00   (or datetime.time(5, 0))
...
time_slots = (slot0, slot1, ...)

不要单独缩进每一行-在每一行的开头将代码块缩进4个空格,这样会将其视为代码。为您编辑。您的问题是什么?似乎没有,只有你拥有的和你想做的。我不知道如何在我的作业算法中添加工期限制。谢谢你的帮助。
exempted_flights_indices = [2, 4, 6]
time_slots = ['4:45', '5:00', '5:15', '5:30', '5:45', '6:00']  
capacity_each_slot = [1, 2, 1, 1, 2, 3] 

capacities = dict(zip(time_slots, capacity_each_slot))

flights_arrivals = ['4:47', '5:02', '5:10', '5:12', '5:14', '5:33', '5:48', '5:50', '5:58']
duration_flights = [500, 400, 1200, 350, 1000, 350, 1000, 250, 300]

durations = dict(zip(flights_arrivals, duration_flights))
capacities = {'4:45': 1, '5:00': 2, '5:15': 1, '5:30': 1, '5:45': 2, '6:00': 3}
durations = {'4:47': 500,
             '5:02': 400,
             '5:10': 1200,
             '5:12': 350,
             '5:14': 1000,
             '5:33': 350,
             '5:48': 1000,
             '5:50': 250,
             '5:58': 300}
import datetime

slot0 = datetime.time(4, 45)              # 4:45
slot1 = datetime.time(5)                  # 5:00   (or datetime.time(5, 0))
...
time_slots = (slot0, slot1, ...)