Code Monkey home page Code Monkey logo

optimal's Introduction

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)
  • Population-based incremental learning (PBIL)

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.

Breaking Changes

09/26/2017

Renamed helpers.binary_to_int offset option to lower_bound, and renamed helpers.binary_to_float minimum and maximum options to lower_bound and upper_bound respectively.

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.

optimal's People

Contributors

justinlovinger 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

optimal's Issues

The content here may need to be corrected

File "F:.........\optimal\algorithms\genalg.py", line 32, in
from optimal import gaoperators, optimize, common

File "F:..........\optimal\optimize.py", line 57
print 'Iteration: %s\nAvg Fitness: %s\nBest Fitness: %s' % (
^
SyntaxError: invalid syntax

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.