Linear programming (LP; linear programming) is a mathematical programming method that maximizes a linear expression among the values of variables that satisfy several linear inequalities and linear programming. This is a method of finding the value to be minimized. The optimization problem that is the target of linear programming is called the linear programming problem. Source: [Wikipedia]
How to find the largest (smallest) objective variable by setting constraints with multiple linear inequalities and linear equations for the target variable
example1
table | Article A | Article B |
---|---|---|
Process 1 | 2 hours | 2 hours |
Process 2 | 3 hours | 5 hours |
Profit | 4,000 yen | 5,000 yen |
Library import
from pulp import *
Problem-solving source code
#Generate problem object
prob = LpProblem(name='Integer programming exapmle1',sense=LpMaximize)
#Variable setting
x1 = LpVariable('x1',lowBound=0)
x2 = LpVariable('x2',lowBound=0)
#Setting the objective variable
prob += 4*x1 + 5*x2
#Constraint setting
prob += 2*x1 + 2*x2 <= 7,'ineq1'
prob += 3*x1 + 5*x2 <= 14,'ineq2'
#Problem output
print('-------Problem information output-------')
print(prob)
#Find a solution
prob.solve()
#How was it solved
print('-------Solution information-------')
print(LpStatus[prob.status])
#Optimal value output
print('-------Optimal value output-------')
print('Optimal Value ={}'.format(value(prob.objective)))
#Optimal solution output
print('-------Optimal solution output-------')
for val in prob.variables():
print('{}={}'.format(val.name,value(val)))
-------Problem information output-------
Integer programming exapmle1:
MAXIMIZE
4*x1 + 5*x2 + 0
SUBJECT TO
ineq1: 2 x1 + 2 x2 <= 7
ineq2: 3 x1 + 5 x2 <= 14
VARIABLES
x1 Continuous
x2 Continuous
-------Solution information-------
Optimal
-------Optimal value output-------
Optimal Value =15.75
-------Optimal solution output-------
x1=1.75
x2=1.75
Recommended Posts