Pyhton中的单纯型LP值

发布于 2025-02-05 18:08:41 字数 1140 浏览 4 评论 0原文

使用python求解线性编程孔洞,目标是一个值:x

互联网你好,

我在试图在python中自动化的过程遇到

麻烦从Excel中找到 Simplex LP方法当时解决一个问题非常有用,但是我需要解决1000或更多问题,所以我真的想节省我的时间

纸浆,这是很好的纸浆但是,只有当您想最大化或最小化目标函数时,我需要将其均衡到一个值:的值,

所以问题是:

我需要将付款分为3个债务,但是我无法进行部分付款,

我认为我可以以目标(总产品)等于付款的方式将布尔(1或0)的债务乘以债务

,以便付款的钱只能偿还我可以完全覆盖

求解器的 债务像这样的作品

solver excel Input

,它提供了正确的方法来分割付款

solver excel输出

所以我需要为python做同样的事情,也许有一个秘密功能可以帮助我解决这个问题或Sympy或其他图书馆,IDK我非常感谢您的帮助

import pandas as pd

data = [['Debt1', 100, 400, True], ['Debt2', 200, 400, True], ['Debt3', 300, 400, True]]

# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['Debt', 'Amount', 'Payment', 'Boolean'])
df

    Debt    Amount  Payment Boolean
0   Debt1   100     400     True
1   Debt2   200     400     True
2   Debt3   300     400     True

Use Python for solving a Linear Programming porblem with the objective being a Value of: x

Hello people of internet

I'm having trouble with a process I'm trying to automatize in Python it´s a LP problem

I found Simplex LP method in Solver from Excel very useful to solve one problem at the time but I need to do it for 1000 or more problems so I really want to save me some time

PuLP is very good doing Solver stuff but only if you want to maximize or minimize the objective function and I need to equalize it to a VALUE OF:

So the problem is this one:

I need to divide the payment into 3 debts, but I cannot make partial payments

I figured I can multiply the debts times a boolean (1 or 0) in a way that the objective (a sum product) equalizes the payment

So the money from the payment would go only to the debts I can completely cover

Solver works like this

Solver Excel Input

And it gives the correct way to divide the payments

Solver Excel Output

So I need to do the same thing for Python Maybe there's a secret function PuLP has that can help me solve this or maybe Sympy or another library, idk I'd really appreciate your help

import pandas as pd

data = [['Debt1', 100, 400, True], ['Debt2', 200, 400, True], ['Debt3', 300, 400, True]]

# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['Debt', 'Amount', 'Payment', 'Boolean'])
df

    Debt    Amount  Payment Boolean
0   Debt1   100     400     True
1   Debt2   200     400     True
2   Debt3   300     400     True

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我心暖 2025-02-12 18:08:41

我想如何做,这是代码

bills = [100, 200, 300]
payment=400
c=0
ceros=[0]* len(rets) 
lista = [item for sublist in zip(ceros, bills) for item in sublist]+[0]      
##Divide payment
payment_div = [] 
for n in range(1, len(bills) + 1):  
    for combination in it.combinations(lista, len(bills)): 
        if c > 1000000: 
            break 
#Some times with different and many values in bills it'd end in infinite loop, break is for exit
            payment_div=[] 
        else: 
            c=c+1 
            if sum(combination) == payment: 
                payment_div.append(combination) 
#Payment div will have all the possible combinations, if empty then there's no possible combinations that equal the x value 
if not payment_div: 
    print("No possible combinations")
else: #This will find the best matching combination to the original bills list
    divi=set(payment_div) 
    divis=list(divi) 
    counts=[] 
    for i in range (len(divis)): 
        div=divis[i] 
        counti=sum(div == bills for div, bills in zip(div, bills)) 
        counts.append(counti) 
    maxx=max(counts) 
    maxind=[index for index, value in enumerate(counts) if value == maxx] 
    for n in range (len(maxind)): 
        op=divis[maxind[n]] 
print(op)

output: (100, 0, 300)

I figured how to do it, here's the code

bills = [100, 200, 300]
payment=400
c=0
ceros=[0]* len(rets) 
lista = [item for sublist in zip(ceros, bills) for item in sublist]+[0]      
##Divide payment
payment_div = [] 
for n in range(1, len(bills) + 1):  
    for combination in it.combinations(lista, len(bills)): 
        if c > 1000000: 
            break 
#Some times with different and many values in bills it'd end in infinite loop, break is for exit
            payment_div=[] 
        else: 
            c=c+1 
            if sum(combination) == payment: 
                payment_div.append(combination) 
#Payment div will have all the possible combinations, if empty then there's no possible combinations that equal the x value 
if not payment_div: 
    print("No possible combinations")
else: #This will find the best matching combination to the original bills list
    divi=set(payment_div) 
    divis=list(divi) 
    counts=[] 
    for i in range (len(divis)): 
        div=divis[i] 
        counti=sum(div == bills for div, bills in zip(div, bills)) 
        counts.append(counti) 
    maxx=max(counts) 
    maxind=[index for index, value in enumerate(counts) if value == maxx] 
    for n in range (len(maxind)): 
        op=divis[maxind[n]] 
print(op)

output: (100, 0, 300)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文