-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
405 lines (327 loc) · 13.1 KB
/
test.py
File metadata and controls
405 lines (327 loc) · 13.1 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import pandas as pd,json
import networkx as nx
from copy import deepcopy
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Resource types
RESOURCE_TYPES = [
"electricity",
"water",
"basic_resources",
"critical_resources",
"heavy_resources",
"personnel",
"data"
]
class Layer:
def __init__(self, name, supported_resources):
self.name = name
self.supported_resources = supported_resources
self.edges = []
def connect_nodes(self, from_node, to_node, attributes):
"""Connect two nodes with an edge having specific attributes"""
edge = Edge(
from_node=from_node,
to_node=to_node,
attributes=attributes
)
self.edges.append(edge)
return edge
class Edge:
def __init__(self, from_node, to_node, attributes):
self.from_node = from_node
self.to_node = to_node
self.attributes = attributes
self.resources_in_transit = {resource: [] for resource in RESOURCE_TYPES}
def send_resource(self, resource_type, amount):
"""Send resources through this edge"""
if resource_type not in self.from_node.produces:
return False
travel_time = self.attributes.get("travel_time", 1)
self.resources_in_transit[resource_type].append({
'amount': amount,
'ticks_remaining': travel_time
})
return True
def tick(self):
"""Process one time unit of transportation"""
arrived_resources = {resource: 0 for resource in RESOURCE_TYPES}
for resource_type in RESOURCE_TYPES:
for resource in self.resources_in_transit[resource_type][:]:
resource['ticks_remaining'] -= 1
if resource['ticks_remaining'] <= 0:
arrived_resources[resource_type] += resource['amount']
self.resources_in_transit[resource_type].remove(resource)
if arrived_resources[resource_type] > 0:
self.to_node.receive_supplies(resource_type, arrived_resources[resource_type])
return arrived_resources
class Building:
def __init__(self, id, requires=None, produces=None, priority=1):
self.id = id
self.priority = priority
self.requires = requires or {}
self.produces = produces or {}
self.status = "active" # active, offline, destroyed
self.resources = {resource: 0 for resource in RESOURCE_TYPES}
def works(self) -> bool:
for resource, amount in self.requires.items():
if self.resources.get(resource, 0) < amount:
return False
return True
def update_status(self):
if self.works():
self.status = "active"
return
self.status = "offline"
def tick(self):
"""Base tick method to be implemented by subclasses"""
# Check if all required resources are available
for resource, amount in self.requires.items():
if self.resources[resource] < amount:
return False
# Consume required resources
for resource, amount in self.requires.items():
self.resources[resource] -= amount
return True
def receive_supplies(self, resource_type, amount):
"""Generic method to receive supplies of any resource type"""
if resource_type in RESOURCE_TYPES:
self.resources[resource_type] += amount
class Hospital(Building):
def __init__(self, id, resources=None, consumption=None, priority=1):
resources = resources or {}
consumption = consumption or {"electricity": 5, "water": 3, "basic_resources": 10}
super().__init__(
id=id,
requires=consumption,
produces={},
priority=priority
)
# Initialize resources
for resource_type, amount in resources.items():
self.resources[resource_type] = amount
class PowerPlant(Building):
def __init__(self, id, resources=None, consumption=None, production=None, priority=2):
resources = resources or {}
consumption = consumption or {"water": 5}
production = production or {"electricity": 20}
super().__init__(
id=id,
requires=consumption,
produces=production,
priority=priority
)
# Initialize resources
for resource_type, amount in resources.items():
self.resources[resource_type] = amount
def tick(self):
if super().tick():
# Produce resources
for resource, amount in self.produces.items():
self.resources[resource] += amount
return True
return False
class Magazine(Building):
def __init__(self, id, resources=None, consumption=None, production=None, priority=2):
resources = resources or {}
consumption = consumption or {"electricity": 3}
production = production or {"basic_resources": 10}
super().__init__(
id=id,
requires=consumption,
produces=production,
priority=priority
)
# Initialize resources
for resource_type, amount in resources.items():
self.resources[resource_type] = amount
def tick(self):
if super().tick():
# Produce resources
for resource, amount in self.produces.items():
self.resources[resource] += amount
return True
return False
class Infrastructure(Layer):
"""Base class for all infrastructure components"""
_instances = {} # Class variable to hold singleton instances
def __new__(cls, *args, **kwargs):
# Singleton pattern - one instance per layer type
if cls not in cls._instances:
cls._instances[cls] = super(Infrastructure, cls).__new__(cls)
return cls._instances[cls]
def __init__(self, name, supported_resources, default_attributes=None):
# Only initialize if not already initialized
if not hasattr(self, 'initialized'):
super().__init__(name, supported_resources)
self.default_attributes = default_attributes or {
"capacity": 100,
"status": "active",
"travel_time": 1
}
self.initialized = True
def connect_buildings(self, from_building, to_building, custom_attributes=None):
"""Connect two buildings with this infrastructure layer"""
attributes = self.default_attributes.copy()
if custom_attributes:
attributes.update(custom_attributes)
attributes.setdefault("layer",self.name)
return self.connect_nodes(from_building, to_building, attributes)
# Define the infrastructure layer types
class RoadNetwork(Infrastructure):
def __init__(self):
super().__init__(
name="Road Network",
supported_resources=["personnel", "basic_resources", "critical_resources", "heavy_resources"],
default_attributes={"capacity": 50, "travel_time": 2, "status": "active"}
)
class EnergyGrid(Infrastructure):
def __init__(self):
super().__init__(
name="Energy Grid",
supported_resources=["electricity"],
default_attributes={"capacity": 100, "travel_time": 1, "status": "active"}
)
class WaterNetwork(Infrastructure):
def __init__(self):
super().__init__(
name="Water Network",
supported_resources=["water"],
default_attributes={"capacity": 80, "travel_time": 1, "status": "active"}
)
class RailwayNetwork(Infrastructure):
def __init__(self):
super().__init__(
name="Railway Network",
supported_resources=["heavy_resources", "personnel"],
default_attributes={"capacity": 200, "travel_time": 4, "status": "active"}
)
class TelecomNetwork(Infrastructure):
def __init__(self):
super().__init__(
name="Telecom Network",
supported_resources=["data"],
default_attributes={"capacity": 1000, "travel_time": 1, "status": "active"}
)
# snapshotów może być kilka (np. po każdym ticku)
G_snapshots = []
def build_graph(buildings, edges):
G = nx.DiGraph()
for b in buildings:
G.add_node(
b.id,
layer=getattr(b, "layer", "Unknown"),
status=getattr(b, "status", "active"),
supply=getattr(b, "produces", {}),
demand=getattr(b, "requires", {}),
)
for e in edges:
G.add_edge(
e.from_node.id,
e.to_node.id,
capacity=e.attributes.get("capacity", 1.0),
type=e.attributes.get("layer", "generic")
)
return G
def simulate():
print("Critical Infrastructure Simulator")
# Create buildings
hospital = Hospital(
id="HOSP_001",
resources={"basic_resources": 100}
)
power_plant = PowerPlant(
id="POWER_001",
resources={"water": 100}
)
magazine = Magazine(
id="MAG_001",
resources={"basic_resources": 500, "electricity": 0}
)
# Create infrastructure layers
road_network = RoadNetwork()
energy_grid = EnergyGrid()
# Connect buildings
road_edge = road_network.connect_buildings(
magazine, hospital, {"travel_time": 3}
)
energy_edge = energy_grid.connect_buildings(
power_plant, magazine
)
buildings = [hospital,power_plant,magazine]
edges = [road_edge,energy_edge]
snapshots = []
df_nodes = pd.DataFrame([{
"id": b.id,
"type": b.__class__.__name__,
"status": b.status,
"priority": b.priority,
"requires": json.dumps(b.requires),
"produces": json.dumps(b.produces),
"resources": json.dumps(b.resources),
} for b in buildings])
df_nodes.to_csv("nodes.csv", index=False)
df_edges = pd.DataFrame([{
"from": e.from_node.id,
"to": e.to_node.id,
"layer": e.attributes.get("layer", ""),
"capacity": e.attributes.get("capacity", ""),
"travel_time": e.attributes.get("travel_time", ""),
"status": e.attributes.get("status", ""),
"attributes_json": json.dumps(e.attributes),
} for e in edges])
df_edges.to_csv("edges.csv", index=False)
for tick in range(1, 11):
if(tick == 3):
power_plant.resources['electricity'] = 0
power_plant.status = "offline"
print(f"\nTick: {tick}")
# Process power plant
power_plant.tick()
print(f"Power plant {power_plant.id} electricity: {power_plant.resources['electricity']}")
# Send electricity to magazine
if power_plant.resources['electricity'] > 0:
amount_to_send = min(power_plant.resources['electricity'], 10)
power_plant.resources['electricity'] -= amount_to_send
energy_edge.send_resource("electricity", amount_to_send)
# Process magazine
magazine.tick()
print(f"Magazine {magazine.id} resources: {magazine.resources}")
# Send supplies to hospital
if magazine.resources['basic_resources'] > 0:
amount_to_send = min(magazine.resources['basic_resources'], 10)
magazine.resources['basic_resources'] -= amount_to_send
road_edge.send_resource("basic_resources", amount_to_send)
# Process hospital
hospital_success = hospital.tick()
print(f"Hospital {hospital.id} operational: {hospital_success}")
print(f"Hospital {hospital.id} resources: {hospital.resources}")
# Process infrastructure networks
energy_grid_results = energy_edge.tick()
road_results = road_edge.tick()
print(f"Resources in transit (energy): {energy_edge.resources_in_transit}")
print(f"Resources in transit (road): {road_edge.resources_in_transit}")
G = build_graph(buildings, edges)
snapshots.append(deepcopy(G))
for b in buildings:
b.update_status()
return snapshots
if __name__ == "__main__":
# uruchom symulację i odbierz migawki grafu
G_snapshots = simulate()
if not G_snapshots:
raise SystemExit("No snapshots to animate")
# stały układ węzłów (na podstawie pierwszego snapshotu)
fig, ax = plt.subplots(figsize=(8, 6))
pos = nx.spring_layout(G_snapshots[0], seed=42)
def update(frame):
ax.clear()
G = G_snapshots[frame]
colors = []
for n, d in G.nodes(data=True):
status = d.get("status", "active")
colors.append({"active": "green", "offline": "red", "UPS": "yellow"}.get(status, "gray"))
nx.draw(G, pos, ax=ax, node_color=colors, with_labels=True, arrows=True)
ax.set_title(f"Tick {frame+1}")
ani = FuncAnimation(fig, update, frames=len(G_snapshots), interval=800)
plt.show()