Metadata-Version: 2.1
Name: Simple_Genetic_Algorithm
Version: 0.1.7
Summary: Genetic Algorithm Framework
Home-page: https://github.com/xXAI-botXx/Genetic-Algorithm
Download-URL: https://github.com/xXAI-botXx/Genetic-Algorithm/archive/v_01.tar.gz
Author: Tobia Ippolito
License: MPL-2.0
Keywords: Optimization,Genetic-Algorithm,Hyperparameter-Tuning
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Build Tools
Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Description-Content-Type: text/markdown
License-File: License.txt
Requires-Dist: joblib

# 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_01/logo.jpeg"></img>



### Usage

1. Download the project

2. Import the class and helper function

   ``````python
   import sys
   sys.path.insert(0, '../path_to_GA_py_dir')
   # ./ => this folder
   # ../ => supfolder
   
   from genetic_algorithm import GA, get_random
   ``````

3. 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"]
   ``````

4. Create and run genetic algorithm and pass the input, which will be used in the calculate_fitness function

   ``````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)
   ``````



For more examples, see:
- [Regression with RandomForrestRegressor](./example.ipynb)
- [Knapsack problem](./example_2.ipynb)




### 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.







