forked from masoodad/InverseKinematics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
In_Kin.py
53 lines (37 loc) · 1.83 KB
/
In_Kin.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
import numpy as np
import Gen_Algo1 as GA
initial_value=np.random.uniform(low=-3.14, high=3.14, size=60)
new_population=initial_value.reshape(-1,6)
num_generation = 10
num_parents_mating = 5
for generation in range(num_generation):
print("Generation : ",generation)
#Measuring the fitness of each chromosome in the population
print(new_population)
[fitness , err] = GA.cal_fitness(new_population)
#Selecting the best parents in the population for mating.
parents = GA.select_mating_pool(new_population, fitness, num_parents_mating)
#print(parents)
#Generating next generation using crossover.
offspring_size=(new_population.shape[0]-parents.shape[0],new_population.shape[1])
offspring_crossover = GA.crossover(parents, offspring_size)
#print(offspring_crossover)
#Adding mutation to do final magic.
offspring_mutation = GA.mutation(offspring_crossover)
# Creating the new population based on the parents and offspring.
new_population[0:parents.shape[0], :] = parents
new_population[parents.shape[0]:, :] = offspring_mutation
# The best result in the current iteration.
print(new_population)
best_match_idx = fitness.argmin()
fitness = np.array(fitness).T
print("Best solution fitness : ", fitness[best_match_idx])
# Getting the best solution after iterating finishing all generations.
#At first, the fitness is calculated for each solution in the final generation.
[fitness_V, err] = GA.cal_fitness(new_population)
fitness = np.array(fitness_V).T
# Then return the index of that solution corresponding to the best fitness.
best_match_idx = fitness.argmin()
#print(new_population)
print("Best solution : ", new_population[best_match_idx, :])
print("Best solution fitness : ", fitness[best_match_idx])