Ejemplo n.º 1
0
def opt_tsp(r, und_a, pre_cal, allo):
    if und_a[0] <= 0:
        return None
    node_ind = range(len(und_a[1]) + 1)
    order_dict, stay_t, pick_t, require_t, num, mini_start = {}, [
        r[2][-1] - r[1][-1]
    ], [0], [0], [0], [r[1][-1]]
    initial = (0, 0)
    for o_idb in und_a[1]:
        # delivery
        o_id = o_idb + 'd'
        temp_ind = pre_cal['index'][o_id]
        stay_t.append(pre_cal['stay time'][temp_ind])
        pick_t.append(0)
        require_t.append(pre_cal['delivery time'][temp_ind])
        num.append(0)
        mini_start.append(0)
    # Now travel time
    travel_t = {}
    for i in node_ind:
        for j in range(i + 1, len(und_a[1]) + 1):
            t_travel_time = pre_cal['travel time'][i][j]
            travel_t[(i, j)] = t_travel_time
            travel_t[(j, i)] = t_travel_time
    obj, res_list, status = opt_with_solver(node_ind, order_dict, travel_t,
                                            stay_t, pick_t, require_t, num,
                                            mini_start, initial)
    if res_list is None:
        print(status)
        return None, None
    route = [r[4][-1]] + und_a[1]
    o_route = route[:]
    for ind, position in zip(node_ind, res_list):
        o_route[position] = route[ind]
    opt_nodes, opt_pck = r[0][:], r[3][:]
    for n_o_id in o_route[1:]:
        opt_nodes.append(allo.at[n_o_id, 'dest_id'])
        opt_pck.append(-allo.at[n_o_id, 'num'])
    o_r_str = [opt_nodes, [], [], opt_pck, r[4] + o_route[1:]]
    merge.recal_time(o_r_str, allo)
    return o_r_str, obj
Ejemplo n.º 2
0
def str_to_route(r_str, allo, is_ll=False):
    r_nodes, pck = [], []
    r_ord = r_str.split(',')[:-1]
    pick_set = {}
    for ord_id in r_ord:
        if ord_id in pick_set:
            # delivery
            if pick_set[ord_id] > 1:
                raise Exception('replicated order in ' + r_str + ' with order id ' + 'ord_id')
            r_nodes.append(allo.at[ord_id, 'dest_id'])
            pck.append(-allo.at[ord_id, 'num'])
            pick_set[ord_id] += 1
        else:
            # pickup
            r_nodes.append(allo.at[ord_id, 'ori_id'])
            pck.append(allo.at[ord_id, 'num'])
            pick_set[ord_id] = 0
    new_r = [r_nodes, [], [], pck, r_ord]
    mg.recal_time(new_r, allo, is_ll=is_ll)
    last, und = mg.find_last(new_r)
    mg.append_to_route(new_r, [last, und])
    return new_r
Ejemplo n.º 3
0
def opt_tsp(r, und_a, pre_cal, allo):
    if und_a[0] <= 0:
        return None
    node_ind = range(len(und_a[1]) + 1)
    order_dict, stay_t, pick_t, require_t, num, mini_start = {}, [r[2][-1]-r[1][-1]], [0], [0], [0], [r[1][-1]]
    initial = (0, 0)
    for o_idb in und_a[1]:
        # delivery
        o_id = o_idb + 'd'
        temp_ind = pre_cal['index'][o_id]
        stay_t.append(pre_cal['stay time'][temp_ind])
        pick_t.append(0)
        require_t.append(pre_cal['delivery time'][temp_ind])
        num.append(0)
        mini_start.append(0)
    # Now travel time
    travel_t = {}
    for i in node_ind:
        for j in range(i + 1, len(und_a[1]) + 1):
            t_travel_time = pre_cal['travel time'][i][j]
            travel_t[(i, j)] = t_travel_time
            travel_t[(j, i)] = t_travel_time
    obj, res_list, status = opt_with_solver(node_ind, order_dict, travel_t, stay_t, pick_t,
                                            require_t, num, mini_start, initial)
    if res_list is None:
        print(status)
        return None, None
    route = [r[4][-1]] + und_a[1]
    o_route = route[:]
    for ind, position in zip(node_ind, res_list):
        o_route[position] = route[ind]
    opt_nodes, opt_pck = r[0][:], r[3][:]
    for n_o_id in o_route[1:]:
        opt_nodes.append(allo.at[n_o_id, 'dest_id'])
        opt_pck.append(-allo.at[n_o_id, 'num'])
    o_r_str = [opt_nodes, [], [], opt_pck, r[4] + o_route[1:]]
    merge.recal_time(o_r_str, allo)
    return o_r_str, obj
Ejemplo n.º 4
0
def str_to_route(r_str, allo, is_ll=False):
    r_nodes, pck = [], []
    r_ord = r_str.split(',')[:-1]
    pick_set = {}
    for ord_id in r_ord:
        if ord_id in pick_set:
            # delivery
            if pick_set[ord_id] > 1:
                raise Exception('replicated order in ' + r_str +
                                ' with order id ' + 'ord_id')
            r_nodes.append(allo.at[ord_id, 'dest_id'])
            pck.append(-allo.at[ord_id, 'num'])
            pick_set[ord_id] += 1
        else:
            # pickup
            r_nodes.append(allo.at[ord_id, 'ori_id'])
            pck.append(allo.at[ord_id, 'num'])
            pick_set[ord_id] = 0
    new_r = [r_nodes, [], [], pck, r_ord]
    mg.recal_time(new_r, allo, is_ll=is_ll)
    last, und = mg.find_last(new_r)
    mg.append_to_route(new_r, [last, und])
    return new_r
Ejemplo n.º 5
0
def monte_tsp(r, und, allo, try_num=400000):
    best_r, best_obj = [], np.inf
    indd = 0
    while indd < try_num:
        indd += 1
        if np.abs(indd / 10000.0 - np.floor(indd / 10000.0)) <= 0.0001:
            print(indd)
        test_order = und[1][:]
        rdm.shuffle(test_order)
        test_r = copy.deepcopy(r)
        for ordid in test_order:
            test_r[0].append(allo.at[ordid, 'dest_id'])
            test_r[3].append(-allo.at[ordid, 'num'])
            test_r[4].append(ordid)
        test_r, punish = mg.recal_time(test_r, allo, True)
        test_obj = punish[0] + test_r[2][-1]
        if test_obj < best_obj:
            best_r = test_r
            best_obj = test_obj
    return best_r, best_obj
Ejemplo n.º 6
0
def cal_c(r_str, allo, is_punish=True):
    r_nodes, pck = [], []
    r_ord = r_str.split(',')[:-1]
    pick_set = {}
    for ord_id in r_ord:
        if ord_id in pick_set:
            # delivery
            if pick_set[ord_id] > 1:
                raise Exception('replicated order in ' + r_str + ' with order id ' + 'ord_id')
            r_nodes.append(allo.at[ord_id, 'dest_id'])
            pck.append(-allo.at[ord_id, 'num'])
            pick_set[ord_id] += 1
        else:
            # pickup
            r_nodes.append(allo.at[ord_id, 'ori_id'])
            pck.append(allo.at[ord_id, 'num'])
            pick_set[ord_id] = 0
    new_r = [r_nodes, [], [], pck, r_ord]
    new_r, p_info = mg.recal_time(new_r, allo, is_punish, is_ll=True)
    return p_info[0] + new_r[2][-1]
Ejemplo n.º 7
0
def monte_tsp(r, und, allo, try_num=400000):
    best_r, best_obj = [], np.inf
    indd = 0
    while indd < try_num:
        indd += 1
        if np.abs(indd/10000.0 - np.floor(indd/10000.0)) <= 0.0001:
            print(indd)
        test_order = und[1][:]
        rdm.shuffle(test_order)
        test_r = copy.deepcopy(r)
        for ordid in test_order:
            test_r[0].append(allo.at[ordid, 'dest_id'])
            test_r[3].append(-allo.at[ordid, 'num'])
            test_r[4].append(ordid)
        test_r, punish = mg.recal_time(test_r, allo, True)
        test_obj = punish[0] + test_r[2][-1]
        if test_obj < best_obj:
            best_r = test_r
            best_obj = test_obj
    return best_r, best_obj
Ejemplo n.º 8
0
def cal_c(r_str, allo, is_punish=True):
    r_nodes, pck = [], []
    r_ord = r_str.split(',')[:-1]
    pick_set = {}
    for ord_id in r_ord:
        if ord_id in pick_set:
            # delivery
            if pick_set[ord_id] > 1:
                raise Exception('replicated order in ' + r_str +
                                ' with order id ' + 'ord_id')
            r_nodes.append(allo.at[ord_id, 'dest_id'])
            pck.append(-allo.at[ord_id, 'num'])
            pick_set[ord_id] += 1
        else:
            # pickup
            r_nodes.append(allo.at[ord_id, 'ori_id'])
            pck.append(allo.at[ord_id, 'num'])
            pick_set[ord_id] = 0
    new_r = [r_nodes, [], [], pck, r_ord]
    new_r, p_info = mg.recal_time(new_r, allo, is_punish, is_ll=True)
    return p_info[0] + new_r[2][-1]
Ejemplo n.º 9
0
def gener_routes(allo, re_cal=False, find_last=False):
    r1 = [['A083', 'A083', 'A083', 'A083', 'A083', 'B5800', 'B7555', 'B7182', 'B8307', 'B8461'], [], [],
          [34, 11, 19, 12, 63, -34, -63, -12, -19, -11],
          ['F6344', 'F6360', 'F6358', 'F6353', 'F6354', 'F6344', 'F6354', 'F6353', 'F6358', 'F6360']]
    r2 = [['A083', 'A083', 'A083', 'B6528', 'S245', 'B3266', 'B3266', 'B2337'], [], [],
          [46, 53, 39, -46, 1, -1, -53, -39],
          ['F6349', 'F6325', 'F6314', 'F6349', 'E0895', 'E0895', 'F6325', 'F6314']]
    r3 = [['A083', 'A083', 'A083', 'A083', 'S294', 'B1940', 'B6104', 'B8926', 'B9072', 'B6103'], [], [],
          [36, 27, 36, 33, 1, -33, -36, -1, -36, -27],
          ['F6366', 'F6345', 'F6346', 'F6308', 'E1088', 'F6308', 'F6346', 'E1088', 'F6366', 'F6345']]
    if re_cal:
        mg.recal_time(r1, allo)
        mg.recal_time(r2, allo)
        mg.recal_time(r3, allo)
    if find_last:
        last1, und1 = mg.find_last(r1)
        last2, und2 = mg.find_last(r2)
        last3, und3 = mg.find_last(r3)
        mg.append_to_route(r1, [last1, und1])
        mg.append_to_route(r2, [last2, und2])
        mg.append_to_route(r3, [last3, und3])
        return [r1, r2, r3]
    return [r1, r2, r3]
Ejemplo n.º 10
0
def gener_routes(allo, re_cal=False, find_last=False):
    r1 = [[
        'A083', 'A083', 'A083', 'A083', 'A083', 'B5800', 'B7555', 'B7182',
        'B8307', 'B8461'
    ], [], [], [34, 11, 19, 12, 63, -34, -63, -12, -19, -11],
          [
              'F6344', 'F6360', 'F6358', 'F6353', 'F6354', 'F6344', 'F6354',
              'F6353', 'F6358', 'F6360'
          ]]
    r2 = [['A083', 'A083', 'A083', 'B6528', 'S245', 'B3266', 'B3266', 'B2337'],
          [], [], [46, 53, 39, -46, 1, -1, -53, -39],
          [
              'F6349', 'F6325', 'F6314', 'F6349', 'E0895', 'E0895', 'F6325',
              'F6314'
          ]]
    r3 = [[
        'A083', 'A083', 'A083', 'A083', 'S294', 'B1940', 'B6104', 'B8926',
        'B9072', 'B6103'
    ], [], [], [36, 27, 36, 33, 1, -33, -36, -1, -36, -27],
          [
              'F6366', 'F6345', 'F6346', 'F6308', 'E1088', 'F6308', 'F6346',
              'E1088', 'F6366', 'F6345'
          ]]
    if re_cal:
        mg.recal_time(r1, allo)
        mg.recal_time(r2, allo)
        mg.recal_time(r3, allo)
    if find_last:
        last1, und1 = mg.find_last(r1)
        last2, und2 = mg.find_last(r2)
        last3, und3 = mg.find_last(r3)
        mg.append_to_route(r1, [last1, und1])
        mg.append_to_route(r2, [last2, und2])
        mg.append_to_route(r3, [last3, und3])
        return [r1, r2, r3]
    return [r1, r2, r3]
Ejemplo n.º 11
0
        prob += t[j] >= t[i] + 1 + big_m*(x[(i, j)] - 1)
        prob += load[j] >= load[i] + num[i] + big_m*(x[(i, j)] - 1)
    prob.solve(lp.CPLEX(msg=1, timelimit=CPLEX_TIME_LIMIT, options=['set logfile cplex/cplex%d.log' % os.getpid()]))
    # set threads 100
    if lp.LpStatus[prob.status] != 'Infeasible':
        sol_list = [int(round(t[i].varValue)) for i in node_ind]
        return lp.value(prob.objective), sol_list, lp.LpStatus[prob.status]
    else:
        return None, None, lp.LpStatus[prob.status]


if __name__ == '__main__':
    import time
    import test
    import cPickle as cP
    f = open('allo', 'rb')
    allo1 = cP.load(f)
    f.close()
    r_t, und = test.generate_route(allo1, 4)
    merge.recal_time(r_t, allo1)
    pre_cal1 = merge.generate_distance_time(r_t, und, allo1)
    t1 = time.time()
    opr1 = merge.bb_tsp(r_t, allo1, und, pre_cal=pre_cal1, append_und=False)
    t2 = time.time()
    opr2, obj1 = opt_tsp(r_t, und, pre_cal1, allo1)
    t3 = time.time()
    print(t2-t1)
    print(t3-t2)
    print(opr1)
    print(opr2)
Ejemplo n.º 12
0
        lp.CPLEX(msg=1,
                 timelimit=CPLEX_TIME_LIMIT,
                 options=['set logfile cplex/cplex%d.log' % os.getpid()]))
    # set threads 100
    if lp.LpStatus[prob.status] != 'Infeasible':
        sol_list = [int(round(t[i].varValue)) for i in node_ind]
        return lp.value(prob.objective), sol_list, lp.LpStatus[prob.status]
    else:
        return None, None, lp.LpStatus[prob.status]


if __name__ == '__main__':
    import time
    import test
    import cPickle as cP
    f = open('allo', 'rb')
    allo1 = cP.load(f)
    f.close()
    r_t, und = test.generate_route(allo1, 4)
    merge.recal_time(r_t, allo1)
    pre_cal1 = merge.generate_distance_time(r_t, und, allo1)
    t1 = time.time()
    opr1 = merge.bb_tsp(r_t, allo1, und, pre_cal=pre_cal1, append_und=False)
    t2 = time.time()
    opr2, obj1 = opt_tsp(r_t, und, pre_cal1, allo1)
    t3 = time.time()
    print(t2 - t1)
    print(t3 - t2)
    print(opr1)
    print(opr2)