[英]How to - finding solution for variables in many matrices with multiple constraints using Python w/ Gekko?
this is my first post here.这是我在此的头一篇博文。
I have a real-life financial Budgeting problem.我有一个现实生活中的财务预算问题。
Having a total Budget values for BusinessUnit per each of 30 countries in my scope plus 4 quarters, I need to distribute (find) correct values for many (like up to 30) Product lines per BusinessUnit for each country total in each quarter.在我的 scope 加上 4 个季度中,每个国家/地区的 30 个国家/地区的业务单位的总预算值,我需要为每个国家/地区的每个国家/地区总数的多个(最多 30 个)产品线分配(查找)正确值。 I also have target values for each country FullYear.
我也有每个国家全年的目标值。 Correct values are defined by constrains:
正确的值由约束定义:
each quarter for each country sums up to total BusinessUnit (columns, simple sum) - constrain每个国家/地区的每个季度总计为总 BusinessUnit(列,简单总和)- 约束
each Product line sums up to FullYear for one Country (rows, simple sum) - constrain每个产品线对一个国家/地区的总和为 FullYear(行,简单总和)- 约束
for every Product line total (simple sum) for those 30 countries we calculate seasonality (Quarter[n] / FullYear) which should also be equal to a given constrain.对于这 30 个国家/地区的每个产品线总计(简单总和),我们计算季节性 (Quarter[n] / FullYear),它也应该等于给定的约束。
finally all the values I'm looking for needs to sum up to Ultimate total constrained value (with ~99% of accuracy) = total Budget target for the next year - constrain最后,我正在寻找的所有值都需要总结为最终总约束值(准确率约为 99%)= 明年的总预算目标 - 约束
please check screenshot of an example structure of the data I have in my excel input file.请检查我在 excel 输入文件中的数据示例结构的屏幕截图。
For years I have been using excel solver tool, quite successfully.多年来,我一直在使用 excel 求解器工具,非常成功。 This year complexity grew so significantly excel solver is no longer working - too many variables and constraints.
今年复杂性增长如此显着 excel 求解器不再工作 - 变量和约束太多。 As I use python and pandas on daily basis I decided to lookup for the solution with those tools, but as I have no experience with linear programming / solution finding / decision making etc. I really do not understand much of the documentation.
当我每天使用 python 和 pandas 时,我决定使用这些工具查找解决方案,但由于我没有线性编程/解决方案查找/决策等方面的经验。我真的不太了解文档。 Looking at Gekko package tutorials I think it might be useful to solve the problem, but I don't know how to apply any of the features that Gekko includes.
查看 Gekko package 教程我认为它可能对解决问题很有用,但我不知道如何应用 Gekko 包含的任何功能。
What I'm also looking for is use of Pandas for this exercise to copy/paste big chunk of data (io excel / jupyter notebook) and easily create matrices / dictionaries (this I'm able to do anyways) to use with proposed solution.我还在寻找的是在此练习中使用 Pandas 来复制/粘贴大块数据(io excel / jupyter notebook)并轻松创建矩阵/字典(无论如何我都能做到)以与建议的解决方案一起使用.
Anyone could help here?任何人都可以在这里提供帮助吗? Not necessarily with Gekko but in general - could you please advise how to approach the problem using Python?
不一定是 Gekko,但一般来说 - 你能告诉我如何使用 Python 来解决这个问题吗? What tool / library I could use to find correct numbers distribution?
我可以使用什么工具/库来找到正确的数字分布? If possible pls suggest snippets... thank you!
如果可能,请建议片段...谢谢!
Thank you in advance先感谢您
There are a few examples of using matrices in Gekko to get started:在 Gekko 中有几个使用矩阵的例子:
There is also an example of using matrix operations with Gekko variables and Numpy with test_arrays.py .还有一个使用 Gekko 变量的矩阵运算和使用 test_arrays.py 的Numpy的示例。
import numpy as np
from gekko import GEKKO
m = GEKKO(remote=False)
# Random 3x3
A = np.random.rand(3,3)
# Random 3x1
b = np.random.rand(3,1)
# Gekko array 3x3
p = m.Array(m.Param,(3,3))
# Gekko array 3x1
y = m.Array(m.Var,(3,1))
# Dot product of A p
x = np.dot(A,p)
# Dot product of x y
w = np.dot(x,y)
# Dot product of p y
z = np.dot(p,y)
# Trace (sum of diag) of p
t = np.trace(p)
# solve Ax = b
s = m.axb(A,b)
m.solve()
Here is another example with test_matrix.py :这是test_matrix.py的另一个示例:
from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
ni = 3; nj = 2; nk = 4
# solve AX=B
A = m.Array(m.Var,(ni,nj),lb=0)
X = m.Array(m.Var,(nj,nk),lb=0)
AX = np.dot(A,X)
B = m.Array(m.Var,(ni,nk),lb=0)
# equality constraints
m.Equations([AX[i,j]==B[i,j] for i in range(ni) \
for j in range(nk)])
m.Equation(5==m.sum([m.sum([A[i][j] for i in range(ni)]) \
for j in range(nj)]))
m.Equation(2==m.sum([m.sum([X[i][j] for i in range(nj)]) \
for j in range(nk)]))
# objective function
m.Minimize(m.sum([m.sum([B[i][j] for i in range(ni)]) \
for j in range(nk)]))
m.solve()
print(A)
print(X)
print(B)
Pandas DataFrames can be used to initialize the matrix initial guess or matrix input parameters. Pandas DataFrames 可用于初始化矩阵初始猜测或矩阵输入参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.