-
Notifications
You must be signed in to change notification settings - Fork 2
/
Island_mdp.py
1226 lines (1033 loc) · 43.7 KB
/
Island_mdp.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import pandas as pd
from Member_mdp import Member, colored
from Land_mdp import Land
from utils.save import path_decorator
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from time import time
import pickle
import sys
from collections import defaultdict
def _requirement_for_reproduction(
member_1: Member,
member_2: Member
) -> bool:
return (
(
member_1.vitality + member_1.cargo
+ member_2.vitality + member_2.cargo
) >= Island._REPRODUCE_REQUIREMENT
and
member_1.is_qualified_to_reproduce
and
member_2.is_qualified_to_reproduce
)
def _requirement_for_offer(
member_1: Member,
member_2: Member
) -> bool:
return member_1.cargo * Member._MIN_OFFER_PERCENTAGE >= Member._MIN_OFFER
def _requirement_for_offer_land(
member_1: Member,
member_2: Member
) -> bool:
return member_1.land_num > 1
class Island():
_MIN_MAX_INIT_RELATION = {
"victim": [-50, 100], # 若随机到负值,则该记忆初始化为0
"benefit": [-50, 100],
"benefit_land": [-3, 3],
}
_NEIGHBOR_SEARCH_RANGE = 1000
_REPRODUCE_REQUIREMENT = 150 # 生育条件:双亲血量和仓库之和大于这个值
assert _REPRODUCE_REQUIREMENT > Member._CHILD_VITALITY
# 记录/输出周期
_RECORD_PERIOD = 1
def __init__(
self,
init_member_number: int,
land_shape: Tuple[int, int],
random_seed: int = None
) -> None:
# 设置并记录随机数种子
self._create_from_file = False
self._file_name = ""
if random_seed is not None:
self._random_seed = int(random_seed)
else:
self._random_seed = int(time())
self._rng = np.random.default_rng(self._random_seed)
# 初始人数,当前人数
self._NAME_LIST = self._rng.permutation(np.loadtxt("/Users/lichenyu/leviathan/name_list.txt", dtype=str))
self.init_member_num = init_member_number
self.current_member_num = self.init_member_num
# 初始人物列表,全体人物列表,当前人物列表
self.init_members = [Member(self._NAME_LIST[i], id=i, surviver_id=i, rng=self._rng) for i in range(self.init_member_num)]
self.all_members = self._backup_member_list(self.init_members)
self.current_members = self._backup_member_list(self.init_members)
# 地图
assert land_shape[0] * land_shape[1] > init_member_number, "土地面积应该大于初始人口"
self.land = Land(land_shape)
# 为初始人口分配土地
_loc_idx_list = self._rng.choice(
a = range(land_shape[0] * land_shape[1]),
size = self.init_member_num,
replace = False,
)
_loc_list = [(int(loc_idx / land_shape[0]), loc_idx % land_shape[0]) for loc_idx in _loc_idx_list]
for idx in range(self.init_member_num):
self._acquire_land(self.all_members[idx], _loc_list[idx])
# 初始人物关系
# 关系矩阵M,第j行 (M[j, :]) 代表第j个主体的被动记忆(受伤/受赠……)
# 若要修改(增减)人物关系,需要修改:self.relationship_dict, Member.DECISION_INPUT_NAMES, Member._generate_decision_inputs()
self.relationship_dict = {}
for key, (min, max) in Island._MIN_MAX_INIT_RELATION.items():
rela = self._rng.uniform(
min,
max,
size=(self.init_member_num, self.init_member_num)
)
rela[rela < 0] = 0 # 若随机到负值,则该记忆设为0
np.fill_diagonal(rela, np.nan)
self.relationship_dict[key] = rela
assert len(self.relationship_dict) == len(Member._RELATION_SCALES), "关系矩阵数量和关系矩阵缩放量数量不一致"
# 记录动作 (每Island._RECORD_PERIOD输出、清空一次)
self.record_action_dict = {
"attack": {},
"benefit": {},
"benefit_land": {},
}
self.record_born = []
self.record_death = []
# 记录状态 (每Island._RECORD_PERIOD向末尾增append一个0)
self.record_total_production = [0]
self.record_total_consumption = [0]
self.record_total_dict = {
"attack": [0],
"benefit": [0],
"benefit_land": [0],
}
self.record_historic_ratio_list = np.array([(0,0,0,0)])
self.record_historic_ranking_list = [(0,0,0)]
self.record_land = [self.land.owner_id]
self.previous_vitalities = {}
self.vitality_diff = {}
# 回合数
self.current_round = 0
############################################################################
################################ 基本操作 ####################################
def member_by_name(
self,
name: str,
) -> Member:
for member in self.current_members:
if member.name == name:
return member
for member in self.all_members:
if member.name == name:
return member
raise KeyError(f"Member {name} not found!")
# =============================== 成员增减 ===================================
def _backup_member_list(
self,
member_list: List[Member]
) -> List[Member]:
"""复制member_list"""
return [member for member in member_list]
def _member_list_append(
self,
append: List[Member] = [],
appended_rela_rows: np.ndarray = [],
appended_rela_columnes: np.ndarray = [],
) -> None:
"""
向current_members,all_members增加一个列表的人物,
增加current_member_num,
修改relationships矩阵,
修改人物surviver_id,
"""
appended_num = len(append)
prev_member_num = self.current_member_num
if not isinstance(appended_rela_columnes, np.ndarray):
raise ValueError("关系矩阵增添的列应该是ndarray类型")
if not isinstance(appended_rela_rows, np.ndarray):
raise ValueError("关系矩阵增添的行应该是ndarray类型")
assert appended_rela_columnes.shape == (prev_member_num, appended_num), "输入关系列形状不匹配"
assert appended_rela_rows.shape == (appended_num, prev_member_num), "输入关系行形状不匹配"
# 向列表中增加人物
for member in append:
member.surviver_id = self.current_member_num
self.current_members.append(member)
self.all_members.append(member)
self.current_member_num += 1
# 记录出生
self.record_born = self.record_born + append
# 修改关系矩阵
for key in self.relationship_dict.keys():
# 无法直接进行赋值,需修改原数组尺寸后填入数值
tmp_old = self.relationship_dict[key].copy()
tmp_new = np.zeros((self.current_member_num, self.current_member_num))
tmp_new[:prev_member_num, :prev_member_num] = tmp_old
tmp_new[:prev_member_num, prev_member_num:] = appended_rela_columnes
tmp_new[prev_member_num:, :prev_member_num] = appended_rela_rows
np.fill_diagonal(tmp_new, np.nan)
self.relationship_dict[key] = tmp_new
return
def _member_list_drop(
self,
drop: List[Member] = []
) -> None:
"""
从current_members删除人物,
减少current_member_num,
修改relationships矩阵,
重新修改全体人物surviver_id
"""
drop_id = np.array([member.id for member in drop]) # 校对id,确保正确删除
drop_sur_id = np.array([member.surviver_id for member in drop])
if (drop_sur_id == None).any():
raise AttributeError(f"被删除对象应该有surviver_id")
for member in drop:
assert member.owned_land == [], "被删除对象应该没有土地"
# 排序,确保正确移除
argsort_sur_id = np.argsort(drop_sur_id)[::-1]
drop_id = drop_id[argsort_sur_id]
drop_sur_id = drop_sur_id[argsort_sur_id]
# 从列表中移除人物
for idx in range(len(drop_id)):
id_to_drop = drop_id[idx]
sur_id_to_drop = drop_sur_id[idx]
assert self.current_members[sur_id_to_drop].id == id_to_drop, "删除对象不匹配"
self.current_members[sur_id_to_drop].surviver_id = None
del self.current_members[sur_id_to_drop]
self.current_member_num -= 1
# 修改关系矩阵
for key in self.relationship_dict.keys():
# 无法直接进行赋值,需修改原数组尺寸后填入数值
tmp = np.delete(self.relationship_dict[key], drop_sur_id, axis=0)
tmp = np.delete(tmp, drop_sur_id, axis=1)
self.relationship_dict[key] = tmp
# 重新编号存活成员
for sur_id in range(self.current_member_num):
self.current_members[sur_id].surviver_id = sur_id
return
def member_list_modify(
self,
append: List[Member] = [],
drop: List[Member] = [],
appended_rela_rows: np.ndarray = np.empty(0),
appended_rela_columnes: np.ndarray = np.empty(0)
) -> None:
"""
修改member_list,先增加人物,后修改
记录出生、死亡
"""
if append != []:
self._member_list_append(
append=append,
appended_rela_rows=appended_rela_rows, appended_rela_columnes=appended_rela_columnes
)
if drop != []:
self._member_list_drop(
drop=drop
)
return
@property
def is_dead(self,) -> bool:
return self.current_member_num == 0
# ================================ 关系矩阵修改 ==================================
def _overlap_of_relations(
self,
principal: Member,
object: Member
) -> List[float]:
"""计算关系网内积"""
def normalize(arr):
"""剔除nan,归一化向量"""
arr[principal.surviver_id] = 0
arr[object.surviver_id] = 0
norm = np.linalg.norm(arr)
if norm == 0:
return 0
else:
return arr / norm
overlaps = []
for relationship in list(self.relationship_dict.values()):
pri_row = normalize(relationship[principal.surviver_id, :].copy())
pri_col = normalize(relationship[:, principal.surviver_id].copy())
obj_row = normalize(relationship[object.surviver_id, :].copy())
obj_col = normalize(relationship[:, object.surviver_id].copy())
overlaps.append((
np.sum(pri_row * obj_row)
+ np.sum(pri_row * obj_col)
+ np.sum(pri_col * obj_row)
+ np.sum(pri_col * obj_col)) / 4
)
return overlaps
def _relations_w_normalize(
self,
principal: Member,
object: Member
) -> List[float]:
"""计算归一化(tanh)后的关系矩阵元"""
elements = []
for relationship in list(self.relationship_dict.values()):
elements.append(relationship[principal.surviver_id, object.surviver_id])
elements.append(relationship[object.surviver_id, principal.surviver_id])
elements = np.array(elements)
return np.tanh(elements * np.repeat(Member._RELATION_SCALES, 2))
def relationship_modify(
self,
relationship_name,
member_1: Member,
member_2: Member,
add_value: float
) -> None:
"""
增加矩阵元[member_1.surviver_id, member_2.surviver_id]
"""
assert member_1 is not member_2, "不能修改关系矩阵中的对角元素"
relationship = self.relationship_dict[relationship_name]
relationship[member_1.surviver_id, member_2.surviver_id] += add_value
# =================================== 土地 ======================================
def _acquire_land(
self,
member: Member,
location: Tuple[int, int],
) -> None:
assert self.land[location] is None, "获取的土地应该没有主人"
loc_0, loc_1 = location
self.land.owner[loc_0][loc_1] = member
member.acquire_land(location)
def _discard_land(
self,
member: Member,
location: Tuple[int, int],
) -> None:
assert location in member.owned_land, "只能丢弃拥有的土地"
assert self.land[location] == member, "只能丢弃自己的土地"
loc_0, loc_1 = location
self.land.owner[loc_0][loc_1] = None
member.discard_land(location)
def _get_neighbors(self, member: Member) -> None:
"""
存储四个列表:
- clear_list: 允许通行
- self_blocked_list: 与member直接接壤
- neighbor_blocked_list: 与member间接接壤的成员以及作为桥梁的地主,存储格式为(地主,间接接壤成员)
- empty_loc_list: 闲置土地
"""
(
member.current_clear_list,
member.current_self_blocked_list,
member.current_neighbor_blocked_list,
member.current_empty_loc_list,
) = self.land.neighbors(
member,
self,
Island._NEIGHBOR_SEARCH_RANGE,
decision_threshold=1,
)
def _find_targets(
self,
member: Member,
target_list: List[Member],
decision_name: str,
prob_of_action: float = 1.0,
other_requirements: Callable = None,
bilateral: bool = False,
land_owner_decision: str = "",
) -> List[Member]:
"""
根据决策函数,从潜在对象列表中选出对象
decision_name: Member.parameter_dict的keys之一
other_requirements: 函数,输入为两个Member,输出True(通过要求)/False
bilateral: 设为True后,决策函数的判定为双向符合
landlord_decision: 地主的决策
"""
if target_list == []:
return []
selected_target = []
for tgt in target_list:
if self._rng.uniform(0, 1) > prob_of_action:
continue
# 检查tgt是元组(包含地主)还是单一成员
if isinstance(tgt, tuple):
land_owner, obj = tgt
elif isinstance(tgt, Member):
obj = tgt
land_owner = None
else:
raise ValueError("请在列表中输入正确的目标:成员、或(土地主人,成员)")
# 检查是否重复判断obj
if obj in selected_target:
continue
if other_requirements is not None:
if not other_requirements(member, obj):
continue
if not member.decision(
decision_name,
obj,
self
):
continue
if bilateral:
if not obj.decision(
decision_name,
member,
self
):
continue
if land_owner_decision != "" and land_owner is not None:
if not land_owner.decision(
land_owner_decision,
obj,
self
):
continue
selected_target.append(obj)
return selected_target
# ##############################################################################
# ##################################### 记录 ####################################
def _record_actions(
self,
record_name: str,
member_1: Member,
member_2: Member,
value_1: float,
value_2: float = None
):
record_dict = self.record_action_dict[record_name]
# 记录双方的动作
try:
record_dict[(member_1.id, member_2.id)] += value_1
except KeyError:
record_dict[(member_1.id, member_2.id)] = value_1
if value_2 is not None:
try:
record_dict[(member_2.id, member_1.id)] += value_2
except KeyError:
record_dict[(member_2.id, member_1.id)] = value_2
# 记录总动作
if value_2 is not None:
self.record_total_dict[record_name][-1] += value_1 + value_2
else:
self.record_total_dict[record_name][-1] += value_1
def generate_decision_history(self) -> None:
if not hasattr(self, 'decision_history'):
self.decision_history = {}
for member_1 in self.all_members:
if member_1 not in self.decision_history:
self.decision_history[member_1] = {}
self.decision_history[member_1][self.current_round] = (0, 0, 0)
for (member_1, member_2) in self.record_action_dict['attack']:
member_1 = self.all_members[member_1]
prev_decisions = self.decision_history[member_1][self.current_round]
self.decision_history[member_1][self.current_round] = (1, prev_decisions[1], prev_decisions[2])
for (member_1, member_2) in self.record_action_dict['benefit']:
member_1 = self.all_members[member_1]
prev_decisions = self.decision_history[member_1][self.current_round]
self.decision_history[member_1][self.current_round] = (prev_decisions[0], 1, prev_decisions[2])
for (member_1, member_2) in self.record_action_dict['benefit_land']:
member_1 = self.all_members[member_1]
prev_decisions = self.decision_history[member_1][self.current_round]
self.decision_history[member_1][self.current_round] = (prev_decisions[0], prev_decisions[1], 1)
def _record_historic_ratio(self):
current_attack_ratio = self.record_total_dict['attack'][-1]/(self.current_member_num)
current_benefit_ratio = self.record_total_dict['benefit'][-1]/(self.current_member_num)
current_benefit_land_ratio = self.record_total_dict['benefit_land'][-1]/(self.current_member_num)
# current_reproduce_ratio = self.record_total_dict[-1]['reproduce']/(self.current_member_num) #预留reproduce
self.record_historic_ratio_list = np.append(self.record_historic_ratio_list, [[current_attack_ratio, current_benefit_ratio, current_benefit_land_ratio, 0]], axis=0)
def _record_historic_ranking(self):
# 计算排位
current_attack_ranking = (sorted(self.record_historic_ratio_list[:,0]).index(self.record_historic_ratio_list[:,0][-1]) + 1)/len(self.record_historic_ratio_list[:,0])
current_benefit_ranking = (sorted(self.record_historic_ratio_list[:,1]).index(self.record_historic_ratio_list[:,1][-1]) + 1)/len(self.record_historic_ratio_list[:,1])
current_benefit_land_ranking = (sorted(self.record_historic_ratio_list[:,2]).index(self.record_historic_ratio_list[:,2][-1]) + 1)/len(self.record_historic_ratio_list[:,2])
self.record_historic_ranking_list.append((current_attack_ranking, current_benefit_ranking, current_benefit_land_ranking))
def _calculate_histoic_quartile(self):
# Flatten the list of tuples
flattened_list = [value for t in self.record_historic_ranking_list for value in t]
# Sort the flattened list
sorted_list = sorted(flattened_list)
# Determine quartile boundaries
q1_boundary = sorted_list[len(sorted_list) // 4]
q2_boundary = sorted_list[len(sorted_list) // 2]
q3_boundary = sorted_list[3 * len(sorted_list) // 4]
def determine_quartile(value):
if value <= q1_boundary:
return 1
elif value <= q2_boundary:
return 2
elif value <= q3_boundary:
return 3
else:
return 4
# Map each value in the tuples to its corresponding quartile
# Use a dictionary with the round as the key and the quartile tuple as the value
self.record_historic_quartile_dict = {current_round: (determine_quartile(val1), determine_quartile(val2), determine_quartile(val3))
for current_round, (val1, val2, val3) in enumerate(self.record_historic_ranking_list)}
def _generate_collective_actions_transition_matrix(self):
# Importing the required libraries and retrying the process
# Given list of tuples (sequence of events)
events = self.record_historic_quartile_dict
# Generate all possible tuple states
all_states = [(i, j, k) for i in range(1, 5) for j in range(1, 5) for k in range(1, 5)]
# Create a dictionary to track transitions
transitions = defaultdict(lambda: defaultdict(int))
# Populate the transitions dictionary
for i in range(len(events) - 1):
current_state = events[i]
next_state = events[i + 1]
transitions[current_state][next_state] += 1
# Normalize the counts to get probabilities
for current_state, next_states in transitions.items():
total_transitions = sum(next_states.values())
for next_state, count in next_states.items():
transitions[current_state][next_state] = count / total_transitions
# Create the 64x64 transition matrix
transition_matrix = []
for current_state in all_states:
row = []
for next_state in all_states:
row.append(transitions[current_state].get(next_state, 0))
transition_matrix.append(row)
self.collective_actions_transition_matrix = transition_matrix
def compute_vitality_difference(self):
round_diff = {}
for member in self.previous_vitalities.get(member):
current_vitality = member.vitality
prev_vitality = self.previous_vitalities.get(member.name, current_vitality) # Default to current vitality if not found
round_diff[member.name] = current_vitality - prev_vitality
self.previous_vitalities[member.name] = current_vitality
self.vitality_diff[self.current_round] = round_diff
def compute_payoff_matrix(self):
action_combinations = [(i, j, k) for i in [0,1] for j in [0,1] for k in [0,1]]
tuple_states = list(self.record_historic_quartile_dict.values())
payoff_matrix = np.zeros((8, 64))
for idx_a, action_a in enumerate(action_combinations):
for idx_t, tuple_state in enumerate(tuple_states):
total_vitality_change = 0
count = 0
# Assuming each member has a decision history
for member in self.current_members:
decisions = self.decision_history[member] # Access the decision history from the dictionary in the Island class
for round, decision in decisions.items():
if decision == action_a and tuple_state == self.record_historic_quartile_dict[round]:
total_vitality_change += self.vitality_diff[round][member]
count += 1
if count != 0:
avg_vitality_change = total_vitality_change / count
else:
avg_vitality_change = 0
payoff_matrix[idx_a][idx_t] = avg_vitality_change
return payoff_matrix
############################################################################
def save_current_island(self, path):
current_member_df = self.current_members[0].save_to_row()
for sur_id in range(1, self.current_member_num):
current_member_df = pd.concat([
current_member_df,
self.current_members[sur_id].save_to_row()],
axis=0
)
info_df = pd.DataFrame({
"_create_from_file": [self._create_from_file],
"_file_name": [self._file_name],
"_seed": [self._random_seed],
"init_member_num": [self.init_member_num],
"current_member_num": [self.current_member_num],
"current_round": [self.current_round],
"land_shape": [f"{self.land.shape[0]} {self.land.shape[1]}"],
})
relationship_df = pd.DataFrame()
for key, rela in self.relationship_dict.items():
rela_df = pd.DataFrame(rela, index=None, columns=None)
relationship_df = pd.concat([relationship_df, rela_df], axis=0)
# 本轮之前保存的动作
action_list = []
for action_name, action_dict in self.record_action_dict.items():
sub_action_info = [[key[0], key[1], value] for key, value in action_dict.items()]
sub_action_df = pd.DataFrame(
sub_action_info,
columns=[f"{action_name}_1", f"{action_name}_2", "value"]
)
action_list.append(sub_action_df)
born_df = pd.DataFrame(
[member.id for member in self.record_born],
columns=["born"]
)
action_list.append(born_df)
death_df = pd.DataFrame(
[member.id for member in self.record_death],
columns=["death"]
)
action_list.append(death_df)
action_df = pd.concat(action_list, axis=1)
# 土地
land_df = pd.DataFrame(
self.land.owner_id()
)
current_member_df.to_csv(path + "members.csv")
info_df.to_csv(path + "island_info.csv")
relationship_df.to_csv(path + "relationships.csv")
action_df.to_csv(path + "action.csv")
land_df.to_csv(path + "land.csv")
@classmethod
def load_island(cls, path):
current_member_df = pd.read_csv(path + "members.csv")
info_df = pd.read_csv(path + "island_info.csv")
relationship_df = pd.read_csv(path + "relationships.csv")
action_df = pd.read_csv(path + "action.csv")
land_df = pd.read_csv(path + "land.csv")
"""
Not finished yet
"""
def save_to_pickle(self, file_name: str) -> None:
sys.setrecursionlimit(50000)
file = open(file_name, 'wb')
pickle.dump(self, file)
@classmethod
def load_from_pickle(cls, file_name: str) -> "Island":
file = open(file_name, 'rb')
return pickle.load(file)
############################################################################
################################## 模拟 #####################################
@property
def shuffled_members(self) -> List[Member]:
"""
打乱整个current_members列表
"""
shuffled_members = self._backup_member_list(self.current_members)
self._rng.shuffle(shuffled_members)
return shuffled_members
def declare_dead(self, member: Member):
# 立即丢失所有土地
for loc in member.owned_land.copy():
self._discard_land(member, loc)
# 清除member_2
self.member_list_modify(drop=[member])
# 记录死亡
self.record_death.append(member)
def produce(self) -> None:
"""
生产
1. 根据生产力和土地,增加食物存储
"""
for member in self.current_members:
self.record_total_production[-1] += member.produce()
def _attack(
self,
member_1: Member,
member_2: Member
) -> None:
# 计算攻击、偷盗值
strength_1 = member_1.strength
steal_1 = member_1.steal
if steal_1 > member_2.cargo:
steal_1 = member_2.cargo
# 结算攻击、偷盗
member_2.vitality -= strength_1
member_2.cargo -= steal_1
# 修改关系矩阵
self.relationship_modify("victim", member_2, member_1, strength_1 + steal_1)
# 记录动作
self._record_actions(
"attack",
member_1,
member_2,
strength_1 + steal_1,
)
# 结算死亡
if member_2.autopsy():
# 结算死亡
self.declare_dead(member_2)
# member_1 立即获得扩张机会一次
self._expand(member_1)
# 若攻击目标的颜色和自身相同,攻击者恢复颜色(退出组织)
if np.allclose(member_1._current_color, member_2._current_color):
member_1._current_color = member_1._color.copy()
def fight(
self,
prob_to_fight: float = 1.0
):
"""
战斗
"""
for member in self.shuffled_members:
self._get_neighbors(member)
# 从邻居中寻找目标
target_list = (
member.current_clear_list
+ member.current_self_blocked_list
+ member.current_neighbor_blocked_list
)
attack_list = self._find_targets(
member = member,
target_list = target_list,
decision_name = "attack",
prob_of_action = prob_to_fight,
other_requirements = None,
bilateral = False,
land_owner_decision = "attack"
)
for target in attack_list:
self._attack(member, target)
def _offer(
self,
member_1: Member,
member_2: Member,
parameter_influence: bool = True
) -> None:
"""
member_1 给予 member_2
若member_1能给予的数量<1,不会给予
"""
amount = member_1.offer
if amount < 1:
return
# 结算给予
member_2.cargo += amount
member_1.cargo -= amount
# 修改关系矩阵
self.relationship_modify("benefit", member_2, member_1, amount)
# 记录
if amount > 1e-15:
self._record_actions(
"benefit",
member_1,
member_2,
amount,
None
)
# 被给予者的参数受到影响
if parameter_influence:
member_2.parameter_absorb(
[member_1, member_2],
[1 - Member._PARAMETER_INFLUENCE, Member._PARAMETER_INFLUENCE],
0
)
# 被给予者被染色
member_2._current_color = member_1._current_color
def trade(
self,
prob_to_trade: float = 1.0
):
"""
交易与交流
"""
for member in self.shuffled_members:
self._get_neighbors(member)
# 从邻居中寻找目标
trade_list = self._find_targets(
member = member,
target_list = (
member.current_clear_list
+ member.current_self_blocked_list
+ member.current_neighbor_blocked_list
),
decision_name = "offer",
prob_of_action = prob_to_trade,
other_requirements = _requirement_for_offer,
bilateral = False,
land_owner_decision = "offer"
)
self._rng.shuffle(trade_list)
for target in trade_list:
self._offer(member, target, parameter_influence=True)
def _expand(
self,
member: Member,
):
"""
扩张
"""
self._get_neighbors(member)
if len(member.current_empty_loc_list) > 0:
self._acquire_land(member, member.current_empty_loc_list[0])
def colonize(
self,
) -> None:
"""
集体扩张
"""
for member in self.shuffled_members:
self._expand(member)
def consume(
self,
):
"""
消费
1. 计算消耗量。消耗量会随着年龄逐步提升
2. 从血量中扣除消耗量,若血量小于零则记为死亡
3. 从仓库中吃食物回满血
4. 若有死亡案例,更新集体列表,更新编号,更新关系矩阵
"""
for member in self.current_members:
consumption = member.consume()
# 记录
self.record_total_consumption[-1] += consumption
if member.autopsy():
self.declare_dead(member)
for member in self.current_members:
member.recover()
def _offer_land(
self,
member_1: Member,
member_2: Member,
parameter_influence: bool = True,
assigned_pos: float = None,
) -> None:
"""
member_1 给予 member_2。
选出离自己最远的,离对方最近的land。
在提供“理想”位置时,会自动在给予者的土地中选出离assigned_pos最近的土地。
"""
# 选出离自己最远的,离对方最近的land
if member_1.land_num == 0:
raise RuntimeError("给予土地的人应该拥有至少一块土地")
if member_1.land_num == 0 and assigned_pos is None:
raise RuntimeError("在没有指定位置的情况下,接受土地的人应该拥有至少一块土地")
pos_1 = member_1.center_of_land(self.land)
if assigned_pos is None:
pos_2 = member_2.center_of_land(self.land)
else:
pos_2 = assigned_pos
farthest_distance = -np.inf
for land in member_1.owned_land:
distance = self.land.distance(pos_1, land) - self.land.distance(pos_2, land)
# distance = np.sum((pos_1 - land)**2) - np.sum((pos_2 - land)**2)
if distance > farthest_distance:
farthest_distance = distance
farthest_pos = land
pos = farthest_pos
# 结算给予
self._discard_land(member_1, pos)
self._acquire_land(member_2, pos)
# 修改关系矩阵
self.relationship_modify("benefit_land", member_2, member_1, 1)
# 记录
self._record_actions(
"benefit_land",
member_1,
member_2,
1,
None
)
# 被给予者的参数受到影响
if parameter_influence:
member_2.parameter_absorb(
[member_1, member_2],
[1 - Member._PARAMETER_INFLUENCE, Member._PARAMETER_INFLUENCE],
0
)
# 被给予者被染色
member_2._current_color = member_1._current_color
def land_distribute(
self,
prob_to_distr: float = 1.0
):
"""
交易与交流
"""
for member in self.shuffled_members:
self._get_neighbors(member)
# 从邻居中寻找目标
distr_list = self._find_targets(
member = member,