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])
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)
Out[45]:
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]:
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)
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
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: