Reputation: 57
I'm currently having some issue regarding accessing indices in Gekko array. I'm trying to convert following code:
m = GEKKO()
s42 = m.Array(m.Var, 8)
s44 = m.Array(m.Var, 8)
m.Equation(s42[0] == p42_raw[0] / (n34 + n44) * (n34 * (p34_norm[0] + p34_norm[4]) + n44 * (s44[0] + s44[4]) / np.sum(s44)))
m.Equation(s42[1] == p42_raw[1] / (n34 + n44) * (n34 * (p34_norm[0] + p34_norm[4]) + n44 * (s44[0] + s44[4]) / np.sum(s44)))
m.Equation(s42[2] == p42_raw[2] / (n34 + n44) * (n34 * (p34_norm[1] + p34_norm[5]) + n44 * (s44[1] + s44[5]) / np.sum(s44)))
m.Equation(s42[3] == p42_raw[3] / (n34 + n44) * (n34 * (p34_norm[1] + p34_norm[5]) + n44 * (s44[1] + s44[5]) / np.sum(s44)))
m.Equation(s42[4] == p42_raw[4] / (n34 + n44) * (n34 * (p34_norm[2] + p34_norm[6]) + n44 * (s44[2] + s44[6]) / np.sum(s44)))
m.Equation(s42[5] == p42_raw[5] / (n34 + n44) * (n34 * (p34_norm[2] + p34_norm[6]) + n44 * (s44[2] + s44[6]) / np.sum(s44)))
m.Equation(s42[6] == p42_raw[6] / (n34 + n44) * (n34 * (p34_norm[3] + p34_norm[7]) + n44 * (s44[3] + s44[7]) / np.sum(s44)))
m.Equation(s42[7] == p42_raw[7] / (n34 + n44) * (n34 * (p34_norm[3] + p34_norm[7]) + n44 * (s44[3] + s44[7]) / np.sum(s44)))
...
to something shorter like below:
m.Equation([s42[i] for i in range(8)] ==
[p42_raw[i] / (n34 + n44) * (n34 * (p34_norm[np.uint(i / 2)] + p34_norm[np.uint(4 + i / 2)])
+ n44 * (s44[np.uint(i / 2)] + s44[np.uint(4 + i / 2)]) / sum(s44))
for i in range(8)])
but currently getting following errors:
File "/Users/tomino/opt/anaconda3/lib/python3.7/site-packages/gekko/gk_operators.py", line 144, in __len__
return len(self.value)
TypeError: object of type 'int' has no len()
I've been stuck in this problem for a while but cannot find any suitable solution yet. Can anyone please help me resolving this issue?
Upvotes: 3
Views: 77
Reputation: 31389
Isn't this what you're after?
for i in range(8):
h = i // 2
m.Equation(s42[i] == p42_raw[i] / (n34 + n44) * (n34 * (p34_norm[h] + p34_norm[h+4]) + n44 * (s44[h] + s44[h+4]) / np.sum(s44)))
Upvotes: 3