-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_knowledge.py
More file actions
176 lines (152 loc) · 6.28 KB
/
agent_knowledge.py
File metadata and controls
176 lines (152 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from kripke_model import get_proposition
class AgentKnowledge:
"""
Monitors and maintains the agent knowledge for the game.
Processes the secret code to update the agent1 knowledge.
Processes every move to update the common knowledge
and deduced knowledge for agent2 accrodingly.
"""
def __init__(self):
self.agent1 = []
self.agent2 = []
self.common_knowledge = []
def update_code_maker_knowledge(self, code):
"""
Extracts knowledge from the secret code and
adds it to the individual knowledge of agent1.
"""
self.__extract_direct_knowledge(code)
self.__extract_indirect_knowledge(code)
return
def update_move_knowledge(self, move, feedback):
"""
Extracts knowledge from the move and its feedback,
and adds it to the common knowledge of the model
and the individual knowledge of agent2, accordingly.
"""
for i in range(0, len(move)):
if feedback[i] == 1:
self.__add_knowledge_for_perfectly_correct_element(
i+1, move[i])
if feedback[i] == -1:
self.__add_knowledge_for_incorrect_element(i+1, move[i])
if feedback[i] == 0:
self.__add_knowledge_for_wrongly_positioned_element(
i+1, move[i])
return
def __extract_direct_knowledge(self, code):
"""
From the secret code, extracts direct knowledge about
the colors being present at given positions,
and adds it to agent1 individual knowledge.
"""
for i in range(0, len(code)):
proposition = get_proposition(i+1, code[i])
self.agent1.append(proposition)
return
def __extract_indirect_knowledge(self, code):
"""
From the secret code, extracts indirect knowledge about
the other colors NOT being present at given positions,
and adds it to agent1 individual knowledge.
"""
all_available_colors = [1, 2, 3, 4, 5, 6]
for i in range(0, len(code)):
for c in all_available_colors:
if c != code[i]:
proposition = get_proposition(i+1, c)
negative = generate_negative_proposition(proposition)
self.agent1.append(negative)
return
def __add_knowledge_for_perfectly_correct_element(self, index, color_number):
"""
From the move and feedback, extracst knowledge about
the colors which are perfectly positioned.
Adds direct knowledge about those colors being present
to the common knowledge.
Invokes extraction of indirect knowledge
"""
proposition = get_proposition(index, color_number)
self.common_knowledge.append(proposition)
self.__deduce_knowledge_for_correct_element(index, color_number)
return
def __deduce_knowledge_for_correct_element(self, index, color_number):
"""
Extracts indirect knowledge about perfectly positioned colors,
that is, other colors NOT being present at those positions,
and adds it to individual knowledge of agent2.
"""
all_available_colors = [1, 2, 3, 4, 5, 6]
for c in all_available_colors:
if c != color_number:
proposition = get_proposition(index, c)
negative = generate_negative_proposition(proposition)
self.agent2.append(negative)
return
def __add_knowledge_for_incorrect_element(self, index, color_number):
"""
From the move and feedback, extracts knowledge about
the colors which are incorrectly guessed.
Extracts direct knowledge about these colors NOT being
present anywhere, and adds it to the common knowledge.
Invokes extraction of indirect knowledge.
"""
for i in range(1, 5):
proposition = get_proposition(i, color_number)
negative = generate_negative_proposition(proposition)
self.common_knowledge.append(negative)
self.__deduce_knowledge_for_incorrect_element(index, color_number)
return
def __deduce_knowledge_for_incorrect_element(self, index, color_number):
"""
Extracts indirect knowledge about incorrectly guessed color,
that is, MAY BE some other color is present at that position,
and adds it to individual knowledge of agent2.
"""
all_available_colors = [1, 2, 3, 4, 5, 6]
for c in all_available_colors:
if c != color_number:
proposition = get_proposition(index, c)
may_be = generate_may_be_proposition(proposition)
self.agent2.append(may_be)
return
def __add_knowledge_for_wrongly_positioned_element(self, index, color_number):
"""
From the move and feedback, extracts knowledge about
the colors which are wrongly positioned.
Extracts direct knowledge about these colors NOT being
present at that position, and adds it to the common knowledge.
Invokes extraction of indirect knowledge
"""
proposition = get_proposition(index, color_number)
negative = generate_negative_proposition(proposition)
self.common_knowledge.append(negative)
self.__deduce_knowledge_for_wrongly_positioned_element(
index, color_number)
return
def __deduce_knowledge_for_wrongly_positioned_element(self, index, color_number):
"""
Extracts indirect knowledge about wrongly positioned color,
that is, this color is MAY BE present at some other position,
and adds it to individual knowledge of agent2.
"""
for i in range(1, 5):
if i != index:
proposition = get_proposition(i, color_number)
may_be = generate_may_be_proposition(proposition)
self.agent2.append(may_be)
return
def generate_negative_proposition(proposition):
"""
Get negative proposition:
1:red -> ~(1:red)
"""
negative = "~(" + proposition + ")"
return negative
def generate_may_be_proposition(proposition):
"""
Get may be proposition:
1:red -> *(1:red)
"""
may_be = "*(" + proposition + ")"
return may_be