英文:
Why np.sum() dosen't works if i want use this in lambda function
问题
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
英文:
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
答案1
得分: 0
你应该包括整个错误消息:
SyntaxError: invalid character '–' (U+2013)
问题在于你认为那两行中的减号实际上不是减号-它是Unicode "en dash"字符。U+2013。这可能来自你用于编辑文本的工具。你应该进入纯文本编辑器(如vim,nano等),并用简单的减号替换这两个字符。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论