diff options
| -rw-r--r-- | back/eliminame | 0 | ||||
| -rw-r--r-- | doc/eliminame | 0 | ||||
| -rw-r--r-- | front/eliminame | 0 | ||||
| -rw-r--r-- | test/.ipynb_checkpoints/GA-checkpoint.ipynb | 112 | ||||
| -rw-r--r-- | test/GA.ipynb | 112 |
5 files changed, 224 insertions, 0 deletions
diff --git a/back/eliminame b/back/eliminame new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/back/eliminame diff --git a/doc/eliminame b/doc/eliminame new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/doc/eliminame diff --git a/front/eliminame b/front/eliminame new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/front/eliminame 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 +} diff --git a/test/GA.ipynb b/test/GA.ipynb new file mode 100644 index 0000000..bab966d --- /dev/null +++ b/test/GA.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 +} |
