예제 #1
0
    def slice_equally(self, cutter: Agent, amount: int) -> List['CakeSlice']:
        """
        Slices this slice into several parts equal in value according to the cutter.

        :param cutter: cutter of the slices, determines the value for each slice
        :param amount: amount of parts to slice into
        :return: list containing `amount` slices, each equal in value according to
            `cutter`.

        >>> s = CakeSlice(0, 1)
        >>> a = PiecewiseConstantAgent([1, 3, 11], "agent")
        >>> s.slice_equally(a, 2)
        [(0,0.5), (0.5,1)]
        >>> s = CakeSlice(0, 1)
        >>> a = PiecewiseConstantAgent([1, 3, 11], "agent")
        >>> s.slice_equally(a, 4)
        [(0,0.25), (0.25,0.5), (0.5,0.75), (0.75,1)]
        """

        slices = []
        slice_value = cutter.eval(self.start, self.end) / amount
        last_start = self._start

        for i in range(amount - 1):
            end = cutter.mark(last_start, slice_value)
            slices.append(self._create_slice_part(last_start, end))
            last_start = end

        slices.append(self._create_slice_part(last_start, self.end))

        return slices
예제 #2
0
    def mark(self, agent: Agent, slice: CakeSlice,
             desired_value: float) -> float:
        """
        Adds a mark, made by `agent` on a given slice.
        :param agent: agent making the mark
        :param slice: slice to mark
        :param desired_value: satisfaction value wanted by agent, such that
            slice.start -> mark position = `desired_value.
        :return: mark position

        >>> s = CakeSlice(0, 2)
        >>> a = PiecewiseConstantAgent([33, 33], "agent")
        >>> m = Marking().mark(a, s, 33)
        >>> m
        1.0
        """
        position = agent.mark(slice.start, desired_value)

        if slice not in self._slice_to_marks:
            self._slice_to_marks[slice] = []
        self._slice_to_marks[slice].append((agent, position))

        return position