修正したコード例です。
クラス設計を変更した他、変数名・メソッド名も適当に変更した箇所があります。
このクラスがプラグラムの他の部分でどう使われているのか分からないので、この修正で良いかどうかは分かりません。あくまで参考程度に考えて、実際の修正はご自分で行ってください。
python
1 import random
2 import numpy as np
3
4
5 class Agent :
6 def __init__ ( self , agent_id ) :
7 self . id = agent_id
8 self . likability_of_agents = { } # 他のエージェントの好感度
9 self . agents_talk_to = [ ] # 話しかけるエージェントのID
10
11 def set_likability_of_agents ( self , agents ) :
12 """自分から見た他のエージェントの好感度を設定する"""
13 for agent in agents :
14 if agent is self :
15 continue
16 likability = np . random . uniform ( low = - 0.1 , high = 0.1 , size = None )
17 self . likability_of_agents [ agent . id ] = likability
18
19 def decide_agents_talk_to ( self , agents , num_of_talk_to ) :
20 """話しかける相手を決める
21 num_of_talk_to: 話しかける人数
22 注:random.choices の選択は重複あり
23 """
24 agents_except_me = [ ]
25 weights_for_me = [ ]
26 z = 0
27 for agent in agents :
28 if agent is self :
29 continue
30 h = self . likability_of_agents [ agent . id ] + agent . likability_of_agents [ self . id ]
31 h = ( h + 0.8 ) / 1.6
32 z += h
33 for agent in agents :
34 if agent is self :
35 continue
36 h = self . likability_of_agents [ agent . id ] + agent . likability_of_agents [ self . id ]
37 h = ( h + 0.8 ) / 1.6
38 weight = h / z
39 agents_except_me . append ( agent . id )
40 weights_for_me . append ( weight )
41 self . agents_talk_to = random . choices ( agents_except_me , weights = weights_for_me , k = num_of_talk_to )
42
43
44 class Simulation :
45 def __init__ ( self ) :
46 self . agents = [ ]
47
48 def generate_agents ( self , agents_num ) :
49 self . agents = [ Agent ( agent_id ) for agent_id in range ( agents_num ) ]
50 for agent in self . agents :
51 agent . set_likability_of_agents ( self . agents )
52
53 def update_agents_talk_to ( self , num_of_talk_to ) :
54 for agent in self . agents :
55 agent . decide_agents_talk_to ( self . agents , num_of_talk_to )
56
57
58 agents_num = 10 # エージェントの人数
59 num_of_talk_to = 3 # 話しかける相手の人数
60
61 simulation = Simulation ( )
62 simulation . generate_agents ( agents_num )
63 simulation . update_agents_talk_to ( num_of_talk_to )
64
65 for agent in simulation . agents :
66 print ( "Likabilities for Agent" , agent . id , ":" )
67 for agent_id in agent . likability_of_agents :
68 print ( " agent" , agent_id , ":" , agent . likability_of_agents [ agent_id ] )
69 print ( "Agent" , agent . id , "talk to agents:" , agent . agents_talk_to )
70 print ( )
実行結果例:
sh
1 Likabilities for Agent 0 :
2 agent 1 : 0.003593791220430312
3 agent 2 : -0.06671394549157375
4 agent 3 : -0.06401199018940962
5 agent 4 : 0.013920278293366617
6 agent 5 : 0.04516375543511855
7 agent 6 : -0.011912824254704718
8 agent 7 : 0.026153025114075773
9 agent 8 : 0.011868768479560948
10 agent 9 : 0.04240143720637654
11 Agent 0 talk to agents: [9, 3, 3]
12
13 Likabilities for Agent 1 :
14 agent 0 : -0.09781358022021767
15 agent 2 : -0.03220283667278549
16 agent 3 : 0.09983247539891038
17 agent 4 : 0.020370231363201152
18 agent 5 : -0.052236895694299214
19 agent 6 : -0.041826511490220014
20 agent 7 : 0.07382746158645145
21 agent 8 : -0.019834025506131975
22 agent 9 : -0.037920267500814835
23 Agent 1 talk to agents: [9, 9, 5]
24
25 Likabilities for Agent 2 :
26 agent 0 : 0.007480112525538596
27 agent 1 : 0.06622925842696986
28 agent 3 : -0.05937665708998981
29 agent 4 : -0.09797388087204717
30 agent 5 : 0.004092525403284336
31 agent 6 : -0.04524524368054875
32 agent 7 : -0.024428248596180008
33 agent 8 : 0.09806301314116633
34 agent 9 : 0.06706246347669592
35 Agent 2 talk to agents: [3, 9, 9]
36
37 Likabilities for Agent 3 :
38 agent 0 : 0.06318506659488099
39 agent 1 : 0.04154805913455695
40 agent 2 : 0.046824471192876305
41 agent 4 : -0.04841415159437523
42 agent 5 : 0.030412330217516353
43 agent 6 : 0.011326247826077385
44 agent 7 : -0.01947311717839298
45 agent 8 : 0.03953325741188035
46 agent 9 : 0.037230015904898495
47 Agent 3 talk to agents: [6, 8, 4]
48
49 Likabilities for Agent 4 :
50 agent 0 : 0.035071453810674974
51 agent 1 : 0.007708342806642052
52 agent 2 : 0.09629976405160354
53 agent 3 : 0.047898922319321524
54 agent 5 : -0.08225314707129967
55 agent 6 : 0.0732762172352446
56 agent 7 : -0.03483131375444026
57 agent 8 : -0.09498875689721964
58 agent 9 : 0.04799264995912833
59 Agent 4 talk to agents: [9, 9, 1]
60
61 Likabilities for Agent 5 :
62 agent 0 : 0.09712575280879204
63 agent 1 : -0.017286012632382164
64 agent 2 : -0.0464031944152709
65 agent 3 : -0.01162986221361706
66 agent 4 : 0.026563041670285753
67 agent 6 : -0.017724797205174656
68 agent 7 : -0.02187775667083669
69 agent 8 : 0.009053454160764349
70 agent 9 : -0.0023210838221180835
71 Agent 5 talk to agents: [3, 8, 2]
72
73 Likabilities for Agent 6 :
74 agent 0 : -0.01233904663365934
75 agent 1 : -0.08623998475316791
76 agent 2 : 0.08752930090693042
77 agent 3 : 0.03171909263521927
78 agent 4 : 0.06068698765865413
79 agent 5 : 0.03423829116205521
80 agent 7 : 0.02967733664736394
81 agent 8 : 0.08138695922830874
82 agent 9 : -0.01405451600718019
83 Agent 6 talk to agents: [7, 4, 5]
84
85 Likabilities for Agent 7 :
86 agent 0 : -0.0628985022361885
87 agent 1 : 0.028492129344750927
88 agent 2 : -0.017138207697649246
89 agent 3 : 0.04575839878181304
90 agent 4 : 0.04550300157454565
91 agent 5 : 0.014520931571067042
92 agent 6 : -0.028712286071273296
93 agent 8 : -0.09290597399555577
94 agent 9 : 0.043532811971355
95 Agent 7 talk to agents: [8, 6, 1]
96
97 Likabilities for Agent 8 :
98 agent 0 : 0.023586100926503414
99 agent 1 : 0.0192945084126281
100 agent 2 : -0.02963447113959075
101 agent 3 : 0.04566196880340906
102 agent 4 : -0.06215453197642884
103 agent 5 : -0.03314459931403349
104 agent 6 : -0.09762054989356109
105 agent 7 : 0.02394935196646117
106 agent 9 : 0.0059884445387679425
107 Agent 8 talk to agents: [4, 4, 0]
108
109 Likabilities for Agent 9 :
110 agent 0 : -0.05404362270134604
111 agent 1 : 0.09942924969581102
112 agent 2 : -0.014090528960642154
113 agent 3 : 0.07190045452630944
114 agent 4 : 0.054587551771866555
115 agent 5 : 0.010071609749546837
116 agent 6 : -0.08798323916148171
117 agent 7 : -0.04588835725730183
118 agent 8 : 0.07595674818349404
119 Agent 9 talk to agents: [4, 8, 7]
120