Reputation: 55
I have an Objective function with four parameters to be optimized (n1, n2, n3 and n4). This optimization should be done in a loop "for T in T" as the Objective function is a function of T. As a result I get for each T, a set of parameters. How can I build and print individualized lists containing T, n1, n2, n3 and n4?
I have tried different ways of "print", inside the loop and outside of it. I have also searched for examples here and in the GEKKO's documentation but it is still a challenge to me. Thanks in advance.
import math
import numpy as np
from gekko import GEKKO
T = np.arange(1, 10, 2)
for T in T:
A = 3/(T**2)
B = 20-T**2
C = 3+T
D = T
mA = A-T*C
mB = B-T*D
# Minimization routine
from gekko import GEKKO
m = GEKKO()
# Variables to be minimized:
n1, n2, n3, n4 = [m.Var() for i in range(4)]
var = [n1, n2, n3, n4]
# Initial values:
n0 = [3,2,3,3]
nL = np.ones(len(n0))*10**-10
nU = np.ones(len(n0))*10**10
for i,x in enumerate(var):
x.value = n0[i]
x.lower = nL[i]
x.upper = nU[i]
nt = m.Intermediate(n1 + n2 + n3 + n4)
# Objective:
m.Obj(T*A*mA + B*mB + C*D/nt)
# Set global options
m.options.IMODE = 3
# Solve minimization
m.solve()
print(n1, n2, n3, n4)
print(str(n1.value), str(n2.value), str(n3.value), str(n4.value))
Upvotes: 1
Views: 777
Reputation: 14346
There are multiple ways to export data from Python as shown here. You need to get the results into a 2 dimensional list, NumPy array, or Pandas data frame. The following are modifications to your original script if you need to get the values into a form that Excel can open, such as a CSV file. This script uses NumPy to write the file.
import math
import numpy as np
from gekko import GEKKO
T = np.arange(1, 10, 2)
z = [None]*len(T)
for j,T in enumerate(T):
A = 3/(T**2)
B = 20-T**2
C = 3+T
D = T
mA = A-T*C
mB = B-T*D
# Minimization routine
from gekko import GEKKO
m = GEKKO()
# Variables to be minimized:
n1, n2, n3, n4 = [m.Var() for i in range(4)]
var = [n1, n2, n3, n4]
# Initial values:
n0 = [3,2,3,3]
nL = np.ones(len(n0))*10**-10
nU = np.ones(len(n0))*10**10
for i,x in enumerate(var):
x.value = n0[i]
x.lower = nL[i]
x.upper = nU[i]
nt = m.Intermediate(n1 + n2 + n3 + n4)
# Objective:
m.Obj(T*A*mA + B*mB + C*D/nt)
# Set global options
m.options.IMODE = 3
# Solve minimization
m.solve(disp=False)
result = [T]
for x in var:
result.append(x.value[0])
z[j] = result
zn = np.array(z)
np.savetxt('z.txt',zn,delimiter=',',comments='',header='T,n1,n2,n3,n4')
Upvotes: 1
Reputation: 14346
Gekko is built for time-varying systems so the results are returned as a list. Steady state optimization only returns one value so you'll need to access the first element of the value
property. Try accessing an individual result with n1.value[0]
. The T
value is a numpy array so you can access the values with T[0], etc. Try something like the following at the end of your program script:
# Solve minimization
m.solve(disp=False)
result = [T]
for x in var:
result.append(x.value[0])
print(result)
It creates an initial list with the value of T
and then appends the other values. I set the option disp=False
to not show the solver output. The modified script produces these results:
[1, 3457333826.4, 3457319622.4, 3457333826.4, 3457333826.4]
[3, 2482924773.0, 2482917643.6, 2482924773.0, 2482924773.0]
[5, 2201591127.3, 2201587887.6, 2201591127.3, 2201591127.3]
[7, 2105335286.6, 2105331976.7, 2105335286.6, 2105335286.6]
[9, 1630154729.7, 1630153615.7, 1630154729.7, 1630154729.7]
It appears that all of the parameter values are equal for this problem and they don't have a connection to the other equations except for calculation of nt
and the objective term C*D/nt
.
Upvotes: 1