微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

谷歌 OR 工具 - 重新审视列车调度问题

如何解决谷歌 OR 工具 - 重新审视列车调度问题

在此 example 中,假设我们不关心重叠,我们如何允许将路线分配给 1 列以上的列车?

例如,一条路线理论上可以由多于 1 列火车执行,但火车仍遵守其余里程数和其他限制条件。

更具体地说,如果我们有火车,我会接受基于 12 小时连续时段的分配来覆盖每日路线,如下所示: 火车 1: 07:00 - 19:00 火车2:12:00 - 24:00

我所说的连续小时是指满足 07:00-11:00 + 14:00-15:00 + 17:00-24:00 形式的 12 小时火车班次解决方案的解决方案不应该可以接受。我已尝试修改代码,但仍然无法获得最佳结果。

from itertools import combinations
from ortools.sat.python import cp_model
import numpy as np


def main():
    model = cp_model.CpModel()
    solver = cp_model.cpsolver()

    # data
    route_km = {
        "0": 30,"1": 30,"2": 30,"3": 30,"5": 30,"4": 30,"6": 30,"7": 30,"8": 30,"9": 30,"10": 30,"11": 30,"12": 30,"13": 30,"14": 30,"15": 30,"16": 30,"17": 30,"18": 30,"19": 30,"20": 30,"21": 30,"22": 30,"23": 30,"24": 30,"25": 30,"26": 30,"27": 30,"28": 30,"29": 30,"30": 30,"31": 30,"32": 30,"33": 30}

    train_cum_km = {
        'T1': 0,'T2': 0}

    route_times = {
        "0",('07:00','07:30'),"1",('07:30','08:00'),"2",('08:00','08:30'),"3",('08:30','09:00'),"5",('09:30','10:00'),"4",('09:00','09:30'),"6",('10:00','10:30'),"7",('10:30','11:00'),"8",('11:00','11:30'),"9",('11:30','12:00'),"10",('12:00','12:30'),"11",('12:30','13:00'),"12",('13:00','13:30'),"13",('13:30','14:00'),"14",('14:00','14:30'),"15",('14:30','15:00'),"16",('15:00','15:30'),"17",('15:30','16:00'),"18",('16:00','16:30'),"19",('16:30','17:00'),"20",('17:00','17:30'),"21",('17:30','18:00'),"22",('18:00','18:30'),"23",('18:30','19:00'),"24",('19:00','19:30'),"25",('19:30','20:00'),"26",('20:00','20:30'),"27",('20:30','21:00'),"28",('21:00','21:30'),"29",('21:30','22:00'),"30",('22:00','22:30'),"31",('22:30','23:00'),"32",('23:00','23:30'),"33",('23:30','24:00')}

    trains = list(train_cum_km.keys())
    routes = list(route_km.keys())
    num_trains = len(trains)
    num_routes = len(routes)

    assignments = {(t,r): model.NewBoolVar('assignment_%s%s' % (t,r))
                   for t in trains for r in routes}

    # constraint 1: each route must be assigned
    for r in routes:
        model.Add(sum(assignments[(t,r)] for t in trains) > =1)

    # constraint 2: each driver must do at least one route and max 24 routes
    for t in trains:
        model.Add(sum(assignments[(t,r)] for r in routes) >= 1)
        model.Add(sum(assignments[(t,r)] for r in routes) <= 24)

    # constraint 3: ensure the end of day cum km is less than 720
    # create a new variable which must be in the range (0,720)
    day_end_km = {
        t: model.NewIntvar(720,720,'train_%s_day_end_km' % t)
        for t in trains
    }

    for r1 in routes:
        for r2 in routes[routes.index(r1):]:
            for t in trains:
                if abs(sum(list(route_km.values())[:routes.index(r1)])-   sum(list(
                    route_km.values())[:routes.index(r2)])) > 30:
                model.Add(assignments[t,r1] == 0)

    for t in trains:
        # this will be constrained because day_end_km[t] is in domain [0,720]
        tmp = sum(assignments[t,r]*route_km[r] for r in routes) + train_cum_km[t]
        model.Add(day_end_km[t] == tmp)

    status = solver.solve(model)
    assert status in [cp_model.FEASIBLE,cp_model.OPTIMAL]

    for t in trains:
        t_routes = [r for r in routes if solver.Value(assignments[t,r])]
        print(f'Driver {t} does route {t_routes} '
              f'with end of day cumulative work of '
              f'{solver.Value(day_end_km[t])}')


if __name__ == '__main__':
    main()

解决方法

我使用以下函数解决了连续槽的问题:

def negated_bounded_span(works,start,length):
    """Filters an isolated sub-sequence of variables assined to True.
  Extract the span of Boolean variables [start,start + length),negate them,and if there is variables to the left/right of this span,surround the span by
  them in non negated form.
  Args:
    works: a list of variables to extract the span from.
    start: the start to the span.
    length: the length of the span.
  Returns:
    a list of variables which conjunction will be false if the sub-list is
    assigned to True,and correctly bounded by variables assigned to False,or by the start or end of works.
  """
    sequence = []
    # Left border (start of works,or works[start - 1])
    if start > 0:
        sequence.append(works[start - 1])
    for i in range(length):
        sequence.append(works[start + i].Not())
    # Right border (end of works or works[start + length])
    if start + length < len(works):
        sequence.append(works[start + length])
    return sequence

更多信息可以在here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?