In [29]:
import random
import numpy as np

def normalize(arr):
    s = sum(arr)

    if s == 0:
        s = 1
        arr[0] = 1
    
    for i, val in enumerate(arr):
        arr[i] = val/s
    return arr


def generate(width, height):
    matrix = []

    for i in range(height):
        matrix.append([])

        for j in range(width):
            matrix[i].append(float(random.randint(0, 1000))/1000)
        matrix[i] = normalize(matrix[i])

        matrix[i] = [round(x, 3) for x in matrix[i]]
        
    return np.matrix(matrix)

print generate(4,4)


print normalize([.444,.45, .34, .0])


[[ 0.364  0.167  0.2    0.268]
 [ 0.375  0.066  0.365  0.194]
 [ 0.13   0.007  0.461  0.402]
 [ 0.236  0.178  0.057  0.529]]
[0.35980551053484605, 0.3646677471636953, 0.2755267423014587, 0.0]

In [45]:
def affinity(personality_matrix, num):

    print "matrix:", personality_matrix
    matrix = []
    for elem in range(len(personality_matrix)):
        if elem == num:
            matrix.append(float(random.randint(0, 1000) + 750)/1000)
        else:
            matrix.append(float(random.randint(0, 1000))/1000)

    print "accesing", personality_matrix[num]
    print "changing to:", matrix
    print "normie:", normalize(matrix)
    matrix = normalize(matrix)
    matrix = [round(x, 3) for x in matrix]
    personality_matrix[num] = matrix

  
    print "after:", personality_matrix
    return personality_matrix

affinity(generate(4,4),0)


matrix: [[ 0.185  0.275  0.54   0.001]
 [ 0.373  0.397  0.163  0.067]
 [ 0.048  0.51   0.056  0.386]
 [ 0.04   0.217  0.434  0.309]]
accesing [[ 0.185  0.275  0.54   0.001]]
changing to: [0.966, 0.076, 0.895, 0.119]
normie: [0.4698443579766537, 0.03696498054474708, 0.4353112840466926, 0.05787937743190661]
after: [[ 0.47   0.037  0.435  0.058]
 [ 0.373  0.397  0.163  0.067]
 [ 0.048  0.51   0.056  0.386]
 [ 0.04   0.217  0.434  0.309]]
Out[45]:
matrix([[ 0.47 ,  0.037,  0.435,  0.058],
        [ 0.373,  0.397,  0.163,  0.067],
        [ 0.048,  0.51 ,  0.056,  0.386],
        [ 0.04 ,  0.217,  0.434,  0.309]])

In [42]:
#input:num characters
#output: num personality matricies, and closeness vectors

def initialize(num):
    out_matricies = []
    
    for i in range(num * 2):
        out_matricies.append(generate(4,4))
    
    closeness_vectors = []
    for x in range(num):
        close_vector = []
        for i in range(num):
            if i == x:
                close_vector.append(float(0))
            else:
                close_vector.append(float(random.randint(0,1000))/1000)
        closeness_vectors.append(normalize(close_vector))
    return out_matricies, closeness_vectors

initialize(2)


Out[42]:
([matrix([[ 0.208,  0.114,  0.275,  0.403],
          [ 0.129,  0.574,  0.292,  0.005],
          [ 0.137,  0.277,  0.322,  0.264],
          [ 0.36 ,  0.104,  0.015,  0.52 ]]),
  matrix([[ 0.153,  0.549,  0.171,  0.127],
          [ 0.007,  0.303,  0.137,  0.552],
          [ 0.039,  0.143,  0.546,  0.272],
          [ 0.102,  0.46 ,  0.393,  0.045]]),
  matrix([[ 0.415,  0.441,  0.056,  0.088],
          [ 0.277,  0.048,  0.189,  0.486],
          [ 0.104,  0.463,  0.1  ,  0.333],
          [ 0.28 ,  0.103,  0.28 ,  0.337]]),
  matrix([[ 0.279,  0.261,  0.024,  0.436],
          [ 0.033,  0.141,  0.299,  0.526],
          [ 0.378,  0.107,  0.348,  0.167],
          [ 0.283,  0.464,  0.04 ,  0.213]])],
 [[0.0, 1.0], [1.0, 0.0]])

In [3]:
def traverse_row(matrix):
    rand  = float(random.randint(0,1000))/1000
    count = 0
    for i, elem in enumerate(matrix):
        if rand > count and rand < count + elem:
            return i
        count += elem
    return len(matrix) - 1

def translate(num):
    if num == 0:
        return "Happy"
    if num == 1:
        return "Sad"
    if num == 2:
        return "Angry"
    if num == 3:
        return "Fear"     
    
def translate_to_num(string):
    if num == "Happy":
        return 0
    if num == "Sad":
        return 1
    if num == "Angry":
        return 2
    if num == "Fear":
        return 3

In [4]:
#This takes in one character, all the other characters in the frame, and all the matricies for 
#every character

def new_emotion_one_character(character_acting_id,    #id of acting character in frame
                              character_in_frame_ids, #id of other characters in frame
                              char_emotions,          #array of emotions of characters in frame
                              personality_matricies,  #array of personality matricies of characters in frame
                              impact_matricies,       #array of impact matricies of characters in frame
                              socialbility_params,    #array of socialbility of characters in frame
                              closeness_vectors):     #array of closeness vectors of characters in frame
    
    if len(character_in_frame_ids) == 0:
        P0   = personality_matricies[character_acting_id]
        emo0 = char_emotions[character_acting_id]
        
        if random.randint(0,1) == 1:
            position_and_direction = ['right', 'left']
        else:
            position_and_direction = ['left', 'right']
        
        return (traverse_row(P0.A[emo0]),position_and_direction)
    
    else:
        P0   = personality_matricies[character_acting_id]
        emo0 = char_emotions[character_acting_id]
        s0   = socialbility_params[character_acting_id]
        
        if random.randint(0,1) == 1:
            position_and_direction = ['right', 'left']
        else:
            position_and_direction = ['left', 'right']
        
        total_influence = np.matrix([[0.0, 0.0, 0.0, 0.0],
                                     [0.0, 0.0, 0.0, 0.0],
                                     [0.0, 0.0, 0.0, 0.0],
                                     [0.0, 0.0, 0.0, 0.0]])
        
        for elem in character_in_frame_ids:
            M = impact_matricies[elem].A[char_emotions[elem]]
            v = closeness_vectors[character_acting_id][elem]
            M = np.repeat(M[np.newaxis,:], 4, 0)
            total_influence += v*M
    
        transition_matrix = (1-s0) * P0 + s0 *(total_influence)
    
        #return(traverse_row(transition_matrix.A[emo0]), position_and_direction)
        return(traverse_row(transition_matrix.A[emo0]), transition_matrix)

In [5]:
matrixes = initialize(3)
list1, list2 = matrixes
p0, im0, p1, im1, p2, im2 = list1
v0, v1, v2 = list2
soc0 = .3
soc1 = .4
soc2 = .5
emo0 = 0
emo1 = 0
emo2 = 0

print "Before:"
print "char0"
print "Personality Matrix 0:"
print p0
print "Impact Matrix 0:"
print im0
print "emotion:", translate(emo0)
print
print "char1"
print "Personality Matrix 1:"
print p1
print "Impact Matrix 1:"
print im1
print "emotion:", translate(emo1)
print
print "char2"
print "Personality Matrix 2:"
print p2
print "Impact Matrix 2:"
print im2
print "emotion:", translate(emo2)


Before:
char0
Personality Matrix 0:
[[ 0.259  0.133  0.331  0.277]
 [ 0.028  0.046  0.241  0.685]
 [ 0.519  0.022  0.409  0.05 ]
 [ 0.424  0.358  0.205  0.013]]
Impact Matrix 0:
[[ 0.123  0.218  0.364  0.294]
 [ 0.256  0.207  0.399  0.137]
 [ 0.357  0.496  0.008  0.14 ]
 [ 0.133  0.326  0.373  0.168]]
emotion: Happy

char1
Personality Matrix 1:
[[ 0.4    0.092  0.481  0.026]
 [ 0.213  0.351  0.239  0.197]
 [ 0.477  0.349  0.001  0.174]
 [ 0.245  0.224  0.186  0.345]]
Impact Matrix 1:
[[ 0.247  0.276  0.004  0.473]
 [ 0.37   0.252  0.334  0.044]
 [ 0.169  0.355  0.234  0.243]
 [ 0.384  0.051  0.39   0.174]]
emotion: Happy

char2
Personality Matrix 2:
[[ 0.416  0.376  0.013  0.195]
 [ 0.309  0.08   0.204  0.408]
 [ 0.481  0.276  0.2    0.043]
 [ 0.153  0.19   0.295  0.363]]
Impact Matrix 2:
[[ 0.318  0.327  0.1    0.255]
 [ 0.013  0.421  0.454  0.112]
 [ 0.243  0.144  0.505  0.108]
 [ 0.122  0.309  0.35   0.219]]
emotion: Happy

In [6]:
emo_0, direction_0 = new_emotion_one_character( 0, [1,2], [emo0, emo1, emo2], [p0, p1, p2],
                          [im0, im1, im2], [soc0, soc1, soc2], [v0, v1, v2])
emo_1, direction_1 = new_emotion_one_character( 1, [0,2], [emo0, emo1, emo2], [p0, p1, p2],
                          [im0, im1, im2], [soc0, soc1, soc2], [v0, v1, v2])
emo_2, direction_2 = new_emotion_one_character( 2, [0,1], [emo0, emo1, emo2], [p0, p1, p2],
                          [im0, im1, im2], [soc0, soc1, soc2], [v0, v1, v2])

print "After interaction:"
print "char0 emotion:", emo_0
print "char1 emotion:", translate(emo_1)
print "char2 emotion:", translate(emo_2)

print direction_0
print direction_1
print direction_2


After interaction:
char0 emotion: 3
char1 emotion: Angry
char2 emotion: Happy
[[ 0.26682072  0.18410362  0.24834211  0.30073355]
 [ 0.10512072  0.12320362  0.18534211  0.58633355]
 [ 0.44882072  0.10640362  0.30294211  0.14183355]
 [ 0.38232072  0.34160362  0.16014211  0.11593355]]
[[ 0.31798155  0.15848815  0.39523421  0.12744369]
 [ 0.20578155  0.31388815  0.25003421  0.23004369]
 [ 0.36418155  0.31268815  0.10723421  0.21624369]
 [ 0.22498155  0.23768815  0.21823421  0.31884369]]
[[ 0.31748162  0.31944301  0.04919853  0.31376379]
 [ 0.26398162  0.17144301  0.14469853  0.42026379]
 [ 0.34998162  0.26944301  0.14269853  0.23776379]
 [ 0.18598162  0.22644301  0.19019853  0.39776379]]

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: