线性规划(Simplex LP)PuLP? [英] Linear Programming (Simplex LP) PuLP?

查看:953
本文介绍了线性规划(Simplex LP)PuLP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,并使用Pandas数据框中的数据,如何使用

解决方案

一般来说,您创建一个变量字典(在这种情况下为 x )和一个模型变量( mod 在这种情况下)。要创建目标,您可以使用$ code> mod 将变量乘以一些标量,将该结果添加到 mod 。您通过再次计算变量的线性组合来构造约束,使用> = < = == ,并将该约束添加到 mod 。最后,您使用 mod.solve()来获取解决方案。

 进口纸

#创建变量和模型
x = pulp.LpVariable.dicts(x,df.index,lowBound = 0)
mod = pulp.LpProblem(预算,pulp.LpMaximize)

#目标函数
objvals = {idx:(1.0 /(df ['30 -day Cost'] [idx] / df ['Trials'] [ idx]))*(df ['Success'] [idx] / float(df ['Trials'] [idx]))df.index中的idx}
mod + = sum([x [idx]对于df.index中的idx,objvals [idx]]

#df.index中的idx的

mod + = x [idx]> ; = df ['Cost Min'] [idx]
mod + = x [idx] <= df ['Cost Max'] [idx]

#预算金额
mod + = sum(df.index中idx的[x [idx]])== 5000.0

#解析模型
mod.solve()

#输出解决方案
在df.index中的idx:
打印idx,x [idx] .value()
#0 2570.0
#1 1350.0
#2 1080.0

print'Objective',pulp.value(mod.objective)
#Objecti ve 1798.70495012

数据:

  import numpy as np 
import pandas as pd
idx = [0,1,2]
d = {'channel':pd.Series(['Channel1' ,'Channel2','Channel3'],index = idx),
'30 -day Cost':pd.Series([1765.21,2700.,2160.],index = idx),
'试验':pd.Series([9865,1500,1200],index = idx),
'Success':pd.Series([812,900,333],index = idx),
'成本最小值:pd.Series([882.61,1350.00,1080.00],index = idx),
'Cost Max':pd.Series([2647.82,4050.00,3240.00],index = idx)}
df = pd.DataFrame(d)
df
#30天成本成本最高成本最小成功试用频道
#0 1765.21 2647.82 882.61 812 9865频道1
#1 2700.00 4050.00 1350.00 900 1500 Channel2
#2 2160.00 3240.00 1080.00 333 1200 Channel3


In Python only, and using data from a Pandas dataframe, how can I use PuLP to solve linear programming problems the same way I can in Excel? How much budget should be allocated to each Channel under the New Budget column so we maximize the total number of estimated successes? I'm really looking for a concrete example using data from a dataframe and not really high-level advice.

Problem Data Setup

    Channel  30-day Cost  Trials  Success  Cost Min  Cost Max  New Budget
0  Channel1      1765.21    9865      812    882.61   2647.82           0
1  Channel2      2700.00   15000      900   1350.00   4050.00           0
2  Channel3      2160.00   12000      333   1080.00   3240.00           0

This is a Maximization problem.

The objective function is:

objective_function = sum((df['New Budget']/(df['30-day Cost']/df['Trials']))*(df['Success']/df['Trials']))

The constraints are:

  1. The sum of df['New Budget'] must equal 5000
  2. The New Budget for a given channel can go no lower than the Cost Min
  3. The New Budget for a given channel can go no higher than the Cost Max

Any ideas how to translate this pandas dataframe solver linear problem using PuLP or any other solver approach? The end-result would be what you see in the image below.

解决方案

In general you create a dictionary of variables (x in this case) and a model variable (mod in this case). To create the objective you use sum over the variables times some scalars, adding that result to mod. You construct constraints by again computing linear combinations of variables, using >=, <=, or ==, and adding that constraint to mod. Finally you use mod.solve() to get the solutions.

import pulp

# Create variables and model
x = pulp.LpVariable.dicts("x", df.index, lowBound=0)
mod = pulp.LpProblem("Budget", pulp.LpMaximize)

# Objective function
objvals = {idx: (1.0/(df['30-day Cost'][idx]/df['Trials'][idx]))*(df['Success'][idx]/float(df['Trials'][idx])) for idx in df.index}
mod += sum([x[idx]*objvals[idx] for idx in df.index])

# Lower and upper bounds:
for idx in df.index:
    mod += x[idx] >= df['Cost Min'][idx]
    mod += x[idx] <= df['Cost Max'][idx]

# Budget sum
mod += sum([x[idx] for idx in df.index]) == 5000.0

# Solve model
mod.solve()

# Output solution
for idx in df.index:
    print idx, x[idx].value()
# 0 2570.0
# 1 1350.0
# 2 1080.0

print 'Objective', pulp.value(mod.objective)
# Objective 1798.70495012

Data:

import numpy as np
import pandas as pd
idx = [0, 1, 2]
d = {'channel': pd.Series(['Channel1', 'Channel2', 'Channel3'], index=idx),
     '30-day Cost': pd.Series([1765.21, 2700., 2160.], index=idx),
     'Trials': pd.Series([9865, 1500, 1200], index=idx),
     'Success': pd.Series([812, 900, 333], index=idx),
     'Cost Min': pd.Series([882.61, 1350.00, 1080.00], index=idx),
     'Cost Max': pd.Series([2647.82, 4050.00, 3240.00], index=idx)}
df = pd.DataFrame(d)
df
#    30-day Cost  Cost Max  Cost Min  Success  Trials   channel
# 0      1765.21   2647.82    882.61      812    9865  Channel1
# 1      2700.00   4050.00   1350.00      900    1500  Channel2
# 2      2160.00   3240.00   1080.00      333    1200  Channel3

这篇关于线性规划(Simplex LP)PuLP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆