Skip to content

Produce market

Hamill and Gilbert's produce market.

  • Class: ProduceMarketSystem
  • Import: from skcomplex.systems.economics.markets import ProduceMarketSystem
  • Field: economics/markets
  • Topics: market, agents, heterogeneity
  • References:
    • Hamill and Gilbert 2016

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.economics.markets import ProduceMarketSystem

trajectory = ProduceMarketSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
n_shoppers 50
n_stalls 9
n_items_stocked 6
random_state 0
solver 'auto'
backend 'auto'

Tags

The system with its default parameters has these capability tags, which solvers and analyses check.

Tag Values
Time discrete
Space none
Laws update
Randomness deterministic
Domains integer, real
Structure variable membership

Class

The source of the class shows how it builds its components from its model parameters.

class ProduceMarketSystem(BaseSystem):
    """Hamill and Gilbert's produce market.

    Shoppers with lists of produce visit stalls at random. There are twelve kinds of fruit and vegetables. Every stall
    sells a random selection of them at its own markup of 1 to 30 percent on the wholesale prices, and every shopper
    wants between one and eight of them. The system has the groups `stalls` and `shoppers`, the mean number of items
    left on the lists `items_left`, and the mean money spent `spent`.

    Args:
        n_shoppers:
            The number of shoppers.

        n_stalls:
            The number of stalls.

        n_items_stocked:
            The number of kinds of produce that every stall sells.

        random_state:
            The seed or generator of the stocks, the markups, and the shopping lists.

        label:
            The label of the system, which names it when it is nested in another one.

        solver:
            The solver of the problem, or `'auto'` for the one the system chooses.

        backend:
            The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.

    Examples:
        >>> from skcomplex.systems.economics.markets import ProduceMarketSystem
        >>> [group.size for group in ProduceMarketSystem(10, 3).prepare().system_.elements]
        [3, 10]
    """

    field = 'economics/markets'
    topics = ('market', 'agents', 'heterogeneity')
    references = ('Hamill and Gilbert 2016',)
    until = 10
    observables = (
        Observable('items_left', group='shoppers', variable='wanted', reduce=_get_items_left),
        Observable('spent', group='shoppers', variable='spent'),
    )

    def __init__(
        self,
        n_shoppers: int = 50,
        n_stalls: int = 9,
        *,
        n_items_stocked: int = 6,
        random_state: int | np.random.Generator | None = 0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.n_shoppers = n_shoppers
        self.n_stalls = n_stalls
        self.n_items_stocked = n_items_stocked
        self.random_state = random_state

    @staticmethod
    def _get_random_masks(sizes: npt.NDArray[np.int64], rng: np.random.Generator) -> Array:
        """Return one bit mask per size.

        Each mask has that many distinct items chosen at random.
        """
        n_items = len(WHOLESALE_PRICES)
        masks = [sum(1 << int(item) for item in rng.choice(n_items, size, replace=False)) for size in sizes]
        return np.array(masks, dtype=float)

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of the components built from the model parameters."""
        super()._prepare(context)
        rng = np.random.default_rng(self.random_state)
        stocks = self._get_random_masks(np.full(self.n_stalls, self.n_items_stocked), rng)
        markups = rng.integers(1, MAX_MARKUP + 1, self.n_stalls) / 100
        lists = self._get_random_masks(rng.integers(1, MAX_LIST_LENGTH + 1, self.n_shoppers), rng)
        stalls = StallElement(
            'stalls',
            stall=np.arange(self.n_stalls, dtype=float),
            stock=stocks,
            markup=markups,
            size=self.n_stalls,
        )
        shoppers = ShopperElement('shoppers', wanted=lists, size=self.n_shoppers)
        self.system_ = System(
            self.label,
            elements=[stalls, shoppers],
            interactions=[ShoppingInteraction('shopping', target='shoppers', stalls='stalls')],
        )

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

Interactions:

  • ShoppingInteraction, from Interaction
class StallElement(Element):
    """Market stall that sells some kinds of produce.

    It sells at the wholesale price plus its markup.

    Args:
        label:
            The label of the agent or group.

        stall:
            The index of the stall.

        stock:
            The bit mask of the kinds of produce that the stall sells.

        markup:
            The relative markup on the wholesale prices.

        values:
            Further values by name, or `None` for none.

        size:
            The number of stalls, or `None` for one.

        space:
            The space of the stalls, or `None` for none.

    Examples:
        >>> from skcomplex.systems.economics.markets import StallElement
        >>> StallElement('stalls', stall=[0, 1, 2], size=3).size
        3
    """

    def __init__(
        self,
        label: str,
        *,
        stall: Annotated[object, NN] = 0,
        stock: Annotated[object, NN] = 0,
        markup: Annotated[object, RealSet.closed_open(0, oo)] = 0.1,
        values: Mapping[str, object] | None = None,
        size: int | None = None,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, values=values, size=size, space=space)
        self.stall = stall
        self.stock = stock
        self.markup = markup

class ShopperElement(Element):
    """Shopper with a shopping list, who visits stalls by `ShoppingInteraction`.

    Args:
        label:
            The label of the agent or group.

        wanted:
            The bit mask of the items on the shopping list.

        visited:
            The bit mask of the stalls already visited.

        missed:
            The bit mask of the items given up.

        spent:
            The money spent.

        values:
            Further values by name, or `None` for none.

        size:
            The number of shoppers, or `None` for one.

        space:
            The space of the shoppers, or `None` for none.

    Examples:
        >>> from skcomplex.systems.economics.markets import ShopperElement
        >>> ShopperElement('shoppers', wanted=3, size=5).size
        5
    """

    def __init__(
        self,
        label: str,
        *,
        wanted: Annotated[object, NN] = 0,
        visited: Annotated[object, NN] = 0,
        missed: Annotated[object, NN] = 0,
        spent: Annotated[object, RealSet.closed_open(0, oo)] = 0.0,
        values: Mapping[str, object] | None = None,
        size: int | None = None,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, values=values, size=size, space=space)
        self.wanted = wanted
        self.visited = visited
        self.missed = missed
        self.spent = spent

class ShoppingInteraction(Interaction):
    """Shopping of every shopper, who visits one stall a step and buys what it wants there.

    A shopper only visits stalls it has not visited yet, and gives up the items that it has not found once it has
    visited every stall.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the group of shoppers.

        stalls:
            The path of the group of stalls.

    Examples:
        >>> from skcomplex.systems.economics.markets import ShoppingInteraction
        >>> ShoppingInteraction('shopping', target='shoppers', stalls='stalls').stalls
        'stalls'
    """

    law_classes = (UpdateLaw,)

    def __init__(self, label: str, *, target: Role, stalls: Role) -> None:
        super().__init__(label)
        self.target = target
        self.stalls = stalls

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the shopping rule of every shopper.

        Args:
            context:
                The namespace of the shoppers and the stalls.
        """
        super()._prepare(context)
        self.laws_: list[BaseLaw] = [UpdateLaw(context.target, self.shop)]

    def shop(self, agent: ShopperView, system: SystemView) -> None:
        """Visit a random stall not visited yet and buy every wanted item that it sells.

        Args:
            agent:
                The shopper.

            system:
                The system.
        """
        wanted, visited = int(agent.wanted), int(agent.visited)
        if not wanted:
            return
        unvisited = [
            stall for stall in cast(list[StallView], system.get_agents('stalls')) if not visited >> int(stall.stall) & 1
        ]
        if not unvisited:
            agent.missed, agent.wanted = wanted, 0
            return
        stall = unvisited[int(system.rng.integers(len(unvisited)))]
        bought = wanted & int(stall.stock)
        agent.visited = visited | 1 << int(stall.stall)
        agent.spent += (1 + stall.markup) * sum(WHOLESALE_PRICES[item] for item in _items_of(bought))
        agent.wanted = wanted & ~bought

Equations

The equations are those that symbolic returns for the system with its default parameters.