簡體   English   中英

PuLP 不適用於一個列表,但適用於另一個列表。 這兩者有什么區別?

[英]PuLP doesn't work with one list but works with another. What is the difference between these two?

我是 python 的新手,因為優化而選擇了它,我想學習一些新東西。 我使用 PyQt5 和 PuLP 制作了一個 lp 求解器程序。 這個概念很簡單:用戶在 QTextEdit 小部件中輸入 Lp 問題,然后單擊求解按鈕,然后在 QTextBrowser 中獲取結果。

我正在使用並嘗試復制的示例練習:

prob = LpProblem("LP problem", LpMaximize)
x1 = LpVariable("x1", lowBound=0, cat='Integer') # Integer variable x1 >= 0
x2 = LpVariable("x2", lowBound=0, cat='Integer') # Integer variable x2 >= 0

prob += x2<=4
prob += 4*x1 + 2*x2 <= 20
prob += 1*x1 + 4*x2 <= 12
prob += 4*x1 + 4*x2

prob.solve()

這種方式就像一個魅力。 按鈕的 function:

def textToVar(self):
    prob = LpProblem("LP problem", LpMaximize)
    x1 = LpVariable("x1", lowBound=0, cat='Integer') # Integer variable x1 >= 0
    x2 = LpVariable("x2", lowBound=0, cat='Integer') # Integer variable x2 >= 0
    mytext = self.lpInput.toPlainText()
    split = mytext.splitlines()

    for ele in range(0, len(split)):
        prob += split[ele]

    prob.solve()

    for v in prob.variables():
        self.lpOutput.append(str(v.name) + ' = ' + str(v.varValue))


    vmax = (value(prob.objective))
    self.lpOutput.append('max = ' + str(vmax))

它不起作用,我認為這是因為split = mytext.splitlines()生成['x2<=4', '4*x1+2*x2<=20', '1*x1+4*x2<=12', '4*x1+4*x2']而不是[x2<=4, 4*x1+2*x2<=20, 1*x1+4*x2<=12, 4*x1+4*x2] 如何將我的列表從第一個轉換為第二個? 也許我可以使用另一種方法將輸入存儲在列表或變量中,而不是splitlines()

先感謝您!

您可以使用exec() ,就像@AirSquid 在他們的評論中指出的那樣,但這確實會引發安全問題。 另一種方法是解析字符串,因為您知道它們將包含什么。 然后,如果出現意外情況,您很容易拋出錯誤。

import re
import pulp as pl

x1 = pl.LpVariable("x1", lowBound=0, cat='Integer') # Integer variable x1 >= 0
x2 = pl.LpVariable("x2", lowBound=0, cat='Integer') # Integer variable x2 >= 0

def parse_equation(string):
    string_parts = re.split("(<=|=|>=)", string)
    if len(string_parts) == 1:
        # Objective function
        return parse_equation_part(string_parts[0])
    if len(string_parts) != 3:
        raise Exception(f"Unexpected number of parts in {string_parts}")
    lhs, comparator, rhs = (
        parse_equation_part(string_parts[0]),
        string_parts[1],
        parse_equation_part(string_parts[2])
    )

    if comparator == "<=":
        return lhs <= rhs
    if comparator == ">=":
        return lhs >= rhs
    return lhs == rhs

def parse_equation_part(string):
    addition_parts = re.split("(\+|-)", string)
    result = parse_addition_part(addition_parts.pop(0))

    while addition_parts:
        symbol, addition_part, addition_parts =\
            addition_parts[0], addition_parts[1], addition_parts[2:]
        part_result = parse_addition_part(addition_part)

        if symbol not in ('+', '-'):
            raise Exception(f"Unexpected value {symbol}")
        if symbol == '-':
            result -= part_result
        else:
            result += part_result
    return result

def parse_addition_part(string):
    parts = string.split("*")
    result = 1

    for part in parts:
        if part == 'x1':
            result *= x1
        elif part == 'x2':
            result *= x2
        elif part.isnumeric():
            result *= float(part)
        else:
            raise Exception(f"Unexpected part {part}, expected number or x1/x2")

    return result

for s in ['x2>=4', '4*x1+2*x2=20', '1*x1-4*x2<=12', '4*x1+4*x2']:
    print(s.ljust(20, ' '), '->', parse_equation(s))

產量

x2>=4                -> x2 >= 4.0
4*x1+2*x2=20         -> 4.0*x1 + 2.0*x2 = 20.0
1*x1-4*x2<=12        -> x1 - 4.0*x2 <= 12.0
4*x1+4*x2            -> 4.0*x1 + 4.0*x2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM