summaryrefslogtreecommitdiff
path: root/unused/weight.py
blob: 4bd4af4a34ec9533c721623cd5637fb134f9b849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
        weights_total = sum(
            map(lambda x: x["weight"], self.probabilities())
        ) + (255 * 255 * 255)
        choice = random.randint(0, weights_total)
        position = 0
        for elem in self.probabilities():
            position += elem["weight"]
            if position >= choice:
                self.value = elem["value"]
                return

        weights_total = sum(
                map(lambda x: x["weight"], self.probabilities())
            )# + self.range_max - self.range_min
        if weights_total < 100:
            weights_total = 100;
        choice = random.randint(0, weights_total)
        import sys
        sys.stderr.write("choosing %s: random_int: %s, probabilities: %s\n" % (
            self.name,
            choice,
            self.probabilities()))
        position = 0
        for elem in self.probabilities():
            position += elem["weight"]
            if position >= choice:
                self.value = elem["value"]
                return


        weights_total = sum(map(lambda x: x["weight"], self.probabilities()))
        choice = random.randint(0, weights_total)
        position = 0
        for elem in self.probabilities():
            position += elem["weight"]
            if position >= choice:
                self.value = elem["value"]
                break
    def _choose_heaviest(self):
        heaviest_idx = 0
        heaviest_weight = 0
        idx = 0
        if (len(self.options())):
            for elem in self.options():
                if elem["weight"] > heaviest_weight:
                    heaviest_weight = elem["weight"]
                    heaviest_idx = idx
                idx += 1
            return self.options()[heaviest_idx]["value"]
        else:
            self.randomize()

    def heaviest(self):
        self.value = self._choose_heaviest()

    """default value is the probability with the heaviest weight"""
    self.default(self._choose_heaviest())