遗传算法
This commit is contained in:
51
.idea/csv-editor.xml
generated
51
.idea/csv-editor.xml
generated
@@ -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>
|
||||||
|
|||||||
2
.idea/dataSources.local.xml
generated
2
.idea/dataSources.local.xml
generated
@@ -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>
|
||||||
|
|||||||
@@ -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
1
.idea/sqldialects.xml
generated
@@ -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" />
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -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 "
|
||||||
|
|||||||
BIN
GA_Agent_0925/convergence0119.png
Normal file
BIN
GA_Agent_0925/convergence0119.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 76 KiB |
64
GA_Agent_0925/convergence0119_data.csv
Normal file
64
GA_Agent_0925/convergence0119_data.csv
Normal 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
|
||||||
|
BIN
GA_Agent_0925/convergence1.png
Normal file
BIN
GA_Agent_0925/convergence1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 245 KiB |
198
GA_Agent_0925/count_firm.csv
Normal file
198
GA_Agent_0925/count_firm.csv
Normal 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
|
||||||
|
259
GA_Agent_0925/count_firm_prod.csv
Normal file
259
GA_Agent_0925/count_firm_prod.csv
Normal 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
|
||||||
|
73
GA_Agent_0925/count_prod.csv
Normal file
73
GA_Agent_0925/count_prod.csv
Normal 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
|
||||||
|
@@ -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(
|
||||||
|
|||||||
@@ -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)} 个脆弱产品")
|
||||||
|
|
||||||
# ===============================
|
# ===============================
|
||||||
|
|||||||
BIN
GA_Agent_0925/ga_convergence_ma_.png
Normal file
BIN
GA_Agent_0925/ga_convergence_ma_.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 116 KiB |
@@ -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:
|
||||||
|
gene_key = tuple(round(x, 4) for x in ind)
|
||||||
|
|
||||||
|
if gene_key in fitness_cache:
|
||||||
|
ind.fitness.values = fitness_cache[gene_key]
|
||||||
|
else:
|
||||||
controller_db_obj.reset_sample_db()
|
controller_db_obj.reset_sample_db()
|
||||||
controller_db_obj.prepare_list_sample()
|
controller_db_obj.prepare_list_sample()
|
||||||
ind.fitness.values = fitness(ind, controller_db_obj=controller_db_obj)
|
|
||||||
|
|
||||||
# 选择、交叉、变异
|
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:
|
||||||
|
gene_key = tuple(round(x, 4) for x in ind)
|
||||||
|
|
||||||
|
if gene_key in fitness_cache:
|
||||||
|
ind.fitness.values = fitness_cache[gene_key]
|
||||||
|
else:
|
||||||
controller_db_obj.reset_sample_db()
|
controller_db_obj.reset_sample_db()
|
||||||
controller_db_obj.prepare_list_sample()
|
controller_db_obj.prepare_list_sample()
|
||||||
ind.fitness.values = fitness(ind, controller_db_obj=controller_db_obj)
|
|
||||||
|
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()
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -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"
|
||||||
}
|
}
|
||||||
@@ -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")
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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"
|
||||||
|
@@ -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"
|
||||||
|
58
GA_Agent_0925/绘图.py
Normal file
58
GA_Agent_0925/绘图.py
Normal 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()
|
||||||
@@ -1 +1 @@
|
|||||||
db_name_prefix: with_exp
|
db_name_prefix: without_exp
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
# 网络初始化
|
# 网络初始化
|
||||||
|
|||||||
Reference in New Issue
Block a user