简体   繁体   English

gekko 中的变量索引

[英]Variable indexing in gekko

How can I index variables to solve a nonlinear optimization problem in python gekko package?如何索引变量以解决 python gekko package 中的非线性优化问题? For example, how can I write the objective function below into a single equation indexing the variable x?例如,我如何将下面的目标 function 写成一个索引变量 x 的方程式?

例如:

Use the m.Array() function to create a variable (or parameter) array.使用m.Array() function 创建变量(或参数)数组。 The np.prod() and m.sum() functions are used to define the objective function m.Maximize() and the equation m.Equation() . np.prod()m.sum()函数用于定义目标 function m.Maximize()和方程m.Equation()

import numpy as np
from gekko import GEKKO
n = 5
m = GEKKO(remote=False)
x = m.Array(m.Var,n,value=1)
m.Maximize(np.prod([m.sqrt(xi) for xi in x]))
m.Equation(m.sum(x)==100)
m.solve(disp=False)
print(x)

Solution:解决方案:

[[20.0] [20.0] [20.0] [20.0] [20.0]]

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

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