Skip to main content

A python metaheuristic optimization library. Currently supports Genetic Algorithms, Gravitational Search, and Cross Entropy.

Project description

Optimal (beta)

A python metaheuristic optimization library. Built for easy extension and usage.

Warning: Optimal is in beta. API may change. I will do my best to note any breaking changes in this readme, but no guarantee is given.

Supported metaheuristics:

  • Genetic algorithms (GA)

  • Gravitational search algorithm (GSA)

  • Cross entropy (CE)

Installation

pip install optimal

Usage

import math

from optimal import GenAlg
from optimal import Problem
from optimal import helpers

# The genetic algorithm uses binary solutions.
# A decode function is useful for converting the binary solution to real numbers
def decode_ackley(binary):
    # Helpful functions from helpers are used to convert binary to float
    # x1 and x2 range from -5.0 to 5.0
    x1 = helpers.binary_to_float(binary[0:16], -5.0, 5.0)
    x2 = helpers.binary_to_float(binary[16:32], -5.0, 5.0)
    return x1, x2

# ackley is our fitness function
# This is how a user defines the goal of their problem
def ackley_fitness(solution):
    x1, x2 = solution

    # Ackley's function
    # A common mathematical optimization problem
    output = -20 * math.exp(-0.2 * math.sqrt(0.5 * (x1**2 + x2**2))) - math.exp(
        0.5 * (math.cos(2 * math.pi * x1) + math.cos(2 * math.pi * x2))) + 20 + math.e

    # You can prematurely stop the metaheuristic by returning True
    # as the second return value
    # Here, we consider the problem solved if the output is <= 0.01
    finished = output <= 0.01

    # Because this function is trying to minimize the output,
    # a smaller output has a greater fitness
    fitness = 1 / output

    # First return argument must be a real number
    # The higher the number, the better the solution
    # Second return argument is a boolean, and optional
    return fitness, finished

# Define a problem instance to optimize
# We can optionally include a decode function
# The optimizer will pass the decoded solution into your fitness function
# Additional fitness function and decode function parameters can also be added
ackley = Problem(ackley_fitness, decode_function=decode_ackley)

# Create a genetic algorithm with a chromosome size of 32,
# and use it to solve our problem
my_genalg = GenAlg(32)
best_solution = my_genalg.optimize(ackley)

print best_solution

Important notes:

  • Fitness function must take solution as its first argument

  • Fitness function must return a real number as its first return value

For further usage details, see comprehensive doc strings.

Major Changes

08/27/2017

Moved a number of options from Optimizer to Optimizer.optimize

07/26/2017

Renamed common.random_solution_binary to common.random_binary_solution, and common.random_solution_real to common.random_real_solution

11/10/2016

problem now an argument of Optimizer.optimize, instead of Optimizer.__init__.

11/10/2016

max_iterations now an argument of Optimizer.optimize, instead of Optimizer.__init__.

11/8/2016

Optimizer now takes a problem instance, instead of a fitness function and kwargs.

11/5/2016

Library reorganized with greater reliance on __init__.py.

Optimizers can now be imported with:

from optimal import GenAlg, GSA, CrossEntropy

Etc.

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

optimal-0.2.0.zip (49.1 kB view details)

Uploaded Source

File details

Details for the file optimal-0.2.0.zip.

File metadata

  • Download URL: optimal-0.2.0.zip
  • Upload date:
  • Size: 49.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for optimal-0.2.0.zip
Algorithm Hash digest
SHA256 a99324b2b8a34cd912dda9729c2cd4d2830cd0b62bade475ce40d84ed3764ba7
MD5 8591f221fc2963716b25041846075928
BLAKE2b-256 4a887dc0dc4673d2b2496ad24e76ed68f6d713a496da22f67ed0e1b483164631

See more details on using hashes here.

Supported by

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