Exemple #1
0
def alt_combine_markov_chains(main, adjustment, alpha):
    r"""
    Takes two Pykov chains and creates a linear combination of them.

    #. `main_chain`: the starting point for the combined chain
    #. `adjustment_chain`: steers the output away from `main_chain`
    #. 0 < `adjustment` < 0.5: the magnitude of the adjustment

    **Note: name is due to improvement over older version.**
    """
    LOG.debug('Main chain: {m}'.format(m=main))
    main_copy = pykov.Chain(main)
    adjustment_copy = pykov.Chain(adjustment)

    main_keys = set((key for key in main))
    adjustment_keys = set((key for key in adjustment))
    main_start_keys = set((key[0] for key in main_keys))
    adjustment_start_keys = set((key[0] for key in adjustment_keys))

    _alt_supplement_chain(main_copy, main_keys, main_start_keys,
                          adjustment_copy, adjustment_keys,
                          adjustment_start_keys)
    _alt_supplement_chain(adjustment_copy, adjustment_keys,
                          adjustment_start_keys, main_copy, main_keys,
                          main_start_keys)
    return (1 - alpha) * main_copy + alpha * adjustment_copy
Exemple #2
0
def to_pykov_chains(global_structure):
    global_structure_new = dict()
    for key, chain_dict in global_structure.iteritems():
        for name, chain in chain_dict.iteritems():
            chain_dict[name] = pykov.Chain(
                chain)  # replace dict with pykov chain
    return global_structure
 def testCounter(self):
     import pykov
     mc = pykov.Chain()
     mc["A", "B"] += 1
     self.assertEqual(mc["A", "B"], 1, msg="Increment counter once")
     mc["A", "B"] += 1
     self.assertEqual(mc["A", "B"], 2, msg="Increment counter twice")
def transition_matrix_to_pykov_chain(matrix):
    chain = pykov.Chain()

    for i, row in enumerate(matrix):
        for j, column in enumerate(row):
            chain[(i, j)] = column
    return chain
Exemple #5
0
def to_mc(
    self
):  # prende in argomento una matrice stocastica e la transforma in una catena di markov
    n = len(self)
    mcs = {}
    for i in range(0, n):
        for j in range(0, n):
            mcs[(i, j)] = self[i][j]
    return pykov.Chain(mcs)
    def setFirstLocationAndWhen(self, first_location, first_when):
        self.current_cell = first_location
        self.current_when = first_when
        self.where_inputs.append(self.current_cell)
        self.when_inputs.append(self.current_when)

        self.counter_vector = pykov.Vector({self.current_cell: 1})
        self.normalized_vector = self.counter_vector.copy()
        self.T = pykov.Chain({(self.current_cell, self.current_cell): 1})
        self.learn_max_probable_cells_from_each_cells(
            self.most_probable_transition_matrix, self.t)
Exemple #7
0
    def generate(self,
                 head: Any = None) -> Generator[Tuple[Any, str], None, None]:
        """Return a generator that yields a new message on every next call.

        Args:
            head: User to start the interaction with. If no user supplied, the sender of the earliest message is used. Note, this user will not send a message but will be the user the first generated message responds to.

        Examples:
            # Generate 2 messages
            >>> gen = cc.generate('Alice')
            >>> for _ in range(2):
                    tpl = next(gen)
                    print(": ".join(tpl))

            Bob: Hey, what's up?
            Charlie: Shuf is fantastic. I tried it on a 78 billion line text file.

            # Generates an infinite amount of messages
            >>> for tpl in cc.generate('Alice')():
                    print(": ".join(tpl))

            Bob: Hey, what's up?
            Charlie: Shuf is fantastic. I tried it on a 78 billion line text file.
            ...
        """
        user = pykov.Vector({self.messages[0][0] if head is None else head:
                             1})  # starting point for markov chain
        while True:
            choice = (
                user *
                self.Musers).choose()  # choose next user to write a message
            if choice is None: return
            user = pykov.Vector(
                {choice: 1})  # create pykov vector for future user calculation
            if choice in self.word_matrices:
                Mword = self.word_matrices[choice]
            else:  # create word matrix
                Mword = pykov.Matrix({(1, 1):
                                      1})  # create matrix for word chain
                for mes in [m for m in self.messages
                            if m[0] == choice]:  # add every word to matrix
                    splt = mes[1].split(' ')
                    if (0, splt[0]) not in Mword: Mword[(0, splt[0])] = 0
                    Mword[(0, splt[0])] += 1
                    for word, nxt in zip(splt[:-1], splt[1:]):
                        if (word, nxt) not in Mword: Mword[(word, nxt)] = 0
                        Mword[(word, nxt)] += 1
                    if (splt[-1], 1) not in Mword: Mword[(splt[-1], 1)] = 0
                    Mword[(splt[-1], 1)] += 1
                Mword.stochastic()
                if not self.enhance_speed: self.word_matrices[choice] = Mword

            C = pykov.Chain(Mword)
            yield (choice, " ".join(C.walk(self.MAX_WALK_LENGTH, 0, 1)[1:-1]))
 def _markov_chain(time_step, day_time_series, time_step_size):
     next_time_step = WeekMarkovChain._add_delta_to_time(time_step, time_step_size)
     current_vector = day_time_series.ix[time_step]
     next_vector = day_time_series.ix[next_time_step]
     chain_elements = [((current_state, next_state), WeekMarkovChain._probability(current_state,
                                                                                  next_state,
                                                                                  current_vector,
                                                                                  next_vector))
                       for current_state in Activity
                       for next_state in Activity]
     return pykov.Chain(OrderedDict(chain_elements))
Exemple #9
0
    def __init__(self, has_bike=False):
        self.utilities = pk.Chain()
        self.has_bike = has_bike
        self.user_base = the_base

        self.user_base.add_user(self)

        ## Initialize utilities
        self.utilities["sweat"] = 0
        self.utilities["scenery"] = 0
        self.utilities["social"] = 0
        self.utilities["time"] = 0
def get_markov_chain(labels, prob_matrix):
    mc = pk.Chain()
    for rowindex, rowlabel in enumerate(labels):
        for colindex, collabel in enumerate(labels):
            logging.debug(
                "Adding entry (%s, %s) -> %s" %
                (rowlabel, collabel, prob_matrix[rowindex][colindex]))
            mc[rowlabel, collabel] = prob_matrix[rowindex][colindex]
    logging.debug("chain = %s, converting to stochastic" % mc)
    mc.stochastic()
    logging.debug("final chain = %s" % mc)
    return mc
Exemple #11
0
def main():
    # N = 100
    # B = list(range(0, int(N/2), 1))
    # C1 = [int((N - i)/2) for i in B]
    # C2 = [N - B[i] - C1[i] for i in range(len(B))]
    # k = 20
    # alpha = list(range(int(k/2), k+1))
    N = 100
    B = 20
    C1 = 40
    C2 = 40
    k = 20
    alpha = 15
    states = []
    for i in range(int((C1 + C2) / 2), C1 + C2 + 1):
        states.append((i, C1 + C2 - i))
    chain = pykov.Chain()
    for i in range(1, len(states) - 1):
        # print(states[i])
        # Prob of going up is prob choosing red and getting maj blue
        up = (states[i][0] / (C1 + C2)) * hyper(N, states[i][1], k, alpha)
        # Prob of going down is prob of choosing blue
        down = (states[i][1] / (C1 + C2)) * hyper(N, states[i][0], k, alpha)
        # Prob of staying is prob of choosing blue and getting maj blue
        # or choosing red and getting maj red
        # stay = ((states[i][0]/(C1+C2))*hyper(N, states[i][0], k, alpha))+((states[i][1]/(C1+C2))*hyper(N, states[i][1], k, alpha))
        stay = 1 - up - down
        chain[(str(states[i]), str(states[i - 1]))] = up
        chain[(str(states[i]), str(states[i + 1]))] = down
        chain[(str(states[i]), str(states[i]))] = stay
    chain[(str(states[0]), str(
        states[0]))] = 1 - (states[0][1] /
                            (C1 + C2)) * hyper(N, states[0][0], k, alpha)
    chain[(str(states[0]),
           str(states[1]))] = (states[0][1] /
                               (C1 + C2)) * hyper(N, states[0][0], k, alpha)
    chain[(str(states[len(states) - 1]), str(
        states[len(states) -
               1]))] = 1 - (states[i][0] /
                            (C1 + C2)) * hyper(N, states[i][1], k, alpha)
    chain[(str(states[len(states) - 1]),
           str(states[len(states) -
                      2]))] = (states[i][0] /
                               (C1 + C2)) * hyper(N, states[i][1], k, alpha)
    # for state_i in states:
    #     for state_j in states:
    for k in chain:
        print(k, chain[k])
    p = pykov.Vector({str((40, 40)): 1})
    print(chain.pow(p, 300000))
def buildMarkovChain(phrasesFile):
    phrases = construct_phrases_list(phrasesFile)
    print(phrases)
    wordCount = buildWordCount(phrases)
    connectionsCount = buildConnectionsCount(phrases)

    vector = pykov.Matrix()

    for connection in connectionsCount:
        initialState = connection[0]
        #print("(" + str(connectionsCount[connection]) + ", " + str(wordCount[initialState]) + ")")
        probability = (float(connectionsCount[connection]) /
                       float(wordCount[initialState]))
        #print(probability)
        vector[connection] = probability

    return pykov.Chain(vector)
Exemple #13
0
def transition_matrix(x):
    a = x.FeedbackColor.replace(['green', 'yellow'], [1, 0]).values
    b = np.zeros((2, 2))
    for (x, y), c in Counter(zip(a, a[1:])).iteritems():
        b[x - 1, y - 1] = c

    green_green = b[0][0]
    green_yellow = b[0][1]
    yellow_green = b[1][0]
    yellow_yellow = b[1][1]

    chain = pykov.Chain({
        ('green', 'yellow'): green_yellow,
        ('yellow', 'green'): yellow_green,
        ('green', 'green'): green_green,
        ('yellow', 'yellow'): yellow_yellow
    })
    return chain
def markov_chain(states):
    T = pykov.Matrix()
    markov_states = []
    for i in xrange(0, len(states) - 1):
        markov_states.append((
            states[i],
            states[i + 1],
        ))

    for i in xrange(len(markov_states)):
        total = len([j for j in markov_states if j[0] == markov_states[i][0]]),
        amount = markov_states.count(markov_states[i])
        markov_states[i] += (amount / float(total[0]), )
    for i in markov_states:
        if (i[0], i[1]) not in T:
            T[(i[0], i[1])] = i[2]

    return pykov.Chain(T)
Exemple #15
0
def generate():
    import pykov

    phrasesData = ''
    with open('sayings.txt', "rt") as f:
        phrasesData = f.read()
    phrasesLines = phrasesData.split("\n")
    phrases = phrasesLines

    wordCount = dict()
    for phrase in phrases:
        words = phrase.split(" ")
        for index in range(len(words)):
            word = words[index]
            if word not in wordCount:
                wordCount[word] = 0
            wordCount[word] = wordCount.get(word) + 1

    connectionsCount = dict()
    for phrase in phrases:
        words = phrase.split(" ")
        for index in range(len(words) - 1):
            currState = (words[index], words[index + 1])
            if currState not in connectionsCount:
                connectionsCount[currState] = 0
            connectionsCount[currState] = connectionsCount.get(currState) + 1

    vector = pykov.Matrix()

    for connection in connectionsCount:
        initialState = connection[0]
        probability = (float(connectionsCount[connection]) /
                       float(wordCount[initialState]))
        vector[connection] = probability
    mc = pykov.Chain(vector)

    newPhrases = mc.walk(100, '+', '+\r')
    newPhrases = newPhrases[1:len(newPhrases) - 1]

    newPhrases = " ".join(newPhrases)
    newPhrase = newPhrases.split('+')[0].strip()
    return newPhrase.replace("í", "'")
def markov_chain(states, transitions, policy):
    import pykov
    T = pykov.Chain()
    q = Queue.Queue()
    visited = [False] * len(states)
    visited[0] = True
    q.put(0)
    start = pykov.Vector({states[0]: 1})
    while not q.empty():
        state_idx = q.get()
        pol = policy[state_idx]
        for i, p in enumerate(transitions[pol][state_idx]):
            if p > 0:
                if i == len(states):
                    T[(states[state_idx], "exit")] = p
                    T[("exit", "exit")] = 1
                else:
                    T[(states[state_idx], states[i])] = p
                    if not visited[i]:
                        q.put(i)
                        visited[i] = True
    return T, start
Exemple #17
0
    def __init__(self, ax, span=60, feedback=False):
        self.x = x[half_w]
        self.target = ax.plot(self.x,
                              0,
                              ' o',
                              color='darkgreen',
                              alpha=0.75,
                              markersize=12,
                              animated=True)[0]

        self.ax = ax
        self.feedback = feedback
        self.errs = []
        self.colors, self.fake_colors = [], []
        self.greens, self.yellows = [], []
        self.span = span  # average over 60 measurements @ 60FPS = 1 second
        self.fake = pykov.Chain({
            ('green', 'green'): 0.99688064531915932,
            ('yellow', 'yellow'): 0.98290598290598286,
            ('yellow', 'green'): 0.017094017094017096,
            ('green', 'yellow'): 0.0031193546808406512
        })

        self.fake_walk = self.fake.walk(2000)
Exemple #18
0
    def walk_mc(self, choice=None, i1=None, i2=None):
        song = pykov.Chain(self.oChain)

        if choice == None or choice == 'RAND':
            choice = random.choice(self.totallst).rstrip()
        if i1 == None:
            i1 = 6
        if i2 == None:
            i2 = 12

        #print("WALK")
        randLength = random.randint(i1, i2)
        song.move(choice)
        output = "['"
        output += choice
        output += "', '"
        reccTimes = 0
        dualReccTimes = 0
        noteList = []
        noteList.append(choice)
        for x in range(0, randLength):
            if x == 0:
                note = song.move(choice)
                noteList.append(note)
                output += note
                #output += "', '"
            else:
                tempNote = song.move(note)

                #handles recurring notes
                if tempNote == noteList[x - 1]:
                    #save first note to come into the duplicate note
                    #handling
                    if reccTimes == 0:
                        firstCaughtDuplicate = tempNote

                    #if note that came in as duplicate isn't the original
                    #need to reset the times it has occured and reset
                    #the first caught duplicate
                    if firstCaughtDuplicate != tempNote:
                        reccTimes = 0
                        firstCaughtDuplicate = tempNote

                    #if note has occurred twice in a row, new note needs
                    #to be generated and reocc needs to be reset
                    #else, increment the amount of times the note has recurred
                    if reccTimes >= MAXIMUM_RECURRING_TIMES:
                        if song.walk_propability([tempNote, tempNote]) == 0:
                            tempNote = song.move(
                                random.choice(self.totallst.rstrip()))
                        while tempNote == note:
                            tempNote = song.move(note)
                        reccTimes = 0
                    else:
                        reccTimes += 1

                #handles recurring dual notes
                if x > 2 and noteList[x - 3] == noteList[x - 1] and noteList[
                        x - 2] == tempNote:
                    #save first note to come into the duplicate note
                    #handling
                    if dualReccTimes == 0:
                        dualNote1 = noteList[x - 1]
                        dualNote2 = tempNote

                    #if note that came in as duplicate isn't the original
                    #need to reset the times it has occured and reset
                    #the first caught duplicate
                    if dualNote1 != noteList[x - 1] and dualNote2 != tempNote:
                        dualReccTimes = 0
                        dualNote1 = noteList[x - 1]
                        dualNote2 = tempNote

                    #if dual note set has occurred twice in a row, new note needs
                    #to be generated and dualRecc needs to be reset
                    #else, increment the amount of times the note has recurred
                    if reccTimes >= MAXIMUM_RECURRING_TIMES:
                        while tempNote == noteList[x - 1]:
                            tempNote = song.move(note)
                        dualReccTimes = 0
                    else:
                        dualReccTimes += 1
                note = tempNote
                noteList.append(note)
                output += "', '"
                output += note
            #print ("%d: %s" % (x, note))
        output += "']"
        print(noteList)
        #print (song.walk_propability(choice))
        return song.walk(random.randint(i1, i2), choice)
a = np.array([s, i, h, u, o, r])
str_states = ['S', 'I', 'H', 'U', 'O', 'R']
states = dict(zip(list(range(1, len(a) + 1)), str_states))
dic = {(states[i + 1], states[j + 1]): a[i][j]
       for i in range(len(a)) for j in range(len(a[i]))}
M = pykov.Matrix(dic)
print(M, end='\n\n')

# Todos os estados
print(M.states(), end='\n\n')
# Antecessores de cada estado
print(M.pred(), end='\n\n')
# Sucessores de cada estado
print(M.succ(), end='\n\n')

C = pykov.Chain(dic)
state = pykov.Vector(S=1)
# Distribuição de prob após N dias, começando no estado state
n_inicio = 282  # dias, entre o dia do primeiro infetado em Portugal e o dia da submissão do projeto
dia274 = C.pow(state, n_inicio)
n_fim = 365 - (31 + 28 + 1)
fim2020 = C.pow(state, n_fim)

# % aumento de recuperados esperado entre 09/12/2020 e 01/01/2021
rate_r = (fim2020 - dia274)['R'] * 100
# média de recuperados diários esperada entre 09/12/2020 e 01/01/2021
pop = 10.28 * 10**6
dias = n_fim - n_inicio
avg_r_daily = (rate_r / 100 * pop / dias)
print('rate_r {}\navg_r_daily {}\n'.format(round(rate_r, 2),
                                           round(avg_r_daily)))
    ('coal', 'NHN'): 0.2658,
    ('oil', 'WW'): 0.1399,
    ('oil', 'COD'): 0.1820,
    ('oil', 'NHN'): 0.2666,
    ('chemistry', 'WW'): 0.1397,
    ('chemistry', 'COD'): 0.1826,
    ('chemistry', 'NHN'): 0.1916,
    ('smelt', 'WW'): 0.0759,
    ('smelt', 'COD'): 0.0515,
    ('smelt', 'NHN'): 0.0417,
    ('farm', 'WW'): 0.0458,
    ('farm', 'COD'): 0.1116,
    ('farm', 'NHN'): 0.0355,
})

T = pykov.Chain(d)

import matplotlib.pyplot as plt
import numpy as np

# create some data to use for the plot
dt = 0.001
t = np.arange(0.0, 10.0, dt)
r = np.exp(-t[:1000] / 0.05)  # impulse response
x = np.random.randn(len(t))
s = np.convolve(x, r)[:len(x)] * dt  # colored noise

# the main axes is subplot(111) by default
plt.plot(t, s)
plt.axis([0, 1, 1.1 * np.amin(s), 2 * np.amax(s)])
plt.xlabel('time (s)')
Exemple #21
0
    def __init__(self, num_states, rand=True, arm_count=0):
        self.num_states = num_states

        self.cumulative_reward = 0.0
        self.pull_count = 0

        self.rand = rand
        self.arm_count = arm_count

        if self.rand:
            # create ergodic transition matrix
            self.tran_matrix = np.zeros((num_states, num_states))
            ergodic = False
            while ergodic == False:
                P = np.random.rand(num_states, num_states)
                P = preprocessing.normalize(P,
                                            norm='l1',
                                            axis=1,
                                            copy=True,
                                            return_norm=False)
                dic = {}
                for i in range(num_states):
                    for j in range(num_states):
                        dic[(i, j)] = P[i, j]
                T = pykov.Chain(dic)
                G = nx.DiGraph(list(T.keys()))
                if nx.is_strongly_connected(G) and nx.is_aperiodic(G):
                    ergodic = True
                    self.tran_matrix = P
        else:
            if self.arm_count == 0:
                self.tran_matrix = np.array([[0., 1.], [.01, .99]])
            if self.arm_count == 1:
                self.tran_matrix = np.array([[.99, .01], [1., 0.]])

        # find the steady state distribution
        mc = markovChain(self.tran_matrix)
        mc.computePi('linear')  # We can also use 'power', 'krylov' or 'eigen'
        self.steady_state = np.asmatrix(mc.pi)

        # find ergodic constants
        P_tilde = np.zeros(
            (self.num_states,
             self.num_states))  # P_tilde as per Formula 2.1 in Page 65
        for x in range(self.num_states):
            for y in range(self.num_states):
                P_tilde[x, y] = self.steady_state[0, y] * self.tran_matrix[
                    y, x] / self.steady_state[0, x]

        MP = np.dot(self.tran_matrix, P_tilde)
        eigenval, _ = np.linalg.eig(MP)
        self.eta = np.sort(eigenval)[-2]  # Second largest eigenvalue
        self.eta = np.sqrt(self.eta)  # sqrt for TV norm

        # Now we need to compute C using chi_0 square in Thm 2.7
        # Here we don't have information about current state,
        # given a steady state distribution, chi_0 is maximized when current state is of the form (1,0,0,0,0,0)
        curr_state = np.zeros((1, self.num_states))
        index = np.argmin(self.steady_state)
        curr_state[0, index] = 1.0

        self.C = 0.25 * sum([(self.steady_state[0, i] - curr_state[0, i])**2 /
                             self.steady_state[0, i]
                             for i in range(self.num_states)])
        self.C = np.sqrt(self.C)  # sqrt for TV norm

        if self.rand:
            self.reward_type = np.random.randint(0, 3)
            self.set_reward_parameters()
        else:
            if self.arm_count == 0:
                self.rewards = np.array([0, 1])
            elif self.arm_count == 1:
                self.rewards = np.array([.5, .5])
Exemple #22
0
    26, 24, 43, 45, 35, 48, 22, 41, 47, 60, 47, 33, 32, 31, 39, 23, 20, 38, 33,
    24, 37, 29, 58, 31, 42, 30, 39, 28, 41, 29, 38, 35, 42, 35, 32, 49, 23, 39,
    37, 31, 42, 29, 34, 37, 46, 31, 35, 31, 32, 52, 53, 41, 34, 34, 46, 29, 35,
    33, 35, 40, 39, 34, 19, 28, 16, 30, 41, 32
])

total_pcts = (total_kills - total_errors) / total_attempts

# Load set scores.

with open("./set_scores.csv") as f:
    reader = csv.reader(f)
    set_scores = list(reader)

# The csv module doesn't do any conversion for us, so convert the set scores
# from strings to integers.

# 2017-09-03: I note that at least one set has less than 25 plays recorded.
# This is impossible for a 25-point set, but it looks like it's only one set,
# so I don't really care right now.
set_scores = [[int(s) for s in score] for score in set_scores]

transition_matrix = estimate_transition_probs(set_scores)
P = transition_matrix
chain = pykov.Chain({
    ("OU", "OPP"): P[0, 1],
    ("OU", "OU"): P[1, 1],
    ("OPP", "OU"): P[1, 0],
    ("OPP", "OPP"): P[0, 0]
})
def prepare_pykov_chain_with_single_user_mobility_states(
        grouped_by_date_grid_by_minute_of_day_mp_fetched, current_day_str,
        predicted_day_str):
    list_of_day_str_applicable = support.difference_of_days(
        current_day_str, predicted_day_str)

    from_to_user_entry_dict = {}
    for date, grid_by_minute_of_day_mp_fetched in grouped_by_date_grid_by_minute_of_day_mp_fetched.items(
    ):
        if support.calc_day(date) in list_of_day_str_applicable:

            #It seems this day is a contender in the prediction.
            #For each minute of the day look at the cell id(source) and look at the next because that is the destination
            #Each pair of source and destination should be added to the from_to user_entry_dict (date doesn't matter anymore as link is formed)
            #Using the source as the key and adding the destination to the list

            i = 0
            for source_mp_fetched in grid_by_minute_of_day_mp_fetched:
                if i == common_config.MINUTES_IN_A_DAY - 1:
                    #We have reached the end of the day, must look at the next if possible in the next day

                    next_date = date + support.Range.RESOLUTION_DATETIME_DAY
                    if support.calc_day(
                            next_date) in list_of_day_str_applicable:
                        #It seems the adjoining day is also a contender in the prediction. Configure the destination correctly
                        destination_mp_fetched = grouped_by_date_grid_by_minute_of_day_mp_fetched[
                            next_date][0]
                    else:
                        #There is no connection to the next day
                        destination_mp_fetched = None
                else:
                    #In the middle of the day, destination is next in the grid
                    destination_mp_fetched = grid_by_minute_of_day_mp_fetched[
                        i + 1]

                if destination_mp_fetched:
                    source_user_entry = UserEntry(
                        source_mp_fetched.day, source_mp_fetched.minute_of_day,
                        source_mp_fetched.cell_id)
                    destination_user_entry = UserEntry(
                        destination_mp_fetched.day,
                        destination_mp_fetched.minute_of_day,
                        destination_mp_fetched.cell_id)

                    #Add it to the from to user entry dict but with the source_user_entry as key
                    #Add it as a key if it doesn't exist yet
                    if source_user_entry in from_to_user_entry_dict:
                        from_to_user_entry_dict[source_user_entry].append(
                            destination_user_entry)
                    else:
                        from_to_user_entry_dict[source_user_entry] = [
                            destination_user_entry
                        ]

                i += 1

    #Calculate all percentages the same from and to user entry coincide
    pykov_chain_entries = {}
    for starting_user_entry, destinations in from_to_user_entry_dict.items():
        starting_key = starting_user_entry.to_pykov_key()
        total_amount = float(len(destinations))
        grouped_by_cell_id = support.group_by(
            destinations, lambda user_entry: user_entry.cell_id)

        for destination_cell_id, destinations_with_same_cell_id in grouped_by_cell_id.items(
        ):
            destination_key = destinations_with_same_cell_id[0].to_pykov_key()
            amount_of_destinations_with_same_cell_id = len(
                destinations_with_same_cell_id)
            percentage = float(
                amount_of_destinations_with_same_cell_id) / total_amount

            pykov_chain_entries[starting_key, destination_key] = percentage

    return pykov.Chain(pykov_chain_entries)
Exemple #24
0
def language_game(number_of_agents, number_of_rounds, plot_time_points,
                  word_frequencies, initial_word_memory,
                  initial_word_transitions, reward):

    history = []
    agents = {}

    for agent_int in range(number_of_agents):
        agents[str(agent_int)] = {
            'id': agent_int,
            'word memory': deepcopy(initial_word_memory),
            'word transitions': pykov.Chain(deepcopy(initial_word_transitions))
        }

    def update_association_strength(agent, word, speaker_association, reward):
        successor_states = agent['word transitions'].succ(word)
        transition_probabilities = markov_update_words(successor_states,
                                                       speaker_association,
                                                       reward)
        for state, value in transition_probabilities.items():
            agent['word transitions'][word, state] = value

    def communicate(speaker, hearer, word, speaker_association,
                    hearer_association):
        success = 0
        if hearer_association != speaker_association:
            pass
        else:
            success = 1
            update_association_strength(hearer, word, speaker_association,
                                        reward)
            update_association_strength(speaker, word, speaker_association,
                                        reward)
            speaker['word memory'] = markov_update_words(
                speaker['word memory'], speaker_association, reward)
            hearer['word memory'] = markov_update_words(
                hearer['word memory'], speaker_association, reward)
        return success

    def communicate_teach(speaker, hearer, word, speaker_association,
                          hearer_association):
        success = 0
        if hearer_association != speaker_association:
            update_association_strength(hearer, word, speaker_association,
                                        reward)
            hearer['word memory'] = markov_update_words(
                hearer['word memory'], speaker_association, reward)
        else:
            success = 1
            update_association_strength(hearer, word, speaker_association,
                                        reward)
            update_association_strength(speaker, word, speaker_association,
                                        reward)
            speaker['word memory'] = markov_update_words(
                speaker['word memory'], speaker_association, reward)
            hearer['word memory'] = markov_update_words(
                hearer['word memory'], speaker_association, reward)
        return success

    for i in range(number_of_rounds):

        random_agents = random.sample(range(number_of_agents), 2)
        speaker_id = random_agents[0]
        hearer_id = random_agents[1]
        speaker = agents[str(speaker_id)]
        hearer = agents[str(hearer_id)]

        word = speaker['word memory'].choose()

        if i % plot_time_points == 0:
            word_index = word_frequencies['word indices'][word]
            index = int(i / plot_time_points)
            word_frequencies['word frequencies'][word_index][index] += 1

        speaker_association = speaker['word transitions'].move(word)
        hearer_association = hearer['word transitions'].move(word)

        success = communicate(speaker, hearer, word, speaker_association,
                              hearer_association)

        history.append(success)

        agent_memories = []

        word_frequencies['word associations'][word_frequencies['word indices'][
            word]][word_frequencies['word indices'][speaker_association]] += 1

    agent_memory_matrix = [
        np.zeros((len(initial_word_memory), len(initial_word_memory)))
        for i in range(number_of_agents)
    ]

    for agent_id, agent_values in agents.items():
        word_memory = agent_values['word memory'].values()
        agent_memories.append(list(word_memory))
    word_frequencies['word game counts'].append(np.mean(agent_memories,
                                                        axis=0))

    return history, word_frequencies
Exemple #25
0
 def _add_transition(self, day, time, from_activity, to_activity, probability):
     chain = self.__chain[day][time]
     elements = OrderedDict(chain)
     elements[(from_activity, to_activity)] = probability
     self.__chain[day][time] = pykov.Chain(elements)