4

I'm using Spyder to run Python 3.7 where I installed gekko. However, I tried running a simple gekko code from Wikipedia and it gives me the following error:

ImportError: cannot import name 'dump_csp_header' from 'werkzeug.http' (C:\Users\zulfan.adiputra\AppData\Local\Continuum\anaconda3\envs\PythonNew\lib\site-packages\werkzeug\http.py)

When I check in the Anaconda prompt, the werkzeug installed is 1.0.0. What to do in this regard?

Thanks

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
Zulfan
  • 75
  • 4
  • 1
    Welcome to stack overflow. Could you post the sample script you tried to run and the output of `pip list`? That should help us figure out where things may have gone wrong. – Daniel Hill Apr 04 '20 at 02:30
  • 1
    Were you using the `GUI=True` in `m.solve`? That is the only place the package uses werkzeug to my knowledge. The GUI feature can be somewhat unstable at the moment depending on what you are looking to do. – Daniel Hill Apr 04 '20 at 02:52

1 Answers1

1

You can resolve the error with:

  1. Set GUI=False in m.solve()
  2. Run the Python program from the command line with python myProgram.py. There are sometimes problems with the Flask server if you try to use the GUI by running from an IDE like Spyder or IDLE.

Instead of using the GUI option, it is relatively easy to plot the results with matplotlib. Here is an example script:

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt

m = GEKKO()  # initialize gekko
nt = 101
m.time = np.linspace(0, 2, nt)
# Variables
x1 = m.Var(value=1)
x2 = m.Var(value=0)
u = m.Var(value=0, lb=-1, ub=1)
p = np.zeros(nt)  # mark final time point
p[-1] = 1.0
final = m.Param(value=p)
# Equations
m.Equation(x1.dt() == u)
m.Equation(x2.dt() == 0.5 * x1 ** 2)
m.Obj(x2 * final)  # Objective function
m.options.IMODE = 6  # optimal control mode
m.solve()  # solve
plt.figure(1)  # plot results
plt.plot(m.time, x1.value, "k-", label=r"$x_1$")
plt.plot(m.time, x2.value, "b-", label=r"$x_2$")
plt.plot(m.time, u.value, "r--", label=r"$u$")
plt.legend(loc="best")
plt.xlabel("Time")
plt.ylabel("Value")
plt.show()

Example of how to plot results

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