Skip to content

Commit

Permalink
Merge pull request #27 from BorjaEst/dev
Browse files Browse the repository at this point in the history
improve session flexibility
  • Loading branch information
BorjaEst authored Mar 27, 2023
2 parents 9fd0336 + b130b77 commit a071639
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 190 deletions.
32 changes: 15 additions & 17 deletions README.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@
{
"data": {
"text/plain": [
"[{'id': UUID('8b77fc1d-befe-4ad3-924c-1774223b7b60'),\n",
"[{'id': UUID('8aa2c907-479b-444c-8c15-995a9015183a'),\n",
" 'experiment': None,\n",
" 'created': datetime.datetime(2023, 3, 4, 15, 24, 49, 325435),\n",
" 'created': datetime.datetime(2023, 3, 27, 11, 25, 55, 969290),\n",
" 'parents': [],\n",
" 'generation': 1,\n",
" 'score': None,\n",
" 'chromosome_1': Haploid([0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0], dtype=uint8),\n",
" 'chromosome_2': Haploid([1, 0, 1, 1, 1, 0, 0, 1, 0, 1], dtype=uint8),\n",
" 'chromosome_1': Haploid([1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0], dtype=uint8),\n",
" 'chromosome_2': Haploid([0, 1, 0, 0, 0, 1, 0, 0, 1, 0], dtype=uint8),\n",
" 'simple_attribute': 1.0},\n",
" {'id': UUID('a4460974-a45a-4ed2-8937-55ea211bb520'),\n",
" {'id': UUID('94f78963-e4bf-4059-8b81-03b62587edd3'),\n",
" 'experiment': None,\n",
" 'created': datetime.datetime(2023, 3, 4, 15, 24, 49, 325564),\n",
" 'created': datetime.datetime(2023, 3, 27, 11, 25, 55, 969420),\n",
" 'parents': [],\n",
" 'generation': 1,\n",
" 'score': None,\n",
" 'chromosome_1': Haploid([1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0], dtype=uint8),\n",
" 'chromosome_2': Haploid([1, 0, 1, 1, 1, 0, 0, 1, 0, 1], dtype=uint8),\n",
" 'chromosome_1': Haploid([1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1], dtype=uint8),\n",
" 'chromosome_2': Haploid([0, 0, 0, 1, 0, 1, 0, 1, 1, 1], dtype=uint8),\n",
" 'simple_attribute': 1.0}]"
]
},
Expand Down Expand Up @@ -140,7 +140,7 @@
{
"data": {
"text/plain": [
"<__main__.MyFitness at 0x7f19e0744f40>"
"<__main__.MyFitness at 0x7fdb4574c490>"
]
},
"execution_count": 2,
Expand Down Expand Up @@ -199,7 +199,7 @@
{
"data": {
"text/plain": [
"MyAlgorithm(selection1=<gevopy.tools.selection.Tournaments object at 0x7f19906ca680>, mutation=<gevopy.tools.mutation.SinglePoint object at 0x7f19906ca710>, selection2=<gevopy.tools.selection.Uniform object at 0x7f19906ca770>, crossover=<gevopy.tools.crossover.Uniform object at 0x7f19906c8ee0>, survival_rate=0.4)"
"MyAlgorithm(selection1=<gevopy.tools.selection.Tournaments object at 0x7fdb4574fac0>, mutation=<gevopy.tools.mutation.SinglePoint object at 0x7fdb4574c4f0>, selection2=<gevopy.tools.selection.Uniform object at 0x7fdb4574faf0>, crossover=<gevopy.tools.crossover.Uniform object at 0x7fdb4574fb50>, survival_rate=0.4)"
]
},
"execution_count": 3,
Expand Down Expand Up @@ -254,8 +254,8 @@
"data": {
"text/plain": [
"Evolutionary algorithm execution report:\n",
" Executed generations: 12\n",
" Best phenotype: 7b13630f-d07c-4ff6-8be1-df6d6ceb06ca\n",
" Executed generations: 17\n",
" Best phenotype: 4558f0a4-26c7-4803-8c65-91b110a3d84e\n",
" Best score: 10"
]
},
Expand All @@ -267,13 +267,11 @@
"source": [
"import gevopy as ea\n",
"\n",
"experiment = ea.Experiment(\n",
" fitness=MyFitness(cache=True, scheduler=\"synchronous\"),\n",
" algorithm=MyAlgorithm(survival_rate=0.2),\n",
")\n",
"\n",
"experiment = ea.Experiment()\n",
"with experiment.session() as session:\n",
" session.add_phenotypes([MyGenotype() for _ in range(20)])\n",
" session.algorithm = MyAlgorithm(survival_rate=0.2)\n",
" session.fitness = MyFitness(cache=True, scheduler=\"synchronous\")\n",
" statistics = session.run(max_generation=20, max_score=10)\n",
"\n",
"experiment.close()\n",
Expand Down
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,29 +159,23 @@ instantiating the experiment to store all phenotypes during the execution.
```python
import gevopy as ea

experiment = ea.Experiment(
fitness=MyFitness(cache=True, scheduler="synchronous"),
algorithm=MyAlgorithm(survival_rate=0.2),
)

experiment = ea.Experiment()
with experiment.session() as session:
session.add_phenotypes([MyGenotype() for _ in range(20)])
session.algorithm = MyAlgorithm(survival_rate=0.2)
session.fitness = MyFitness(cache=True, scheduler="synchronous")
statistics = session.run(max_generation=20, max_score=10)

experiment.close()
statistics
```




Evolutionary algorithm execution report:
Executed generations: 12
Best phenotype: 7b13630f-d07c-4ff6-8be1-df6d6ceb06ca
Best score: 10



>The method `run` forces the evolution of the experiment which is updated on
each cycle. After the method is completed, you can force again te evolution
process using higher inputs for `max_generations` or `max_score`.
Expand Down
36 changes: 20 additions & 16 deletions examples/database_evolution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,34 @@
"name": "stderr",
"output_type": "stream",
"text": [
"2022-12-15 09:06:03,357 - gevopy.Experiment - INFO\n",
" [44d71da0-2345-4686-842e-b7dbee7000af]: [gen:0]: Start of evolutionary experiment execution\n",
"2022-12-15 09:06:03,391 - gevopy.Experiment - INFO\n",
" [44d71da0-2345-4686-842e-b7dbee7000af]: [gen:1]: Completed cycle; 8\n",
"2022-12-15 09:06:03,411 - gevopy.Experiment - INFO\n",
" [44d71da0-2345-4686-842e-b7dbee7000af]: [gen:2]: Completed cycle; 9\n",
"2022-12-15 09:06:03,433 - gevopy.Experiment - INFO\n",
" [44d71da0-2345-4686-842e-b7dbee7000af]: [gen:3]: Completed cycle; 9\n",
"2022-12-15 09:06:03,454 - gevopy.Experiment - INFO\n",
" [44d71da0-2345-4686-842e-b7dbee7000af]: [gen:4]: Completed cycle; 10\n",
"2022-12-15 09:06:03,455 - gevopy.Experiment - INFO\n",
" [44d71da0-2345-4686-842e-b7dbee7000af]: [gen:4]: Experiment execution completed successfully\n"
"2023-03-27 11:23:48,181 - gevopy.Experiment - INFO\n",
" [MyExperiment]: Experiment evolution phase:1 start\n",
"2023-03-27 11:23:48,182 - gevopy.Experiment - INFO\n",
" [gen:0]: Start of evolutionary experiment execution\n",
"2023-03-27 11:23:48,208 - gevopy.Experiment - INFO\n",
" [gen:1]: Completed cycle; 8\n",
"2023-03-27 11:23:48,223 - gevopy.Experiment - INFO\n",
" [gen:2]: Completed cycle; 9\n",
"2023-03-27 11:23:48,237 - gevopy.Experiment - INFO\n",
" [gen:3]: Completed cycle; 9\n",
"2023-03-27 11:23:48,252 - gevopy.Experiment - INFO\n",
" [gen:4]: Completed cycle; 10\n",
"2023-03-27 11:23:48,253 - gevopy.Experiment - INFO\n",
" [gen:4]: Experiment execution completed successfully\n"
]
}
],
"source": [
"experiment = ea.Experiment(\n",
" fitness=MyFitness(cache=True, scheduler=\"synchronous\"),\n",
" algorithm=MyAlgorithm(survival_rate=0.2),\n",
" database=Neo4jInterface(URI, auth=AUTH),\n",
" name=\"MyExperiment\",\n",
")\n",
"\n",
"with experiment.session() as session:\n",
" session.add_phenotypes([MyGenotype() for _ in range(12)])\n",
" session.algorithm = MyAlgorithm(survival_rate=0.2)\n",
" session.fitness = MyFitness(cache=True, scheduler=\"synchronous\")\n",
" session.logger.info(\"Experiment evolution phase:1 start\")\n",
" statistics = session.run(max_score=10)\n"
]
},
Expand All @@ -122,7 +126,7 @@
"text/plain": [
"Evolutionary algorithm execution report:\n",
" Executed generations: 4\n",
" Best phenotype: 2123579d-b38d-4f19-8f37-2fd261f83b68\n",
" Best phenotype: 00897083-f603-4b24-8d83-dff50af2b61a\n",
" Best score: 10"
]
},
Expand Down Expand Up @@ -170,7 +174,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
"version": "3.10.10"
},
"orig_nbformat": 4,
"vscode": {
Expand Down
173 changes: 173 additions & 0 deletions examples/stepped_evolution.ipynb
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
}
2 changes: 1 addition & 1 deletion src/gevopy/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.2
1.1.0
Loading

0 comments on commit a071639

Please sign in to comment.