I am doing an optimal control problem of complex network of a variation model of SIR model. The network contains 5000 nodes and I will count the number of nodes of each degree (from 4 to 187). The txt file is the file of degrees, the first column is the non-zero degrees in the network and the second line is the number of nodes in this degree.
There are total 76 non-zero degrees, so the number of equations is 76*3. The simulation time is 1000,and my purpose is to find the optimal control. I used IMODE=6 and solver=3.
But the running time is too long. In addition, most of time it can't extract Hessian Matrix, so the running process often stacked. I don't know how to speed up the calculation or make the calculation more stable (slow can be accepted but I need to make sure it is running normally).
Here is the code.
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as plt
import os
N = 5000
T = 1000
beta = 0.025
phi = 0.005
gamma = 0.01
sigma = 0.001
kmin = 4
kmax = 187
k_mean = 3.9968
data = np.loadtxt("degrees_5000_4_187_72.txt")
pk = data[:,2].tolist()
degrees = data[:,0].tolist()
nums_k = len(degrees)
m = GEKKO(server="https://byu.apmonitor.com")#remote = False
#set the variables
Uk = m.Array(m.MV,nums_k)
for i in range(nums_k):
Uk[i].LOWER = 0
Uk[i].UPPER = 1
Uk[i].STATUS = 1
Sk_initial = 0.8
Sk = m.Array(m.Var,nums_k)
for i in range(nums_k):
Sk[i].LOWER = 0
Sk[i].UPPER = 1
Sk[i].value = Sk_initial
Sk[i].value = Sk[i].value.value
Ik_initial = 0.1
Ik = m.Array(m.Var,nums_k)
for i in range(nums_k):
Ik[i].LOWER = 0
Ik[i].UPPER = 1
Ik[i].value = Ik_initial
Ik[i].value = Ik[i].value.value
Vk_initial = 0.1
Vk = m.Array(m.Var,nums_k)
for i in range(nums_k):
Vk[i].LOWER = 0
Vk[i].UPPER = 1
Vk[i].value = Vk_initial
Vk[i].value = Vk[i].value.value
#set the target
J1 = m.Var()
J1_initial = 0
J1.value = J1_initial
J1.value = J1.value.value
J1.LOWER = 0
J2 = m.Var()
J2_initial = 0
J2.value = J2_initial
J2.value = J2.value.value
#set the equations
eqns = []
for i,k in enumerate(degrees):
eqns.append(Sk[i].dt() == -k*beta*Sk[i]*sum(np.array(degrees)*pk*Ik)/k_mean-Uk[i]*Sk[i]+phi*Vk[i]+gamma*Ik[i])
eqns.append(Ik[i].dt() == k*beta*(Sk[i]+sigma*Vk[i])*sum(np.array(degrees)*pk*Ik)/k_mean-gamma*Ik[i])
eqns.append(Vk[i].dt() == -sigma*k*beta*Vk[i]*sum(np.array(degrees)*pk*Ik)/k_mean+Uk[i]*Sk[i]-phi*Vk[i])
eqns.append(J1.dt() == sum(Ik))
eqns.append(J2.dt() == sum(Uk))
m.Equations(eqns)
# set the time
t = np.linspace(0, T, int(T/10)+1)
m.time = t
p = np.zeros(int(T/10)+1);p[-1] = 1
final=m.Param(value=p)
# initialize with simulation
m.options.IMODE=6
m.options.NODES=2
m.options.MAX_ITER=2000
m.options.SOLVER = 3
m.options.TIME_SHIFT = 0
m.options.OTOL = 1e-3
m.options.RTOL = 1e-3
m.Minimize(J1*final)
m.solve(disp=True)
and the txt file is like this.
4 1682 0.3364
5 970 0.194
6 541 0.1082
7 370 0.074
8 282 0.0564
9 197 0.0394
10 171 0.0342
11 118 0.0236
12 101 0.0202
13 73 0.0146
14 68 0.0136
15 54 0.0108
16 40 0.008
17 37 0.0074
18 39 0.0078
19 20 0.004
20 20 0.004
21 10 0.002
22 17 0.0034
23 13 0.0026
24 9 0.0018
25 13 0.0026
26 7 0.0014
27 13 0.0026
28 11 0.0022
29 7 0.0014
30 9 0.0018
31 6 0.0012
32 8 0.0016
33 3 0.0006
34 6 0.0012
35 4 0.0008
36 3 0.0006
37 9 0.0018
38 4 0.0008
39 6 0.0012
40 5 0.001
41 4 0.0008
42 2 0.0004
44 1 0.0002
46 3 0.0006
47 1 0.0002
48 2 0.0004
49 1 0.0002
50 1 0.0002
51 4 0.0008
53 2 0.0004
56 2 0.0004
57 1 0.0002
62 1 0.0002
63 1 0.0002
64 1 0.0002
68 2 0.0004
69 2 0.0004
72 1 0.0002
76 1 0.0002
77 1 0.0002
79 1 0.0002
84 1 0.0002
86 1 0.0002
91 2 0.0004
95 1 0.0002
96 1 0.0002
97 1 0.0002
102 1 0.0002
104 2 0.0004
121 1 0.0002
122 1 0.0002
147 3 0.0006
148 2 0.0004
158 1 0.0002
187 1 0.0002