Closed adavidzh closed 3 years ago
You can resolve this by moving the print statements after the m.solve()
command.
from gekko import GEKKO
m = GEKKO()
# variable array dimension
n = 3 # rows
p = 2 # columns
# create array
x = m.Array(m.Var,(n,p))
for i in range(n):
for j in range(p):
x[i,j].value = 2.0
x[i,j].lower = -10.0
x[i,j].upper = 10.0
# create parameter
y = m.Param(value = 1.0)
# sum columns
z = [None]*p
for j in range(p):
z[j] = m.Intermediate(sum([x[i,j] for i in range(n)]))
# objective
m.Obj(sum([z[j]**2 + y for j in range(p)]))
# minimize objective
m.solve()
print('x',x)
print('z',z)
Here is the solution:
x [[[-9.1115858651e-16] [-9.1115858651e-16]]
[[-9.1115858527e-16] [-9.1115858527e-16]]
[[5.4547859199e-15] [5.4547859199e-15]]]
z [[3.6324687481e-15], [3.6324687481e-15]]
Before the m.solve()
command, the values are initialized to zero unless you specifically initialize a starting value to something else. There is no initialization for intermediate variables so they'll always be zero at the start, before the m.solve()
command.
Ok, thanks for answering the noob question.
Two things:
z
is working properly. I.e., I would have had to dig deeper before being scared about all the zeroes, since in the new example, z
converges to a non-zero value.
TL;DR - With python 3.8.5 on MacOS 10.15, it seems that any arithmetic operation with
Const
orVar
yields zero. Same behaviour seen for0.2.8
,0.2.7
,0.2.6
,0.2.5
, and0.2.0
.In more detail:
I was just getting started with GEKKO and trying to run the example associated with
GEKKO().Array()
(no direct link; search forz[j] = m.Intermediate
). Here's a version with an couple extra printouts:that outputs
The fact that
z
is all zeroes when thex
are all twos looked very fishy so I tracked down what seems to be the issue and the following is a minimum working example to illustrate the problem:yields the following to my surprise:
Am I just doing something blatantly wrong?