Skip to main content

Multi-index Addon for PuLP

Project description

PuLP-MiA is an Multi-index Addon for PuLP. It was created to simplify working with multi-index variables when forming constraints and an objective function.

Installation

The easiest way to install pulp-mia is via PyPi

If pip is available on your system:

pip install pulp-mia

Otherwise follow the download instructions on the PyPi page.

PuLP-MiA requires:
  • Python >= 3.4

  • PuLP >= 1.6.10 (see PuLP README file for more details)

Examples

Use Task() to create new LP-task. To create with name MyTask and in debug mode use

task = Task(Name='MyTask', debug=True)

Debug mode updates the list of variables for any changes. Release mode updates ones only before solving a Lp-problem.

Use VariablesType flag to set type of variables

task = Task(VariablesType='Integer')     # Continuous, Integer, Boolean

Continuous is a default type.

To form the task use Constraint() class.

To create the Objective function (criterion) use

c = Constraint('MAX')     # 'MAX, 'MIN'

To set some coeffients on old or new variables use

c.setCoeff(('v'), 0.3)     # v-indicator with weight 0.3
c.setCoeff(('p'), 0.7)     # p-indicator with weight 0.7

To set an Objective function on the task use

task.setObjective(c)

To create some Constraints use

a = Constraint('<=')      # '<=', '==', '>='

To set some coeffients on old or new variables use

# 50x11 + 100x12 + 50x21 + 100x22 <= 200
a.setCoeff(('x', 1, 1), 50)
a.setCoeff(('x', 1, 2), 100)
a.setCoeff(('x', 2, 1), 50)
a.setCoeff(('x', 2, 2), 100)

To set a sum of line combine of all variables in a-constraint use

a.setBValue(200)

To set a Constraint on the task use

task.addConstraint(a)

Let’s create a Constraint with v-indicator and p-indicator use

a = Constraint('==')
a.setCoeff(('x', 1, 1), 1)
a.setCoeff(('x', 1, 2), 1)
a.setCoeff(('v'), -1)     # v = x11 + x12
task.addConstraint(a)

a = Constraint('==')
a.setCoeff(('x', 2, 1), 1)
a.setCoeff(('x', 2, 2), 1)
a.setCoeff(('p'), -1)     # p = x21 + x22
task.addConstraint(a)

To run auto-solver by PuLP and watch result use

from pprint import pprint

print(task)               # name & size of task
print(task.Plan)          # solve and show status & objective value
print('PLAN')
pprint(task.PDict)        # show the plan as a dict

Once solved Plan is here. By default here is no zero variables. To get full Plan use

pprint(task.Plan.getPDict(with_zeroe_values=True))

If you’ll make some modifications (new Objective or Constraints) - Plan will be lost.

Before solving you can get elements of task in matrix-like style

print('\nAMatrix')
pprint(task.AMatrix)
print('\nBVector')
pprint(task.BVector)
print('\nCVector')
pprint(task.CVector)

To see generated variables in task, status and value of Objective use

pprint(task._Variables)
print('\nStatus', task.Status)
print('\nObjective', task.PValue)

Finally, to get the generated pulp problem, use

prob = task.Prob

Now see how some Assignment problem can be solved by PuLP-MiA

from itertools import product
from pprint import pprint

from pulp_mia import Task, Constraint

# SETS
i_set = list(range(5))
j_set = list(range(2))

m_set = list(range(2))
g_set = list(range(4))
s_set = list(range(5))
k_set = list(range(5))

t_zad = 0.3
t_s_set = [0.05*(s + 1) for s in s_set]

G = [1, 0.5, 0.36, 0.5]
k_set_var = [0.5*(k + 1) for k in k_set]

def get_p(k, g):
    return 1/(G[g]/(1.7*k_set_var[k]) + 1)

alfa_p = 0.5
alfa_v = 0.5

# task
task = Task(debug=True)

# Objective
c_new = Constraint('MAX')
c_new.setCoeff(('v'), alfa_v/len(i_set))
for i, j, m, g, s, k in product(i_set, j_set, m_set, g_set, s_set, k_set):
    c_new.setCoeff(('x', i, j, m, g, s, k), alfa_p*get_p(k, g)/(1/(min(G)/(1.7*max(k_set_var)) + 1)*len(i_set)))
task.setObjective(c_new)

# Constraints
# Constraint 1
for i, m, g, s, k in product(i_set, m_set, g_set, s_set, k_set):
    a_new = Constraint('<=')
    for j in j_set:
        a_new.setCoeff(('x', i, j, m, g, s, k), 1)
    a_new.setBValue(1)
    task.addConstraint(a_new)

# Constraint 1.5
for i in i_set:
    a_new = Constraint('<=')
    for j, m, g, s, k in product(j_set, m_set, g_set, s_set, k_set):
        a_new.setCoeff(('x', i, j, m, g, s, k), 1)
    a_new.setBValue(1)
    task.addConstraint(a_new)

# Constraint 2
for i, j in product(i_set, j_set):
    a_new = Constraint('<=')
    for m, g, s, k in product(m_set, g_set, s_set, k_set):
        a_new.setCoeff(('x', i, j, m, g, s, k), 1)
    a_new.setBValue(1)
    task.addConstraint(a_new)

# Constraint 3
for j in j_set:
    a_new = Constraint('<=')
    for i, m, g, s, k in product(i_set, m_set, g_set, s_set, k_set):
        a_new.setCoeff(('x', i, j, m, g, s, k), t_s_set[s])
    a_new.setBValue(t_zad)
    task.addConstraint(a_new)

# Constraint 4
a_new = Constraint('==')
for i, j, m, g, s, k in product(i_set, j_set, m_set, g_set, s_set, k_set):
    a_new.setCoeff(('x', i, j, m, g, s, k), 1)
a_new.setCoeff(('v'), -1)
a_new.setBValue(0)
task.addConstraint(a_new)


print(task)
print(task.Plan)
print('PLAN')
pprint(task.PDict)

Copyright Dmitriy A. Pavlov (dpavlov239@mail.ru) under MIT license

See the LICENSE file for copyright information.

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

PuLP_MiA-0.1.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

PuLP_MiA-0.1.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file PuLP_MiA-0.1.1.tar.gz.

File metadata

  • Download URL: PuLP_MiA-0.1.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.7

File hashes

Hashes for PuLP_MiA-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c66ec156fa11d0e6871354ee26bd9ee5579708534e67d0b3dcabc7315f2c0856
MD5 c5691339c45dd95f68f65079f90cac66
BLAKE2b-256 27104dfa8094780c2de89e3fbed4fe70f9fa7bb93a41f3bf9eda97762a73ab59

See more details on using hashes here.

File details

Details for the file PuLP_MiA-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: PuLP_MiA-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.7

File hashes

Hashes for PuLP_MiA-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3ac42eaeda250e98d0ac129a2f1755d39ef4f69c5a033d68b50bbe48bdeb8c45
MD5 4cf9f7936266a0674ace9f26b00bb51d
BLAKE2b-256 1b781c1a5326d6d138b403d0e04baa4d60e944f45af03e271b2af3a4aa749744

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