代码地址(非免费):https://download.csdn.net/download/wq6qeg88/89016256
python+算法+并行遗传算法GA
并行的遗传算法GA
# pip install numpy
# pip install func_timeout
# pip install matplotlib
import numpy as np
from Genetic_algorithm import GA
# 目标函数(四个决策变量):
def objective(X):
X1 = X[0]
X2 = X[1]
X3 = X[2]
X4 = X[3]
return (X1 + X2) / (X3 + X4 + 0.5)
if __name__ == '__main__':
varbound = np.array([[1, 3], [1, 4], [0.5, 1.5], [2, 20], ])
vartype = np.array([['int'], ['int'], ['real'], ['int'], ])
parameters = {'max_num_iteration': None,
'population_size': 400,
'mutation_probability': 0.1,
'elit_ratio': 0.1,
'crossover_probability': 0.5,
'parents_portion': 0.3,
'crossover_type': 'uniform',
'max_iteration_without_improv': None,
'Number_of_processes': 'max',
'Population_file_path': "pop.csv"}
Genetic = GA(objective, 4,
variable_type_mixed=vartype,
variable_boundaries=varbound,
function_timeout=5000,
algorithm_parameters=parameters)
# this line is to run the code for the first time
Genetic.run()
------------------------------------------
generation: 0
Average fitness: 0.5130578085499143
Variance: 0.13204943415789872
Max fitness: 0.09866432014162585
best var: [ 1. 1. 0.77075236 19. ]
------------------------------------------
|_________________________________________________ 2.9% GA is running...
------------------------------------------
generation: 1
Average fitness: 0.3282432299405501
Variance: 0.07409130059914024
Max fitness: 0.09098944303502747
best var: [ 1. 1. 1.48057196 20. ]
------------------------------------------
|||_______________________________________________ 5.7% GA is running...
------------------------------------------
generation: 2
Average fitness: 0.26434740226517395
Variance: 0.06176844333074767
Max fitness: 0.0909842449359865
best var: [ 1. 1. 1.48182775 20. ]
------------------------------------------
||||______________________________________________ 8.6% GA is running...
------------------------------------------
generation: 3
Average fitness: 0.2256923928363497
Variance: 0.04589546096955364
Max fitness: 0.0909842449359865
best var: [ 1. 1. 1.48182775 20. ]
------------------------------------------
||||||____________________________________________ 11.4% GA is running...
------------------------------------------
generation: 4
Average fitness: 0.18679314458964477
Variance: 0.02571627048316776
Max fitness: 0.0909842449359865
best var: [ 1. 1. 1.48182775 20. ]
------------------------------------------
|||||||___________________________________________ 14.3% GA is running...
------------------------------------------
generation: 5
Average fitness: 0.15287312995270186
Variance: 0.013301936586711686
Max fitness: 0.0909842449359865
best var: [ 1. 1. 1.48182775 20. ]
------------------------------------------
|||||||||_________________________________________ 17.1% GA is running...
------------------------------------------
generation: 6
Average fitness: 0.15398807487129937
Variance: 0.013071028095969774
Max fitness: 0.0909842449359865
best var: [ 1. 1. 1.48182775 20. ]
------------------------------------------
||||||||||________________________________________ 20.0% GA is running...
------------------------------------------
generation: 7
Average fitness: 0.15984053090313216
Variance: 0.020463423121385736
Max fitness: 0.0909842449359865
best var: [ 1. 1. 1.48182775 20. ]
------------------------------------------
|||||||||||_______________________________________ 22.9% GA is running...
------------------------------------------
generation: 8
Average fitness: 0.1433135810319052
Variance: 0.008584247503427032
Max fitness: 0.09095244845467447
best var: [ 1. 1. 1.48951248 20. ]
------------------------------------------
|||||||||||||_____________________________________ 25.7% GA is running...
------------------------------------------
generation: 9
Average fitness: 0.13188214342218804
Variance: 0.006486674347021874
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
||||||||||||||____________________________________ 28.6% GA is running...
------------------------------------------
generation: 10
Average fitness: 0.1313129526591849
Variance: 0.009386195431357136
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
||||||||||||||||__________________________________ 31.4% GA is running...
------------------------------------------
generation: 11
Average fitness: 0.13118919879140228
Variance: 0.0067811859902897976
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
|||||||||||||||||_________________________________ 34.3% GA is running...
------------------------------------------
generation: 12
Average fitness: 0.14455272507921535
Variance: 0.010146418172648513
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
|||||||||||||||||||_______________________________ 37.1% GA is running...
------------------------------------------
generation: 13
Average fitness: 0.1394339916421611
Variance: 0.014040229255055468
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
||||||||||||||||||||______________________________ 40.0% GA is running...
------------------------------------------
generation: 14
Average fitness: 0.13329007212823643
Variance: 0.007109027242534198
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
|||||||||||||||||||||_____________________________ 42.9% GA is running...
------------------------------------------
generation: 15
Average fitness: 0.12396506926335085
Variance: 0.0070365971579838945
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
|||||||||||||||||||||||___________________________ 45.7% GA is running...
------------------------------------------
generation: 16
Average fitness: 0.13394317291255706
Variance: 0.011520185084709285
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
||||||||||||||||||||||||__________________________ 48.6% GA is running...
------------------------------------------
generation: 17
Average fitness: 0.13573964957888254
Variance: 0.010693815063542732
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
||||||||||||||||||||||||||________________________ 51.4% GA is running...
------------------------------------------
generation: 18
Average fitness: 0.13244348466520528
Variance: 0.011261261037155704
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
|||||||||||||||||||||||||||_______________________ 54.3% GA is running...
------------------------------------------
generation: 19
Average fitness: 0.12532933175518732
Variance: 0.007124612117180216
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
|||||||||||||||||||||||||||||_____________________ 57.1% GA is running...
------------------------------------------
generation: 20
Average fitness: 0.11417752324708559
Variance: 0.0036864892838004504
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
||||||||||||||||||||||||||||||____________________ 60.0% GA is running...
------------------------------------------
generation: 21
Average fitness: 0.12174994879779776
Variance: 0.006562482249272596
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
|||||||||||||||||||||||||||||||___________________ 62.9% GA is running...
------------------------------------------
generation: 22
Average fitness: 0.11866686163748721
Variance: 0.002834635194324299
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
|||||||||||||||||||||||||||||||||_________________ 65.7% GA is running...
------------------------------------------
generation: 23
Average fitness: 0.1290390588726832
Variance: 0.006194298070950644
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
||||||||||||||||||||||||||||||||||________________ 68.6% GA is running...
------------------------------------------
generation: 24
Average fitness: 0.13213572498439485
Variance: 0.007512033801557782
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
||||||||||||||||||||||||||||||||||||______________ 71.4% GA is running...
------------------------------------------
generation: 25
Average fitness: 0.13889803897287614
Variance: 0.007588410471404488
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
|||||||||||||||||||||||||||||||||||||_____________ 74.3% GA is running...
------------------------------------------
generation: 26
Average fitness: 0.12380082382596212
Variance: 0.004903607457856975
Max fitness: 0.09091882785397193
best var: [ 1. 1. 1.49764391 20. ]
------------------------------------------
|||||||||||||||||||||||||||||||||||||||___________ 77.1% GA is running...
------------------------------------------
generation: 27
Average fitness: 0.12397188352839411
Variance: 0.006210913514402342
Max fitness: 0.09091061692874249
best var: [ 1. 1. 1.49963071 20. ]
------------------------------------------
||||||||||||||||||||||||||||||||||||||||__________ 80.0% GA is running...
------------------------------------------
generation: 28
Average fitness: 0.13590248957295192
Variance: 0.01280542293900503
Max fitness: 0.09091061692874249
best var: [ 1. 1. 1.49963071 20. ]
------------------------------------------
|||||||||||||||||||||||||||||||||||||||||_________ 82.9% GA is running...
------------------------------------------
generation: 29
Average fitness: 0.13054364360066956
Variance: 0.01166575475711078
Max fitness: 0.09091061692874249
best var: [ 1. 1. 1.49963071 20. ]
------------------------------------------
|||||||||||||||||||||||||||||||||||||||||||_______ 85.7% GA is running...
------------------------------------------
generation: 30
Average fitness: 0.13680864389166073
Variance: 0.014056644627933825
Max fitness: 0.09091061692874249
best var: [ 1. 1. 1.49963071 20. ]
------------------------------------------
||||||||||||||||||||||||||||||||||||||||||||______ 88.6% GA is running...
------------------------------------------
generation: 31
Average fitness: 0.12740370383354002
Variance: 0.004787769802776117
Max fitness: 0.09091061692874249
best var: [ 1. 1. 1.49963071 20. ]
------------------------------------------
||||||||||||||||||||||||||||||||||||||||||||||____ 91.4% GA is running...
------------------------------------------
generation: 32
Average fitness: 0.12555658396365119
Variance: 0.00615248753906642
Max fitness: 0.09091061692874249
best var: [ 1. 1. 1.49963071 20. ]
------------------------------------------
|||||||||||||||||||||||||||||||||||||||||||||||___ 94.3% GA is running...
------------------------------------------
generation: 33
Average fitness: 0.1379148739813375
Variance: 0.010271782396587935
Max fitness: 0.09091061692874249
best var: [ 1. 1. 1.49963071 20. ]
------------------------------------------
|||||||||||||||||||||||||||||||||||||||||||||||||_ 97.1% GA is running...
------------------------------------------
generation: 34
Average fitness: 0.12637176449103937
Variance: 0.004453944988037379
Max fitness: 0.09091061692874249
best var: [ 1. 1. 1.49963071 20. ]
------------------------------------------
|||||||||||||||||||||||||||||||||||||||||||||||||| 100.0% GA is running...
------------------------------------------
generation: 35
Average fitness: 0.1338887722107841
Variance: 0.007739498967700428
Max fitness: 0.09091061692874249
best var: [ 1. 1. 1.49963071 20. ]
------------------------------------------
Process finished with exit code 0
代码地址(非免费):https://download.csdn.net/download/wq6qeg88/89016256