In [20]:
import sys
import seaborn as sns
import random
sys.path.append("..")
%matplotlib inline
sns.set(rc={'image.cmap': 'Purples_r'})

Strategies

The strategy object describes the behaviour of an agent, given its vocabulary. The main algorithms that vary among strategies are:

  • how to choose a link (meaning-word) to enact,
  • how to guess a meaning from a word
  • how to update the vocabulary

In [21]:
import naminggamesal.ngstrat as ngstrat
import naminggamesal.ngvoc as ngvoc

Let's create a strategy. We will also need two vocabularies to work on (speaker and hearer). (more info on other strategy types: Design_newStrategy.ipynb)


In [210]:
M = 5
W = 10
voc_cfg = {
    'voc_type':'matrix',
    'M':M,
    'W':W
    }

nlink = 0

voctest_speaker = ngvoc.Vocabulary(**voc_cfg)
for i in range(0, nlink):
    voctest_speaker.add(random.randint(0,M-1), random.randint(0,W-1), 0.2)
    
voctest_hearer=ngvoc.Vocabulary(**voc_cfg)
for i in range(0, nlink):
    voctest_hearer.add(random.randint(0,M-1), random.randint(0,W-1), 0.2)

print("Speaker:")
print(voctest_speaker)
print(" ")
print("Hearer:")
print(voctest_hearer)


Speaker:
                          Words
        [[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
         [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
Meanings [ 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.]]

 
Hearer:
                          Words
        [[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
         [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
Meanings [ 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.]]


In [211]:
strat_cfg={"strat_type":"success_threshold_epirob",'vu_cfg':{'vu_type':'BLIS'},}

teststrat=ngstrat.Strategy(**strat_cfg)
teststrat


Out[211]:
<naminggamesal.ngstrat.success_threshold.StratSuccessThresholdEpirob at 0x7fc2f5f67790>

Now that we have a strategy, we can test the different functions. Exec

!! Vocabularies are modified, but this way you can observe progressive growth of the number of links !!


In [262]:
memory_s=teststrat.init_memory(voctest_speaker) #Not important for the naive strategy, here it simply is {}
memory_h=teststrat.init_memory(voctest_hearer)

print("Initial vocabulary of the speaker:")
print(voctest_speaker)
print(" ")
print("Initial vocabulary of the hearer:")
print(voctest_hearer)
print(" ")

ms=teststrat.pick_m(voctest_speaker,memory_s,context=[])
print("Meaning chosen by speaker:")
print(ms)
print (" ")

w=teststrat.pick_w(voc=voctest_speaker,mem=memory_s,m=ms,context=[])
print("Word uttered by speaker:")
print(w)
print (" ")

mh=teststrat.guess_m(w,voctest_hearer,memory_h,context=[])
print("Meaning interpreted by hearer:")
print(mh)
print (" ")

if (ms==mh):
    print("Success!")
    
    bool_succ = 1
else:
    bool_succ = 0
    print("Failure!")
print(" ")

teststrat.update_speaker(ms,w,mh,voctest_speaker,memory_s,bool_succ)
teststrat.update_hearer(ms,w,mh,voctest_hearer,memory_h,bool_succ)

print("Updated vocabulary of the speaker:")
print(voctest_speaker)
print(" ")
print("Updated vocabulary of the hearer:")
print(voctest_hearer)


Initial vocabulary of the speaker:
                               Words
        [[ 0.   0.   0.   0.   0.   0.   0.   0.   0.   0.9]
         [ 0.   0.   0.   0.   1.   0.   0.   0.   0.   0. ]
Meanings [ 0.   0.   0.   0.   0.   0.   0.   0.   1.   0. ]
         [ 1.   0.   0.   0.   0.   0.   0.   0.   0.   0. ]
         [ 0.   0.   0.   0.   0.   0.   1.   0.   0.   0. ]]

 
Initial vocabulary of the hearer:
                               Words
        [[ 0.   0.   0.   0.   0.   0.   0.   0.   0.   0.9]
         [ 0.   0.   0.   0.   1.   0.   0.   0.   0.   0. ]
Meanings [ 0.   0.   0.   0.   0.   0.   0.   0.   1.   0. ]
         [ 1.   0.   0.   0.   0.   0.   0.   0.   0.   0. ]
         [ 0.   0.   0.   0.   0.   0.   1.   0.   0.   0. ]]

 
Meaning chosen by speaker:
0
 
Word uttered by speaker:
9
 
Meaning interpreted by hearer:
0
 
Success!
 
Updated vocabulary of the speaker:
                          Words
        [[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
         [ 0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]
Meanings [ 0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]
         [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
         [ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]]

 
Updated vocabulary of the hearer:
                          Words
        [[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
         [ 0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]
Meanings [ 0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]
         [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
         [ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]]

Here you can modify by hand the 2 vocabularies before re-executing the code:


In [19]:
#voctest_speaker.add(0,0,1)
#voctest_speaker.add(0,0,0)

#voctest_hearer.add(0,0,1)
#voctest_hearer.add(0,0,0)

print("Speaker:")
print(voctest_speaker)
print(" ")
print("Hearer:")
print(voctest_hearer)


Speaker:
                               Words
        [[ 0.   0.   0.   0.   0.   0.   0.   0.   0.   0. ]
         [ 0.   0.   0.   0.   0.   0.   0.   0.   0.   0. ]
Meanings [ 0.   0.   0.   0.   1.   0.   0.   0.   0.   0. ]
         [ 0.2  0.   0.   0.   0.   0.   0.   0.   0.   0. ]
         [ 0.   0.   0.   0.   0.   0.   0.   0.   0.   0. ]]

 
Hearer:
                               Words
        [[ 0.   0.   0.   0.   0.   0.   0.   0.   0.   0. ]
         [ 0.   0.   0.   0.   0.   0.   0.   0.   0.   0. ]
Meanings [ 0.   0.   0.   0.   1.   0.   0.   0.   0.   0. ]
         [ 0.   0.   0.   0.   0.   0.   0.   0.   0.   0. ]
         [ 0.   0.   0.   0.   0.   0.   0.2  0.   0.   0. ]]

Approximation of the probability density of the different procedures of the strategy:


In [42]:
voctest_speaker.visual()
teststrat.visual(voc=voctest_speaker,mem=memory_s,vtype="pick_m",iterr=500)


                          Words
        [[ 0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]
         [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
Meanings [ 0.  0.  0.  0.  0.  1.  0.  0.  0.  0.]
         [ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
         [ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]]


In [43]:
voctest_speaker.visual()
teststrat.visual(voc=voctest_speaker,mem=memory_s,vtype="pick_mw",iterr=500)


                          Words
        [[ 0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]
         [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
Meanings [ 0.  0.  0.  0.  0.  1.  0.  0.  0.  0.]
         [ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
         [ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]]


In [44]:
voctest_speaker.visual()
teststrat.visual(voc=voctest_speaker,mem=memory_s,vtype="pick_w",iterr=500)


                          Words
        [[ 0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]
         [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
Meanings [ 0.  0.  0.  0.  0.  1.  0.  0.  0.  0.]
         [ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
         [ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]]


In [17]:
voctest_hearer.visual()
teststrat.visual(voc=voctest_hearer,mem=memory_h,vtype="guess_m",iterr=500)


                          Words
        [[ 0.  0.  1.  0.  0.  0.  0.  0.  0.  0.]
         [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
Meanings [ 1.  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.]]


In [ ]:


In [ ]:


In [ ]:


In [16]:
dir(teststrat)
teststrat.voc_update


Out[16]:
<naminggamesal.ngstrat.voc_update.imitation.Imitation at 0x7fc2f5dc3610>

In [ ]:


In [ ]: