I am building a Python application for use in sientific computing. The application is a Model Predicive Controler (MPC) and I am using scipy.optimize.minimize function as the optimization algorithm.
solution_guess = minimize(objectiveFunction,
U_guess,
arg_guess,
callback= None,
method = "SLSQP")
where the objective function is a self made function where a simulation of my system is executed. it looks some like this:
def objectiveFunction(x,*arg):
U_test = x
dt_test = arg[0]
setpoint_test = arg[1]
pred_horizion_length_test = arg[2]
initStateValue_test = arg[3]
# Defining Model Arrays
NS_pred_horizion_test = int(pred_horizion_length_test/dt_test)+1
pred_horizion_array_test = np.linspace(0, pred_horizion_length_test, NS_pred_horizion_test)
SP_array_test = np.zeros(NS_pred_horizion_test) + setpoint_test
Y_array_test = SP_array_test * 0
# Defining parameters for the testing model
timeDelay_test = 50
initDelayValue_test = 0
K_test = 4
Tc1_test = 30
Tc2_test = 60
# Defining Model Object
obj_model_test = model.secDegModel(dt = dt_test,
K = K_test,
Tc1 = Tc1_test,
Tc2 = Tc2_test,
timeDelay = timeDelay_test,
initStateValue = initStateValue_test,
initDelayValue = initDelayValue_test
)
###########################################
#|||||||||||||||||||||||||||||||||||||||||#
# Testing Values for U on Model #
#|||||||||||||||||||||||||||||||||||||||||#
###########################################
# Running simulation of "real" model function
for k in range(NS_pred_horizion_test):
Y_array_test[k] = obj_model_test.run(u_k = U_test)
error = np.sum(abs(SP_array_test-Y_array_test))
return error
What i struggle with no is how to get back the Y_array_test array so I can plot it every time the optimization is done. I tryed using global variables but i did not get it to work nor do I think its good coding manner to use global variables. does any one know a nice way to solv my problem? maybe using callback function? (if callback is the way to go, I do not fully understand how this method works or how to implement it in a nice way)
Why don't you just do the following?
Modify your objectiveFunction as follows
from numpy import savez
def objectiveFunction(x,*arg):
.
.
.
.
.
# Running simulation of "real" model function
for k in range(NS_pred_horizion_test):
Y_array_test[k] = obj_model_test.run(u_k = U_test)
# Just save Y_array_test in a file
# Add some call_no if you don't want to overwrite
# then filename would be 'Y_array_test_' + str(call_no)
# You could increment this call_no, every time by
# call_no = call_no + 1
savez(file_name, Y_array_test)
# Then you could plot it outside using matplotlib
error = np.sum(abs(SP_array_test-Y_array_test))
return error