Swarm Intelligence ใน Python
(อัลกอริธึมทางพันธุกรรม, การเพิ่มประสิทธิภาพฝูงอนุภาค, การหลอมแบบจำลอง, อัลกอริธึมฝูงมด, อัลกอริธึมภูมิคุ้มกัน, อัลกอริธึมฝูงปลาประดิษฐ์ใน Python)
เอกสารประกอบ: https://scikit-opt.github.io/scikit-opt/#/en/
หัวข้อ: https://scikit-opt.github.io/scikit-opt/#/zh/
ซอร์สโค้ด: https://github.com/guofei9987/scikit-opt
ช่วยเราปรับปรุง scikit-opt https://www.wjx.cn/jq/50964691.aspx
pip ติดตั้ง scikit-opt
สำหรับเวอร์ชันนักพัฒนาปัจจุบัน:
git clone [email protected]:guofei9987/scikit-opt.git
cd scikit-opt
pip install .
UDF (ฟังก์ชั่นที่ผู้ใช้กำหนด) วางจำหน่ายแล้ว!
ตัวอย่างเช่น คุณเพิ่งสร้างฟังก์ชัน selection
รูปแบบใหม่
ตอนนี้ฟังก์ชัน selection
ของคุณจะเป็นดังนี้:
-> รหัสสาธิต: examples/demo_ga_udf.py#s1
# ขั้นตอนที่ 1: กำหนดโอเปอเรเตอร์ของคุณเอง:def Select_tournament(algorithm, tourn_size):FitV = Algorithm.FitVsel_index = []for i in range(algorithm.size_pop):aspirants_index = np.random.choice(range(algorithm.size_pop), size =tourn_size)sel_index.append(max(aspirants_index, key=lambda i: FitV[i]))algorithm.Chrom = Algorithm.Chrom[sel_index, :] # อัลกอริธึมการส่งคืนรุ่นถัดไป Chrom
นำเข้าและสร้าง ga
-> รหัสสาธิต: examples/demo_ga_udf.py#s2
นำเข้าจำนวนมากเป็น npfrom sko.GA นำเข้า GA, GA_TSPdemo_func = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + (x[2] - 0.5) ** 2ga = GA(func =demo_func, n_dim=3, size_pop=100, max_iter=500, prob_mut=0.001,lb=[-1, -10, -5], ub=[2, 10, 2], ความแม่นยำ=[1e-7, 1e-7, 1])
ลงทะเบียน udf ของคุณไปที่ GA
-> รหัสสาธิต: examples/demo_ga_udf.py#s3
ga.register(operator_name='selection', โอเปอเรเตอร์=selection_tournament, tourn_size=3)
scikit-opt ยังมีตัวดำเนินการบางตัวด้วย
-> รหัสสาธิต: examples/demo_ga_udf.py#s4
จากการจัดอันดับการนำเข้า sko.operators, การเลือก, ครอสโอเวอร์,mututga.register(operator_name='ranking',operator=ranking.ranking) ลงทะเบียน(operator_name='crossover', โอเปอเรเตอร์=crossover.crossover_2point) ลงทะเบียน (operator_name='mutation',operator=mutation.mutation)
ตอนนี้ทำ GA ตามปกติแล้ว
-> รหัสสาธิต: examples/demo_ga_udf.py#s5
best_x, best_y = ga.run()พิมพ์('best_x:', best_x, 'n', 'best_y:', best_y)
จนถึงขณะนี้ udf surport
crossover
,mutation
,selection
,ranking
ของ GA scikit-opt มีตัวดำเนินการหลายสิบราย ดูที่นี่
สำหรับผู้ใช้ขั้นสูง:
-> รหัสสาธิต: examples/demo_ga_udf.py#s6
คลาส MyGA(GA):การเลือก def(ตัวเอง, tourn_size=3):FitV = self.FitVsel_index = []สำหรับฉันในช่วง (self.size_pop):aspirants_index = np.random.choice(ช่วง(self.size_pop), ขนาด =tourn_size)sel_index.append(max(aspirants_index, key=lambda i: FitV[i]))self.Chrom = self.Chrom[sel_index, :] # รุ่นต่อไปกลับมา self.Chromranking = Ranking.rankingdemo_func = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + (x[2] - 0.5) ** 2my_ga = MyGA(func=demo_func, n_dim=3, size_pop=100, max_iter=500, ปอนด์=[-1, -10, -5], ub=[2, 10, 2], ความแม่นยำ=[1e-7, 1e-7, 1])best_x, best_y = my_ga.run()พิมพ์('best_x :', best_x, 'n', 'best_y:', best_y)
(ใหม่ในเวอร์ชัน 0.3.6)
เรียกใช้อัลกอริทึมสำหรับการวนซ้ำ 10 ครั้ง จากนั้นเรียกใช้การวนซ้ำอีก 20 ครั้งตามการวนซ้ำ 10 ครั้งก่อนหน้า:
จาก sko.GA นำเข้า GAfunc = lambda x: x[0] ** 2ga = GA(func=func, n_dim=1)ga.run(10)ga.run(20)
การทำเวกเตอร์
มัลติเธรด
การประมวลผลหลายตัว
แคชไว้
ดูhttps://github.com/guofei9987/scikit-opt/blob/master/examples/example_function_modes.py
เรากำลังพัฒนาการคำนวณ GPU ซึ่งจะเสถียรในเวอร์ชัน 1.0.0
ตัวอย่างมีอยู่แล้ว: https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_gpu.py
ขั้นตอนที่ 1 :กำหนดปัญหาของคุณ
-> รหัสสาธิต: examples/demo_de.py#s1
'''ต่ำสุด f(x1, x2, x3) = x1^2 + x2^2 + x3^2s.t. x1*x2 >= 1 x1*x2 <= 5 x2 + x3 = 1 0 <= x1, x2, x3 <= 5'''def obj_func(p):x1, x2, x3 = ล่วงหน้า x1 ** 2 + x2 ** 2 + x3 ** 2constraint_eq = [แลมบ์ดา x: 1 - x[1] - x[2] ]constraint_ueq = [แลมบ์ดา x: 1 - x[0] * x[1],แลมบ์ดา x: x[0] * x[1] - 5]
ขั้นตอนที่ 2 : ทำวิวัฒนาการที่แตกต่าง
-> รหัสสาธิต: examples/demo_de.py#s2
จาก sko.DE นำเข้า DEde = DE(func=obj_func, n_dim=3, size_pop=50, max_iter=800, lb=[0, 0, 0], ub=[5, 5, 5],constraint_eq=constraint_eq, constraint_ueq =ข้อจำกัด_ueq)best_x, best_y = de.run()พิมพ์('best_x:', best_x, 'n', 'best_y:', best_y)
ขั้นตอนที่ 1 :กำหนดปัญหาของคุณ
-> รหัสสาธิต: examples/demo_ga.py#s1
นำเข้าตัวเลขเป็น npdef schaffer (p): '' '' ฟังก์ชั่นนี้มีขั้นต่ำในท้องถิ่นมากมายโดยมีค่าต่ำสุดทั่วโลกที่แรงกระแทกอย่างรุนแรงที่ (0,0) ด้วยค่า 0 https://en.wikipedia.org/wiki/Test_functions_for_optimization ''' x1, x2 = ppart1 = np.square(x1) - np.square(x2)part2 = np.square(x1) + np.square(x2)ส่งคืน 0.5 + (np.square(np.sin(part1)) - 0.5) / np.square(1 + 0.001 * ส่วนที่ 2)
ขั้นตอนที่ 2 : ทำขั้นตอนวิธีทางพันธุกรรม
-> รหัสสาธิต: examples/demo_ga.py#s2
จาก sko.GA นำเข้า GAga = GA(func=schaffer, n_dim=2, size_pop=50, max_iter=800, prob_mut=0.001, lb=[-1, -1], ub=[1, 1], ความแม่นยำ=1e -7)best_x, best_y = ga.run()พิมพ์('best_x:', best_x, 'n', 'best_y:', best_y)
-> รหัสสาธิต: examples/demo_ga.py#s3
นำเข้าแพนด้าเป็น pdimport matplotlib.pyplot เป็น pltY_history = pd.DataFrame(ga.all_history_Y)fig, ax = plt.subplots(2, 1)ax[0].plot(Y_history.index, Y_history.values, '.', color='red')Y_history.min(axis=1).cummin().plot(kind='line')plt.show()
เพียงนำเข้า GA_TSP
มันจะโอเวอร์โหลด crossover
โอเวอร์ mutation
เพื่อแก้ไข TSP
ขั้นตอนที่ 1 : กำหนดปัญหาของคุณ เตรียมพิกัดคะแนนและเมทริกซ์ระยะทางของคุณ
ที่นี่ฉันสร้างข้อมูลแบบสุ่มเป็นการสาธิต:
-> รหัสสาธิต: examples/demo_ga_tsp.py#s1
นำเข้า numpy เป็น npfrom scipy นำเข้า spatialimport matplotlib.pyplot เป็น pltnum_points = 50points_coordinate = np.random.rand(num_points, 2) # สร้างพิกัดของ pointdistance_matrix = spatial.distance.cdist (points_coordinate, point_coordinate, metric='euclidean')def cal_total_distance(ประจำ):''''ฟังก์ชันวัตถุประสงค์ รูทีนอินพุตส่งคืนระยะทางทั้งหมด cal_total_distance(np.arange(num_points)) '''num_points, = Routine.shapereturn sum([distance_matrix[routine[i % num_points], Routine[(i + 1) % num_points]] สำหรับฉันในช่วง (num_points)])
ขั้นตอนที่ 2 : ทำ GA
-> รหัสสาธิต: examples/demo_ga_tsp.py#s2
จาก sko.GA นำเข้า GA_TSPga_tsp = GA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=50, max_iter=500, prob_mut=1)best_points, best_distance = ga_tsp.run()
ขั้นตอนที่ 3 : พล็อตผลลัพธ์:
-> รหัสสาธิต: examples/demo_ga_tsp.py#s3
รูปที่ ax = plt.subplots(1, 2)best_points_ = np.concatenate([best_points, [best_points[0]]])best_points_coordinate = point_coordinate[best_points_, :]ax[0].plot(best_points_coordinate[:, 0] , best_points_coordinate[:, 1], 'หรือ')ขวาน[1].พล็อต(ga_tsp.รุ่น_best_Y)plt.show()
ขั้นตอนที่ 1 : กำหนดปัญหาของคุณ:
-> รหัสสาธิต: examples/demo_pso.py#s1
def demo_func(x):x1, x2, x3 = xreturn x1 ** 2 + (x2 - 0.05) ** 2 + x3 ** 2
ขั้นตอนที่ 2 : ทำ PSO
-> รหัสสาธิต: examples/demo_pso.py#s2
จาก sko.PSO นำเข้า PSOpso = PSO(func=demo_func, n_dim=3, pop=40, max_iter=150, lb=[0, -1, 0.5], ub=[1, 1, 1], w=0.8, c1=0.5, c2=0.5)pso.run()พิมพ์('best_x คือ ', pso.gbest_x, 'best_y คือ', pso.gbest_y)
ขั้นตอนที่ 3 : พล็อตผลลัพธ์
-> รหัสสาธิต: examples/demo_pso.py#s3
นำเข้า matplotlib.pyplot เป็น pltplt.plot(pso.gbest_y_hist)plt.show()
หากคุณต้องการข้อจำกัดเชิงเส้น เช่น (x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2<=0
รหัสมีลักษณะดังนี้:
constraint_ueq = (แลมบ์ดา x: (x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2, )pso = PSO(func=demo_func, n_dim=2, pop=40, max_iter=max_iter, lb=[-2, -2], ub=[2, 2] , constraint_ueq=ข้อจำกัด_ueq)
โปรดทราบว่าคุณสามารถเพิ่มข้อจำกัดแบบไม่เชิงเส้นได้มากกว่าหนึ่งรายการ เพียงเพิ่มลงใน constraint_ueq
ยิ่งไปกว่านั้น เรามีแอนิเมชัน:
↑ ดูตัวอย่าง/demo_pso_ani.py
ขั้นตอนที่ 1 : กำหนดปัญหาของคุณ
-> รหัสสาธิต: examples/demo_sa.py#s1
demo_func = แลมบ์ดา x: x[0] ** 2 + (x[1] - 0.05) ** 2 + x[2] ** 2
ขั้นตอนที่ 2 : ทำ SA
-> รหัสสาธิต: examples/demo_sa.py#s2
จาก sko.SA นำเข้า SAsa = SA(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, L=300, max_stay_counter=150)best_x, best_y = sa.run() พิมพ์ ('best_x:', best_x, 'best_y', best_y)
ขั้นตอนที่ 3 : พล็อตผลลัพธ์
-> รหัสสาธิต: examples/demo_sa.py#s3
นำเข้า matplotlib.pyplot เป็น pltimport pandas เป็น pdplt.plot(pd.DataFrame(sa.best_y_history).cummin(axis=0))plt.show()
นอกจากนี้ scikit-opt ยังมี Simulated Annealing ให้เลือก 3 ประเภท: Fast, Boltzmann, Cauchy ดูเพิ่มเติม
ขั้นตอนที่ 1 : โอ้ ใช่ กำหนดปัญหาของคุณ น่าเบื่อที่จะคัดลอกขั้นตอนนี้
ขั้นตอนที่ 2 : DO SA สำหรับ TSP
-> รหัสสาธิต: examples/demo_sa_tsp.py#s2
จาก sko.SA นำเข้า SA_TSPsa_tsp = SA_TSP(func=cal_total_distance, x0=range(num_points), T_max=100, T_min=1, L=10 * num_points)best_points, best_distance = sa_tsp.run()print(best_points, best_distance, cal_total_distance (ดีที่สุด_คะแนน))
ขั้นตอนที่ 3 : พล็อตผลลัพธ์
-> รหัสสาธิต: examples/demo_sa_tsp.py#s3
จาก matplotlib.ticker นำเข้า FormatStrFormatterfig, ax = plt.subplots(1, 2)best_points_ = np.concatenate([best_points, [best_points[0]]])best_points_coordinate = point_coordinate[best_points_, :]ax[0].plot(sa_tsp.best_y_history)ax[0].set_xlabel("การวนซ้ำ")ax[0].set_ylabel("ระยะทาง")ax[1].plot(best_points_coordinate[:, 0], best_points_coordinate [:, 1], marker='o', markerfacecolor='b', color='c', linestyle='-')ขวาน[1].xaxis.set_major_formatter(FormatStrFormatter('%.3f'))ax[1].yaxis.set_major_formatter(FormatStrFormatter('%.3f'))ax[1].set_xlabel(" ลองจิจูด")ax[1].set_ylabel("Latitude")plt.show()
เพิ่มเติม: พล็อตแอนิเมชั่น:
↑ ดูตัวอย่าง/demo_sa_tsp.py
-> รหัสสาธิต: examples/demo_aca_tsp.py#s2
จาก sko.ACA นำเข้า ACA_TSPaca = ACA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=50, max_iter=200, Distance_matrix=distance_matrix)best_x, best_y = aca.run()
-> รหัสสาธิต: examples/demo_ia.py#s2
จาก sko.IA นำเข้า IA_TSPia_tsp = IA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=500, max_iter=800, prob_mut=0.2,T=0.7, alpha=0.95)best_points, best_distance = ia_tsp.run()print('best กิจวัตรประจำวัน:', best_points, 'best_distance:', best_distance)
-> รหัสสาธิต: examples/demo_afsa.py#s1
def func(x):x1, x2 = xreturn 1 / x1 ** 2 + x1 ** 2 + 1 / x2 ** 2 + x2 ** 2จาก sko.AFSA นำเข้า AFSAafsa = AFSA(func, n_dim=2, size_pop= 50, max_iter=300,max_try_num=100, ขั้นตอน=0.5, ภาพ=0.3,q=0.98, เดลต้า=0.5)best_x, best_y = afsa.run()พิมพ์(best_x, best_y)
หยู, เจ., เขา, วาย., หยาน, คิว., และคัง, เอ็กซ์. (2021) SpecView: กรอบงานการแสดงสเปกตรัมมัลแวร์พร้อมการแปลงสเปกตรัมแบบเอกพจน์ ธุรกรรม IEEE เกี่ยวกับนิติวิทยาศาสตร์และความปลอดภัยข้อมูล, 16, 5093-5107
Zhen, H., Zhai, H., Ma, W., Zhao, L., Weng, Y., Xu, Y., ... & He, X. (2021) การออกแบบและการทดสอบเครื่องกำเนิดโซลูชันการไหลของกำลังที่เหมาะสมโดยใช้การเรียนรู้เสริมแรง รายงานพลังงาน
Heinrich, K., Zschech, P., Janiesch, C., & Bonin, M. (2021) คุณสมบัติของข้อมูลประมวลผลมีความสำคัญ: ขอแนะนำเครือข่ายประสาทเทียมแบบมีรั้วรอบขอบชิด (GCNN) และเครือข่ายความสนใจที่คาดการณ์คีย์-ค่า-ทำนาย (KVP) สำหรับการทำนายเหตุการณ์ถัดไปด้วยการเรียนรู้เชิงลึก ระบบสนับสนุนการตัดสินใจ, 143, 113494.
Tang, HK และ Goh, SK (2021) Meta-heuristic Optimizer ที่ไม่ใช่ประชากรซึ่งได้รับแรงบันดาลใจจากปรัชญาของ Yi Jing arXiv พิมพ์ล่วงหน้า arXiv:2104.08564
Wu, G., Li, L., Li, X., Chen, Y., Chen, Z., Qiao, B., ... & Xia, L. (2021) การฝังกราฟการจับคู่กิจกรรมโซเชียลแบบเรียลไทม์สำหรับคำแนะนำ EBSN เวิลด์ไวด์เว็บ, 1-22.
Pan, X., Zhang, Z., Zhang, H., Wen, Z., Ye, W., Yang, Y., ... & Zhao, X. (2021) อัลกอริธึมการตรวจจับก๊าซผสมและความเข้มข้นที่รวดเร็วและมีประสิทธิภาพโดยอาศัยกลไกความสนใจที่ติดตั้งโครงข่ายประสาทเทียมที่เกิดซ้ำพร้อมฟังก์ชันการสูญเสียสองเท่า เซ็นเซอร์และแอคชูเอเตอร์ B: เคมี, 342, 129982
คาสเตลลา บัลเซลล์, ม. (2021) การเพิ่มประสิทธิภาพของระบบรักษาสถานีสำหรับกังหันลมนอกชายฝั่งแบบลอยน้ำ WindCrete
Zhai, B., Wang, Y., Wang, W., & Wu, B. (2021) กลยุทธ์การควบคุมการจำกัดความเร็วแบบแปรผันที่เหมาะสมที่สุดบนส่วนทางด่วนภายใต้สภาพที่มีหมอกหนา arXiv พิมพ์ล่วงหน้า arXiv:2107.14406
แยป เอ็กซ์เอช (2021) การจำแนกประเภทหลายฉลากสำหรับข้อมูลเชิงเส้นเฉพาะที่: การประยุกต์กับการทำนายความเป็นพิษของสารเคมี
เกบฮาร์ด, แอล. (2021). การวางแผนการขยายโครงข่ายไฟฟ้าแรงต่ำโดยใช้การเพิ่มประสิทธิภาพอาณานิคมของมด
หม่า เอ็กซ์ โจว เอช และหลี่ ซ. (2021) การออกแบบที่เหมาะสมที่สุดสำหรับการพึ่งพาอาศัยกันระหว่างไฮโดรเจนและระบบไฟฟ้า ธุรกรรม IEEE ในการใช้งานทางอุตสาหกรรม
เดอเคอร์โซ, TDC (2021) Estudo do modelo Johansen-Ledoit-Sornette de bolhas การเงิน.
Wu, T., Liu, J., Liu, J., Huang, Z., Wu, H., Zhang, C., ... & Zhang, G. (2021) กรอบงานใหม่ที่ใช้ AI สำหรับการวางแผนวิถีโคจรที่เหมาะสมที่สุด AoI ในเครือข่ายเซ็นเซอร์ไร้สายที่ได้รับความช่วยเหลือจาก UAV ธุรกรรม IEEE เกี่ยวกับการสื่อสารไร้สาย
Liu, H., Wen, Z., & Cai, W. (2021, สิงหาคม) FastPSO: สู่อัลกอริธึม Swarm Intelligence ที่มีประสิทธิภาพบน GPU ในการประชุมนานาชาติเรื่องการประมวลผลแบบขนานครั้งที่ 50 (หน้า 1-10)
มาห์บับ ร. (2020) อัลกอริทึมและเทคนิคการปรับให้เหมาะสมสำหรับการแก้ปัญหา TSP
Li, J., Chen, T., Lim, K., Chen, L., Khan, SA, Xie, J., & Wang, X. (2019) การเรียนรู้เชิงลึกเร่งการสังเคราะห์คลัสเตอร์นาโนทองคำ ระบบอัจฉริยะขั้นสูง 1(3) 1900029