-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from BorjaEst/dev
improve session flexibility
- Loading branch information
Showing
8 changed files
with
422 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Import dependencies and configure globals\n", | ||
"This example, shows how to use gevopy to evolve a simple Haploid genotype in order to obtain a phenotype with at least 10 ones in it's chromosome. In the process, all the phenotypes are stored inside a neo4j database.\n", | ||
"\n", | ||
"To do so, it is required to import gevopy and generate the Genotype, Fitness and Algorithm required. For the aim of simplicity, we will use some already defined examples from this library. Additionally it is required to import the class Neo4jInterface to specify the connection with our database.\n", | ||
"\n", | ||
"Additionally, you can use neo4j library to verify the connection and the logging module to increase the verbosity of the evolution process." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import logging\n", | ||
"\n", | ||
"import gevopy as ea\n", | ||
"from examples.algorimths import BasicPonderated as MyAlgorithm\n", | ||
"from examples.evaluation import MostOnes, Random\n", | ||
"from examples.genotypes import Bacteria as MyGenotype\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"LOG_FORMAT = \"%(asctime)s - %(name)s - %(levelname)s\\n %(message)s\"\n", | ||
"logging.basicConfig(level=\"INFO\", format=LOG_FORMAT)\n", | ||
"logger = logging.getLogger(\"gevopy.Experiment\")\n", | ||
"logger.setLevel(logging.INFO) # Only Experiment INFO\n" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Create and run the experiment\n", | ||
"Execute an experiment by creating an instance and assigning the argument database with the driver. The experiment will automaticaly manage sessions and use the methods .execute_read and .execute_write.\n", | ||
"\n", | ||
"Use the experiment session as a normal example to evolve a Genotype. As minimalist example, add some phenotypes as a base and start the evolution by using .run method." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"experiment = ea.Experiment(\n", | ||
" algorithm=MyAlgorithm(survival_rate=0.2),\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"2023-03-27 11:21:18,005 - gevopy.Experiment - INFO\n", | ||
" [c410ba56-a2c5-429b-bd50-b39f9924669a]: Starting experiment phase 1\n", | ||
"2023-03-27 11:21:18,007 - gevopy.Experiment - INFO\n", | ||
" [gen:0]: Start of evolutionary experiment execution\n", | ||
"2023-03-27 11:21:18,023 - gevopy.Experiment - INFO\n", | ||
" [gen:1]: Completed cycle; 9\n", | ||
"2023-03-27 11:21:18,031 - gevopy.Experiment - INFO\n", | ||
" [gen:2]: Completed cycle; 9\n", | ||
"2023-03-27 11:21:18,039 - gevopy.Experiment - INFO\n", | ||
" [gen:3]: Completed cycle; 9\n", | ||
"2023-03-27 11:21:18,047 - gevopy.Experiment - INFO\n", | ||
" [gen:4]: Completed cycle; 10\n", | ||
"2023-03-27 11:21:18,048 - gevopy.Experiment - INFO\n", | ||
" [gen:4]: Experiment execution completed successfully\n", | ||
"2023-03-27 11:21:18,049 - gevopy.Experiment - INFO\n", | ||
" [c410ba56-a2c5-429b-bd50-b39f9924669a]: Starting experiment phase 2\n", | ||
"2023-03-27 11:21:18,050 - gevopy.Experiment - INFO\n", | ||
" [gen:0]: Start of evolutionary experiment execution\n", | ||
"2023-03-27 11:21:18,061 - gevopy.Experiment - INFO\n", | ||
" [gen:1]: Completed cycle; 0.842471456650234\n", | ||
"2023-03-27 11:21:18,069 - gevopy.Experiment - INFO\n", | ||
" [gen:2]: Completed cycle; 0.9858978453747482\n", | ||
"2023-03-27 11:21:18,070 - gevopy.Experiment - INFO\n", | ||
" [gen:2]: Experiment execution completed successfully\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"Evolutionary algorithm execution report:\n", | ||
" Executed generations: 2\n", | ||
" Best phenotype: f43d92c9-ccdc-4e08-9f0b-17c991037d14\n", | ||
" Best score: 0.9858978453747482" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"with experiment.session() as session:\n", | ||
" session.add_phenotypes([MyGenotype() for _ in range(12)])\n", | ||
" session.fitness = MostOnes()\n", | ||
" session.logger.info(\"Starting experiment phase 1\")\n", | ||
" statistics_1 = session.run(max_score=10)\n", | ||
" session.reset_score()\n", | ||
" session.fitness = Random()\n", | ||
" session.logger.info(\"Starting experiment phase 2\")\n", | ||
" statistics_2 = session.run(max_score=0.95)\n", | ||
"\n", | ||
"statistics_2" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Close connections\n", | ||
"Unless you created them using the with statement, call the .close method on all Driver and Session instances out of the experiment to release any resources still held by them." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"experiment.close()\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3.10.8 64-bit", | ||
"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.10.10" | ||
}, | ||
"orig_nbformat": 4, | ||
"vscode": { | ||
"interpreter": { | ||
"hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.0.2 | ||
1.1.0 |
Oops, something went wrong.