遗传算法

This commit is contained in:
Cricial
2026-03-15 17:15:30 +08:00
parent 03ac80715f
commit a69e272e43
27 changed files with 934 additions and 191 deletions

51
.idea/csv-editor.xml generated
View File

@@ -3,7 +3,42 @@
<component name="CsvFileAttributes"> <component name="CsvFileAttributes">
<option name="attributeMap"> <option name="attributeMap">
<map> <map>
<entry key="C:\Users\www\Desktop\python项目\数据\抽样第3次数据\firm_amended.csv"> <entry key="\GA_Agent_0925\convergence0119_data.csv">
<value>
<Attribute>
<option name="separator" value="," />
</Attribute>
</value>
</entry>
<entry key="\GA_Agent_0925\count_firm.csv">
<value>
<Attribute>
<option name="separator" value="," />
</Attribute>
</value>
</entry>
<entry key="\GA_Agent_0925\count_firm_prod.csv">
<value>
<Attribute>
<option name="separator" value="," />
</Attribute>
</value>
</entry>
<entry key="\GA_Agent_0925\count_prod.csv">
<value>
<Attribute>
<option name="separator" value="," />
</Attribute>
</value>
</entry>
<entry key="\GA_Agent_0925\risk_ay\count.csv">
<value>
<Attribute>
<option name="separator" value="," />
</Attribute>
</value>
</entry>
<entry key="\GA_Agent_0925\risk_ay\count_dcp.csv">
<value> <value>
<Attribute> <Attribute>
<option name="separator" value="," /> <option name="separator" value="," />
@@ -24,6 +59,13 @@
</Attribute> </Attribute>
</value> </value>
</entry> </entry>
<entry key="\GA_Agent_0925\vulnerable35_match_results\vulnerable20_match_results_new260111.csv">
<value>
<Attribute>
<option name="separator" value="," />
</Attribute>
</value>
</entry>
<entry key="\GA_Agent_0925\vulnerable35_match_results\vulnerable35_match_results.csv"> <entry key="\GA_Agent_0925\vulnerable35_match_results\vulnerable35_match_results.csv">
<value> <value>
<Attribute> <Attribute>
@@ -38,6 +80,13 @@
</Attribute> </Attribute>
</value> </value>
</entry> </entry>
<entry key="\GA_Agent_0925\vulnerable35_match_results\vulnerable63_match_results_new260111.csv">
<value>
<Attribute>
<option name="separator" value="," />
</Attribute>
</value>
</entry>
<entry key="\count_firm.csv"> <entry key="\count_firm.csv">
<value> <value>
<Attribute> <Attribute>

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="dataSourceStorageLocal" created-in="PY-242.23726.102"> <component name="dataSourceStorageLocal" created-in="PY-242.26775.22">
<data-source name="@localhost" uuid="3ce7b935-0ff7-47a3-aaa8-91063c963644"> <data-source name="@localhost" uuid="3ce7b935-0ff7-47a3-aaa8-91063c963644">
<database-info product="MySQL" version="8.0.36" jdbc-version="4.2" driver-name="Amazon Web Services (AWS) Advanced JDBC Wrapper" driver-version="Amazon Web Services (AWS) Advanced JDBC Wrapper 2.3.7 ( Revision: 7591851e8da4e1c705ba232a8bd07824a5cfd276 )" dbms="MYSQL" exact-version="8.0.36" exact-driver-version="2.3"> <database-info product="MySQL" version="8.0.36" jdbc-version="4.2" driver-name="Amazon Web Services (AWS) Advanced JDBC Wrapper" driver-version="Amazon Web Services (AWS) Advanced JDBC Wrapper 2.3.7 ( Revision: 7591851e8da4e1c705ba232a8bd07824a5cfd276 )" dbms="MYSQL" exact-version="8.0.36" exact-driver-version="2.3">
<extra-name-characters>#@</extra-name-characters> <extra-name-characters>#@</extra-name-characters>

View File

@@ -964,7 +964,7 @@ iiabmdb_20250926|schema||iiabm_user|localhost|UPDATE|G</Grants>
<CollationName>utf8mb4_general_ci</CollationName> <CollationName>utf8mb4_general_ci</CollationName>
</schema> </schema>
<schema id="289" parent="1" name="iiabmdb_20250925"> <schema id="289" parent="1" name="iiabmdb_20250925">
<LastIntrospectionLocalTimestamp>2025-11-21.16:00:59</LastIntrospectionLocalTimestamp> <LastIntrospectionLocalTimestamp>2026-01-26.12:22:45</LastIntrospectionLocalTimestamp>
<CollationName>utf8mb4_0900_ai_ci</CollationName> <CollationName>utf8mb4_0900_ai_ci</CollationName>
</schema> </schema>
<schema id="290" parent="1" name="iiabmdb_20250926"> <schema id="290" parent="1" name="iiabmdb_20250926">
@@ -1311,7 +1311,7 @@ iiabmdb_20250926|schema||iiabm_user|localhost|UPDATE|G</Grants>
<UnderlyingIndexName>PRIMARY</UnderlyingIndexName> <UnderlyingIndexName>PRIMARY</UnderlyingIndexName>
</key> </key>
<column id="362" parent="299" name="id"> <column id="362" parent="299" name="id">
<AutoIncrement>11616939</AutoIncrement> <AutoIncrement>22818</AutoIncrement>
<NotNull>1</NotNull> <NotNull>1</NotNull>
<Position>1</Position> <Position>1</Position>
<StoredType>int|0s</StoredType> <StoredType>int|0s</StoredType>
@@ -1365,7 +1365,7 @@ iiabmdb_20250926|schema||iiabm_user|localhost|UPDATE|G</Grants>
<UnderlyingIndexName>PRIMARY</UnderlyingIndexName> <UnderlyingIndexName>PRIMARY</UnderlyingIndexName>
</key> </key>
<column id="373" parent="300" name="id"> <column id="373" parent="300" name="id">
<AutoIncrement>3631</AutoIncrement> <AutoIncrement>364</AutoIncrement>
<NotNull>1</NotNull> <NotNull>1</NotNull>
<Position>1</Position> <Position>1</Position>
<StoredType>int|0s</StoredType> <StoredType>int|0s</StoredType>

1
.idea/sqldialects.xml generated
View File

@@ -2,6 +2,7 @@
<project version="4"> <project version="4">
<component name="SqlDialectMappings"> <component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/GA_Agent_0925/SQL_analysis_risk_ga.sql" dialect="MySQL" /> <file url="file://$PROJECT_DIR$/GA_Agent_0925/SQL_analysis_risk_ga.sql" dialect="MySQL" />
<file url="file://$PROJECT_DIR$/GA_Agent_0925/vulnerable35_match_results/query_top35_products.sql" dialect="MySQL" />
<file url="file://$PROJECT_DIR$/SQL_analysis_experiment.sql" dialect="MySQL" /> <file url="file://$PROJECT_DIR$/SQL_analysis_experiment.sql" dialect="MySQL" />
<file url="file://$PROJECT_DIR$/SQL_analysis_risk.sql" dialect="MySQL" /> <file url="file://$PROJECT_DIR$/SQL_analysis_risk.sql" dialect="MySQL" />
<file url="file://$PROJECT_DIR$/SQL_db_user_create.sql" dialect="MySQL" /> <file url="file://$PROJECT_DIR$/SQL_db_user_create.sql" dialect="MySQL" />

View File

@@ -8,5 +8,5 @@ test: # only for test scenarios
n_iter: 100 n_iter: 100
not_test: # normal scenarios not_test: # normal scenarios
n_sample: 5 n_sample: 1
n_iter: 60 n_iter: 50

View File

@@ -1,10 +1,10 @@
{ {
"pop_size": 20, "pop_size": 10,
"n_gen": 10, "n_gen": 60,
"n_var": 12, "n_var": 12,
"bound_min": -5, "bound_min": -5,
"bound_max": 5, "bound_max": 5,
"cx_prob": 0.5, "cx_prob": 0.8,
"mut_prob": 0.2, "mut_prob": 0.2,
"cx_alpha": 0.5, "cx_alpha": 0.5,
"mut_sigma": 0.1, "mut_sigma": 0.1,

View File

@@ -328,7 +328,7 @@ class ControllerDB:
)).scalar() )).scalar()
# 控制 n_sample数量 作为后面的参数 # 控制 n_sample数量 作为后面的参数
n_sample = 0 if res is None else res n_sample = 0 if res is None else res
# print(f'There are a total of {n_sample} samples.') print(f'There are a total of {n_sample} samples.')
# 查找 is_done_flag = -1 也就是没有运行的 样本 运行后会改为0 # 查找 is_done_flag = -1 也就是没有运行的 样本 运行后会改为0
res = db_session.execute( res = db_session.execute(
text(f"SELECT id FROM {self.db_name_prefix}_sample " text(f"SELECT id FROM {self.db_name_prefix}_sample "

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

View File

@@ -0,0 +1,64 @@
Timestamp,Generation,Best_Fitness_Percentage
2026-01-26 21:42:26,0,0.2090216731702263
2026-01-26 22:30:22,1,0.2052078225397421
2026-01-26 21:42:26,2,0.2016374981412617
2026-01-26 22:30:22,3,0.2013488674598191
2026-01-26 21:58:42,4,0.1993414391454412
2026-01-26 22:30:22,5,0.1988803893310773
2026-01-26 21:10:30,6,0.1987272799730525
2026-01-26 22:30:22,7,0.194483284141369
2026-01-26 22:30:22,8,0.1940582914278802
2026-01-26 21:42:26,9,0.1928494295868045
2026-01-26 21:58:42,10,0.1898885073092019
2026-01-26 21:42:26,11,0.1889512197824896
2026-01-26 21:58:42,12,0.1858551470980505
2026-01-26 21:42:26,13,0.1845113472201131
2026-01-26 22:15:59,14,0.184417748143425
2026-01-26 21:42:26,15,0.1826658943130832
2026-01-26 21:42:26,16,0.1811738541290951
2026-01-26 21:58:42,17,0.1803782808697044
2026-01-26 21:58:42,18,0.180378251293931
2026-01-26 21:10:30,19,0.1802064263206075
2026-01-26 21:42:26,20,0.1799530185714123
2026-01-26 21:42:26,21,0.1799530185714123
2026-01-26 21:42:26,22,0.1799530185714123
2026-01-26 21:58:42,23,0.1776493750092811
2026-01-26 21:58:42,24,0.1776493750092811
2026-01-26 21:58:42,25,0.1776493750092811
2026-01-26 21:58:42,26,0.1776493750092811
2026-01-26 21:58:42,27,0.1776493750092811
2026-01-26 22:30:22,28,0.1774372678590745
2026-01-26 22:30:22,29,0.1774372678590745
2026-01-26 22:30:22,30,0.1774372678590745
2026-01-26 22:30:22,31,0.1774372678590745
2026-01-26 22:30:22,32,0.1774372678590745
2026-01-26 22:30:22,33,0.1774372678590745
2026-01-26 22:30:22,34,0.1774372678590745
2026-01-26 22:30:22,35,0.1774372678590745
2026-01-26 22:30:22,36,0.1774372678590745
2026-01-26 22:30:22,37,0.1774372678590745
2026-01-26 22:30:22,38,0.1774372678590745
2026-01-26 22:30:22,39,0.1774372678590745
2026-01-26 22:30:22,40,0.1774372678590745
2026-01-26 22:30:22,41,0.1774372678590745
2026-01-26 22:30:22,42,0.1774372678590745
2026-01-26 22:30:22,43,0.1774372678590745
2026-01-26 22:30:22,44,0.1774372678590745
2026-01-26 22:30:22,45,0.1774372678590745
2026-01-26 22:30:22,46,0.1774372678590745
2026-01-26 22:30:22,47,0.1774372678590745
2026-01-26 22:30:22,48,0.1774372678590745
2026-01-26 22:30:22,49,0.1774372678590745
2026-01-26 22:30:22,50,0.1774372678590745
2026-01-26 22:30:22,51,0.1774372678590745
2026-01-26 22:30:22,52,0.1774372678590745
2026-01-26 22:30:22,53,0.1774372678590745
2026-01-26 22:30:22,54,0.1774372678590745
2026-01-26 22:30:22,55,0.1774372678590745
2026-01-26 22:30:22,56,0.1774372678590745
2026-01-26 22:30:22,57,0.1774372678590745
2026-01-26 22:30:22,58,0.1774372678590745
2026-01-26 22:30:22,59,0.1774372678590745
2026-01-26 22:30:22,60,0.1774372678590745
2026-01-26 22:30:22,61,0.1774372678590745
2026-01-26 22:30:22,62,0.1774372678590745
1 Timestamp Generation Best_Fitness_Percentage
2 2026-01-26 21:42:26 0 0.2090216731702263
3 2026-01-26 22:30:22 1 0.2052078225397421
4 2026-01-26 21:42:26 2 0.2016374981412617
5 2026-01-26 22:30:22 3 0.2013488674598191
6 2026-01-26 21:58:42 4 0.1993414391454412
7 2026-01-26 22:30:22 5 0.1988803893310773
8 2026-01-26 21:10:30 6 0.1987272799730525
9 2026-01-26 22:30:22 7 0.194483284141369
10 2026-01-26 22:30:22 8 0.1940582914278802
11 2026-01-26 21:42:26 9 0.1928494295868045
12 2026-01-26 21:58:42 10 0.1898885073092019
13 2026-01-26 21:42:26 11 0.1889512197824896
14 2026-01-26 21:58:42 12 0.1858551470980505
15 2026-01-26 21:42:26 13 0.1845113472201131
16 2026-01-26 22:15:59 14 0.184417748143425
17 2026-01-26 21:42:26 15 0.1826658943130832
18 2026-01-26 21:42:26 16 0.1811738541290951
19 2026-01-26 21:58:42 17 0.1803782808697044
20 2026-01-26 21:58:42 18 0.180378251293931
21 2026-01-26 21:10:30 19 0.1802064263206075
22 2026-01-26 21:42:26 20 0.1799530185714123
23 2026-01-26 21:42:26 21 0.1799530185714123
24 2026-01-26 21:42:26 22 0.1799530185714123
25 2026-01-26 21:58:42 23 0.1776493750092811
26 2026-01-26 21:58:42 24 0.1776493750092811
27 2026-01-26 21:58:42 25 0.1776493750092811
28 2026-01-26 21:58:42 26 0.1776493750092811
29 2026-01-26 21:58:42 27 0.1776493750092811
30 2026-01-26 22:30:22 28 0.1774372678590745
31 2026-01-26 22:30:22 29 0.1774372678590745
32 2026-01-26 22:30:22 30 0.1774372678590745
33 2026-01-26 22:30:22 31 0.1774372678590745
34 2026-01-26 22:30:22 32 0.1774372678590745
35 2026-01-26 22:30:22 33 0.1774372678590745
36 2026-01-26 22:30:22 34 0.1774372678590745
37 2026-01-26 22:30:22 35 0.1774372678590745
38 2026-01-26 22:30:22 36 0.1774372678590745
39 2026-01-26 22:30:22 37 0.1774372678590745
40 2026-01-26 22:30:22 38 0.1774372678590745
41 2026-01-26 22:30:22 39 0.1774372678590745
42 2026-01-26 22:30:22 40 0.1774372678590745
43 2026-01-26 22:30:22 41 0.1774372678590745
44 2026-01-26 22:30:22 42 0.1774372678590745
45 2026-01-26 22:30:22 43 0.1774372678590745
46 2026-01-26 22:30:22 44 0.1774372678590745
47 2026-01-26 22:30:22 45 0.1774372678590745
48 2026-01-26 22:30:22 46 0.1774372678590745
49 2026-01-26 22:30:22 47 0.1774372678590745
50 2026-01-26 22:30:22 48 0.1774372678590745
51 2026-01-26 22:30:22 49 0.1774372678590745
52 2026-01-26 22:30:22 50 0.1774372678590745
53 2026-01-26 22:30:22 51 0.1774372678590745
54 2026-01-26 22:30:22 52 0.1774372678590745
55 2026-01-26 22:30:22 53 0.1774372678590745
56 2026-01-26 22:30:22 54 0.1774372678590745
57 2026-01-26 22:30:22 55 0.1774372678590745
58 2026-01-26 22:30:22 56 0.1774372678590745
59 2026-01-26 22:30:22 57 0.1774372678590745
60 2026-01-26 22:30:22 58 0.1774372678590745
61 2026-01-26 22:30:22 59 0.1774372678590745
62 2026-01-26 22:30:22 60 0.1774372678590745
63 2026-01-26 22:30:22 61 0.1774372678590745
64 2026-01-26 22:30:22 62 0.1774372678590745

Binary file not shown.

After

Width:  |  Height:  |  Size: 245 KiB

View File

@@ -0,0 +1,198 @@
id_firm,count
214851100,303
3111603340,299
70634828,282
395736790,122
340093034,117
29223617,96
25980377,81
3330358736,79
2337727838,63
331545755,60
2326722141,60
3191869223,54
517675473,53
2327605629,51
591350440,48
728969035,46
532328014,45
2336923756,43
303926772,38
16116663,38
471121089,37
2349179532,35
16210433,34
2349705416,31
1452048,5
515770253,3
2349345463,2
6,2
5971532,2
1,2
5849940,2
2424229017,2
495782506,2
420984285,2
300186799,2
37873062,2
3392803162,2
33822284,2
3312358902,2
2728939,2
3226664625,2
79938367,2
78979697,2
14913649,2
80158773,2
8,2
11807506,2
169978927,2
4607820,1
441623911,1
367669349,1
410030851,1
395739442,1
808524154,1
38852110,1
385766513,1
8114841,1
382080545,1
466148111,1
805940123,1
675729777,1
863973253,1
35404067,1
3462551351,1
3429928077,1
3420061649,1
340603317,1
872394725,1
9032550,1
907433543,1
950849442,1
3378606529,1
3362063909,1
3358892171,1
961017,1
9620005,1
862404568,1
493002466,1
668539285,1
507827038,1
676597455,1
688155470,1
664591135,1
648145286,1
644252759,1
643954924,1
640700057,1
6333996,1
618469306,1
696450846,1
71271700,1
733657390,1
3268669333,1
5979030,1
737770776,1
742704658,1
596368303,1
59234665,1
756272716,1
758879940,1
578803019,1
562681526,1
551856519,1
543470507,1
762501019,1
771821595,1
518871190,1
778745779,1
78576577,1
598808584,1
314846874,1
3226232,1
2333843479,1
2310534839,1
2311838590,1
2314659369,1
2316150629,1
2323069589,1
2326520912,1
2326655246,1
2326903290,1
2334430421,1
225958786,1
2337952436,1
2341774429,1
23421122,1
2344471631,1
2345982379,1
2347561020,1
2348894245,1
2348987001,1
2310406050,1
205960791,1
3222821993,1
1524794108,1
1160497810,1
1171244159,1
1208566436,1
1247902451,1
1253552935,1
13854344,1
1444449910,1
1476953321,1
157087137,1
203314437,1
1605495,1
1651310523,1
16715045,1
1698501971,1
178452970,1
189427260,1
193814549,1
197362120,1
2349349655,1
2349588257,1
2349742676,1
3145389278,1
2978926070,1
2989649772,1
29954548,1
3010580773,1
3011933107,1
3031766093,1
3118140206,1
3135349256,1
3147958370,1
2349746655,1
10437056,1
3151377261,1
31732840,1
3195293647,1
3196033145,1
3203980088,1
3220049148,1
3222664794,1
2962064709,1
29452962,1
2944593082,1
290636928,1
2350443114,1
2350544061,1
2351592628,1
2352421906,1
2353389310,1
2353549582,1
2382390052,1
2448521375,1
247297633,1
25685135,1
26487185,1
265133300,1
26895145,1
27042865,1
280281699,1
286335813,1
28667694,1
9746245,1
1 id_firm count
2 214851100 303
3 3111603340 299
4 70634828 282
5 395736790 122
6 340093034 117
7 29223617 96
8 25980377 81
9 3330358736 79
10 2337727838 63
11 331545755 60
12 2326722141 60
13 3191869223 54
14 517675473 53
15 2327605629 51
16 591350440 48
17 728969035 46
18 532328014 45
19 2336923756 43
20 303926772 38
21 16116663 38
22 471121089 37
23 2349179532 35
24 16210433 34
25 2349705416 31
26 1452048 5
27 515770253 3
28 2349345463 2
29 6 2
30 5971532 2
31 1 2
32 5849940 2
33 2424229017 2
34 495782506 2
35 420984285 2
36 300186799 2
37 37873062 2
38 3392803162 2
39 33822284 2
40 3312358902 2
41 2728939 2
42 3226664625 2
43 79938367 2
44 78979697 2
45 14913649 2
46 80158773 2
47 8 2
48 11807506 2
49 169978927 2
50 4607820 1
51 441623911 1
52 367669349 1
53 410030851 1
54 395739442 1
55 808524154 1
56 38852110 1
57 385766513 1
58 8114841 1
59 382080545 1
60 466148111 1
61 805940123 1
62 675729777 1
63 863973253 1
64 35404067 1
65 3462551351 1
66 3429928077 1
67 3420061649 1
68 340603317 1
69 872394725 1
70 9032550 1
71 907433543 1
72 950849442 1
73 3378606529 1
74 3362063909 1
75 3358892171 1
76 961017 1
77 9620005 1
78 862404568 1
79 493002466 1
80 668539285 1
81 507827038 1
82 676597455 1
83 688155470 1
84 664591135 1
85 648145286 1
86 644252759 1
87 643954924 1
88 640700057 1
89 6333996 1
90 618469306 1
91 696450846 1
92 71271700 1
93 733657390 1
94 3268669333 1
95 5979030 1
96 737770776 1
97 742704658 1
98 596368303 1
99 59234665 1
100 756272716 1
101 758879940 1
102 578803019 1
103 562681526 1
104 551856519 1
105 543470507 1
106 762501019 1
107 771821595 1
108 518871190 1
109 778745779 1
110 78576577 1
111 598808584 1
112 314846874 1
113 3226232 1
114 2333843479 1
115 2310534839 1
116 2311838590 1
117 2314659369 1
118 2316150629 1
119 2323069589 1
120 2326520912 1
121 2326655246 1
122 2326903290 1
123 2334430421 1
124 225958786 1
125 2337952436 1
126 2341774429 1
127 23421122 1
128 2344471631 1
129 2345982379 1
130 2347561020 1
131 2348894245 1
132 2348987001 1
133 2310406050 1
134 205960791 1
135 3222821993 1
136 1524794108 1
137 1160497810 1
138 1171244159 1
139 1208566436 1
140 1247902451 1
141 1253552935 1
142 13854344 1
143 1444449910 1
144 1476953321 1
145 157087137 1
146 203314437 1
147 1605495 1
148 1651310523 1
149 16715045 1
150 1698501971 1
151 178452970 1
152 189427260 1
153 193814549 1
154 197362120 1
155 2349349655 1
156 2349588257 1
157 2349742676 1
158 3145389278 1
159 2978926070 1
160 2989649772 1
161 29954548 1
162 3010580773 1
163 3011933107 1
164 3031766093 1
165 3118140206 1
166 3135349256 1
167 3147958370 1
168 2349746655 1
169 10437056 1
170 3151377261 1
171 31732840 1
172 3195293647 1
173 3196033145 1
174 3203980088 1
175 3220049148 1
176 3222664794 1
177 2962064709 1
178 29452962 1
179 2944593082 1
180 290636928 1
181 2350443114 1
182 2350544061 1
183 2351592628 1
184 2352421906 1
185 2353389310 1
186 2353549582 1
187 2382390052 1
188 2448521375 1
189 247297633 1
190 25685135 1
191 26487185 1
192 265133300 1
193 26895145 1
194 27042865 1
195 280281699 1
196 286335813 1
197 28667694 1
198 9746245 1

View File

@@ -0,0 +1,259 @@
id_firm,id_product,count
395736790,95,122
340093034,95,117
29223617,95,96
2337727838,90,63
2326722141,90,60
331545755,90,60
3191869223,91,54
517675473,92,53
2327605629,94,51
591350440,91,48
728969035,93,46
532328014,99,45
2336923756,91,43
303926772,99,38
471121089,93,37
2349179532,93,35
70634828,55,34
16210433,92,34
16116663,92,34
214851100,52,33
3111603340,52,33
214851100,55,32
70634828,52,31
3111603340,53,31
214851100,51,31
3111603340,55,31
2349705416,94,31
3111603340,50,30
70634828,53,30
70634828,50,29
214851100,53,27
3111603340,54,27
214851100,54,26
214851100,50,26
70634828,51,25
3111603340,51,25
214851100,49,24
214851100,44,24
3111603340,47,24
214851100,48,23
70634828,46,22
214851100,47,22
3111603340,48,21
3111603340,46,20
70634828,47,20
3111603340,49,20
70634828,54,20
3330358736,41,19
70634828,48,19
3111603340,44,19
70634828,49,18
70634828,44,18
3330358736,42,18
25980377,41,18
214851100,46,18
3111603340,45,17
214851100,45,16
25980377,43,15
70634828,45,15
25980377,38,14
25980377,42,13
25980377,39,12
3330358736,38,12
3330358736,43,11
3330358736,39,10
3330358736,40,9
25980377,40,9
1452048,9,3
515770253,9,3
495782506,19,2
420984285,16,2
5971532,9,2
37873062,9,2
79938367,9,2
8,37,2
3392803162,9,2
33822284,9,2
80158773,69,2
6,10,2
1,10,2
14913649,9,2
16116663,11,2
2350443114,74,1
5979030,9,1
562681526,25,1
578803019,10,1
5849940,26,1
5849940,36,1
16116663,9,1
59234665,65,1
596368303,12,1
598808584,15,1
543470507,8,1
16116663,10,1
618469306,9,1
6333996,66,1
640700057,9,1
643954924,9,1
644252759,61,1
648145286,35,1
664591135,9,1
551856519,33,1
1651310523,9,1
675729777,10,1
518871190,9,1
35404067,9,1
367669349,31,1
193814549,33,1
382080545,9,1
385766513,24,1
38852110,10,1
189427260,9,1
395739442,9,1
410030851,27,1
178452970,25,1
441623911,79,1
4607820,9,1
466148111,9,1
169978927,9,1
493002466,9,1
169978927,66,1
507827038,10,1
1698501971,9,1
16715045,10,1
668539285,17,1
676597455,68,1
2350544061,9,1
808524154,9,1
778745779,9,1
78576577,9,1
78979697,61,1
78979697,74,1
11807506,36,1
1171244159,32,1
1160497810,12,1
805940123,72,1
8114841,9,1
762501019,9,1
862404568,11,1
863973253,9,1
872394725,70,1
9032550,34,1
907433543,10,1
950849442,9,1
961017,23,1
9620005,9,1
771821595,31,1
758879940,65,1
688155470,30,1
13854344,9,1
696450846,9,1
1605495,9,1
157087137,73,1
1524794108,9,1
1476953321,10,1
1452048,30,1
1452048,11,1
1444449910,20,1
1253552935,9,1
756272716,32,1
1247902451,9,1
1208566436,62,1
70634828,7,1
71271700,27,1
11807506,9,1
733657390,10,1
737770776,34,1
742704658,9,1
3462551351,13,1
3429928077,9,1
3420061649,9,1
340603317,9,1
280281699,60,1
286335813,72,1
28667694,10,1
290636928,9,1
2348987001,9,1
2944593082,9,1
29452962,9,1
2962064709,9,1
2978926070,8,1
2989649772,71,1
29954548,18,1
300186799,10,1
300186799,11,1
3010580773,9,1
3011933107,9,1
3031766093,9,1
2348894245,11,1
2347561020,9,1
2345982379,67,1
2728939,71,1
2728939,63,1
27042865,9,1
247297633,18,1
2351592628,10,1
2352421906,17,1
2353389310,15,1
2353549582,23,1
2382390052,9,1
2424229017,26,1
2424229017,9,1
2448521375,25,1
25685135,11,1
26895145,9,1
2349746655,10,1
2349742676,33,1
2349588257,10,1
2349349655,9,1
2349345463,59,1
2349345463,35,1
26487185,62,1
265133300,9,1
2344471631,9,1
23421122,9,1
2341774429,9,1
2310534839,64,1
3226664625,13,1
3226664625,28,1
3268669333,67,1
3312358902,59,1
3312358902,79,1
2316150629,10,1
2314659369,20,1
2311838590,97,1
2310406050,60,1
3222821993,9,1
225958786,11,1
214851100,7,1
3358892171,64,1
3362063909,10,1
3378606529,9,1
205960791,63,1
203314437,22,1
197362120,15,1
3226232,10,1
3222664794,9,1
2337952436,24,1
3135349256,9,1
2334430421,73,1
2333843479,70,1
10437056,9,1
2326903290,9,1
2326655246,9,1
2326520912,10,1
3111603340,7,1
3118140206,68,1
3145389278,9,1
3220049148,9,1
3147958370,12,1
314846874,11,1
3151377261,29,1
31732840,29,1
2323069589,9,1
3195293647,28,1
3196033145,9,1
3203980088,10,1
9746245,97,1
1 id_firm id_product count
2 395736790 95 122
3 340093034 95 117
4 29223617 95 96
5 2337727838 90 63
6 2326722141 90 60
7 331545755 90 60
8 3191869223 91 54
9 517675473 92 53
10 2327605629 94 51
11 591350440 91 48
12 728969035 93 46
13 532328014 99 45
14 2336923756 91 43
15 303926772 99 38
16 471121089 93 37
17 2349179532 93 35
18 70634828 55 34
19 16210433 92 34
20 16116663 92 34
21 214851100 52 33
22 3111603340 52 33
23 214851100 55 32
24 70634828 52 31
25 3111603340 53 31
26 214851100 51 31
27 3111603340 55 31
28 2349705416 94 31
29 3111603340 50 30
30 70634828 53 30
31 70634828 50 29
32 214851100 53 27
33 3111603340 54 27
34 214851100 54 26
35 214851100 50 26
36 70634828 51 25
37 3111603340 51 25
38 214851100 49 24
39 214851100 44 24
40 3111603340 47 24
41 214851100 48 23
42 70634828 46 22
43 214851100 47 22
44 3111603340 48 21
45 3111603340 46 20
46 70634828 47 20
47 3111603340 49 20
48 70634828 54 20
49 3330358736 41 19
50 70634828 48 19
51 3111603340 44 19
52 70634828 49 18
53 70634828 44 18
54 3330358736 42 18
55 25980377 41 18
56 214851100 46 18
57 3111603340 45 17
58 214851100 45 16
59 25980377 43 15
60 70634828 45 15
61 25980377 38 14
62 25980377 42 13
63 25980377 39 12
64 3330358736 38 12
65 3330358736 43 11
66 3330358736 39 10
67 3330358736 40 9
68 25980377 40 9
69 1452048 9 3
70 515770253 9 3
71 495782506 19 2
72 420984285 16 2
73 5971532 9 2
74 37873062 9 2
75 79938367 9 2
76 8 37 2
77 3392803162 9 2
78 33822284 9 2
79 80158773 69 2
80 6 10 2
81 1 10 2
82 14913649 9 2
83 16116663 11 2
84 2350443114 74 1
85 5979030 9 1
86 562681526 25 1
87 578803019 10 1
88 5849940 26 1
89 5849940 36 1
90 16116663 9 1
91 59234665 65 1
92 596368303 12 1
93 598808584 15 1
94 543470507 8 1
95 16116663 10 1
96 618469306 9 1
97 6333996 66 1
98 640700057 9 1
99 643954924 9 1
100 644252759 61 1
101 648145286 35 1
102 664591135 9 1
103 551856519 33 1
104 1651310523 9 1
105 675729777 10 1
106 518871190 9 1
107 35404067 9 1
108 367669349 31 1
109 193814549 33 1
110 382080545 9 1
111 385766513 24 1
112 38852110 10 1
113 189427260 9 1
114 395739442 9 1
115 410030851 27 1
116 178452970 25 1
117 441623911 79 1
118 4607820 9 1
119 466148111 9 1
120 169978927 9 1
121 493002466 9 1
122 169978927 66 1
123 507827038 10 1
124 1698501971 9 1
125 16715045 10 1
126 668539285 17 1
127 676597455 68 1
128 2350544061 9 1
129 808524154 9 1
130 778745779 9 1
131 78576577 9 1
132 78979697 61 1
133 78979697 74 1
134 11807506 36 1
135 1171244159 32 1
136 1160497810 12 1
137 805940123 72 1
138 8114841 9 1
139 762501019 9 1
140 862404568 11 1
141 863973253 9 1
142 872394725 70 1
143 9032550 34 1
144 907433543 10 1
145 950849442 9 1
146 961017 23 1
147 9620005 9 1
148 771821595 31 1
149 758879940 65 1
150 688155470 30 1
151 13854344 9 1
152 696450846 9 1
153 1605495 9 1
154 157087137 73 1
155 1524794108 9 1
156 1476953321 10 1
157 1452048 30 1
158 1452048 11 1
159 1444449910 20 1
160 1253552935 9 1
161 756272716 32 1
162 1247902451 9 1
163 1208566436 62 1
164 70634828 7 1
165 71271700 27 1
166 11807506 9 1
167 733657390 10 1
168 737770776 34 1
169 742704658 9 1
170 3462551351 13 1
171 3429928077 9 1
172 3420061649 9 1
173 340603317 9 1
174 280281699 60 1
175 286335813 72 1
176 28667694 10 1
177 290636928 9 1
178 2348987001 9 1
179 2944593082 9 1
180 29452962 9 1
181 2962064709 9 1
182 2978926070 8 1
183 2989649772 71 1
184 29954548 18 1
185 300186799 10 1
186 300186799 11 1
187 3010580773 9 1
188 3011933107 9 1
189 3031766093 9 1
190 2348894245 11 1
191 2347561020 9 1
192 2345982379 67 1
193 2728939 71 1
194 2728939 63 1
195 27042865 9 1
196 247297633 18 1
197 2351592628 10 1
198 2352421906 17 1
199 2353389310 15 1
200 2353549582 23 1
201 2382390052 9 1
202 2424229017 26 1
203 2424229017 9 1
204 2448521375 25 1
205 25685135 11 1
206 26895145 9 1
207 2349746655 10 1
208 2349742676 33 1
209 2349588257 10 1
210 2349349655 9 1
211 2349345463 59 1
212 2349345463 35 1
213 26487185 62 1
214 265133300 9 1
215 2344471631 9 1
216 23421122 9 1
217 2341774429 9 1
218 2310534839 64 1
219 3226664625 13 1
220 3226664625 28 1
221 3268669333 67 1
222 3312358902 59 1
223 3312358902 79 1
224 2316150629 10 1
225 2314659369 20 1
226 2311838590 97 1
227 2310406050 60 1
228 3222821993 9 1
229 225958786 11 1
230 214851100 7 1
231 3358892171 64 1
232 3362063909 10 1
233 3378606529 9 1
234 205960791 63 1
235 203314437 22 1
236 197362120 15 1
237 3226232 10 1
238 3222664794 9 1
239 2337952436 24 1
240 3135349256 9 1
241 2334430421 73 1
242 2333843479 70 1
243 10437056 9 1
244 2326903290 9 1
245 2326655246 9 1
246 2326520912 10 1
247 3111603340 7 1
248 3118140206 68 1
249 3145389278 9 1
250 3220049148 9 1
251 3147958370 12 1
252 314846874 11 1
253 3151377261 29 1
254 31732840 29 1
255 2323069589 9 1
256 3195293647 28 1
257 3196033145 9 1
258 3203980088 10 1
259 9746245 97 1

View File

@@ -0,0 +1,73 @@
id_product,count
95,335
90,183
91,145
92,121
93,118
55,97
52,97
53,88
50,85
9,84
99,83
94,82
51,81
54,73
47,66
48,63
49,62
44,61
46,60
45,48
41,37
42,31
38,26
43,26
10,23
39,22
40,18
11,9
15,3
33,3
7,3
12,3
25,3
18,2
74,2
68,2
69,2
70,2
71,2
72,2
73,2
97,2
79,2
8,2
17,2
19,2
66,2
13,2
16,2
67,2
28,2
65,2
26,2
30,2
31,2
32,2
34,2
35,2
36,2
37,2
27,2
24,2
64,2
23,2
20,2
29,2
60,2
61,2
62,2
63,2
59,2
22,1
1 id_product count
2 95 335
3 90 183
4 91 145
5 92 121
6 93 118
7 55 97
8 52 97
9 53 88
10 50 85
11 9 84
12 99 83
13 94 82
14 51 81
15 54 73
16 47 66
17 48 63
18 49 62
19 44 61
20 46 60
21 45 48
22 41 37
23 42 31
24 38 26
25 43 26
26 10 23
27 39 22
28 40 18
29 11 9
30 15 3
31 33 3
32 7 3
33 12 3
34 25 3
35 18 2
36 74 2
37 68 2
38 69 2
39 70 2
40 71 2
41 72 2
42 73 2
43 97 2
44 79 2
45 8 2
46 17 2
47 19 2
48 66 2
49 13 2
50 16 2
51 67 2
52 28 2
53 65 2
54 26 2
55 30 2
56 31 2
57 32 2
58 34 2
59 35 2
60 36 2
61 37 2
62 27 2
63 24 2
64 64 2
65 23 2
66 20 2
67 29 2
68 60 2
69 61 2
70 62 2
71 63 2
72 59 2
73 22 1

View File

@@ -3,26 +3,29 @@ from deap import creator, base, tools
from evaluate_func import fitness from evaluate_func import fitness
def creating(): def creating():
if "FitnessMax" not in creator.__dict__: if "FitnessMin" not in creator.__dict__:
creator.create("FitnessMax", base.Fitness, weights=(1.0,)) creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
if "Individual" not in creator.__dict__: if "Individual" not in creator.__dict__:
creator.create("Individual", list, fitness=creator.FitnessMax) creator.create("Individual", list, fitness=creator.FitnessMin)
toolbox = base.Toolbox() toolbox = base.Toolbox()
# 基因注册 # ==============================
toolbox.register("n_max_trial", random.randint, 1, 60) # 基因注册(修改后)
toolbox.register("prf_size", random.uniform, 0.0, 1.0) # ==============================
toolbox.register("prf_conn", random.uniform, 0.0, 1.0)
toolbox.register("n_max_trial", random.randint, 50, 200)
toolbox.register("prf_size", random.uniform, 0.05, 0.95)
toolbox.register("prf_conn", random.uniform, 0.05, 0.95)
toolbox.register("cap_limit_prob_type", random.randint, 0, 1) toolbox.register("cap_limit_prob_type", random.randint, 0, 1)
toolbox.register("cap_limit_level", random.randint, 5, 80) toolbox.register("cap_limit_level", random.randint, 10, 50)
toolbox.register("diff_new_conn", random.uniform, 0.0, 1.0) toolbox.register("diff_new_conn", random.uniform, 0.05, 0.95)
toolbox.register("netw_prf_n", random.randint, 1, 20) toolbox.register("netw_prf_n", random.randint, 3, 15)
toolbox.register("s_r", random.uniform, 0.01, 0.5) toolbox.register("s_r", random.uniform, 0.05, 0.4)
toolbox.register("S_r", random.uniform, 0.5, 1.0) toolbox.register("S_r", random.uniform, 0.8, 2.5)
toolbox.register("x", random.uniform, 0.0, 1) toolbox.register("x", random.uniform, 0.1, 0.9)
toolbox.register("k", random.uniform, 0.1, 5.0) toolbox.register("k", random.uniform, 0.2, 4.0)
toolbox.register("production_increase_ratio", random.uniform, 1, 5.0) toolbox.register("production_increase_ratio", random.uniform, 0.3, 3.0)
# 个体与种群注册 # 个体与种群注册
toolbox.register( toolbox.register(

View File

@@ -13,7 +13,6 @@ from my_model import MyModel
from orm import connection, engine from orm import connection, engine
# 🎯 适应度函数(核心目标函数)
def fitness(individual, controller_db_obj): def fitness(individual, controller_db_obj):
""" """
遗传算法适应度函数:用于评估个体(模型参数)的优劣。 遗传算法适应度函数:用于评估个体(模型参数)的优劣。
@@ -57,7 +56,7 @@ def fitness(individual, controller_db_obj):
# print(f" {key}: {value}") # print(f" {key}: {value}")
# ========== 2⃣ 调用 ABM 模型 ========== # ========== 2⃣ 调用 ABM 模型 ==========
# 并行进程数目 # 并行进程数目
job=4 job=6
do_process(controller_db_obj,ga_id,dct_exp,job) do_process(controller_db_obj,ga_id,dct_exp,job)
# ========== 3⃣ 获取数据库连接并提取结果 ========== # ========== 3⃣ 获取数据库连接并提取结果 ==========
simulated_vulnerable_industries = get_vulnerable35_code(connection,ga_id) simulated_vulnerable_industries = get_vulnerable35_code(connection,ga_id)
@@ -65,25 +64,36 @@ def fitness(individual, controller_db_obj):
# ========== 4⃣ 获取目标产业集合 ========== # ========== 4⃣ 获取目标产业集合 ==========
target_vulnerable_industries = get_target_vulnerable_industries() target_vulnerable_industries = get_target_vulnerable_industries()
# ========== 5⃣ 计算误差(集合差异度) ========== """
set_sim = set(simulated_vulnerable_industries) Top-K 加权命中误差(越小越好)
set_target = set(target_vulnerable_industries)
error = len(set_sim.symmetric_difference(set_target))
simulated_set = set(simulated_vulnerable_industries) simulated_vulnerable_industries : list[str]
模型输出的产业排序(风险从高到低)
target_vulnerable_industries : list[str] or set[str]
真实脆弱产业集合(无序)
"""
ranked_list = simulated_vulnerable_industries
target_set = set(target_vulnerable_industries) target_set = set(target_vulnerable_industries)
matching = simulated_set & target_set # 交集 total_weight = 0.0
extra = simulated_set - target_set # 模拟多出的产业 hit_weight = 0.0
missing = target_set - simulated_set # 未覆盖产业
print(f"符合目标的产业数量: {len(matching)}") for rank, industry in enumerate(ranked_list, start=1):
print(matching) # 权重排名越靠前越大1 / rank
print(f"模拟多出的产业数量: {len(extra)}") w = 1.0 /rank
print(f"未覆盖目标产业数量: {len(missing)}") total_weight += w
# ========== 6⃣ 返回适应度(越大越好) ========== if industry in target_set:
return (float(-error),) hit_weight += w
hit_ratio = hit_weight / total_weight if total_weight > 0 else 0.0
error = 1.0 - hit_ratio # GA 里:越小越好
# ---- 调试信息(强烈建议保留) ----
print(f"加权命中率: {hit_ratio:.4f}")
print(f"加权误差: {error:.4f}")
return (error,)
# 目标产业集合 # 目标产业集合
def get_target_vulnerable_industries(): def get_target_vulnerable_industries():
""" """
@@ -156,6 +166,20 @@ def get_target_vulnerable_industries():
'27', '8', '11', '9', '21', '96', '99', '100', '44', '45', '46', '47', '48', '49', '50', '51', '27', '8', '11', '9', '21', '96', '99', '100', '44', '45', '46', '47', '48', '49', '50', '51',
'52', '53', '54', '55' '52', '53', '54', '55'
] ]
# 修改为25年的产业进行比较
merged_list_24 = [
'32', '33', '34', '35', '36', '37',
'38', '39', '41', '42', '43',
'46', '47', '48', '49',
'51', '52', '53', '54', '55',
'56', '57', '58',
'59', '60', '61', '62', '63', '64', '65', '66',
'68', '70', '71', '73', '74', '78', '79',
'90', '91', '92', '93', '94',
'95', '96', '97', '99', '100', '101',
'102', '103', '104', '105', '106', '107', '108', '109'
]
# # 提取所有 chain_id并去重 # # 提取所有 chain_id并去重
# chain_ids = set() # chain_ids = set()
# for item in industry_list: # for item in industry_list:
@@ -166,7 +190,7 @@ def get_target_vulnerable_industries():
# else: # else:
# chain_ids.add(str(item["chain_id"])) # chain_ids.add(str(item["chain_id"]))
return merged_list return merged_list_24
# 从数据库计算脆弱产业集合 # 从数据库计算脆弱产业集合
def get_vulnerable35_code(engine, ga_id): def get_vulnerable35_code(engine, ga_id):
@@ -221,7 +245,7 @@ def get_vulnerable35_code(engine, ga_id):
# =============================== # ===============================
# 5⃣ 选出最脆弱的前100个产品出现次数最多 # 5⃣ 选出最脆弱的前100个产品出现次数最多
# =============================== # ===============================
vulnerable100_product = count_prod.nlargest(35, "count")["id_product"].tolist() vulnerable100_product = count_prod.nlargest(64, "count")["id_product"].tolist()
print(f"[信息] ga_id={ga_id} 查询完成,共找到 {len(vulnerable100_product)} 个脆弱产品") print(f"[信息] ga_id={ga_id} 查询完成,共找到 {len(vulnerable100_product)} 个脆弱产品")
# =============================== # ===============================

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

View File

@@ -1,178 +1,216 @@
import csv
import json import json
import os import os
import random import random
from datetime import datetime from datetime import datetime
from deap import tools from deap import tools
from sqlalchemy.orm import close_all_sessions
from tqdm import tqdm from tqdm import tqdm
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from GA_Agent_0925.creating import creating from GA_Agent_0925.creating import creating
from GA_Agent_0925.orm import connection
from controller_db import ControllerDB from controller_db import ControllerDB
from evaluate_func import fitness, get_vulnerable35_code, get_target_vulnerable_industries from evaluate_func import fitness
# ==============================
# 遗传算法主函数(单进程) # 遗传算法主函数(单进程)
# ==============================
def main(): def main():
# 1⃣ 加载配置 # 1⃣ 加载配置
with open("config.json", "r", encoding="utf-8") as f: with open("config.json", "r", encoding="utf-8") as f:
cfg = json.load(f) cfg = json.load(f)
random.seed(cfg["seed"]) random.seed(cfg["seed"])
print("\n📘 参数配置:") print("\n📘 参数配置:")
for k, v in cfg.items(): for k, v in cfg.items():
print(f" {k}: {v}") print(f" {k}: {v}")
print("-" * 40) print("-" * 40)
# ==============================
# 2⃣ 初始化 ControllerDB数据库连接 # 2⃣ 初始化数据库控制器
# ==============================
controller_db_obj = ControllerDB("without_exp", reset_flag=0) controller_db_obj = ControllerDB("without_exp", reset_flag=0)
controller_db_obj.reset_db(force_drop=True) controller_db_obj.reset_db(force_drop=True)
# 准备样本表
controller_db_obj.prepare_list_sample() controller_db_obj.prepare_list_sample()
# 2⃣ 初始化工具箱
# ==============================
# 3⃣ 初始化 GA
# ==============================
toolbox = creating() toolbox = creating()
pop = toolbox.population(n=cfg["pop_size"]) pop = toolbox.population(n=cfg["pop_size"])
hof = tools.HallOfFame(1) hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values) stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", lambda fits: sum(f[0] for f in fits) / len(fits))
stats.register("max", lambda fits: max(f[0] for f in fits)) stats.register("max", lambda fits: max(f[0] for f in fits))
best_list = [] best_list = []
avg_list = []
# ============================================================ # ==============================
# 🔧 新增内容 1准备保存每代最优个体的文件 # 4⃣ 结果保存准备
# ============================================================ # ==============================
results_dir = "results" results_dir = "results"
os.makedirs(results_dir, exist_ok=True) os.makedirs(results_dir, exist_ok=True)
# 文件名
txt_result_file = os.path.join(results_dir, "best_individual_each_gen.txt") txt_result_file = os.path.join(results_dir, "best_individual_each_gen.txt")
json_result_file = os.path.join(results_dir, "best_result_with_industry.json") json_result_file = os.path.join(results_dir, "best_result_with_industry.json")
csv_file = "convergence0119_data.csv"
# 写入第一行:实验时间(年月日+小时) exp_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(txt_result_file, "w", encoding="utf-8") as f:
exp_time = datetime.now().strftime("%Y-%m-%d %H") # TXT 初始化
f.write(f"实验开始时间(年月日-小时):{exp_time}\n\n") with open(txt_result_file, "a", encoding="utf-8") as f:
f.write("以下为每一代的最优个体基因参数:\n") f.write(f"\n实验开始时间:{exp_time}\n")
f.write("每一代最优个体基因参数如下:\n")
# CSV 表头
write_header = not os.path.exists(csv_file)
if write_header:
with open(csv_file, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow([
"Timestamp",
"Generation",
"Best_Fitness_Percentage"
])
# ============================== # ==============================
# 主进化循环 # 5⃣ Fitness 缓存(关键加速)
# ==============================
fitness_cache = {}
# ==============================
# 6⃣ 主进化循环
# ============================== # ==============================
for gen in tqdm(range(cfg["n_gen"]), desc="进化中", ncols=90): for gen in tqdm(range(cfg["n_gen"]), desc="进化中", ncols=90):
# 计算未评估个体适应度
# ---------- 评估当前种群 ----------
invalid_ind = [ind for ind in pop if not ind.fitness.valid] invalid_ind = [ind for ind in pop if not ind.fitness.valid]
for ind in invalid_ind: for ind in invalid_ind:
controller_db_obj.reset_sample_db() gene_key = tuple(round(x, 4) for x in ind)
controller_db_obj.prepare_list_sample()
ind.fitness.values = fitness(ind, controller_db_obj=controller_db_obj)
# 选择、交叉、变异 if gene_key in fitness_cache:
ind.fitness.values = fitness_cache[gene_key]
else:
controller_db_obj.reset_sample_db()
controller_db_obj.prepare_list_sample()
fit_val = fitness(ind, controller_db_obj=controller_db_obj)
ind.fitness.values = fit_val
fitness_cache[gene_key] = fit_val
# ---------- 选择 ----------
offspring = toolbox.select(pop, len(pop)) offspring = toolbox.select(pop, len(pop))
offspring = list(map(toolbox.clone, offspring)) offspring = list(map(toolbox.clone, offspring))
# ---------- 交叉 ----------
for child1, child2 in zip(offspring[::2], offspring[1::2]): for child1, child2 in zip(offspring[::2], offspring[1::2]):
if random.random() < cfg["cx_prob"]: if random.random() < cfg["cx_prob"]:
toolbox.mate(child1, child2) toolbox.mate(child1, child2)
del child1.fitness.values, child2.fitness.values del child1.fitness.values, child2.fitness.values
# ---------- 变异 ----------
for mutant in offspring: for mutant in offspring:
if random.random() < cfg["mut_prob"]: if random.random() < cfg["mut_prob"]:
toolbox.mutate(mutant) toolbox.mutate(mutant)
del mutant.fitness.values del mutant.fitness.values
# ---------- 评估新个体 ----------
# 更新适应度
invalid_ind = [ind for ind in offspring if not ind.fitness.valid] invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
for ind in invalid_ind: for ind in invalid_ind:
controller_db_obj.reset_sample_db() gene_key = tuple(round(x, 4) for x in ind)
controller_db_obj.prepare_list_sample()
ind.fitness.values = fitness(ind, controller_db_obj=controller_db_obj) if gene_key in fitness_cache:
ind.fitness.values = fitness_cache[gene_key]
else:
controller_db_obj.reset_sample_db()
controller_db_obj.prepare_list_sample()
fit_val = fitness(ind, controller_db_obj=controller_db_obj)
ind.fitness.values = fit_val
fitness_cache[gene_key] = fit_val
pop[:] = offspring pop[:] = offspring
# ---------- 精英保留Elitism ----------
elite = hof[0] if len(hof) > 0 else None
if elite is not None:
# 替换当前种群中最差的个体(假设适应度越大越差)
worst_idx = max(range(len(pop)), key=lambda i: pop[i].fitness.values[0])
pop[worst_idx] = toolbox.clone(elite)
# 更新 hof
hof.update(pop) hof.update(pop)
record = stats.compile(pop) # ---------- 记录最优 ----------
best_list.append(record["max"])
avg_list.append(record["avg"])
# ============================================================
# 🔧 新增内容 2每代实时记录最优基因到文件
# ============================================================
best_ind = tools.selBest(pop, 1)[0] best_ind = tools.selBest(pop, 1)[0]
best_fitness = best_ind.fitness.values[0] # error越小越好
best_gene = list(map(float, best_ind)) best_gene = list(map(float, best_ind))
best_ga_id = getattr(best_ind, "ga_id", None) # 获取 ga_id如果没有就返回 None best_ga_id = getattr(best_ind, "ga_id", None)
# ---------- 维护“历史最优” ----------
if 'prev_best_fitness' not in globals():
prev_best_fitness = best_fitness
prev_best_gene = best_gene
prev_best_ga_id = best_ga_id
else:
if best_fitness < prev_best_fitness: # 更优(误差更小)
prev_best_fitness = best_fitness
prev_best_gene = best_gene
prev_best_ga_id = best_ga_id
# 否则:保持历史最优不变
# 写入 TXT 文件
with open(txt_result_file, "a", encoding="utf-8") as f: with open(txt_result_file, "a", encoding="utf-8") as f:
f.write( f.write(
(f"{gen + 1}最优基因:{best_gene} 最优适应度: {best_ind.fitness.values[0]:.4f}" f"{gen + 1} | 最优适应度: {prev_best_fitness:.4f} | 基因: {prev_best_gene}\n"
if best_gene else "N/A")
+ "\n"
) )
# ============================================================ # CSV
# 新增:删除上一轮产生的临时表 with open(csv_file, "a", newline="", encoding="utf-8") as f:
# ============================================================ writer = csv.writer(f)
writer.writerow([
exp_time,
gen,
best_fitness
])
# ---------- 定期清理 DB ----------
if gen % 3 == 0:
controller_db_obj.drop_table(
"without_exp_result",
keep_ga_id=prev_best_ga_id
)
# 保留当前代最优 ga_id
controller_db_obj.drop_table("without_exp_result", keep_ga_id=best_ga_id)
# 希望彻底删除整张表:
# controller_db_obj.drop_table("without_exp_result")
# ============================== # ==============================
# 输出最结果 # 7 输出最结果
# ============================== # ==============================
print("\n✅ 进化完成!") print("\n✅ 进化完成!")
print(f"🏆 最优个体: {hof[0]}") print(f"🏆 最优个体: {list(map(float, hof[0]))}")
print(f"🌟 最优适应度: {hof[0].fitness.values[0]:.4f}") print(f"🌟 最优适应度: {hof[0].fitness.values[0]:.4f}")
# 绘制收敛曲线 best_list.append(prev_best_fitness)
plt.figure(figsize=(12, 12)) # ==============================
plt.plot(best_list, label="Best Fitness", linewidth=2) # 8⃣ 绘制收敛曲线(仅 Best
plt.plot(avg_list, label="Average Fitness", linestyle="--") # ==============================
plt.title("Genetic Algorithm Convergence") plt.figure(figsize=(12, 8))
plt.plot(best_list, linewidth=2, label="Best Fitness")
plt.xlabel("Generation") plt.xlabel("Generation")
plt.ylabel("Fitness") plt.ylabel("Error")
plt.legend() plt.legend()
plt.grid(True, alpha=0.3) plt.grid(True, alpha=0.3)
plt.tight_layout() plt.tight_layout()
plt.savefig("convergence1.png", dpi=300) plt.savefig("convergence0119.png", dpi=300)
plt.show() plt.close()
# ============================== # ==============================
# 最优个体产业匹配 # 9⃣ 保存 JSON 结果
# ============================== # ==============================
print("\n📊 计算最优个体产业匹配情况...")
# ==============================
# 保存结果到文件
# ==============================
results_dir = "results"
os.makedirs(results_dir, exist_ok=True)
# 固定保存文件名
result_file = os.path.join(results_dir, "best_result_with_industry.json")
result_data = { result_data = {
"config": cfg, "config": cfg,
"best_individual": list(map(float, hof[0])), "best_individual": list(map(float, hof[0])),
"best_fitness": float(hof[0].fitness.values[0]), "best_fitness": float(hof[0].fitness.values[0]),
"fitness_curve": { "fitness_curve": {
"best_list": best_list, "best_list": best_list
"avg_list": avg_list
}, },
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S") "timestamp": exp_time
} }
with open(result_file, "w", encoding="utf-8") as f: with open(json_result_file, "w", encoding="utf-8") as f:
json.dump(result_data, f, indent=4, ensure_ascii=False) json.dump(result_data, f, indent=4, ensure_ascii=False)
print(f"\n💾 最优结果已保存至: {result_file}") print(f"\n💾 最优结果已保存至: {json_result_file}")
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@@ -1,12 +0,0 @@
实验开始时间(年月日-小时2025-11-22 14
以下为每一代的最优个体基因参数:
第 1 代最优基因:[51.0, 0.4550083673391433, 0.4230074859901629, 1.0, 24.0, 0.9607789032744504, 3.0, 0.4271578472186195, 0.5342606663518962, 0.06796086337575724, 4.322914089653634, 1.2286610916299323] 最优适应度: -31.0000
第 2 代最优基因:[51.0, 0.4550083673391433, 0.4230074859901629, 1.0, 24.0, 0.9607789032744504, 3.0, 0.4271578472186195, 0.5342606663518962, 0.06796086337575724, 4.322914089653634, 1.2286610916299323] 最优适应度: -31.0000
第 3 代最优基因:[51.0, 0.4550083673391433, 0.4230074859901629, 1.0, 24.0, 0.9607789032744504, 3.0, 0.4271578472186195, 0.5342606663518962, 0.06796086337575724, 4.322914089653634, 1.2286610916299323] 最优适应度: -31.0000
第 4 代最优基因:[51.0, 0.4550083673391433, 0.4230074859901629, 1.0, 24.0, 0.9607789032744504, 3.0, 0.4271578472186195, 0.5342606663518962, 0.06796086337575724, 4.322914089653634, 1.2286610916299323] 最优适应度: -31.0000
第 5 代最优基因:[51.0, 0.4550083673391433, 0.4230074859901629, 1.0, 24.0, 0.9607789032744504, 3.0, 0.4271578472186195, 0.5342606663518962, 0.06796086337575724, 4.322914089653634, 1.2286610916299323] 最优适应度: -31.0000
第 6 代最优基因:[51.0, 0.4550083673391433, 0.4230074859901629, 1.0, 24.0, 0.9607789032744504, 3.0, 0.4271578472186195, 0.5342606663518962, 0.06796086337575724, 4.322914089653634, 1.2286610916299323] 最优适应度: -31.0000
第 7 代最优基因:[51.0, 0.4550083673391433, 1.2286610916299323, 1.0, 0.06796086337575724, 0.9607789032744504, 3.0, 0.4271578472186195, 0.5342606663518962, 24.0, 4.322914089653634, 0.4230074859901629] 最优适应度: -31.0000
第 8 代最优基因:[51.0, 0.4550083673391433, 0.4230074859901629, 1.0, 24.0, 0.9607789032744504, 3.0, 0.4271578472186195, 0.5342606663518962, 0.06796086337575724, 4.322914089653634, 1.2286610916299323] 最优适应度: -31.0000
第 9 代最优基因:[51.0, 0.4550083673391433, 0.4230074859901629, 1.0, 24.0, 0.9607789032744504, 3.0, 0.4271578472186195, 0.5342606663518962, 0.06796086337575724, 4.322914089653634, 1.2286610916299323] 最优适应度: -31.0000
第 10 代最优基因:[51.0, 0.4550083673391433, 0.4230074859901629, 1.0, 24.0, 0.9607789032744504, 3.0, 0.4271578472186195, 0.5342606663518962, 0.06796086337575724, 4.322914089653634, 1.2286610916299323] 最优适应度: -31.0000

View File

@@ -1,11 +1,11 @@
{ {
"config": { "config": {
"pop_size": 20, "pop_size": 10,
"n_gen": 10, "n_gen": 60,
"n_var": 12, "n_var": 12,
"bound_min": -5, "bound_min": -5,
"bound_max": 5, "bound_max": 5,
"cx_prob": 0.5, "cx_prob": 0.8,
"mut_prob": 0.2, "mut_prob": 0.2,
"cx_alpha": 0.5, "cx_alpha": 0.5,
"mut_sigma": 0.1, "mut_sigma": 0.1,
@@ -15,45 +15,22 @@
"seed": 42 "seed": 42
}, },
"best_individual": [ "best_individual": [
51.0, 64.0,
0.4550083673391433, 1.764206087513454,
0.4230074859901629, 0.07889021951363398,
1.0, 0.6743314165751808,
24.0, 1.0079808674057342,
0.9607789032744504, 0.29096678837813245,
3.0, 0.3695976434649408,
0.4271578472186195, 0.2561432647676939,
0.5342606663518962, 6.0,
0.06796086337575724, 35.0,
4.322914089653634, 1.0079808674057342,
1.2286610916299323 1.0142063414994353
], ],
"best_fitness": -31.0, "best_fitness": 0.1761567188324269,
"fitness_curve": { "fitness_curve": {
"best_list": [ "best_list": []
-31.0,
-31.0,
-31.0,
-31.0,
-31.0,
-31.0,
-31.0,
-31.0,
-31.0,
-31.0
],
"avg_list": [
-32.9,
-32.8,
-32.5,
-32.5,
-32.3,
-32.1,
-32.0,
-32.3,
-32.6,
-32.5
]
}, },
"timestamp": "2025-11-22 19:05:08" "timestamp": "2026-01-26 22:30:22"
} }

View File

@@ -11,9 +11,16 @@ sql_file = "query_top35_products.sql"
# 指定对比列表 # 指定对比列表
specified_list = [ specified_list = [
'61','68','65','66','59','71','77','7','38','95','58','90','56','57','97','98', '32', '33', '34', '35', '36', '37',
'27','8','11','9','21','96','99','100','44','45','46','47','48','49','50','51', '38', '39', '41', '42', '43',
'52','53','54','55' '46', '47', '48', '49',
'51', '52', '53', '54', '55',
'56', '57', '58',
'59', '60', '61', '62', '63', '64', '65', '66',
'68', '70', '71', '73', '74', '78', '79',
'90', '91', '92', '93', '94',
'95', '96', '97', '99', '100', '101',
'102', '103', '104', '105', '106', '107', '108', '109'
] ]
specified_set = set(specified_list) specified_set = set(specified_list)
@@ -35,8 +42,8 @@ print(f"[信息] 查询完成,获取到 {len(df)} 条记录。")
all_results = [] all_results = []
for ga_id, group in df.groupby("ga_id"): for ga_id, group in df.groupby("ga_id"):
vulnerable35_product = group['id_product'].astype(str).tolist() vulnerable_product = group['id_product'].astype(str).tolist()
vulnerable_set = set(vulnerable35_product) vulnerable_set = set(vulnerable_product)
matching = vulnerable_set & specified_set matching = vulnerable_set & specified_set
extra = vulnerable_set - specified_set extra = vulnerable_set - specified_set
@@ -44,7 +51,7 @@ for ga_id, group in df.groupby("ga_id"):
all_results.append({ all_results.append({
"ga_id": ga_id, "ga_id": ga_id,
"vulnerable35": ",".join(vulnerable35_product), "vulnerable": ",".join(vulnerable_product),
"matching_count": len(matching), "matching_count": len(matching),
"matching_products": ",".join(matching), "matching_products": ",".join(matching),
"extra_count": len(extra), "extra_count": len(extra),
@@ -57,5 +64,5 @@ for ga_id, group in df.groupby("ga_id"):
# 输出 CSV # 输出 CSV
# =============================== # ===============================
df_results = pd.DataFrame(all_results) df_results = pd.DataFrame(all_results)
df_results.to_csv("vulnerable20_match_results_new1122.csv", index=False, encoding='utf-8-sig') df_results.to_csv("vulnerable63_match_results_new260111.csv", index=False, encoding='utf-8-sig')
print("[完成] 已生成 vulnerable35_match_results.csv") print("[完成] 已生成 vulnerable35_match_results.csv")

View File

@@ -22,7 +22,7 @@ product_count AS (
), ),
ranked_products AS ( ranked_products AS (
-- 按每个 ga_id 的产品出现次数排序,并取前35 -- 按每个 ga_id 的产品出现次数排序,并取前 x
SELECT *, SELECT *,
ROW_NUMBER() OVER (PARTITION BY ga_id ORDER BY cnt DESC) AS rn ROW_NUMBER() OVER (PARTITION BY ga_id ORDER BY cnt DESC) AS rn
FROM product_count FROM product_count

View File

@@ -0,0 +1,2 @@
ga_id,vulnerable35,matching_count,matching_products,extra_count,extra_products,missing_count,missing_products
1,"95,99,90,91,51,53,93,52,54,55,92,50,94,9,48,45,47,46,49,44",16,"55,49,52,95,51,53,93,90,47,54,48,99,91,94,46,92",4,"9,45,44,50",41,"109,65,66,56,34,100,74,32,42,37,35,61,64,38,68,107,73,108,57,97,59,36,103,58,60,63,96,101,71,39,62,104,105,106,70,78,79,102,33,41,43"
1 ga_id vulnerable35 matching_count matching_products extra_count extra_products missing_count missing_products
2 1 95,99,90,91,51,53,93,52,54,55,92,50,94,9,48,45,47,46,49,44 16 55,49,52,95,51,53,93,90,47,54,48,99,91,94,46,92 4 9,45,44,50 41 109,65,66,56,34,100,74,32,42,37,35,61,64,38,68,107,73,108,57,97,59,36,103,58,60,63,96,101,71,39,62,104,105,106,70,78,79,102,33,41,43

View File

@@ -0,0 +1,2 @@
ga_id,vulnerable,matching_count,matching_products,extra_count,extra_products,missing_count,missing_products
1,"95,99,90,91,51,53,93,52,54,55,92,50,94,9,48,45,47,46,49,44",16,"49,51,46,92,95,99,53,94,52,91,48,93,55,54,90,47",4,"50,44,45,9",41,"109,105,108,103,63,73,42,96,70,74,43,61,66,32,79,58,60,104,97,64,57,33,102,59,106,100,68,35,101,34,107,39,56,36,62,41,65,37,38,78,71"
1 ga_id vulnerable matching_count matching_products extra_count extra_products missing_count missing_products
2 1 95,99,90,91,51,53,93,52,54,55,92,50,94,9,48,45,47,46,49,44 16 49,51,46,92,95,99,53,94,52,91,48,93,55,54,90,47 4 50,44,45,9 41 109,105,108,103,63,73,42,96,70,74,43,61,66,32,79,58,60,104,97,64,57,33,102,59,106,100,68,35,101,34,107,39,56,36,62,41,65,37,38,78,71

58
GA_Agent_0925/绘图.py Normal file
View File

@@ -0,0 +1,58 @@
import pandas as pd
import matplotlib.pyplot as plt
# # ==============================
# # 1⃣ 读取 CSV
# # ==============================
# csv_file = "convergence0119_data.csv"
# df = pd.read_csv(csv_file)
#
# # ==============================
# # 2⃣ 选择一次完整 GA 运行
# # ==============================
# target_ts = "2026-01-26 22:30:22"
# run_df = df[df["Timestamp"] == target_ts].copy()
#
# # 按代数排序,取前 50 代
# run_df = run_df.sort_values("Generation").head(100)
# 读取 CSV
csv_file = "convergence0119_data.csv"
df = pd.read_csv(csv_file)
# 按 Generation 排序
run_df = df.sort_values("Generation")
# ==============================
# 3⃣ 提取数据
# ==============================
generations = run_df["Generation"].values
best_fitness = run_df["Best_Fitness_Percentage"].values
# ==============================
# 4⃣ 计算滑动平均(窗口=5可调
# ==============================
window_size = 5
run_df["MA"] = run_df["Best_Fitness_Percentage"].rolling(
window=window_size,
min_periods=1
).mean()
# ==============================
# 5⃣ 绘图:点 + 连线 + 滑动平均
# ==============================
plt.figure(figsize=(10, 6))
# 原始点
plt.scatter(generations, best_fitness, s=30, alpha=0.6, label="Best Error")
# 连线
plt.plot(generations, best_fitness, linewidth=1, alpha=0.6)
plt.xlabel("Generation")
plt.ylabel("Best Error")
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig("ga_convergence_me_.png", dpi=300)
plt.show()

View File

@@ -1 +1 @@
db_name_prefix: with_exp db_name_prefix: without_exp

View File

@@ -8,5 +8,5 @@ test: # only for test scenarios
n_iter: 100 n_iter: 100
not_test: # normal scenarios not_test: # normal scenarios
n_sample: 10 n_sample: 1
n_iter: 50 n_iter: 50

View File

@@ -61,7 +61,7 @@ class MyModel(Model):
self.cap_limit_prob_type = params['cap_limit_prob_type'] # 产能限制的概率分布类型。 self.cap_limit_prob_type = params['cap_limit_prob_type'] # 产能限制的概率分布类型。
self.cap_limit_level = params['cap_limit_level'] # 产能限制的水平。 self.cap_limit_level = params['cap_limit_level'] # 产能限制的水平。
self.diff_new_conn = params['diff_new_conn'] # 是否允许差异化的新连接。 self.diff_new_conn = params['diff_new_conn'] # 是否允许差异化的新连接。
# 初始化停止时间步,可能是用户通过参数传入 # 初始化停止时间步
self.int_stop_ts = params.get('n_iter', 100) # 默认停止时间为 100 self.int_stop_ts = params.get('n_iter', 100) # 默认停止时间为 100
# 网络初始化 # 网络初始化