I’m terribly sorry for the stupid question,this is my first time using NumPy so I don’t know what I’m doing wrong
Lines with errors are marked #ERROR
I must be accessing the index incorrectly or getting the wrong type of value
Thank you in advance for your help
import numpy as np
p = {1:100, 2:120, 3:130, 4:100, 5:90,6:180}
M = {1:300, 2:120, 3:300}
I = [1,2,3,4,5,6]
J = [1,2,3]
st_per = {(1,1):1, (1,2):2, (1,3):3,
(2,1):4, (2,2):1, (2,3):1,
(3,1):5, (3,2):2, (3,3):4,
(4,1):3, (4,2):1, (4,3):2,
(5,1):1, (5,2):2, (5,3):1,
(6,1):0, (6,2):0, (6,3):0
}
st_per2p = np.empty([len(I), len(J)])
for i in range(len(I)):
for j in range(len(J)):
st_per2p[i,j] = st_per[i+1,j+1]
x0 = np.ones(len(st_per))*100
bounds = list((0,max(p.values())) for _ in range(st_per2p.size))
def ob(x):
ob_func = sum(x[idx]*st_per2p[idx//len(J), idx%len(J)] for idx in range(st_per2p.size))
return ob_func
def st_per1():
tmp = []
for idx in range(0, st_per2p.size, len(J)):
tmp_constr = {'type': 'eq','fun': lambda x, idx: p[idx//len(J) + 1]–np.sum(x[idx: idx + len(J)]),'args': (idx,)} #ERROR
tmp.append(tmp_constr)
return tmp
def st_per2():
tmp = []
for idx in range(0, st_per2p.size, len(I)):
tmp_constr = {'type': 'ineq','fun': lambda x, idx=idx: M[idx//len(I) + 1]–np.sum(x[idx: idx + len(I)])} #ERROR
tmp.append(tmp_constr)
return tmp
>Solution :
You should have included the whole error message:
SyntaxError: invalid character '–' (U+2013)
The PROBLEM is that what you THINK is a minus sign in those two lines is not actually a minus sign — it is the Unicode "en dash" character. U+2013. This probably came from whatever tool you used to edit the text. You should go into a plain text editor (vim, nano, etc), and replace those two with a simple minus sign.