3

I am using GEKKO to find 2D-fourier coefficients of some unknown funtion u expanded in terms of 2D-vectors g. To this end have setup dictionaries and I need to calculate the many terms of the form:

u{ u_fourier_i_j: [m.Var(),m.Var()] ....}
g{ g_i_j : [gx_i_j, gy_i_j].....}
m.Minimize(m.cos(sup.function()))

where sup.function is defined as:

dummy = 0
for i in fourier_x:
   for j in fourier_y:
       dummy += u_fourier_i_j * np.cos(np.dot(g_i_j,[x,y]))
return dummy

Now I'd like to use many fourier coefficients, but Gekko gives me the error:

APM model error: string > 15000 characters Consider breaking up the line into multiple equations

The may also be due to only using newline character CR instead of CR LF (for Windows) or LF (for MacOS/Linux) To fix this problem, save APM file with appropriate newline characters

Is there a way to increase the max string size in the APM Model or a way to circumvent this error all together by structuring the program differently? Computational Ressources shouldn't be an issue.

Abbel
  • 61
  • 1
  • 1
    Have you tried "breaking up the line into multiple equations", as the error message suggests? – ForceBru Nov 24 '21 at 15:59
  • Well the whole expression of the sup.function is the argument of the cosine, so this can't reasonably broken up into multiple expressions. One could use addition theorems I guess, but this is not scalable since this breaks down at like 30 fourier coefficients and I'd like to use hundreds. – Abbel Nov 24 '21 at 16:36

1 Answers1

0

Use m.sum() (gekko summation) to avoid the max length error. It takes a list input and creates a summation that improves the speed of model solution.

dummy = []
for i in fourier_x:
   for j in fourier_y:
       dummy.append(u_fourier_i_j * np.cos(np.dot(g_i_j,[x,y])))
return m.sum(dummy)

If there is still the error, try using a similar strategy by replacing the np.dot() function np.dot(g_i_j,[x,y]) with d:

n = len(g_i_j)
z = [x,y]
d = m.sum([g_i_j[i]*z[i] for i in range(n)])

Please post a complete script so that the fix can be verified.

John Hedengren
  • 12,068
  • 1
  • 21
  • 25