Python基于地理位置的商品打包分配算法实现
import pandas as pd
import numpy as np
from scipy.spatial import distance
# 读取商品信息和会员信息
product_info_df = pd.read_excel('D:\pythonProject3\product_info\product_packaging.xlsx')
member_info_df = pd.read_excel('D:\pythonProject3\packaging_problem\member_info.xlsx')
# 将商品信息和会员信息转换为numpy数组
product_info = product_info_df[['new_product_latitude', 'new_product_longitude', 'product_code', 'packaging_quantity']].values
member_info = member_info_df[['member_code', 'member_latitude', 'member_longitude', 'reserved_product_limit']].values
# 初始化已分配商品的列表
allocated_products = []
# 遍历每个会员
for i in range(len(member_info)):
member_code = member_info[i][0]
member_latitude = member_info[i][1]
member_longitude = member_info[i][2]
reserved_product_limit = member_info[i][3]
# 初始化当前会员已挑选的商品列表和已挑选商品数量
current_member_selected_products = []
current_member_selected_product_quantity = 0
# 遍历每个商品
for j in range(len(product_info)):
product_latitude = product_info[j][0]
product_longitude = product_info[j][1]
product_code = product_info[j][2]
packaging_quantity = product_info[j][3]
# 如果商品已被其他会员挑选过,则跳过
if product_code in allocated_products:
continue
# 计算商品和会员之间的距离
distance_ = distance.euclidean((product_latitude, product_longitude), (member_latitude, member_longitude))
# 如果商品距离超过0.1,则跳过
if distance_ > 0.1:
continue
# 如果当前会员已挑选商品数量加上当前商品数量超过预订商品限额,则跳过
if current_member_selected_product_quantity + packaging_quantity > reserved_product_limit:
continue
# 将当前商品加入当前会员已挑选商品列表,并更新已挑选商品数量
current_member_selected_products.append(product_code)
current_member_selected_product_quantity += packaging_quantity
# 将当前商品加入已分配商品列表
allocated_products.append(product_code)
# 输出当前会员挑选到的商品数量和预订商品限额
print('Member Code:', member_code)
print('Number of Selected Products:', current_member_selected_product_quantity)
print('Reserved Product Limit:', reserved_product_limit)
print()
# 输出当前会员挑选到的商品列表
print('Member Code:', member_code)
print('Selected Products:', current_member_selected_products)
print()
# 输出每个商品被分配到哪个会员
product_allocation = np.zeros(len(product_info))
for i in range(len(product_info)):
product_code = product_info[i][2]
product_allocation[i] = [j+1 for j in range(len(member_info)) if product_code in current_member_selected_products]
print('Product Allocation:', product_allocation)
原文地址: https://www.cveoy.top/t/topic/fAJt 著作权归作者所有。请勿转载和采集!