[PYTHON] Getting started on how to solve linear programming problems with PuLP

What is PuLP

A Python package that solves linear programming problems. https://code.google.com/p/pulp-or/ https://pythonhosted.org/PuLP/index.html

A linear programming problem is an optimization problem in which an objective function and constraints are expressed by a linear expression. For example

Is one of the linear programming problems.

Since it was introduced at PyConJP 2014, when I investigated how to use it, It's pretty easy to use, so I summarized it.

Installation method

sudo pip install pulp

procedure

Create a linear programming problem object

This expresses all the problems. For the constructor, choose the name of the problem and whether to minimize or maximize the objective function.

import pulp
problem = pulp.LpProblem('Problem Name', pulp.LpMinimize) #When minimizing
problem = pulp.LpProblem('Problem Name', pulp.LpMaximize) #When maximizing

Create a variable object

Create variables for use in linear programming. There are two ways to make it. You will often use 2 when you have a large number of variables.

(1) Make one by one

pulp.LpVariable('name', 0, 1, 'Continuous') 

Variables are in order from the front

  1. Variable name
  2. Minimum value of variable
  3. Maximum value of variable
  4. Variable type. 'Continuous' for floats,'Integer' for integers,'Binary' for binary variables

With this code, you can generate a variable with a variable name that takes a continuous value from 0 to 1.

(2) Pass the list and make it all together

var = pulp.LpVariable.dicts('VAR', ([1,2,3], ['a', 'b']), 0, 1, 'Continuous')

Variables are in order from the front

  1. Variable name prefix
  2. Tuple of a list of postfix variable names
  3. Minimum value of variable
  4. Maximum value of variable
  5. Variable type

3-5 is the same as before.

When this code is executed, it takes continuous values with a minimum value of 0 and a maximum value of 1, You can get a dictionary with the variables VAR_1_a, VAR_1_b, VAR_2_a, .... The access method is var [1] ['a'] like this. This will be used in a future article.

Create an objective function

Create an expression using variables and add it to the Mathematical Planning Problem object ** This is amazing.

For example, if you want to maximize the sum of ʻaandb`

import pulp

problem = pulp.LpProblem('test', pulp.LpMinimize)
a = pulp.LpVariable('a', 0, 1)
b = pulp.LpVariable('b', 0, 1)
problem += a + b

If you print the problem in this state, the problem will be displayed.

test:
MINIMIZE
1*a + 1*b + 0
VARIABLES
a <= 1 Continuous
b <= 1 Continuous

Create constraints

Make a comparison using variables and ** add it to a mathematical planning problem object ** This is also great.

For example, for the variables ʻaandb`

  1. ʻa` is greater than or equal to 0
  2. b is 0.1 or more
  3. The sum of ʻaandb` is 0.5 When you want to create a constraint
problem += a >= 0
problem += b >= 0.1
problem += a + b == 0.5

Only the anti-inequality sign (<=, =>) could be used.

solve

problem.solve()

only this.

Depending on the constraints, it may not be possible to solve it, so if you want to know the result,

status = problem.solve()
print pulp.LpStatus[status]

OK if Optimal appears in

Get the result.

You can get the value by calling the value method of the variable added to the problem.

Summary

import pulp

problem = pulp.LpProblem('sample', pulp.LpMinimize)

a = pulp.LpVariable('a', 0, 1)
b = pulp.LpVariable('b', 0, 1)

problem += a + b

problem += a >= 0
problem += b >= 0.1
problem += a + b == 0.5

status = problem.solve()
print "Status", pulp.LpStatus[status]

print problem

print "Result"
print "a", a.value()
print "b", b.value()

Result is,

Status Optimal
sample:
MINIMIZE
1*a + 1*b + 0
SUBJECT TO
_C1: a >= 0

_C2: b >= 0.1

_C3: a + b = 0.5

VARIABLES
a <= 1 Continuous
b <= 1 Continuous

Result
a 0.4
b 0.1

Recommended Posts

Getting started on how to solve linear programming problems with PuLP
Linear Programming with PuLP
How to get started with Scrapy
How to get started with Django
Getting started with USD on Windows
Getting started with Python 3.8 on Windows
How to solve dynamic programming algorithm problems (as seen by beginners)
How to write offline real time Solve F01 problems with Python
How to get started with laravel (Linux)
Getting started with Python with 100 knocks on language processing
How to enjoy programming with Minecraft (Ruby, Python)
Strategy on how to monetize with Python Java
How to install OpenGM on OSX with macports
Materials to read when getting started with Python
Try to solve the programming challenge book with python3
1.1 Getting Started with Python
Getting Started with pandas: Basic Knowledge to Remember First
Getting Started with Golang 2
How to install caffe on OS X with macports
Getting started with apache2
Getting Started with Python
Getting Started with Django 1
Getting Started with Optimization
Solve Sudoku with PuLP
Getting Started with Tensorflow-About Linear Regression Hypothesis and Cost
Getting Started with Golang 3
Autoencoder with Chainer (Notes on how to use + trainer)
Getting Started with Numpy
Getting started with Spark
Materials to read when getting started with Apache Beam
Getting Started with Python
How to enjoy Python on Android !! Programming on the go !!
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
How to operate Firefox with selenium on Windows Memo
Getting Started with Django 2
[Cyberduck] How to exchange files on Linux (CentOS7) started by VirtualBox with mac using GUI
Useful operation when you want to solve all problems in multiple programming languages with Codewars
How to install Python2.7 python3.5 with pyenv (on RHEL5 CentOS5) (2016 Nov)
I wanted to solve the Panasonic Programming Contest 2020 with Python
Getting Started with Mathematics Starting with Python Programming Challenges Personal Notes-Problem 1-1
13th Offline Real-time How to Solve Writing Problems in Python
How to install Theano on Mac OS X with homebrew
How to write offline real-time Solving E05 problems with Python
Translate Getting Started With TensorFlow
Getting Started with Python Functions
Getting Started with Tkinter 2: Buttons
Getting Started with Go Assembly
Linear programming + hands-on of pulp
How to register on pypi
Getting Started with Python Django (4)
How to update with SQLAlchemy?
Getting Started with Python Django (3)
How to cast with Theano
How to Alter with SQLAlchemy?
How to separate strings with','
Getting Started with Python Django (6)
Getting Started with Django with PyCharm
How to RDP with Fedora31
Python3 | Getting Started with numpy