import pandas as pd
import math

# 读取商品信息
product_df = pd.read_excel('商品信息.xlsx')
# 读取会员信息
member_df = pd.read_excel('会员信息.xlsx')

# 初始化会员已选商品数量
member_selected_quantity = {member_id: 0 for member_id in member_df['会员编号']}

# 初始化商品分配情况
product_allocation = {product_id: [] for product_id in product_df['商品编号']}

# 遍历每个会员
for _, member in member_df.iterrows():
    member_id = member['会员编号']
    member_latitude = member['会员GPS纬度']
    member_longitude = member['会员GPS经度']
    member_quota = member['预订商品限额']

    # 查找距离会员0.1以内的商品
    nearby_products = product_df[
        ((product_df['新商品GPS纬度'] - member_latitude)**2 + (product_df['新商品GPS经度'] - member_longitude)**2) <= 0.1**2
    ]

    # 按距离升序排列
    nearby_products['distance'] = nearby_products.apply(
        lambda row: math.sqrt(((row['新商品GPS纬度'] - member_latitude) ** 2 + (row['新商品GPS经度'] - member_longitude) ** 2)),
        axis=1
    )
    nearby_products = nearby_products.sort_values(by='distance')

    # 贪心算法选择商品
    selected_products = []
    total_quantity = 0
    for _, product in nearby_products.iterrows():
        if total_quantity + product['打包数量'] <= member_quota:
            selected_products.append(product['商品编号'])
            total_quantity += product['打包数量']

    # 更新分配信息
    for product_id in selected_products:
        member_selected_quantity[member_id] += product_df.loc[product_df['商品编号'] == product_id, '打包数量'].values[0]
        product_allocation[product_id].append(member_id)

# 输出结果
print('每个会员挑选到的商品数量和预订商品限额:')
for member_id, selected_quantity in member_selected_quantity.items():
    print(f'会员编号:{member_id},挑选到商品数量:{selected_quantity},预订商品限额:{member_df.loc[member_df['会员编号'] == member_id, '预订商品限额'].values[0]}')

print('\n每个商品被分配到的会员和商品数量:')
for product_id, allocated_members in product_allocation.items():
    print(f'商品编号:{product_id},被分配到会员:{allocated_members},商品数量:{product_df.loc[product_df['商品编号'] == product_id, '打包数量'].values[0]}')
基于地理位置和限额的商品分配Python算法

原文地址: https://www.cveoy.top/t/topic/fAI9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录