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.
Metadata
Release files for optimal 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| optimal-0.2.0.zip | 49.1 kB | Without provenance | Details for optimal-0.2.0.zip |
Release files / optimal-0.2.0.zip
| Download URL | optimal-0.2.0.zip |
|---|---|
| Size | 49.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a99324b2b8a34cd912dda9729c2cd4d2830cd0b62bade475ce40d84ed3764ba7
|
|
BLAKE2b-256 checksum How to use checksums |
4a887dc0dc4673d2b2496ad24e76ed68f6d713a496da22f67ed0e1b483164631
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for optimal 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.