import numpy as np
rnd = np.random
rnd.seed(0)
n = 6
t = [i for i in range(1, n)]
V = [0] + [i for i in range(1, n + 1)]
o = 5
operator = [o for o in range(1, o)]
precedence = [[1, 2], [1, 3], [3, 5], [2, 4], [4, 5], [4, 6], [5, 7], [6, 7]]
cost = {(1, 2): 0, (1, 3): 0, (3, 5): 4, (2, 4): 8, (4, 5): 6, (4, 6): 10, (5, 7): 0, (6, 7): 0}
arc = [(i, j) for (i, j) in precedence]
from docplex.mp.model import Model
model = Model('EWA')
x = model.binary_var_dict((i,j,o) for i, j in precedence for o in operator)
model.minimize(model.sum(x[i,j,o]*cost.get((i,j), 0) for i, j in precedence))
Traceback (most recent call last):
File "C:Userskaraba02Anaconda3libsite-packagesIPythoncoreinteractiveshell.py", line 3369, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-7426e90af4f8>", line 1, in <cell line: 1>
runfile('C:\Users\karaba02\PycharmProjects\pythonProject\ergonomic_work_assignment.py', wdir='C:\Users\karaba02\PycharmProjects\pythonProject')
File "C:Program FilesJetBrainsPyCharm 2022.2.2pluginspythonhelperspydev_pydev_bundlepydev_umd.py", line 198, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:Program FilesJetBrainsPyCharm 2022.2.2pluginspythonhelperspydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"n", file, 'exec'), glob, loc)
File "C:Userskaraba02PycharmProjectspythonProjectergonomic_work_assignment.py", line 37, in <module>
model.minimize(model.sum(x[i,j,o]*cost.get((i,j), 0) for i in precedence for j in precedence))
File "C:Userskaraba02Anaconda3libsite-packagesdocplexmpmodel.py", line 3268, in sum
return self._aggregator.sum(args)
File "C:Userskaraba02Anaconda3libsite-packagesdocplexmpaggregator.py", line 198, in sum
sum_res = self._sum_with_iter(sum_args)
File "C:Userskaraba02Anaconda3libsite-packagesdocplexmpaggregator.py", line 221, in _sum_with_iter
for item in args:
File "C:Userskaraba02PycharmProjectspythonProjectergonomic_work_assignment.py", line 37, in <genexpr>
model.minimize(model.sum(x[i,j,o]*cost.get((i,j), 0) for i in precedence for j in precedence))
TypeError: unhashable type: 'list'
我正在考虑给定的错误,因为目标函数是不可哈希的类型:'列表'它可能与定义成本有关,但我无法解决它。你能定义一下目标函数上的误差吗?
你面临一个越界
如果你转动
operator = [o for o in range(1, o)]
到
operator = [o for o in range(1, o+1)]
那么你的模型工作得更好