Skip to main content

A python Non-Linear Programming API with Heuristic approach

Project description

flopt

A python Non-Linear Programming API with Heuristic approach.

Documentation Status PyPI version PyPI - Python Version License: MIT

docs | tutorial


Install

PyPI

pip install flopt

GitHub

git clone https://github.com/flab-coder/flopt.git

Formulatable problems in flopt

  • Linear problem

  • 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)


Heuristic Algorithms

  • Random Search
  • 2-Opt
  • Swarm Intelligence Search
  • Other applications

Simple Example

You can write codes like PuLP application.

from flopt import Variable, Problem, Solver

# Variables
a = Variable('a', lowBound=0, upBound=1, cat='Integer')
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

# Solver
solver = Solver(algo='ScipySearch')  # select the heuristic algorithm
solver.setParams(n_trial=1000)  # setting of the hyper parameters
prob.solve(solver, 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')
c = Variable('c', upBound=3, cat='Continuous')

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

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

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

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

from flopt import Variable, Problem, 

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

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

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

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.4.tar.gz (48.4 kB view hashes)

Uploaded Source

Built Distribution

flopt-0.4-py3-none-any.whl (2.9 MB view hashes)

Uploaded Python 3

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