Skip to content

Python implementation of the NSGA-II genetic algorithm.

Notifications You must be signed in to change notification settings

hugoaboud/nsga2-py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nsga2-py

NSGA-II stands for "Non Sorting Genetic Algorithm II", and it's a fast and elitist multiobjective GA.

This is a simple but very flexible implementation of the algorithm fundamentals.

Setup

TODO: make it a pip package

Usage

A complete example is available, you can run it with:

python3 example/2d_cluster.py

Training individuals

from nsga2 import Individual, NSGA2

class CustomIndividual(Individual):
    def _init_random(self):
        # ...
    def _init_from_parents(self, a, b):
        # ...
    def mutate(self):
        # ...
    def fitness(self):
        # ...

nsga2 = NSGA2(CustomIndividual, pop_size=100)
nsga2.train(epochs=100)

You can pass custom arguments for all individuals, such as a dataset.

class CustomIndividual(Individual):
    def fitness(self):
        do_something(self.dataset)

dataset = [1,2,3]

nsga2 = NSGA2(CustomIndividual, pop_size=100, dataset=dataset)

Genetic Parameter Helpers

These are useful on the Individual methods.

from nsga2.util.genetics import Genetics

Genetics.random_int(0,100)
Genetics.random_float(0,100)
from nsga2.util.genetics import Genetics

class Obj:
    def __init__(self, param):
        self.param = param

a = Obj(1)
b = Obj(2)

Genetics.random_select('param', a, b, prob_a=0.5)
from nsga2.util.genetics import Genetics

val = 10
mutate_int(val, prob=0.3, amp=1, prob_reset=0.01, range=(0,20))
from nsga2.util.genetics import Genetics

val = 12.34
mutate_float(val, prob=0.3, amp=1.0, prob_reset=0.01, range=(0,20))

Log & Plot

from nsga2.util.log import Log

Log.setup(logfile='experiment.log', level='INFO')

logging.debug('Some debug message...')
logging.info('Some info message...')
logging.warn('Some warn message...')
logging.error('Some error message...')
from nsga2.util.plot import Plot

Plot.fitnesses(nsga2)

Standalone Sorting

from nsga2.sort import non_dominated_sort

data = {
    'id0': [1,2,3],
    'id1': [1,2,3],
    'id2': [1,2,3]
}

non_dominated_sort(data)

# [{'id0':[1,2,3]},{'id1':[1,2,3],'id2':[1,2,3]}]

Selecting from fronts

from nsga2.select import select_n_best_of_front

select_n_best_of_front(front)
from nsga2.select import select_n_best

select_n_best(fronts)

About

Python implementation of the NSGA-II genetic algorithm.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages