-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_pf_try.py
70 lines (53 loc) · 1.95 KB
/
main_pf_try.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import numpy as np
import matplotlib.pyplot as plt
import ga
import GlobalIndexKinematical as km
# Inputs or constants
span = [[500,50,50,-100],[1000,250,250,100]]
# Number of variables for optimization
num_var = 4
num_kromo = 10
pop_size = (num_kromo, 1)
new_pop1 = np.random.uniform(span[0][0], span[1][0], size=pop_size)
new_pop2 = np.random.uniform(span[0][1], span[1][1], size=pop_size)
new_pop3 = np.random.uniform(span[0][2], span[1][2], size=pop_size)
new_pop4 = np.random.uniform(span[0][3], span[1][3], size=pop_size)
new_population = np.concatenate((new_pop1,new_pop2, new_pop3, new_pop4),axis=1)
print('Random Population:\n',new_population,'\n')
# Point Cloud
P = km.WorkspaceDesired(500.0,650.0,50.0)
# Number of Parents
num_parents = int(num_kromo/2)
k = 50 # Number of Generations
Global_fitness = []
Avg_fitness=[]
for i in range(k):
# Evaluate Fitness
print('\n',f'Evaluate Fitness of Generation {i}:')
fitness = ga.fitnessK(new_population,P,True)
Global_fitness.append(max(fitness))
print('Avg Fitness',sum(fitness)/len(fitness))
Avg_fitness.append(sum(fitness)/len(fitness))
#print('Fitness:\n',fitness,'\n')
# Select Best Fitness
parents = ga.select_parents(new_population,fitness,num_parents)
print('Parents Selected:\n',parents, '\n')
# Crossover
offspring_cross = ga.crossover(parents,num_kromo)
print('crossover:\n',offspring_cross, '\n')
# Mutation
mut_prob = 0.6
offspring_mut = ga.mutation(offspring_cross,span, mut_prob)
print('Mutation:\n',offspring_mut, '\n')
# Pop
new_population = np.concatenate((parents, offspring_mut))
print('Generation ', i, ':\n', new_population)
print('\n',f'Evaluate Fitness of Generation {k}:')
fitness = ga.fitnessK(new_population,P)
Global_fitness.append(max(fitness))
#print(Global_fitness)
print(new_population[fitness.index(max(fitness))])
plt.plot(Global_fitness,'b-')
plt.plot(Avg_fitness,'r-')
plt.xlabel('# Generaciones')
plt.show()