[go: up one dir, main page]

File: xrandom.py

package info (click to toggle)
solfege 3.10.3-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 12,408 kB
  • ctags: 4,270
  • sloc: python: 22,161; xml: 7,536; ansic: 4,442; makefile: 685; sh: 308
file content (112 lines) | stat: -rw-r--r-- 4,305 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from __future__ import division
# GNU Solfege - free ear training software
# Copyright (C) 2005, 2007, 2008 Tom Cato Amundsen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import random
import cfg

class Random(object):
    def __init__(self, choices):
        #TODO add assert that all choices are unique
        self.m_choices = choices
        self.m_choice_count = len(choices) * [0]
        # The number of randoms generated
        self.m_count = 0
        self.m_last_choices = []
    def reset(self):
        self.m_last_choices = []
        self.m_choice_count = len(self.m_choices) * [0]
        self.m_count = 0
    def get_random_by_random_data(self, available_choices):
        not_selected_factor = 2
        data = []
        count = 0.0
        for k in available_choices:
            count += self.m_choice_count[k] / self.m_count
            data.append([k, self.m_choice_count[k] / self.m_count, 0.0])
        maxval = 0.0
        for v in data:
            if v[1] != 0.0:
                v[2] = 1/pow(v[1], cfg.get_float("app/randomness"))
                maxval = max(maxval, v[2])
        # Give the questions that has not been asked any times, twice as
        # big chance to be asked as the question that has been asked
        # fewest times.
        for v in data:
            if v[1] == 0.0:
                v[2] = maxval * not_selected_factor
        return data
    def random_by_random(self, available_choices):
        """
        Select by random, but make it more likely that choices that
        has been selected few times are selected than questions that has
        been selected many times.
        """
        if self.m_count == 0:
            return random.choice(available_choices)
        data = self.get_random_by_random_data(available_choices)
        f = 0.0
        for v in data:
            f += v[2]
            v.append(f)
        selectval = data[-1][3] * random.random()
        for v in data:
            if selectval < v[3]:
                return v[0]
        return data[-1][0]
    def random_by_random2(self, available_choices):
        """
        Select by random, but make it more likely that choices that
        has been selected few times are selected than questions that has
        been selected many times.
        """
        if self.m_count == 0:
            return random.choice(available_choices)
        data = self.get_random_by_random_data(available_choices)
        if self.m_last_choices:
            data[self.m_last_choices[-1]][2] *= 0.5
        if len(self.m_last_choices) >= 2 and self.m_last_choices[-1] == self.m_last_choices[-2]:
            data[self.m_last_choices[-1]][2] *= 0.5
        f = 0.0
        for v in data:
            f += v[2]
            v.append(f)
        selectval = data[-1][3] * random.random()
        for v in data:
            if selectval < v[3]:
                return v[0]
        return data[-1][0]
    def random_by_selection(self, available_choices):
        """
        The smallest randomness value is 1
        Larger value make things more even.
        Smaller value make things more random.
        """
        v = []
        if self.m_count > len(self.m_choices):
            for k in available_choices:
                if self.m_choice_count[k] / self.m_count < 1 / (len(self.m_choices) * self.get_float("app/randomness")):
                    v.append(k)
            if v:
                return random.choice(v)
        return random.choice(self.m_choices)
    def add(self, idx):
        self.m_choice_count[idx] += 1
        self.m_count += 1
        self.m_last_choices.append(idx)
        if len(self.m_last_choices) > len(self.m_choices) * 2:
            self.m_last_choices.pop(0)