代码拉取完成,页面将自动刷新
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: 徐胜虎
@contact:1813366784@qq.com
@version: 1.0.0
@license: Apache Licence
@file: x.py
@time: 2024/1/28 19:13
"""
import itertools
import sys
from typing import Iterable
import pandas as pd
def cal_distance(elem: str, refer_data: dict = None):
route_list = elem.split('-')
route_num = len(route_list)
route_distance = 0
for i in range(route_num - 1):
start = route_list[i]
end = route_list[i + 1]
elem_key = frozenset([start, end])
if elem_key in refer_data:
route_distance += refer_data.get(elem_key)
else:
raise KeyError(f'字典同时没有键{elem_key}键')
return route_distance
# 武汉-黄冈黄州-浠水-蕲春-武穴-黄梅县
# 以最小距离为最优
def cal_min_path_distance(start: str = None, must_pass: Iterable[str] = None, refer_data: dict = None):
min_route_path = None
min_re = sys.maxsize
for route in itertools.permutations(must_pass):
route_str = start + '-' + '-'.join(route)
res = cal_distance(route_str, refer_data)
if res < min_re:
min_route_path = route_str
min_re = res
return min_route_path, min_re
def cal_min_cost_distance(start: str = '武汉', must_pass: Iterable[str] = None, refer_data: dict = None,
refer_cost: dict = None):
min_route_path = None
min_re = sys.maxsize
for route in itertools.permutations(must_pass):
route_str = start + '-' + '-'.join(route)
total_distance = cal_distance(route_str, refer_data)
rest = total_distance - refer_cost[route[-1]][0]
if rest < 0:
continue
res = rest * 2.5 + refer_cost[route[-1]][1]
# print(total_distance - refer_cost[route[-1]][0])
# total_distance - refer_cost[route[-1]][1]
if res < min_re:
min_route_path = route_str
min_re = res
return min_route_path, min_re
received_data = pd.read_excel('resources/template/1月份账单.xlsx', header=0, keep_default_na=False, sheet_name='Sheet4',
engine='openpyxl')
# noinspection PyTypeChecker
refer_distance_data = pd.read_excel('resources/template/信息录入表.xlsx', header=0, keep_default_na=False,
sheet_name='鄂东数据字典', usecols=('序号', '线路', '公里数', '起点', '终点'))
# noinspection PyTypeChecker
refer_cost_data = pd.read_excel('resources/template/信息录入表.xlsx', header=0, keep_default_na=False,
sheet_name='鄂东数据字典', usecols=('最终站点', '站点公里数', '单价'))
refer_distance_data.drop(columns='序号', inplace=True)
refer_distance_data['路线'] = refer_distance_data[['起点', '终点']].apply(lambda row: '-'.join(row), axis=1)
refer_data_dict = dict(
zip(refer_distance_data[['起点', '终点']].apply(lambda row_: frozenset([row_['起点'], row_['终点']]), axis=1),
refer_distance_data['公里数']))
refer_cost_dict = dict(
zip(refer_cost_data['最终站点'], tuple(zip(refer_cost_data['站点公里数'], refer_cost_data['单价']))))
re = cal_min_path_distance(start='武汉', must_pass=['鄂州五环', '黄冈黄州', '蕲春', '团风'],
refer_data=refer_data_dict)
re = cal_min_cost_distance(start='武汉', must_pass=['黄冈黄州', '浠水', '蕲春', '武穴','黄梅县'],
refer_data=refer_data_dict, refer_cost=refer_cost_dict)
re = cal_min_cost_distance(start='武汉', must_pass=['鄂州五环','黄冈黄州', '团风','浠水', '蕲春', '武穴','黄梅县'],
refer_data=refer_data_dict, refer_cost=refer_cost_dict)
total_temp = []
for i in range(2, 8):
for elem in itertools.combinations(['鄂州五环', '黄冈黄州', '黄梅县', '蕲春', '团风', '武穴', '浠水'], i):
re = cal_min_cost_distance(start='武汉', must_pass=elem,
refer_data=refer_data_dict, refer_cost=refer_cost_dict)
total_temp.append(re)
df = pd.DataFrame(total_temp,columns=['路线','实际运费'])
df.to_excel('resources/temp/result.xlsx')
print(re)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。