1 year ago
#313718
Anas
Why is my list index out of range when using scipy optimization?
I am trying to solve a quadratic program in python using scipy.optimize and keep getting a list out of range error when solving for the objective function. Below is the code linked to excel. I commented the values that are crucial for the program to work and placed them beside their variables in order to have a clear image of what is going on. Code:
import numpy as np
import random
from scipy.optimize import minimize
import openpyxl
# Input Data
random.seed(10) # Seed Fix
wb = openpyxl.load_workbook('Covid-19-Python-Excel-Data.xlsx')
ws = wb.active
Days = ws['A2'].value # Number of Days (80)
τ_min = ws['A12'].value # Tao_Min (11)
τ_max = ws['B12'].value # Tao_Max (27)
ζ_min = ws['A17'].value # Recovery_Min (10)
ζ_max = ws['B17'].value # Recovery_Max (40)
ζ_mode = ws['A20'].value # Recovery_Mode (28)
Ĉ = ws['E2':'E81'] # Cumulative Number of Deaths
# Made Ĉ a List
Ĉ = [Ĉ[i][0].value for i in range(80)] # print(type(Ĉ))
# Logistic Function Trial Values
K = ws['H3'].value
Q = ws['I3'].value
λ = ws['J3'].value
v = ws['K3'].value
x0 = [K, Q, λ, v]
a = [0.001 for i in range(τ_min, τ_max + 1)] # Check + 1 or not for both a & b
b = [0.001 for i in range(ζ_min, ζ_max + 1)]
ε = [random.uniform(-100, 100) for i in range(Days)]
x0.extend(a)
x0.extend(b)
x0.extend(ε)
print(x0)
# Quadratic Program (NLP)
def I(t, K, Q, λ, v): # I(t) Equations Number (1 & 2)
return (K / (1 + Q * np.exp(-λ * t)) ** (1 / v))
def N(t, K, Q, λ, v): # N(t) Equations Number (3 & 4)
if t == 0:
return I(0, K, Q, λ, v)
else:
return I(t, K, Q, λ, v) - I(t - 1, K, Q, λ, v)
def Objective_Function(ε):
Sum = 0
for t in range(Days):
Sum += ε[t] ** 2
return Sum
def Constraint_1(x0):
for t in range(Days):
Sum = 0
for j in range(τ_min, τ_max + 1):
for k in range(Days):
Sum += a[j] * N(k - j, K, Q, λ, v)
Sum = Sum + ε[t] - Ĉ[t - 1]
return Sum
def Constraint_2(x0):
Sum = 0
for j in range(τ_min, τ_max + 1):
Sum += a[j]
for j in range(ζ_min, ζ_max + 1):
Sum += b[j]
Sum = Sum - 1
return Sum
def Constraint_3(x0):
Sum = 0
for j in range(ζ_min + 1, ζ_mode):
Sum = b[j - 1] - b[j]
return Sum
def Constraint_4(x0):
Sum = 0
for j in range(ζ_mode, ζ_max):
Sum = b[j + 1] - b[j]
return Sum
bounds_Logistic_Function_Parameters = [(K, K), (Q, Q), (λ, λ), (v, v)]
bounds_a = [(0, 1) for i in range(τ_min, τ_max + 1)]
bounds_b = [(0, 1) for i in range(ζ_min, ζ_max + 1)]
bounds_ε = [(-100, 100) for i in range(Days)]
Bounds = []
Bounds.extend(bounds_Logistic_Function_Parameters)
Bounds.extend(bounds_a)
Bounds.extend(bounds_b)
Bounds.extend(bounds_ε)
Cons_1 = {'type': 'eq', 'fun': Constraint_1}
Cons_2 = {'type': 'eq', 'fun': Constraint_2}
Cons_3 = {'type': 'ineq', 'fun': Constraint_3}
Cons_4 = {'type': 'ineq', 'fun': Constraint_4}
Constraints = [Cons_1, Cons_2, Cons_3, Cons_4]
Result = minimize(Objective_Function, x0, method = 'SLSQP', bounds = Bounds, constraints = Constraints)
print(Result)
The error message:
Traceback (most recent call last):
File "c:\Users\Toshiba\Desktop\Python Programs Anaconda\QP-Updated.py", line 119, in <module>
File "C:\Users\Toshiba\anaconda3\envs\Meta-H\lib\site-packages\scipy\optimize\_minimize.py", line 631, in minimize
return _minimize_slsqp(fun, x0, args, jac, bounds,
File "C:\Users\Toshiba\anaconda3\envs\Meta-H\lib\site-packages\scipy\optimize\slsqp.py", line 328, in _minimize_slsqp
meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args']))
File "C:\Users\Toshiba\anaconda3\envs\Meta-H\lib\site-packages\scipy\optimize\slsqp.py", line 328, in <listcomp>
meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args']))
File "c:\Users\Toshiba\Desktop\Python Programs Anaconda\QP-Updated.py", line 69, in Constraint_1
Sum += a[j] * N(k - j, K, Q, λ, v)
IndexError: list index out of range
python
optimization
scipy-optimize
quadratic-programming
0 Answers
Your Answer