Skip to main content

Genetic Algorithm Framework

Project description

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.

Usage

  1. Get the code

    • Download the project and add to python module search path in your code

      import sys
      sys.path.insert(0, '../path_to_GA_py_dir')
      # ./ => this folder
      # ../ => supfolder
      
    • Or pip install it (easier)

      pip install Simple-Genetic-Algorithm
      
  2. Import the class and helper function

    from genetic_algorithm import GA, get_random
    
  3. Create 2 functions and parameters

    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 (in kwargs variable)

    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:
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.
The list of parameters are the gene/the solution, so the parameters which are changed and optimized.
The get_random_value method return a random value for a given parameter, so that the solutions can be initialized and mutated.

For more examples, see:

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Simple_Genetic_Algorithm-0.1.8.tar.gz (12.3 kB view details)

Uploaded Source

File details

Details for the file Simple_Genetic_Algorithm-0.1.8.tar.gz.

File metadata

  • Download URL: Simple_Genetic_Algorithm-0.1.8.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for Simple_Genetic_Algorithm-0.1.8.tar.gz
Algorithm Hash digest
SHA256 abde56330c902c31a35c9a299c4b0f55cf3b2557fbe27ee6fbb0b6ce12098459
MD5 2b5dd8108b38dd546d07c0e848e0f9d7
BLAKE2b-256 49fe3656c98fa4b9024a57f4531e427ff40690eaa2855a66c7dc411e62d95244

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page