This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
/
mas_cell.py
244 lines (204 loc) · 6.86 KB
/
mas_cell.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#==================================================
# INFO-H-100 - Introduction à l'informatique
#
# Prof. Thierry Massart
# Année académique 2014-2015
#
# Projet: Système Multi-Agent (SMA)
#
#==================================================
import math
import random
import mas_environment as e
#==================================================
# CELL
#==================================================
# --- Constants ---
MAX_IDX = 3
ENV_IDX = 0 # Environment the cell belongs to
SUGAR_LEVEL_IDX = 1 # Current sugar level
SUGAR_CAPACITY_IDX = 2 # Sugar capacity of the cell
PRESENT_AGENT_IDX = 3 # Agent that is currently on the cell
# --- Private functions ---
# Note: These functions should not be called outside this module.
def __get_property(cell, property_idx):
# Return the value of the given property of the cell.
if not (0 <= property_idx <= MAX_IDX):
raise Exception("Invalid cell property index.")
return cell[property_idx]
def __set_property(cell, property_idx, value):
# Set the value of the given property of the cell.
if not (0 <= property_idx <= MAX_IDX):
raise Exception("Invalid cell property index.")
cell[property_idx] = value
def __empty_instance():
# Return an empty cell instance.
return [None]*(MAX_IDX+1)
# --- Getters and setters ---
def get_env(cell):
"""
Return the environment the given cell belongs to.
(Allows "navigation" between variable levels.)
"""
return __get_property(cell, ENV_IDX)
def set_env(cell, env):
"""
Set the environment the given cell belongs to.
(Allows "navigation" between variable levels.)
"""
__set_property(cell, ENV_IDX, env)
def get_capacity(cell):
"""
Return the sugar capacity of the cell.
"""
return __get_property(cell, SUGAR_CAPACITY_IDX)
def set_capacity(cell, capacity):
"""
Set the sugar capacity of the cell.
"""
env = get_env(cell)
if capacity < 0:
raise Exception("The sugar capacity of a cell cannot be negative.")
if capacity > e.get_max_capacity(env):
print("Capa = ",capacity," max = ",e.get_max_capacity(env))
raise Exception("The sugar capacity of a cell cannot exceed the maximum capacity for the environment.")
__set_property(cell, SUGAR_CAPACITY_IDX, capacity)
def get_sugar_level(cell):
"""
Return the current sugar level of the cell.
"""
return __get_property(cell, SUGAR_LEVEL_IDX)
def set_sugar_level(cell, level):
"""
Set the current sugar level of the cell.
"""
if level < 0:
raise Exception("Error: The sugar level of a cell cannot be negative.")
elif level > get_capacity(cell):
raise Exception("Error: The sugar level of a cell cannot exceed its capacity.")
__set_property(cell, SUGAR_LEVEL_IDX, level)
def get_present_agent(cell):
"""
If an agent is currently present on the cell, return
that agent, otherwise return the constant None.
"""
return __get_property(cell, PRESENT_AGENT_IDX)
def set_present_agent(cell, agent):
"""
Set the agent that is currently present on the cell.
To tell that there is no agent, set agent to None.
"""
__set_property(cell, PRESENT_AGENT_IDX, agent)
# --- Initialisation ---
def new_instance(env):
"""
Return a new cell instance and "declare" to which
environment it belongs to.
"""
cell = __empty_instance()
set_env(cell, env)
# We have to set the capacity before sugar level, because
# the latter uses the former to check for validity.
set_capacity(cell, 0.0)
set_sugar_level(cell, 0.0)
set_present_agent(cell, None)
return cell
# --- Sugar Capacity ---
def add_capacity(cell, capacity):
"""
Add capacity to a cell (in addition to the capacity)
the cell already has.
Usually, this function is only called by other capacity
initialisation functions, e.g. "add_capacity_gaussian"
in the "mas_environment" module.
"""
# Add the current capacity of the cell to the value of the
# parameter "capacity".
capacity += get_capacity(cell)
# Limit it to the maximum capacity of the environment.
env = get_env(cell)
max_capacity = e.get_max_capacity(env)
if capacity <= max_capacity:
set_capacity(cell, capacity)
else:
set_capacity(cell, max_capacity)
# --- Sugar Level ---
def set_sugar_level_to_capacity(cell):
"""
Set the sugar level of the cell to its capacity, i.e.,
to its highest possible value.
"""
capacity = get_capacity(cell)
set_sugar_level(cell, capacity)
def add_sugar_level(cell, level):
"""
Add a given level to the current sugar level of the cell.
"""
# Add the current sugar level of the cell to the value of
# the parameter "level".
level += get_sugar_level(cell)
set_sugar_level(cell, level)
# --- Agence presence ---
def agent_is_present(cell):
"""
Return (boolean) whether or not an agent is currently
present on the cell. (Does not tell actual which agent.)
"""
return get_present_agent(cell) != None
# --- Graphic display ---
def show(cell):
"""
Print some key information about the cell.
Mainly use this for debugging.
"""
print("level:", round(get_sugar_level(cell), 2), \
" capacity:", round(get_capacity(cell), 2), \
" agent present:", agent_is_present(cell))
#==================================================
# CELL RULES (some examples)
#==================================================
#
# Cell rule functions must comply to the following
# signature:
#
# INPUT: The cell the rules is applied on
#
# OUTPUT: None.
#
#==================================================
def regen_two_percent(cell):
"""
Cell rule: regenerate 2% of the cell's capacity.
"""
capacity = get_capacity(cell)
level = get_sugar_level(cell)
level += 0.02*capacity
if level > capacity:
level = capacity
set_sugar_level(cell, level)
def regen_five_percent(cell):
"""
Cell rule: regenerate 5% of the cell's capacity.
"""
capacity = get_capacity(cell)
level = get_sugar_level(cell)
level += 0.05*capacity
if level > capacity:
level = capacity
set_sugar_level(cell, level)
def regen_ten_percent(cell):
"""
Cell rule: regenerate 10% of the cell's capacity.
"""
capacity = get_capacity(cell)
level = get_sugar_level(cell)
level += 0.10*capacity
if level > capacity:
level = capacity
set_sugar_level(cell, level)
def regen_full(cell):
"""
Cell rule: regenerate the level to full capacity.
"""
capacity = get_capacity(cell)
set_sugar_level(cell, capacity)