コード例 #1
0
    def _InitWeights(self):
        """Initializes WeightedCollections for use in generating new beans."""
        # Actual production data ends up being a bit too skewed towards the largest
        # regions for gameplay purposes, so we smooth the distribution out by moving
        # all weights towards the median.
        region_weights = self._params.region_weights.AsDict()
        regions_by_weight = sorted(region_weights.items(), key=lambda x: x[1])
        region_value_median = regions_by_weight[len(region_weights) // 2][1]
        smoothed_region_weights = {
            k: v + (region_value_median - v) * 0.5
            for k, v in region_weights.items()
        }
        scale_factor = sum(region_weights.values()) / sum(
            smoothed_region_weights.values())
        smoothed_region_weights = {
            k: scale_factor * v
            for k, v in smoothed_region_weights.items()
        }

        self._weighted_regions = util_lib.WeightedCollection(
            smoothed_region_weights.keys(), smoothed_region_weights.values())
        self._weighted_rarities = util_lib.WeightedCollection(
            self._params.rarity_weights.AsDict().keys(),
            self._params.rarity_weights.AsDict().values())
        self._weighted_varieties = util_lib.WeightedCollection(
            self._params.variety_weights.AsDict().keys(),
            self._params.variety_weights.AsDict().values())

        self._weighted_regions.Freeze()
        self._weighted_rarities.Freeze()
        self._weighted_varieties.Freeze()
コード例 #2
0
ファイル: util_lib_test.py プロジェクト: jellzilla/hypebot
    def testGetItem_doesNotModifyWeights(self):
        c = util_lib.WeightedCollection(str(x) for x in range(5))

        c.GetItem()
        c.GetItem()

        self.assertTrue(all(v == 0.2 for v in c._prob_table.values()))
コード例 #3
0
ファイル: util_lib_test.py プロジェクト: jellzilla/hypebot
    def testSingleItem(self):
        c = util_lib.WeightedCollection(['a'])

        i = c.GetItem()

        self.assertEqual(i, 'a')
        self.assertEqual(c.GetWeight(i), 1.0)
コード例 #4
0
ファイル: util_lib_test.py プロジェクト: jellzilla/hypebot
    def testInitialWeights_omittedWeightsAreSetToOne(self):
        choices = ['a', 'b', 'c']
        weights = (50, 49)
        expected_weights = (0.5, 0.49, 0.01)

        c = util_lib.WeightedCollection(choices, weights)

        self.assertCountEqual(c._prob_table.values(), expected_weights)
コード例 #5
0
ファイル: util_lib_test.py プロジェクト: jellzilla/hypebot
    def testModifyWeight_usesUpdateFn(self):
        c = util_lib.WeightedCollection(('one', 'two', 'four'))
        expected_weight = 2 / 3

        new_weight = c.ModifyWeight('four', lambda _: expected_weight)

        self.assertAlmostEqual(new_weight, expected_weight)
        self.assertAlmostEqual(sum(c._prob_table.values()), 1.0)
コード例 #6
0
ファイル: util_lib_test.py プロジェクト: jellzilla/hypebot
    def testInitalWeights_areUsed(self):
        choices = ['a', 'b', 'c', 'd']
        weights = [4, 3, 2, 1]
        total_weight = sum(weights)
        expected_weights = [x / total_weight for x in weights]

        c = util_lib.WeightedCollection(choices, weights)

        self.assertCountEqual(c._prob_table.values(), expected_weights)
コード例 #7
0
ファイル: command_lib.py プロジェクト: jellzilla/hypebot
 def __init__(self, params, core):
   self._params = params_lib.HypeParams(self.DEFAULT_PARAMS)
   self._params.Override(params)
   self._params.Lock()
   self.command_prefix = '%' if core.params.execution_mode.dev else '!'
   self._core = core
   self._parsers = []
   self._last_called = defaultdict(lambda: defaultdict(float))
   self._ratelimit_lock = Lock()
   self._spook_replies = util_lib.WeightedCollection(messages.SPOOKY_STRINGS)
コード例 #8
0
ファイル: util_lib_test.py プロジェクト: jellzilla/hypebot
    def testFreeze_doesNotRestrictGets(self):
        item = 'x'
        c = util_lib.WeightedCollection([item])
        c.Freeze()

        i = c.GetItem()
        w = c.GetWeight(i)

        self.assertEqual(i, item)
        self.assertEqual(w, 1.0)
コード例 #9
0
ファイル: util_lib_test.py プロジェクト: jellzilla/hypebot
    def testGetAndDownweightItem_updatesProbabilities(self):
        choices = ['a', 'b', 'c', 'd']
        c = util_lib.WeightedCollection(choices)
        initial_weight = 1 / len(choices)

        r = c.GetAndDownweightItem()

        self.assertEqual(r, 'b')
        self.assertGreater(c.GetWeight('a'), initial_weight)
        self.assertLess(c.GetWeight('b'), initial_weight)
        self.assertGreater(c.GetWeight('c'), initial_weight)
        self.assertGreater(c.GetWeight('d'), initial_weight)
        self.assertAlmostEqual(sum(c._prob_table.values()), 1.0)
コード例 #10
0
 def __init__(self, *args):
     super(StoryCommand, self).__init__(*args)
     self._stories = self._params.stories.AsDict()
     self._story_choices = util_lib.WeightedCollection(self._stories.keys())
     # Store active story keyed on channel id.
     self._active_stories = {}
コード例 #11
0
ファイル: command_lib.py プロジェクト: jellzilla/hypebot
 def __init__(self, *args):
   super(TextCommand, self).__init__(*args)
   self._choices = util_lib.WeightedCollection(self._params.choices)
コード例 #12
0
ファイル: util_lib_test.py プロジェクト: jellzilla/hypebot
    def testEmptyCollection(self):
        c = util_lib.WeightedCollection([])

        i = c.GetItem()

        self.assertIsNone(i)
コード例 #13
0
ファイル: util_lib_test.py プロジェクト: jellzilla/hypebot
    def testFreeze_locksProbabilityTable_fromModifyWeight(self):
        c = util_lib.WeightedCollection(['x'])
        c.Freeze()

        with self.assertRaises(RuntimeError):
            c.ModifyWeight('a', lambda x: x)
コード例 #14
0
ファイル: util_lib_test.py プロジェクト: jellzilla/hypebot
    def testFreeze_locksProbabilityTable_fromGetAndDownweightItem(self):
        c = util_lib.WeightedCollection([])
        c.Freeze()

        with self.assertRaises(RuntimeError):
            c.GetAndDownweightItem()
コード例 #15
0
ファイル: util_lib_test.py プロジェクト: jellzilla/hypebot
    def testInitialProbabilities(self):
        c = util_lib.WeightedCollection(['a', 'b', 'c', 'd'])

        self.assertTrue(all(v == 0.25 for v in c._prob_table.values()))