I am coding Constraint Satisfaction Problem in Artificial Intelligence using Python:
from typing import Generic, TypeVar, Dict, List, Optional
from abc import ABC, abstractmethod
V = TypeVar('V')
D = TypeVar('D')
class Constraint(Generic[V,D], ABC):
def __init__(self, variables: List[V]) -> None:
self.variables = variables
@abstractmethod
def satisfied(self, assignment: Dict[V, D]) -> bool:
...
class CSP(Generic[V, D]):
def __init__(self, variables: List[V], domains: Dict[V, List[D]]) -> None:
self.variables: List[V] = variables
self.domain: Dict[V, List[D]] = domains
self.constraints: Dict[V, List[Constraint[V, D]]] = {}
for variable in self.variables:
self.constraints[variable] = []
def add_constraint(self, constraint: Constraint[V, D]) -> None:
for variable in constraint.variables:
if variable not in constraint.variables:
print("Variable in Constraint, not in CSP")
else:
self.constraints[variable].append(constraint)
def consistent(self, variable: V, assignment: Dict[V, D]) -> bool:
for constraint in self.constraints[variable]:
if not constraint.satisfied(assignment):
return False
return True
def backtracking_search(self, assignment: Dict[V, D] = {}) -> Optional[Dict[V, D]]:
if len(assignment) == len(self.variables):
return assignment
unassigned: List[V] = [v for v in self.variables if v not in assignment]
first: V = unassigned[0]
for value in self.domains[first]:
local_assignment = assignment.copy()
local_assignment[first] = value
if self.consistent(first, local_assignment):
result: Optional[Dict[V, D]] = self.backtracking_search(local_assignment)
if result is not None:
return result
return None
class MapColoringConstraint(Constraint[str, str]):
def __init__(self, place1: str, place2: str) -> None:
super().__init__([place1, place2])
self.place1: str = place1
self.place2: str = place2
def satisfied(self, assignment: Dict[V, D]) -> bool:
if self.place1 not in assignment or self.place2 not in assignment:
return True
return assignment[self.place1] != assignment[self.place2]
variables: List[str] = ["DJ", "SO", "ET", "KE", "UG", "TA", "RW", "BU"]
domains = Dict[str, List[str]] = {}
for variable in variables:
domains[variable].append("Red")
domains[variable].append("Green")
domains[variable].append("Blue")
csp: CSP[str, str] = CSP(variables, domains)
edges = [("DJ", "SO"), ("DJ", "ET"),
("SO", "KE"), ("SO", "ET"),
("ET", "KE"), ("KE", "UG"),
("KE", "TA"), ("UG", "TA"),
("UG", "RW"), ("TA", "BU"),
("TA", "RW"), ("RW", "BU")]
for i, j in edges:
print(i, j)
The error occurs as:
line 64, in domains = Dict[str, List[str]] = {} TypeError: '_SpecialGenericAlias' object does not support item assignment
I don't exactly know the problem that is occurring, i do have to declare the dictionary 'domains' as it is necessary for the question, can anyone help me out?
The problem was assigning a DataType to a variable:
domains = Dict[str, List[str]] = {}
The variable 'domains' could not be assigned a DataType and an assignment of type dictionary. That is why the error was occurring. However, I fixed it using:
domains: Dict[str, List[str]] = {}
Now, it works fine. Sorry for the inconvenience, I really hope it'll help someone one day.