简体   繁体   English

从 PuLP 到 GEKKO:约束、目标函数的语法映射

[英]From PuLP to GEKKO: Syntax Mapping for Constraints, Objective Function

I'm working on a Staff Assignment Problem that was initially modeled in PuLP .我正在处理最初在PuLP建模的员工分配问题 However, during the development, some constraints as well as the objective became non-linear.然而,在开发过程中,一些约束以及目标变得非线性。 After comparing some packages, I chose GEKKO , but somehow can't get my optimization up and running.在比较了一些包之后,我选择了GEKKO ,但不知何故无法启动并运行我的优化。

I'm facing two issues:我面临两个问题:

1. Constraint formulation 1. 约束公式

Let's assume I have simple constraints like these .假设我有简单的限制喜欢这些

In PuLP I have something like:PuLP我有类似的东西:

# Each position p must be assigned to exactly one employee e
for p in position_names:
    succession_prob += pulp.lpSum([X[p][e] for e in employee_names]) == 1

# Only employees e that are qualified for position p can be selected
for p in position_names:
    for e in employee_names:
        succession_prob += X[p][e] <= position_mapping[p][e]

And I tried to do something like this in GEKKO :我试图在GEKKO做这样的事情:

# Each position p must be assigned to exactly one employee e
for p in range(len(position_names)):
    m.Equation(sum(X[p][e] for e in range(len(employee_names))) == 1)

# Only employees e that are qualified for position p can be selected
for p in range(len(position_names)):
    for e in range(len(employee_names)):
        m.Equation(X[p][e] <= position_mapping.iloc[e][p])

This doesn't give me an error but I'm not sure whether it is correct.这不会给我一个错误,但我不确定它是否正确。 However I get an error when I try to split the (rather complex) objective function:但是,当我尝试拆分(相当复杂的)目标函数时出现错误:

2. Split objective function 2. 拆分目标函数

# Dummy functions
numerator = pulp.lpSum(some expression)
denominator = pulp.lpSum(some other expression)

succession_prob += numerator / denominator

Again I tried to do something like this in GEKKO :我再次尝试在GEKKO做这样的事情:

numerator = m.Param(some expression)
denominator = m.Param(some other expression)

# Objective function: RCD moves -> max
m.Obj((numerator / denominator)*(-1))

>>> ERROR: unsupported operand type(s) for *: 'float' and 'generator'

I'm wondering whether my code for the constraints correct (choice of variable types etc.), as well as how to fix the split objective.我想知道我的约束代码是否正确(变量类型的选择等),以及如何修复拆分目标。 I'm grateful for any help, as I already tried out different variables and just can't it running.我很感激任何帮助,因为我已经尝试了不同的变量,但它无法运行。

For #1 your constraint formulation looks correct.对于#1,您的约束公式看起来是正确的。 You can check what Gekko writes by opening the run folder with m.open_folder() and open the .apm file with a text editor.您可以通过使用m.open_folder()打开运行文件夹并使用文本编辑器打开.apm文件来检查 Gekko 写入的内容。 It lists all of the equations, parameters, variables, and objective statements.它列出了所有方程、参数、变量和目标陈述。

For #2 you may have something else wrong besides what you included there.对于#2 ,除了您在那里包含的内容之外,您可能还有其他错误。 Also, parameter values are fixed - the objective function typically has variable types that are adjusted by the optimizer.此外,参数值是固定的——目标函数通常具有由优化器调整的变量类型。 Otherwise, if the objective function consists of only parameter values then it is a constant that won't influence the solution except add or substract a constant from the objective.否则,如果目标函数仅由参数值组成,那么它是一个常数,除了从目标中添加或减去一个常数外,不会影响解决方案。

Below is a simple program that you can use to investigate how Gekko is writing the model .apm file.下面是一个简单的程序,您可以使用它来研究 Gekko 如何编写模型.apm文件。

from gekko import GEKKO
import pandas as pd
m = GEKKO()
print(m._path)
x = m.Array(m.Var,(3,4))

mydict = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},
           {'a': 100, 'b': 200, 'c': 300, 'd': 400},
           {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }]
w = pd.DataFrame(mydict)
y = [0,2]
z = [1,3]

for p in range(len(y)):
    m.Equation(sum(x[p][e] for e in range(len(z)))==1)

for p in range(len(y)):
    for e in range(len(z)):
        m.Equation(x[p][e] < w.iloc[e][p])

for p in range(np.size(x,0)):
    for e in range(np.size(x,1)):
        m.Obj(x[p][e]**2)

m.solve(disp=True)
print(x)
m.open_folder()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM