Giter VIP home page Giter VIP logo

genx's Introduction

genx

crb dcb build size downloads license

genx provides modular building blocks to run simulations of optimization and search problems using Genetic Algorithms (GA).

The vision for genx is to be a flexible and greatly extensible library for implementing genetic algorithm applications. genx is written in Rust. The library's API utilizes functional programming paradigm and exposes it's API in that manner only.

The implementation is split into building blocks which are all represented by traits. This crate provides most common and probably all possible implementation for all building blocks. So it can be used for many problems out of the box.

Documentation

Basic Example

Selection

Here's a trivial example that returns back individual selected using stochastic universal sampling

use genx::selection::stochastic_universal::stochastic_universal_selection;

let num_parents:usize = 10;
let fitness_values = vec![2.4,8.4,3.2,9.4,9.0,11.0,4.5,0.6,4.4,2.3,5.6,10.0,0.2,9.0,4.8,7.7];

let result = stochastic_universal_selection(&fitness_values, num_parents, None)
                .iter()
                .map(|&a| fitness_values[a])
                .collect::<Vec<f32>>();

stochastic_universal_selection takes in the fitness_value vector, number of parents it needs to select and a seed value which is Option<u64>. It returns back the indices of selected individuals which we map to actual fitness values.

Mutation

Mutation function takes in a single individual, distribution index and returns in the mutated individual using polynomial mutation for real valued individual.

use genx::mutation::polynomial::polynomial_mutation;
let individual = 29.11;
let result = polynomial_mutation(individual, 4.2, 4.0, None);

The returned value may or may not be equal as is mutated based on a randomly generated value, which for deterministic results can be seeded.

Building Blocks

The genetic algorithm needs a population that it evolves with each iteration. A population contains a number of individuals. Each individual represents a possible candidate solution for an optimization problem for which the best solution is searched for.

A Genetic Algorithm proceeds through following operations:

  • Encoding: Binary, Real Values, Order, Tree, etc.
  • Selection: Selecting individuals after fitness evaluation.
  • Crossover: Creating new individuals from selected pool of individuals.
  • Mutation: Making stark changes in generated individual for diversification.
  • Convergence: Test for goal accomplishment or convergence.

The building blocks currently available (defined as traits) are:

  • Selection
  • Mutation

This crate provides multiple implementations for each one of those operators. So one can experiment with combining the different implementations to compose the best algorithm for a specific search or optimization problem.

Usage

Add this to your Cargo.toml:

[dependencies]
genx = "0.4.0"

If you are not using Rust 2018 edition add this to your crate root:

extern crate genx;

Why Genetic Algorithms

Genetic Algorithms are at the core of soft computing which is a branch of computing that comes to rescue when problem at hand is not feasible to be solved using hard computing. There are several advantages of genetic algorithms:

  • Algorithms are adaptive and can adjust to the change of dynamic environment​
  • The approach makes use of approximate solutions to solve problems that may be either unsolvable or too time-consuming to solve with current hardware.​
  • Imprecise but usable solutions to complex computational problems allowing researchers to approach some problems that traditional computing cannot process.​

Inspiration

I started this project mainly to learn about genetic algorithms (GAs) as part of my college curriculum where am studying severals methods for soft computing. I found myself looking for simple easy to use modular functions that I can use to learn more about each step of GA.

genx's People

Contributors

arpitshukia avatar king-11 avatar rivalq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

genx's Issues

Example GA Implementation

Binary Encoding

  • Knapsack
  • High Utility Items Mining

Order Encoding

  • TSP
  • CPU Scheduling

Real Encoding

  • 2-degree polynomial
  • 3-degree polynomial
  • Weights Neural Network

Tree Encoding

  • Finding a function from given input and output values

Fix Linting issues

Is your feature request related to a problem? Please describe.
Genx currently has many linting issues those when fixed will allow for better readability of code and also improve its maintainability.

Describe the solution you'd like

rustup self update
rustup component add clippy
cargo clippy

Run the following command currently there are over/around 51 suggestions which we can improve upon.

Describe alternatives you've considered
I haven't considered how we can make linting a part of the CI process.

Crossover Methods

Add in the following crossover methods

Binary Encoding

  • Single Point Crossover
  • Two Point Crossover
  • Multi Point Crossover
  • Shuffle Crossover
  • Uniform Crossover
  • Uniform Crossover with Mask
  • Half Uniform Crossover
  • Three Parent Crossover
  • Matrix Crossover

Order Encoding:

  • Davis' Order Crossover
  • Partially-Mapped Crossover (PMX)
  • Cycle Crossover Operator (CX)

Value Encoding:

  • Linear Crossover
  • Blend Crossover
  • Simulated Binary Crossover

Reference

Optimize Selection Methods

Improve time complexity and memory requirements of all the selection methods.

Some know improvements include:

  • Use prefix array and binary search for roulette Wheel and rank based selection.

Precision Error in Stochastic Universal Selection

At a larger population size (~1000), I encountered out of bound panic. So i decided to find the bug behind that.

for _ in 0..num_parents {
    while fitness_scale[current_offset] < random_offset {
      current_offset += 1;
    }
    selected_indices.push(current_offset);
    random_offset += fitness_step;
 };

I found this code suspicious behind the panic. By printing some values i found out that random_offset was facing precision issues. at ith iteration, random_offset should be equal to initial_random_value + i*fitness_step but it turns to be greater than that.

I propose we should use f64 in our codebase.

Refactor Module Schema

Add in re-exports so that users can use the API and have a better experience without making long use chains

Documenation for Crossover Methods

Binary Encoding

  • Single Point Crossover
  • Multi Point Crossover
  • Shuffle Crossover
  • Uniform Crossover
  • Uniform Crossover with Mask
  • Half Uniform Crossover
  • Three Parent Crossover
  • Matrix Crossover

Order Encoding:

  • Davis' Order Crossover
  • Partially-Mapped Crossover (PMX)
  • Cycle Crossover Operator (CX)

Value Encoding:

  • Linear Crossover
  • Blend Crossover
  • Simulated Binary Crossover

References

Zero Fitness Value

Describe the bug
If the fitness value of an individual is 0 many of the selection methods will fail to run with that individual. Also, there is no proper error message as to how to debug the issue. The selection methods that will fail are:

  • rank_selection
  • roulette_wheel_selection
  • stochastic_universal_selection

To Reproduce
Pass in members with fitness value zero to any of the selection methods cited above

Expected behaviour
They should at least print out a proper error message or workout the issue by adding in a value (method 1 is preferred)

Desktop (please complete the following information):

  • OS: Manajro Orka
  • Version" 0.3.3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.