Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python I';我得到一个名字错误&引用;实例“未定义目录”;_Python - Fatal编程技术网

Python I';我得到一个名字错误&引用;实例“未定义目录”;

Python I';我得到一个名字错误&引用;实例“未定义目录”;,python,Python,我正在尝试实现禁忌搜索算法,但我一直得到以下错误。NameError:未定义名称“instance_dict” def Objfun(instance_dict, solution, show = False): dict = instance_dict t = 0 #starting time objfun_value = 0 for job in solution: C_i = t + dict[job]["processing_time"] d_

我正在尝试实现禁忌搜索算法,但我一直得到以下错误。NameError:未定义名称“instance_dict”

def Objfun(instance_dict, solution, show = False):
dict = instance_dict
t = 0   #starting time
objfun_value = 0
for job in solution:
    C_i = t + dict[job]["processing_time"]  
    d_i = dict[job]["due_date"]   
    T_i = max(0, C_i - d_i)    
    W_i = dict[job]["weight"] 

    objfun_value +=  W_i * T_i
    t = C_i
if show == True:
    print("The Objective function value for {} solution schedule is: {}".format(solution ,objfun_value))
return objfun_value

solution_1 = [1,2,5,6,8,9,10,3,4,7]
solution_2 = [2,3,5,10,6,8,9,4,7,1]

Objfun(instance_dict, solution_1, show=True)
Objfun(instance_dict, solution_2, show=True)

代码的最后两行
Objfun(instance\u dict,solution\u 1,show=True)
引用了一个未在任何地方定义的变量
instance\u dict
。因此,当您试图使用未定义的变量时,会出现错误。

您是否分配过
instance\u dict
?您似乎没有任何类似于
instance\u dict=…
Sidenote的行:使用局部变量对
dict
之类的内置项进行阴影处理通常是个坏主意。考虑选择一个更具描述性的变量名。