moga

compas.numerical.moga(fit_functions, fit_types, num_var, boundaries, num_gen=100, num_pop=100, mutation_probability=0.01, num_bin_dig=None, start_from_gen=False, fit_names=None, fargs=None, fkwargs=None, output_path=None)[source]

Multi-objective Genetic Algorithm optimisation.

Parameters
  • fit_functions (list) – List of functions to be used by the :class’MOGA’ to determine the fitness values. The function must have as a first argument a list of variables that determine the fitness value. Other arguments and keyword arguments can be used to feed the function relevant data.

  • fit_types (list) – List of strings that indicate if the fitness functions are to be minimized or maximized, “min” for minimization and “max” for maximization.

  • num_var (int) – The number of variables used by the fitness function.

  • boundaries (list) – The minimum and vaximum values each variable is allowed to have. Must be a num_var long list of tuples in the form [(min, max),…].

  • num_gen (int, optional [100]) – The maximum number of generations.

  • num_pop (int, optional [100]) – The number of individuals in the population. Must be an even number.

  • mutation_probability (float, optional [0.001]) – Float from 0 to 1. If 0 is used, none of the genes in each individuals chromosome will be mutated. If 1 is used, all of them will mutate.

  • num_bin_dig (list, optional [None]) – Number of genes used to codify each variable. Must be a num_var long list of intergers. If None is given, each variable will be coded with a 8 digit binary number, corresponding to 256 steps.

  • start_from_gen (int, optional [None]) – The generation number to restart a previous optimization process.

  • fit_names (list, optional [None]) – The names of the fitness functions. If None is given, the name of the fitness functions are used.

  • fargs (list, optional [None]) – Arguments fo be fed to the fitness function.

  • fkwargs (dict, optional [None]) – Keyword arguments to be fed to the fitness function.

  • output_path (str, optional [None]) – Path for the optimization result files.

Returns

moga (object) – The resulting :class’MOGA’ instance.

Notes

For more info, see 1.

References

1

Deb K., Multi-Objective Optimization using Evolutionary Algorithms, John Wiley & Sons, Chichester, 2001.

Examples

Zitzler-Deb-Thiele Test problem 3

import os
import compas
import math

def zdt3_f1(X, a):
    fit = X[0]
    return fit

def zdt3_f2(X, a):
    n = len(X)
    totX = 0
    for i in range(1, n):
        totX  = totX + X[i]
    G = 1 + (9 / (n - 1)) * totX
    H = 1 - math.sqrt(X[0] / G) - ((X[0] / G) * math.sin(10 * math.pi * X[0]))
    fit = G * H
    return fit

fit_functions = [zdt3_f1, zdt3_f2]
fit_types = ['min', 'min']
num_var = 30
boundaries = [(0, 1)] * num_var
num_bin_dig  = [8] * num_var
output_path = os.path.join(compas.TEMP, 'moga_out/')

if not os.path.exists(output_path):
    os.makedirs(output_path)

moga = moga(fit_functions,
            fit_types,
            num_var,
            boundaries,
            num_gen=100,
            num_pop=50,
            num_bin_dig=num_bin_dig,
            output_path=output_path)
================================================================================
MOGA summary
================================================================================

- fitness function name: ['zdt3_f1', 'zdt3_f2']

- fitnes function type : ['min', 'min']

- number of generations : 100

- number of individuals : 50

- number of variables : 30

- optimal individuals : [0, 1]

- optimal fitness values : [0.0, -0.7730627737349554]

================================================================================