2

I want to find the value of the parameter m that minimizes my variable x subject to a system of differential equations. I have the following code

from gekko import GEKKO

def run_model_m(days, population, case, k_val, b_val, u0_val, sigma_val, Kmax0, a_val, c_val):
 list_x =[]
 list_u =[]
 list_Kmax =[]
 for i in range(len(days)):
   list_xi=[]
   list_ui=[]
   list_Ki=[]

  for j in range(len(days[i])):
  #try:
    m = GEKKO(remote=False)
    #m.time= days[i][j]
    eval = np.linspace(days[i][j][0], days[i][j][-1], 100, endpoint=True)
    m.time = eval
    x_data= population[i][j]
  
    variable=  np.linspace(population[i][j][0], population[i][j][-1], 100, endpoint=True)
    x = m.Var(value=population[i][j][0], lb=0)
    sigma= m.Param(sigma_val)
    d = m.Param(c_val)
    k = m.Param(k_val)
    b = m.Param(b_val)
    r = m.Param(a_val)
    step = np.ones(len(eval))
    step= 0.2*step
    step[0]=1
    m_param = m.CV(value=1, lb=0, ub=1, integer=True); m_param.STATUS=1
    u = m.Var(value=u0_val, lb=0, ub=1)
    #m.free(u)
    a = m.Param(a_val)
    c= m.Param(c_val)
    Kmax= m.Param(Kmax0) 

    if case == 'case0':
      m.Equations([x.dt()==  x*(r*(1-x/(Kmax))-m_param/(k+b*u)-d), u.dt()== sigma*(m_param*b/((k+b*u)**2))])
    elif case == 'case4':
      m.Equations([x.dt()==  x*(r*(1-u**2)*(1-x/(Kmax))-m_param/(k+b*u)-d), u.dt() == sigma*(-2*u*r*(1-x/(Kmax))+(b*m_param)/(b*u+k)**2)])
    p = np.zeros(len(eval))
    p[-1] = 1.0
    final = m.Param(value=p)
    m.Obj(x)
    m.options.IMODE = 6
    m.options.MAX_ITER=15000
    m.options.SOLVER=1

    # optimize
    m.solve(disp=False, GUI=False)
    #m.open_folder(dataset_path+'inf')
 
    list_xi.append(x.value)
    list_ui.append(u.value)
    list_Ki.append(m_param.value) 
  
 list_x.append(list_xi)
 list_Kmax.append(list_Ki)
 list_u.append(list_ui)
return list_x, list_u, list_Kmax, m.options.OBJFCNVAL



scaled_days[i][j] =[-7.0, 42.0, 83.0, 125.0, 167.0, 217.0, 258.0, 300.0, 342.0]
scaled_pop[i][j] = [0.01762491277346285, 0.020592540360308997, 0.017870838266697213, 0.01690069378982034,0.015512320147187675,0.01506701796298272,0.014096420738841563,0.013991224004743027,0.010543380664478205]
k0,b0,group, case0,  u0, sigma0, K0, a0, c0 = (100, 20, 'Size3, Inc', 'case0', 0.1, 0.05, 2, 0, 0.01)
list_x2, list_u2, list_Kmax2,final =run_model_m(days=[[scaled_days[i][j]]], population= 
[[scaled_pop[i][j]]],case=case1, k_val=list_b1[i0][0], b_val=b1, u0_val=list_u1[i0][j0], 
sigma_val=sigma1, Kmax0=K1, a_val=list_Kmax1[0][0], c_val=c1)

I get the error Data arrays must have the same length, and match time discretization in dynamic problems error but I don't understand why. I have tried making x and m_param arrays, with x=m.Var, m_param =m.MV... But still get the same error, even if they are all arrays of the same length. Is this the right way to find the solution of the minimization problem?

user606273
  • 143
  • 6

1 Answers1

3

I think the error was just that in run_model_m I was passing a list as u0_val and it didn't have the same dimensions as m.time. So it should be u0_val=list_u1[0][0][0]

user606273
  • 143
  • 6