Skip to main content

A python Non-Linear Programming API with Heuristic approach

Project description

flopt

A Python Flexible Modeler for Optimization Problems.

flopt is a modeling tool for optimization problems such as LP, QP, Ising, QUBO, etc. flopt provides various functions for flexible and easy modeling. Users can also solve modeled problems with several solvers to obtain optimal or good solutions.

Documentation Status PyPI version PyPI - Python Version License: MIT

documentation | tutorial | case studies


Install

PyPI

pip install flopt

GitHub

git clone https://github.com/flab-coder/flopt.git
cd flopt && python -m pip install .

Formulatable problems in flopt

  • Linear Programming (LP)
  • Quadratic Programming (QP)
  • Ising
  • Quadratic Unconstrainted Binary Programming (QUBO)
  • Non-Linear problem
    minimize  2*(3*a+b)*c**2 + 3
    s.t       a + b * c <= 3
              0 <= a <= 1
              1 <= b <= 2
                   c <= 3
    
  • BlackBox problem
    minimize  simulator(a, b, c)
    s.t       0 <= a <= 1
              1 <= b <= 2
              1 <= c <= 3
    
  • Finding the best permutation problem (including TSP)
  • Satisfiability problem (including MAX-SAT)

Available Solvers and Heuristic Algorithms

  • CBC, CVXOPT, scipy.optimize(minimize, linprog, milp), Optuna
  • Random Search, 2-Opt, Swarm Intelligence Search

Simple Example

You can write codes like PuLP application.

from flopt import Variable, Problem

# Variables
a = Variable('a', lowBound=0, upBound=1, cat='Continuous')
b = Variable('b', lowBound=1, upBound=2, cat='Continuous')
c = Variable('c', upBound=3, cat='Continuous')

# Problem
prob = Problem()
prob += 2 * (3*a+b) * c**2 + 3 # set the objective function
prob += a + b * c <= 3         # set the constraint

# Solve
prob.solve(timelimit=0.5, msg=True) # run solver to solve the problem

# display the result, incumbent solution
print('obj value', prob.getObjectiveValue())
print('a', a.value())
print('b', b.value())
print('c', c.value())

In addition, you can represent any objective function by CustomExpression

from flopt import Variable, Problem, CustomExpression

# Variables
a = Variable('a', lowBound=0, upBound=1, cat='Integer')
b = Variable('b', lowBound=1, upBound=2, cat='Continuous')

def user_func(a, b):
    from math import sin, cos
    return (0.7*a + 0.3*cos(b)**2 + 0.1*sin(b))*abs(a)

custom_obj = CustomExpression(func=user_func, args=[a, b])

prob = Problem(name='CustomExpression')
prob += custom_obj

# Solve
prob.solve(timelimit=1, msg=True)  # run solver to solve the problem

# display the result, incumbent solution
print('obj value', prob.getObjectiveValue())

In the case you solve TSP, Permutation Variable is useful.

from flopt import Variable, Problem, CustomExpression

N = 4  # Number of city
D = [[0,1,2,3],  # Distance matrix
     [3,0,2,1],
     [1,2,0,3],
     [2,3,1,0]]

# Variables
x = Variable('x', lowBound=0, upBound=N-1, cat='Permutation')

# Object
def tsp_dist(x):
    distance = 0
    for head, tail in zip(x, x[1:]+[x[0]]):
        distance += D[head][tail]  # D is the distance matrix
    return distance
tsp_obj = CustomExpression(func=tsp_dist, args=[x])

# Problem
prob = Problem(name='TSP')
prob += tsp_obj

# Solve
prob.solve(timelimit=10, msg=True)    # run solver to solve the problem

# display the result, incumbent solution
print('obj value', prob.getObjectiveValue())
print('x', x.value())

Learning more

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

flopt-0.5.6.tar.gz (79.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flopt-0.5.6-py3-none-any.whl (114.4 kB view details)

Uploaded Python 3

File details

Details for the file flopt-0.5.6.tar.gz.

File metadata

  • Download URL: flopt-0.5.6.tar.gz
  • Upload date:
  • Size: 79.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for flopt-0.5.6.tar.gz
Algorithm Hash digest
SHA256 b701fa9f54a83f32f678d316869827e43df2c779d1eec0032fa73c5fe07a9c36
MD5 ec8afec6ff5ebe163006a1187eeb56d0
BLAKE2b-256 4eb2bfac1e099727669ac061391b03e89cfcc853c77f3cbb2a61710af299093a

See more details on using hashes here.

File details

Details for the file flopt-0.5.6-py3-none-any.whl.

File metadata

  • Download URL: flopt-0.5.6-py3-none-any.whl
  • Upload date:
  • Size: 114.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for flopt-0.5.6-py3-none-any.whl
Algorithm Hash digest
SHA256 3383a692b0c4f471b789249c4145b22519aab8d95841f9f46a12ae6a93f84e90
MD5 6c200e001af2069e8139c7637c047675
BLAKE2b-256 c0ea57dbab6084b9b9b38817f1f1a44920825a56c337f29bd56bb52d4c0a1782

See more details on using hashes here.

Supported by

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