Code Monkey home page Code Monkey logo

pyheuristics's Introduction

Pyheuristics logo

PyHeuristics

A toolkit for MetaHeuristics implemented in Python


PyHeuristics is a Metaheuristics toolkit, which facilitates the use of various metaheuristic algorithms, such as Simulated Annealing or Ant Colony Optimization among others.

How to Install

TODO

Basic Usage

Using the various abstractions of PyHeuristics, it is really easy to solve an optimization problem.

Using the various abstractions of PyHeuristics, it is really easy to solve an optimization problem. For example, we will show how to find the maximum of this expression using the Simulated Annealing algorithm.

$$f(x) = x^3 - 60x^2 + 900x + 100$$

First of all import SimulatedAnnealing and CoolingSchedule. CoolingSchedule is a class that regulated the temperature of the annealing process.

from pyheuristics.trajectory.annealing.cooling_schedules import GeometricCoolingSchedule
from pyheuristics.trajectory.annealing.simulated_annealing import SimulatedAnnealing

Then, implement a class of the problem that must implement the fn and the move methods.

class MaximizeSimpleFunction(SimulatedAnnealing):
    def fn(self, sol: Any) -> float:
        binary_to_decimal = int(sol, 2)
        return -(
            binary_to_decimal**3
            - 60 * (binary_to_decimal**2)
            + 900 * binary_to_decimal
            + 100
        )

    def move(self) -> Any:
        bit_to_flip = random.randint(0, 4)
        _list = list(self.sol)

        if self.sol[bit_to_flip] == "1":
            _list[bit_to_flip] = "0"
        else:
            _list[bit_to_flip] = "1"

        return "".join(_list)

Finally, run the algorithm to search for the maximum value.

cs = GeometricCoolingSchedule(initial_temp=25000.0, final_temp=2.5, alpha=0.9)
optim_problem = MaximizeSimpleFunction(init_sol="10011", cooling_schedule=cs)
execution_result = optim_problem.run()

print(execution_result.sol) # "01010"
print(execution_result.cost) # 4100

pyheuristics's People

Contributors

michaelistrofficus avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.