简体   繁体   English

如何在 gekko 中动态构建约束?

[英]How could constraints be dynamically constructed in gekko?

I'm a newbie in gekko, and want to use it in my linear programming problems.我是 gekko 的新手,想在我的线性规划问题中使用它。

I have variable names, costs, minimum and maximum bounds in separate dictionaries (my_vars, Cost, Min and Max) with variable names as their keys, and the objective is minimizing total cost with determining the amount of variables satisfying the constraints.我在单独的字典(my_vars、Cost、Min 和 Max)中有变量名称、成本、最小和最大边界,变量名称作为它们的键,目标是通过确定满足约束的变量数量来最小化总成本。

I did as below;我做了如下;

LP = GEKKO(remote=False)
vars = LP.Array(LP.Var, (len(my_vars)))
i=0
for xi in vars:
    xi.lower = Min[list(my_vars)[i]]
    xi.upper = Max[list(my_vars)[i]]
    i += 1

Here I'd like to use variable original names instead of xi, is there any way?这里我想用变量原名代替xi,有什么办法吗?

it continues as;它继续为;

LP.Minimize(sum(float(Cost[list(my_vars)[i]])*vars[i] for i in range(len(my_vars))))
LP.Equation(sum(vars) == 100)

Also I have constraint's left hand side (LHS) (coefficients of variables) and right hand side (RHS) numbers in two pandas data frame files, and like to construct equations using a for loop.此外,我在两个 pandas 数据帧文件中有约束的左侧 (LHS)(变量系数)和右侧 (RHS) 数字,并且喜欢使用 for 循环构造方程。

I don't know how to do this?我不知道该怎么做?

Here is one way to use your dictionary values to construct the problem:这是使用字典值构造问题的一种方法:

from gekko import GEKKO

# stored as list
my_vars = ['x1','x2']
# stored as dictionaries
Cost = {'x1':100,'x2':125}
Min = {'x1':0,'x2':0}
Max = {'x1':70,'x2':40}

LP = GEKKO(remote=False)
va = LP.Array(LP.Var, (len(my_vars)))  # array
vd = {}                                # dictionary
for i,xi in enumerate(my_vars):
    vd[xi] = va[i]
    vd[xi].lower = Min[xi]
    vd[xi].upper = Max[xi]

# Cost function
LP.Minimize(LP.sum([Cost[xi]*vd[xi] for xi in my_vars])) 
# Summation as an array
LP.Equation(LP.sum(va)==100)
# This also works as a dictionary
LP.Equation(LP.sum([vd[xi] for xi in my_vars])==100)
LP.solve(disp=True)

for xi in my_vars:
    print(xi,vd[xi].value[0])
print ('Cost: ' + str(LP.options.OBJFCNVAL))

This produces a solution:这产生了一个解决方案:

EXIT: Optimal Solution Found.

 The solution was found.

 The final value of the objective function is  10750.00174236579
 
 ---------------------------------------------------
 Solver         :  IPOPT (v3.12)
 Solution time  :  0.012199999999999996 sec
 Objective      :  10750.00174236579
 Successful solution
 ---------------------------------------------------
 

x1 69.999932174
x2 30.0000682
Cost: 10750.001742

Here are a few examples of efficient linear programming with Gekko by exploiting problem sparsity.下面是一些利用问题稀疏性使用 Gekko 进行高效线性规划的示例。

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

相关问题 Gekko中的模类型约束 - modulo type constraints in gekko 如何使用GEKKO对日志或sqrt建模? 约束 - How should I model log or sqrt with GEKKO? Constraints 如何将 Python Babel 与动态构造的字符串一起使用 - How to use Python Babel with dynamically constructed string 如何 - 使用 Python w/Gekko 为具有多个约束的许多矩阵中的变量找到解决方案? - How to - finding solution for variables in many matrices with multiple constraints using Python w/ Gekko? 在速度限制下使用 Gekko 进行轨迹优化 - Trajectory optimization with gekko under speed constraints 通过稍微放松约束在 Gekko 中获得解决方案 - Obtain solutions in Gekko by slightly relaxing constraints 我对 GEKKO 中的循环有问题限制 - i have problem constraints with loops in GEKKO 如何动态指定纸浆中的约束? - How specify constraints in pulp dynamically? Django 模板 - 如何检查动态构造的键是否在对象中具有值 - Django Template - How to check if dynamically constructed key has value in Object 如何在没有已知目标函数(比如一些随机函数)和已知变量和约束的情况下使用 gekko 优化器? - how can I use gekko optimizer without a known objective function(let say some random function) and with known variables and constraints?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM