push code

parent 7cee94b1
{
"cells": [],
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# EA"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def cal_pop_fitness(equation_inputs, pop):\n",
" # Calculating the fitness value of each solution in the current population.\n",
" # The fitness function calulates the sum of products between each input and its corresponding weight.\n",
" fitness = numpy.sum(pop*equation_inputs, axis=1)\n",
" return fitness\n",
"\n",
"def select_mating_pool(pop, fitness, num_parents):\n",
" # Selecting the best individuals in the current generation as parents for producing the offspring of the next generation.\n",
" parents = numpy.empty((num_parents, pop.shape[1]))\n",
" for parent_num in range(num_parents):\n",
" max_fitness_idx = numpy.where(fitness == numpy.max(fitness))\n",
" max_fitness_idx = max_fitness_idx[0][0]\n",
" parents[parent_num, :] = pop[max_fitness_idx, :]\n",
" fitness[max_fitness_idx] = -99999999999\n",
" return parents"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def crossover(parents, offspring_size):\n",
" offspring = numpy.empty(offspring_size)\n",
" # The point at which crossover takes place between two parents. Usually, it is at the center.\n",
" crossover_point = numpy.uint8(offspring_size[1]/2)\n",
"\n",
" for k in range(offspring_size[0]):\n",
" # Index of the first parent to mate.\n",
" parent1_idx = k%parents.shape[0]\n",
" # Index of the second parent to mate.\n",
" parent2_idx = (k+1)%parents.shape[0]\n",
" # The new offspring will have its first half of its genes taken from the first parent.\n",
" offspring[k, 0:crossover_point] = parents[parent1_idx, 0:crossover_point]\n",
" # The new offspring will have its second half of its genes taken from the second parent.\n",
" offspring[k, crossover_point:] = parents[parent2_idx, crossover_point:]\n",
" return offspring"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def mutation(offspring_crossover, num_mutations=1):\n",
" mutations_counter = numpy.uint8(offspring_crossover.shape[1] / num_mutations)\n",
" # Mutation changes a number of genes as defined by the num_mutations argument. The changes are random.\n",
" for idx in range(offspring_crossover.shape[0]):\n",
" gene_idx = mutations_counter - 1\n",
" for mutation_num in range(num_mutations):\n",
" # The random value to be added to the gene.\n",
" random_value = numpy.random.uniform(-1.0, 1.0, 1)\n",
" offspring_crossover[idx, gene_idx] = offspring_crossover[idx, gene_idx] + random_value\n",
" gene_idx = gene_idx + mutations_counter\n",
" return offspring_crossover"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment