这是用CSP解决的K-Knights问题,问题似乎是约束。
错误来自这里
init 中的文件“ F:/ Lectures / AI Lab / Codes / nonattacking / aicodes / csp.py”,第23行 self.setUpVariableDomains()
setUpVariableDomains中的文件“ F:/ Lectures / AI Lab / Codes / nonattacking / aicodes / csp.py”,第28行 self.addVariableDomain(var,self._domain)
addVariableDomain中的文件“ F:/ Lectures / AI Lab / Codes / nonattacking / aicodes / csp.py”第35行 self._domainOfVariable [var] = copy.deepcopy(domain)
TypeError:不可散列的类型:“列表”
import variable
import copy
import notEqualConstraint
import simpleInference
import time
import backtrackingSearch
import consoleListener
import nonAttackingConstraint
class CSP():
def __init__(self, variables = [], domains = [], constraints = []):
self._variables = variables
self._domain = domains
self._constraints = constraints
self._domainOfVariable = {}
self._contraintsOfVariable = {}
self.setUpVariableDomains()
self.setUpConstraints()
def setUpVariableDomains(self):
for var in self._variables:
self.addVariableDomain(var, self._domain)
def setUpConstraints(self):
for constraint in self._constraints:
self.addConstraint(constraint)
def addVariableDomain(self,var,domain):
self._domainOfVariable[var] = copy.deepcopy(domain)
def addConstraint(self,constraint):
for var in constraint.getScope():
if var not in self._contraintsOfVariable:
self._contraintsOfVariable[var] = []
self._contraintsOfVariable[var].append(constraint)
def addSingleConstraint(self,constraint):
self._constraints.append(constraint)
for var in constraint.getScope():
if var not in self._contraintsOfVariable:
self._contraintsOfVariable[var] = []
self._contraintsOfVariable[var].append(constraint)
def addVariable(self,variable):
self._variables.append(variable)
self.addVariableDomain(variable,self._domain)
def getVariables(self):
return self._variables
def getDomainValues(self,var):
return self._domainOfVariable[var]
def getConstraints(self,var):
if var not in self._contraintsOfVariable:
return []
return self._contraintsOfVariable[var]
def getVariableDomains(self):
return self._domainOfVariable
def setVariableDomains(self,domainOfVariable):
self._domainOfVariable = domainOfVariable
def copy(self):
variables = copy.deepcopy(self._variables)
domains = copy.deepcopy(self._variables)
constraints = copy.deepcopy(self._variables)
csp = CSP(variables, domains, constraints)
return csp
def getNeighbour(self,variable,constraint):
neigh = []
for va in constraint.getScope():
if va != variable and (va not in neigh):
neigh.append(va)
return neigh
def removeValueFromDomain(self,variable,value):
values = []
for val in self.getDomainValues(variable):
if val != value:
values.append(val)
self._domainOfVariable[variable] = values
if __name__ == '__main__':
domains = ["K","A"]
variables = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
X=[[2,1],[2,-1],[-2,1],[-2,-1],[1,2],[1,-2],[-1,2],[-1,-2]]
constraints = [
nonAttackingConstraint.NotAttackingConstraint(X)
]
Csp = CSP(variables,domains,constraints)
inPro = simpleInference.SimpleInference()
bts = backtrackingSearch.BactrackingSearch(inPro,[consoleListener.ConsoleListener()],variableOrdering = True)
start = time.time()
result = bts.solve(Csp)
end = time.time()
print("%.2f ‐ %.2f" % (start,end))
特别是在上述CSP类中的addVariableDomain处停止,并给了我不可散列的类型:“ list”错误,因为我的约束是列表。问题可识别吗?有解决办法吗?
答案 0 :(得分:0)
列表不能用作字典键,因为它们是可变的,因此不适合用于哈希。尝试将列表转换为元组。
def addVariableDomain(self,var,domain):
self._domainOfVariable[tuple(var)] = copy.deepcopy(domain)
在var
用作字典键的地方,您可能都必须做同样的事情。