"""Generic algorithms which are designed by mutwo authors."""importnumbersimporttypingimportnumpyasnp# type: ignorefrommutwoimportcore_eventsfrommutwoimportcore_utilities__all__=("DynamicChoice",)
[docs]classDynamicChoice(object):"""Weighted random choices with dynamically changing weights. :param value_sequence: The items to choose from. :type value_sequence: typing.Sequence[typing.Any] :param curve_sequence: The dynamically changing weight for each value. :type curve_sequence: typing.Sequence[core_events.Envelope] :param random_seed: The seed which shall be set at class initialisation. :type random_seed: int **Example:** >>> from mutwo import core_events >>> from mutwo import core_generators >>> dynamic_choice = core_generators.DynamicChoice( >>> [0, 1, 2], >>> [ >>> core_events.Envelope([(0, 0), (0.5, 1), (1, 0)]), >>> core_events.Envelope([(0, 0.5), (0.5, 0), (1, 0.5)]), >>> core_events.Envelope([(0, 0.5), (1, 1)]), >>> ], >>> ) >>> dynamic_choice.gamble_at(0.3) 2 >>> dynamic_choice.gamble_at(0.3) 2 >>> dynamic_choice.gamble_at(0.3) 0 """def__init__(self,value_sequence:typing.Sequence[typing.Any],curve_sequence:typing.Sequence[core_events.Envelope],random_seed:int=100,):assertlen(value_sequence)==len(curve_sequence)self._value_sequence=value_sequenceself._curve_sequence=curve_sequenceself._random=np.random.default_rng(random_seed)def__repr__(self)->str:returnf"{type(self).__name__}({self._value_sequence})"
[docs]defgamble_at(self,time:numbers.Real)->typing.Any:"""Return value at requested time. :param time: At which position on the x-Axis shall be gambled. :type time: numbers.Real :return: The chosen value. """weight_list=[curve.value_at(time)forcurveinself._curve_sequence]returnself._random.choice(self._value_sequence,p=core_utilities.scale_sequence_to_sum(weight_list,1))