summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMitsuo Tokumori <[email protected]>2022-04-22 00:15:39 -0500
committerMitsuo Tokumori <[email protected]>2022-04-22 00:15:39 -0500
commit5647ee75b3b0c69ac053cf133c7110a8f8079efa (patch)
tree6e95e35755608ce850edcd9309f4cf546146b19e /test
parent48b9f5cfbb91e555d88e094928c3f66000df338a (diff)
parent98c6b3545bff966e8a2d28427e49234bea8ad716 (diff)
downloadDP1_project-5647ee75b3b0c69ac053cf133c7110a8f8079efa.tar.gz
DP1_project-5647ee75b3b0c69ac053cf133c7110a8f8079efa.tar.bz2
DP1_project-5647ee75b3b0c69ac053cf133c7110a8f8079efa.zip
Merge remote-tracking branch 'refs/remotes/origin/develop' into develop
Adds test/.ipynb_checkpoints Remove later
Diffstat (limited to 'test')
-rw-r--r--test/.ipynb_checkpoints/GA-checkpoint.ipynb112
1 files changed, 112 insertions, 0 deletions
diff --git a/test/.ipynb_checkpoints/GA-checkpoint.ipynb b/test/.ipynb_checkpoints/GA-checkpoint.ipynb
new file mode 100644
index 0000000..bab966d
--- /dev/null
+++ b/test/.ipynb_checkpoints/GA-checkpoint.ipynb
@@ -0,0 +1,112 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "2fd45b3a-9a24-4782-812c-08223edb750e",
+ "metadata": {},
+ "source": [
+ "# Prueba del algoritmo genetico"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "f6b4829a-9001-410c-b20c-01c65c777d8a",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "2c3c85e0-a90c-4fda-86f7-778d7328c74d",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "511ff788-0d1a-4ac7-9575-de182d236574",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "078280b5-70ef-4691-8798-a686d85d188c",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "b3ad92de-b2ed-4f21-a696-1fa2981f89dc",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def genetic_algorithm(population, fitness_fn, ngen=100, pmut=0.1):\n",
+ " \"Algoritmo Genetico \"\n",
+ " \n",
+ " popsize = len(population)\n",
+ " evaluate_population(population, fitness_fn) # evalua la poblacion inicial\n",
+ " ibest = sorted(range(len(population)), key=lambda i: population[i].fitness, reverse=True)[:1]\n",
+ " bestfitness = [population[ibest[0]].fitness]\n",
+ " print(\"Poblacion inicial, best_fitness = {}\".format(population[ibest[0]].fitness))\n",
+ " \n",
+ " for g in range(ngen): # Por cada generacion\n",
+ " \n",
+ " ## Selecciona las parejas de padres para cruzamiento \n",
+ " mating_pool = []\n",
+ " for i in range(int(popsize/2)): mating_pool.append(select_parents_roulette(population)) \n",
+ " \n",
+ " ## Crea la poblacion descendencia cruzando las parejas del mating pool con Recombinación de 1 punto\n",
+ " offspring_population = []\n",
+ " for i in range(len(mating_pool)): \n",
+ " #offspring_population.extend( mating_pool[i][0].crossover_onepoint(mating_pool[i][1]) )\n",
+ " offspring_population.extend( mating_pool[i][0].crossover_uniform(mating_pool[i][1]) )\n",
+ "\n",
+ " ## Aplica el operador de mutacion con probabilidad pmut en cada hijo generado\n",
+ " for i in range(len(offspring_population)):\n",
+ " if random.uniform(0, 1) < pmut: \n",
+ " offspring_population[i] = offspring_population[i].mutate_position()\n",
+ " \n",
+ " ## Evalua la poblacion descendencia\n",
+ " evaluate_population(offspring_population, fitness_fn) # evalua la poblacion inicial\n",
+ " \n",
+ " ## Selecciona popsize individuos para la sgte. generación de la union de la pob. actual y pob. descendencia\n",
+ " population = select_survivors(population, offspring_population, popsize)\n",
+ "\n",
+ " ## Almacena la historia del fitness del mejor individuo\n",
+ " ibest = sorted(range(len(population)), key=lambda i: population[i].fitness, reverse=True)[:1]\n",
+ " bestfitness.append(population[ibest[0]].fitness)\n",
+ " print(\"generacion {}, best_fitness = {}\".format(g, population[ibest[0]].fitness))\n",
+ " \n",
+ " return population[ibest[0]], bestfitness "
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "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.9.2"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}