0
import numpy as np
import matplotlib.pyplot as plt
class Prisoners_Dilemma:
 def __init__(self,n,p):
 self.n = n
 self.p = p
 def decision_array(self):
 self.dict_dict = {}
 for i in range(1,self.n + 1):
 self.dict_dict[i] = []
 list_list = []
 for j in range(1,self.n): 
 #np.random.seed(j)
 self.r = np.random.uniform(0,1)
 if self.r > self.p:
 q = 0
 else:
 q = 1
 list_list.append(q) 
 self.dict_dict[i] = list_list
 return self.dict_dict
 def payoff(self):
 self.dict_dict_2 = {}
 for i in range(1,self.n + 1):
 self.dict_dict_2[i] = []
 list_list_2 = []
 list_list_3=[]
 for j in range(1, i):
 list_list_2.append(self.dict_dict[j][i-2])
 for j in range(i + 1, self.n + 1):
 list_list_2.append(self.dict_dict[j][i-1])
 list_list_2_np = np.array(list_list_2)
 against_i = np.sum(list_list_2_np)
 for_i = np.sum(self.dict_dict[i]) 
 if against_i == 0 and for_i == 0:
 payoff_i = 2
 elif against_i == 0 and for_i != 0:
 payoff_i = 5
 elif against_i != 0 and for_i == 0:
 payoff_i = -5
 else:
 payoff_i = -2
 list_list_3.append(payoff_i)
 self.dict_dict_2[i]=list_list_3
 return self.dict_dict_2
 def gameplay(self, N, initial_count):
 self.counter = initial_count
 for i in range(N):
 for j in range(1, self.n + 1):
 z = self.dict_dict_2[j]
 x = np.array(z)
 self.counter += np.sum(z)
 return self.counter
y = Prisoners_Dilemma(15,0.015)
print (y.gameplay(20,100))

In the above code, the compiler gives the error that instance has no attribute as dict_dict_2 even though its prefixed with self. Moreover, it is perfectly fine with dict_dict. For the sake of completeness I have included the whole code but the problem lies only in payoff and gameplay methods?

asked Jun 23, 2015 at 19:23

2 Answers 2

2

dict_dict_2 is only created in payoff(), therefore you must call it before attempting to call gameplay().

answered Jun 23, 2015 at 19:26
Sign up to request clarification or add additional context in comments.

2 Comments

could you then explain why dict_dict variable works (see payoff and decision_matrix) ?
It "works" by accident, because you never access it.
0

The issue is that you are only creating self.dict_dict_2 variable in the payoff function, but in your logic where you are calling gameplay() function , you are not calling the payoff() function before accessing dict_dict_2 , from the looks of it you are not calling that function anywhere at all.

Not sure what dict_dict_2 holds, but the above is the reason why you are getting the issue, maybe you can move the initialization part of dict_dict_2 to __init__() function , though that would not fix the complete issue, since you would still be trying to access dict_dict_1[j] which can error out if j is not a key in dict_dict_2 .

answered Jun 23, 2015 at 19:27

4 Comments

could you then explain why no such problem with dict_dict variable??
You are not accessing dict_dict variable anywhere in gameplay at all, you try accessing that there (just do simple print(self.dict_dict)) and you will see the error for that as well
not in the gameplay. I invoked dict_dict in decision_matrix and used it in payoff without any special syntax, still it works fine and gives me the list (if you try running the code by commenting the gameplay part).
You are defining dict_dict in the starting of decision_matrix , the line for the definition is - self.dict_dict = {} , this defines the variable dict_dict inside self .

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.