forked from zzzac/Rule-based-forex-trading-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga.py
117 lines (99 loc) · 5.13 KB
/
ga.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# GA
import numpy as np
import matplotlib.pyplot as plt
def cal_pop_fitness(equation_inputs, pop):
# Calculating the fitness value of each solution in the current population.
# The fitness function calulates the sum of products between each input and its corresponding weight.
# fitness = np.sum(pop*equation_inputs, axis=1)
# pop size (8,6)
# fitness size 8x1
logr = equation_inputs[:,0]
temp = pop@equation_inputs[:,1:7].T
fitness = np.sum(temp*logr, axis = 1)/np.sum(pop, axis = 1)
return fitness
def select_mating_pool(pop, fitness, num_parents):
# Selecting the best individuals in the current generation as parents for producing the offspring of the next generation.
parents = np.empty((num_parents, pop.shape[1]))
for parent_num in range(num_parents):
max_fitness_idx = np.where(fitness == np.max(fitness))
max_fitness_idx = max_fitness_idx[0][0]
parents[parent_num, :] = pop[max_fitness_idx, :]
fitness[max_fitness_idx] = -99999999999
return parents
def crossover(parents, offspring_size):
offspring = np.empty(offspring_size)
# The point at which crossover takes place between two parents. Usually, it is at the center.
crossover_point = np.uint8(offspring_size[1]/2)
for k in range(offspring_size[0]):
# Index of the first parent to mate.
parent1_idx = k%parents.shape[0]
# Index of the second parent to mate.
parent2_idx = (k+1)%parents.shape[0]
# The new offspring will have its first half of its genes taken from the first parent.
offspring[k, 0:crossover_point] = parents[parent1_idx, 0:crossover_point]
# The new offspring will have its second half of its genes taken from the second parent.
offspring[k, crossover_point:] = parents[parent2_idx, crossover_point:]
return offspring
def mutation(offspring_crossover, num_mutations=1):
mutations_counter = np.uint8(offspring_crossover.shape[1] / num_mutations)
# Mutation changes a number of genes as defined by the num_mutations argument. The changes are random.
for idx in range(offspring_crossover.shape[0]):
gene_idx = mutations_counter - 1
for _ in range(num_mutations):
# The random value to be added to the gene.
random_value = np.random.uniform(-1.0, 1.0, 1)
offspring_crossover[idx, gene_idx] = offspring_crossover[idx, gene_idx] + random_value
gene_idx = gene_idx + mutations_counter
return offspring_crossover
def cal_pop_fitness(equation_inputs, pop, opt = 0):
# Calculating the fitness value of each solution in the current population.
# Opt = 0 is the GAMSSR model
logr = equation_inputs[:,0] # n,
positions = pop@equation_inputs[:,1:].T
port_r = (positions*logr).astype(np.float64)
SSR = np.mean(port_r, axis = 1)/np.std(port_r, axis = 1)/(-np.sum(port_r[port_r<0]))
return SSR
def GA_train(training_df, optimizing_selection=0, sol_per_pop=8, num_parents_mating=4, num_generations = 200):
"""
Genetic algorithm parameters:
Mating pool size
Population size
"""
#Inputs of the equation.
equation_inputs = training_df.values
# Number of the weights we are looking to optimize.
num_weights = training_df.shape[1]-1
# Defining the population size.
pop_size = (sol_per_pop,num_weights)
# The population will have sol_per_pop chromosome
# where each chromosome has num_weights genes.
# Creating the initial population.
new_population = np.random.uniform(low=-1.0, high=1.0, size=pop_size)
# print(new_population)
best_outputs = []
for generation in range(num_generations):
# print("Generation : ", generation)
# Measuring the fitness of each chromosome in the population.
fitness = cal_pop_fitness(equation_inputs, new_population, optimizing_selection)
best_outputs.append(np.max(fitness))
# Selecting the best parents in the population for mating.
parents = ga.select_mating_pool(new_population, fitness,
num_parents_mating)
# Generating next generation using crossover.
offspring_crossover = ga.crossover(parents,
offspring_size=(pop_size[0]-parents.shape[0], num_weights))
# Adding some variations to the offspring using mutation.
offspring_mutation = ga.mutation(offspring_crossover, num_mutations=2)
# Creating the new population based on the parents and offspring.
new_population[0:parents.shape[0], :] = parents
new_population[parents.shape[0]:, :] = offspring_mutation
# Getting the best solution after iterating finishing all generations.
# At first, the fitness is calculated for each solution in the final generation.
fitness = cal_pop_fitness(equation_inputs, new_population, optimizing_selection)
# Then return the index of that solution corresponding to the best fitness.
best_match_idx = np.where(fitness == np.max(fitness))
plt.plot(best_outputs)
plt.xlabel("Iteration")
plt.ylabel('SSR ratio')
plt.show()
return new_population[best_match_idx]