• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Algoritma Genetik

gerardoradite Feb 15th, 2020 61 in 362 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import numpy
2.
3. def pop_fitness(equation_inputs, pop):
4.     # Hitung fitness value populasi.
5.     fitness = numpy.sum(pop*equation_inputs, axis=1)
6.     return fitness
7.
8. def select_indiv(pop, fitness, num_parents):
9.     # Pilih individual terbaik
10.     parents = numpy.empty((num_parents, pop.shape[1]))
11.     for parent_num in range(num_parents):
12.         max_fitness_idx = numpy.where(fitness == numpy.max(fitness))
13.         max_fitness_idx = max_fitness_idx[0][0]
14.         parents[parent_num, :] = pop[max_fitness_idx, :]
15.         fitness[max_fitness_idx] = -99999999999
16.     return parents
17.
18. def crossover(parents, offspring_size):
19.     offspring = numpy.empty(offspring_size)
20.     # Posisi crossover
21.     crossover_point = numpy.uint8(offspring_size[1]/2)
22.
23.     for k in range(offspring_size[0]):
24.         # Index parent1 - mate.
25.         parent1_idx = k%parents.shape[0]
26.         # Index parent2 - mate.
27.         parent2_idx = (k+1)%parents.shape[0]
28.         # Individual baru - gen bagian I dari parent1.
29.         offspring[k, 0:crossover_point] = parents[parent1_idx, 0:crossover_point]
30.         # Individual baru - gen bagian I dari parent2.
31.         offspring[k, crossover_point:] = parents[parent2_idx, crossover_point:]
32.     return offspring
33.
34. def mutasi(offspring_crossover, num_mutasi=1):
35.     counter_mutasi = numpy.uint8(offspring_crossover.shape[1] / num_mutasi)
36.     for idx in range(offspring_crossover.shape[0]):
37.         gene_idx = counter_mutasi - 1
38.         for mutation_num in range(num_mutasi):
39.             # The random value to be added to the gene.
40.             random_value = numpy.random.uniform(-1.0, 1.0, 1)
41.             offspring_crossover[idx, gene_idx] = offspring_crossover[idx, gene_idx] + random_value
42.             gene_idx = gene_idx + counter_mutasi
43.     return offspring_crossover
44.
45. """
46. Maksimum nilai y:
47.    y = w1x1+w2x2+w3x3+w4x4+w5x5+6wx6
48.    dimana (w1,w2,w3,w4,w5,w6)=(4,-2,3.5,5,-11,-4.7)
49.    Penyelesaian dengan algoritma genetik
50. """
51. equation_inputs = [4,-2,3.5,5,-11,-4.7]
52.
53. # Optimasi
54. num_varx = len(equation_inputs)
55.
56.
57. # Parameter Algoritma genetik :
58.
59. sol_per_pop = 8
60. num_parents_mating = 4
61.
62. # Populasi.
63. pop_size = (sol_per_pop,num_varx)
64. # Initial population.
65. new_population = numpy.random.uniform(low=-4.0, high=4.0, size=pop_size)
66. print(new_population)
67.
68. best_outputs = []
69. num_generations = 1000
70. for generation in range(num_generations):
71.     print("Generation : ", generation)
72.     fitness = pop_fitness(equation_inputs, new_population)
73.     print("Fitness")
74.     print(fitness)
75.
76.     best_outputs.append(numpy.max(numpy.sum(new_population*equation_inputs, axis=1)))
77.     # Hasil terbaik.
78.     print("Best result : ", numpy.max(numpy.sum(new_population*equation_inputs, axis=1)))
79.
80.     # Parents terbaik dalam populasi.
81.     parents = select_indiv(new_population, fitness, num_parents_mating)
82.     print("Parents")
83.     print(parents)
84.
85.     # Pembentukan gen baru dengan crossover.
86.     offspring_crossover = crossover(parents, offspring_size=(pop_size[0]-parents.shape[0], num_varx))
87.     print("Crossover")
88.     print(offspring_crossover)
89.
90.     offspring_mutation = mutasi(offspring_crossover, num_mutasi=2)
91.     print("Mutation")
92.     print(offspring_mutation)
93.
94.     # Populasi baru.
95.     new_population[0:parents.shape[0], :] = parents
96.     new_population[parents.shape[0]:, :] = offspring_mutation
97.
98.
99. fitness = pop_fitness(equation_inputs, new_population)
100. best_match_idx = numpy.where(fitness == numpy.max(fitness))
101.
102. print("Best solution : ", new_population[best_match_idx, :])
103. print("Best solution fitness : ", fitness[best_match_idx])
104.
105. import matplotlib.pyplot
106. matplotlib.pyplot.plot(best_outputs)
107. matplotlib.pyplot.xlabel('Iteration')
108. matplotlib.pyplot.ylabel('Fitness')
109. matplotlib.pyplot.show()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top