# Genetic-Algorithm
Implementation of Genetic-Algorithm for solution finding (optimization)
Easy to use GA implementation. With parallel computing and info-prints. Simple and flexible for your optimal solution finding.
<img src="https://github.com/xXAI-botXx/Genetic-Algorithm/raw/v_015/logo.jpeg"></img>
### Usage
1. Get the code
- Download the project and add to python module search path in your code
``````python
import sys
sys.path.insert(0, '../path_to_GA_py_dir')
# ./ => this folder
# ../ => supfolder
``````
- **Or** pip install it (easier)
``````python
pip install Simple-Genetic-Algorithm
``````
3. Import the class and helper function
``````python
from genetic_algorithm import GA, get_random
``````
4. Create 2 functions and parameters
``````python
class Example_GA(GA):
def calculate_fitness(self, kwargs, params):
# return here the fitness (how good the solution is)
# as bigger as better!
# hint: if you have a loss, you should propably just return -1*loss
# example for sklearn model:
model = RandomForestRegressor(n_estimators=params["n_estimators"], ...)
model = model.fit(kwargs["X_train"], kwargs["y_train"])
# predict
y_pred = model.predict(kwargs["X_test"])
# calc mean absolute error loss
y_true = np.array(kwargs["y_test"])
y_pred = np.array(y_pred)
return - np.mean(np.abs(y_true - y_pred))
def get_random_value(self, param_key):
if param_key == "name_of_parameter_1":
return get_random(10, 1000)
elif param_key == "name_of_parameter_2":
return get_random(["something_1", "something_2", "something_3"])
elif param_key == "name_of_parameter_3":
return get_random(0.0, 1.0)
...
parameters = ["n_estimators", "criterion", "max_depth", "max_features", "bootstrap"]
``````
5. Create and run genetic algorithm and pass the input, which will be used in the calculate_fitness function (in kwargs variable)
``````python
optimizer = Example_GA(generations=10, population_size=15, mutation_rate=0.3, list_of_params=parameters)
optimizer.optimize(X_train=X_train, y_train=y_train, X_test=X_dev, y_test=y_dev)
``````
Short explanation:<br>The **kwargs** are the inputs of optimize-method. These are the values which are needed to calculate the fitness. Maybe you can calculate the fitness without them, depending on what you are optimizing.<br>The **list of parameters** are the gene/the solution, so the parameters which are changed and optimized.<br>The **get_random_value** method return a random value for a given parameter, so that the solutions can be initialized and mutated.
### Examples
- <a href="https://github.com/xXAI-botXx/Genetic-Algorithm/blob/main/example.ipynb">Regression with RandomForrestRegressor</a>
- <a href="https://github.com/xXAI-botXx/Genetic-Algorithm/blob/main/example_2.ipynb">Knapsack problem</a>
<!--
<div style="border: 1px solid black; padding: 10px;">
<iframe src="example.html" style="width:100%; height:400px;"></iframe>
</div>
-->
<!--
<div style="border: 1px solid black; padding: 10px;">
<iframe src="example_2.html" style="width:100%; height:400px;"></iframe>
</div>
-->
### License
Feel free to use it. Of course you don't have to name me in your code :)
-> It is a copy-left license
For all details see the license file.
Raw data
{
"_id": null,
"home_page": "https://github.com/xXAI-botXx/Genetic-Algorithm",
"name": "Simple-Genetic-Algorithm",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "Optimization,Genetic-Algorithm,Hyperparameter-Tuning",
"author": "Tobia Ippolito",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/8a/ea/01d1e9cd51855cd81de991fff815d86d228a6e3879c1608286d16db40fcd/Simple_Genetic_Algorithm-0.1.9.5.tar.gz",
"platform": null,
"description": "# Genetic-Algorithm\r\n\r\nImplementation of Genetic-Algorithm for solution finding (optimization)\r\n\r\n\r\n\r\nEasy to use GA implementation. With parallel computing and info-prints. Simple and flexible for your optimal solution finding.\r\n\r\n\r\n\r\n<img src=\"https://github.com/xXAI-botXx/Genetic-Algorithm/raw/v_015/logo.jpeg\"></img>\r\n\r\n\r\n\r\n### Usage\r\n\r\n1. Get the code\r\n\r\n - Download the project and add to python module search path in your code\r\n \r\n ``````python\r\n import sys\r\n sys.path.insert(0, '../path_to_GA_py_dir')\r\n # ./ => this folder\r\n # ../ => supfolder\r\n ``````\r\n\r\n - **Or** pip install it (easier)\r\n\r\n ``````python\r\n pip install Simple-Genetic-Algorithm\r\n ``````\r\n\r\n3. Import the class and helper function\r\n\r\n ``````python\r\n from genetic_algorithm import GA, get_random\r\n ``````\r\n\r\n4. Create 2 functions and parameters\r\n\r\n ``````python\r\n class Example_GA(GA):\r\n \r\n def calculate_fitness(self, kwargs, params):\r\n # return here the fitness (how good the solution is)\r\n # as bigger as better!\r\n # hint: if you have a loss, you should propably just return -1*loss\r\n # example for sklearn model:\r\n model = RandomForestRegressor(n_estimators=params[\"n_estimators\"], ...)\r\n \t\t model = model.fit(kwargs[\"X_train\"], kwargs[\"y_train\"])\r\n # predict\r\n y_pred = model.predict(kwargs[\"X_test\"])\r\n # calc mean absolute error loss\r\n y_true = np.array(kwargs[\"y_test\"])\r\n y_pred = np.array(y_pred)\r\n return - np.mean(np.abs(y_true - y_pred))\r\n \r\n def get_random_value(self, param_key):\r\n if param_key == \"name_of_parameter_1\":\r\n return get_random(10, 1000)\r\n elif param_key == \"name_of_parameter_2\":\r\n return get_random([\"something_1\", \"something_2\", \"something_3\"])\r\n elif param_key == \"name_of_parameter_3\":\r\n return get_random(0.0, 1.0)\r\n \r\n ...\r\n \r\n parameters = [\"n_estimators\", \"criterion\", \"max_depth\", \"max_features\", \"bootstrap\"]\r\n ``````\r\n\r\n5. Create and run genetic algorithm and pass the input, which will be used in the calculate_fitness function (in kwargs variable)\r\n\r\n ``````python\r\n optimizer = Example_GA(generations=10, population_size=15, mutation_rate=0.3, list_of_params=parameters)\r\n optimizer.optimize(X_train=X_train, y_train=y_train, X_test=X_dev, y_test=y_dev)\r\n ``````\r\n\r\n\r\n\r\nShort explanation:<br>The **kwargs** are the inputs of optimize-method. These are the values which are needed to calculate the fitness. Maybe you can calculate the fitness without them, depending on what you are optimizing.<br>The **list of parameters** are the gene/the solution, so the parameters which are changed and optimized.<br>The **get_random_value** method return a random value for a given parameter, so that the solutions can be initialized and mutated.\r\n\r\n\r\n\r\n### Examples\r\n- <a href=\"https://github.com/xXAI-botXx/Genetic-Algorithm/blob/main/example.ipynb\">Regression with RandomForrestRegressor</a>\r\n- <a href=\"https://github.com/xXAI-botXx/Genetic-Algorithm/blob/main/example_2.ipynb\">Knapsack problem</a>\r\n\r\n<!--\r\n<div style=\"border: 1px solid black; padding: 10px;\">\r\n <iframe src=\"example.html\" style=\"width:100%; height:400px;\"></iframe>\r\n</div> \r\n-->\r\n\r\n<!--\r\n<div style=\"border: 1px solid black; padding: 10px;\">\r\n <iframe src=\"example_2.html\" style=\"width:100%; height:400px;\"></iframe>\r\n</div>\r\n-->\r\n\r\n### License\r\n\r\nFeel free to use it. Of course you don't have to name me in your code :)\r\n\r\n-> It is a copy-left license\r\n\r\nFor all details see the license file.\r\n\r\n\r\n\r\n\r\n\r\n",
"bugtrack_url": null,
"license": "MPL-2.0",
"summary": "Genetic Algorithm Framework",
"version": "0.1.9.5",
"project_urls": {
"Download": "https://github.com/xXAI-botXx/Genetic-Algorithm/archive/v_01.tar.gz",
"Homepage": "https://github.com/xXAI-botXx/Genetic-Algorithm"
},
"split_keywords": [
"optimization",
"genetic-algorithm",
"hyperparameter-tuning"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8aea01d1e9cd51855cd81de991fff815d86d228a6e3879c1608286d16db40fcd",
"md5": "79220bf656bd15b3486b28b2ec0c6c0d",
"sha256": "ba64d1bd339b4eb8120791b7e30f47e2923b370ad55b5f24159baef15c092790"
},
"downloads": -1,
"filename": "Simple_Genetic_Algorithm-0.1.9.5.tar.gz",
"has_sig": false,
"md5_digest": "79220bf656bd15b3486b28b2ec0c6c0d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 12496,
"upload_time": "2024-02-11T12:08:56",
"upload_time_iso_8601": "2024-02-11T12:08:56.659203Z",
"url": "https://files.pythonhosted.org/packages/8a/ea/01d1e9cd51855cd81de991fff815d86d228a6e3879c1608286d16db40fcd/Simple_Genetic_Algorithm-0.1.9.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-11 12:08:56",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "xXAI-botXx",
"github_project": "Genetic-Algorithm",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "simple-genetic-algorithm"
}