转移列表中的商品

0 投票
1 回答
51 浏览
提问于 2025-04-13 13:35
class Goods:
    def __init__(self, goods_list):
        self.goods_list = goods_list  # List of tuples (name, quantity)

    def transfer_goods(self, other_goods, good_index):
        """
        Transfer a random amount of goods from self to other_goods.
        """
        # Get the name and quantity of the goods to transfer
        good_name, self_quantity = self.goods_list[good_index]

        if self_quantity > 0:
            # Randomly select the amount to transfer
            amount = random.randint(1, self_quantity)

            print(
                f"Transferring {amount} {good_name} from Agent {self.unique_id} to Agent {other_goods.unique_id}")

            # Update quantities for the transferred goods
            self_goods_amount = self_quantity - amount
            other_goods_amount = other_goods.goods_list[good_index][1] + amount

            # Update the goods lists
            self.goods_list[good_index] = (good_name, self_goods_amount)
            other_goods.goods_list[good_index] = (good_name, other_goods_amount)

            print(f"After transfer:")
            print(f"Agent {self.unique_id}: {self.goods_list}")
            print(f"Agent {other_goods.unique_id}: {other_goods.goods_list}")
        else:
            print(f"Agent {self.unique_id} has no {good_name} to transfer.")

我现在在做一个代理基础模型(ABM),在这个模型里,代理们需要交换以列表形式存在的商品("名称",数量),但是我无法让这个列表更新。现在发生的情况是,列表中的一个条目只是和另一个代理的条目交换。

我希望如果商品类型相同,就更新数量;如果不同,就添加新的条目。

这一切似乎都发生在 transfer_goods() 这个方法里。

1 个回答

-1

我找到了解决你问题的方法。

我只需要稍微修改一下你使用的类型:我在 Goods 类里添加了一个属性 unique_id。其他的部分保持不变。这个函数 transfer_goods() 是用来把商品从第一个商品列表转移到第二个商品列表的。

你只需要做一个假设:“所有商品在它们的 goods-list 中都有相同的 good_names,并且排序是一样的。”如果不是这样的话,可能用字典会比用元组列表更好。

import random

class Goods:
    def __init__(self, unique_id, goods_list):
        self.unique_id = unique_id # Attribute added
        self.goods_list = goods_list  # List of tuples (name, quantity)

    def transfer_goods(self, other_goods, good_index):
        """
        Transfer a random amount of goods from self to other_goods.
        """
        # Get the name and quantity of the goods to transfer
        good_name, self_quantity = self.goods_list[good_index]

        if self_quantity > 0:
            # Randomly select the amount to transfer
            amount = random.randint(1, self_quantity)

            print(
            f"Transferring {amount} {good_name} from Agent {self.unique_id} to Agent {other_goods.unique_id}")

            # Update quantities for the transferred goods
            self_goods_amount = self_quantity - amount
            other_goods_amount = other_goods.goods_list[good_index][1] + amount

            # Update the goods lists
            self.goods_list[good_index] = (good_name, self_goods_amount)
            other_goods.goods_list[good_index] = (good_name, other_goods_amount)

            print(f"After transfer:")
            print(f"Agent {self.unique_id}: {self.goods_list}")
            print(f"Agent {other_goods.unique_id}: {other_goods.goods_list}")
        else:
            print(f"Agent {self.unique_id} has no {good_name} to transfer.")

例如,你可以用以下商品来测试:

initial_goods = Goods(0, [('patate', 3), ('tomate', 5), ('courgette', 1)])
other_goods = Goods(1, [('patate', 0), ('tomate', 0), ('courgette', 10)])

initial_goods.transfer_goods(other_goods, 0)
other_goods.goods_list

撰写回答