Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

#!/usr/bin/env python # MapColor.py # Skeleton Code. # Imports # ==================================== import networkx import re import copy # Data Structures for the Graph. #...

1 answer below »
#!/us
in/env python
# MapColor.py
# Skeleton Code.
# Imports
# ====================================
import networkx
import re
import copy
# Data Structures for the Graph.
# =================================
class Variable(object):
"""
The Variables will be used to store
the individual variables for our
constraint poblem. Each one will be
defined from the file and will store
the total domain and the cu
ent
domain.
"""
def __init__(self, Name, Values):
"""
XXXXXXXXXXSet the initial value for the variables.
"""
# Set the name.
XXXXXXXXXXself.Name = Name

# The full domain for the values.
XXXXXXXXXXself.FullDomain = set(Values)

# The cu
ent working domain fo
# the variable.
XXXXXXXXXXself.Cu
Domain = set(Values)
def assignValue(self, Val):
"""
XXXXXXXXXXAssign a specific value to the variable by
XXXXXXXXXXcutting the domain down to a single value.
"""
XXXXXXXXXXself.Cu
Domain = set([Val])

def dropValue(self, Val):
"""
XXXXXXXXXXDrop the specified value from
XXXXXXXXXXthe Cu
ent Domain.
"""
XXXXXXXXXXself.Cu
Domain.remove(Val)
def restoreValue(self, Val):
"""
XXXXXXXXXXAdd in the value back into the
XXXXXXXXXXcu
ent domain. Raise an e
o
XXXXXXXXXXif it is not in the primary.
"""
XXXXXXXXXXself.Cu
Domain.add(Val)
def resetCu
Domain(self):
"""
XXXXXXXXXXRestore the oroginal domain.
"""
XXXXXXXXXXself.Cu
Domain = copy.copy(self.FullDomain)


class ConstraintGraph(object):
"""
The constraint graph class will store the
individual variables and not equal constraints.
"""
def __init__(self, Variables, Constraints):
"""
XXXXXXXXXXDefine the basic contents.
"""
# Set storage for the variables and arcs.
XXXXXXXXXXself.Variables = {}
#self.Constraints = set()
# The relationship can be used as a graph.
XXXXXXXXXXself.Graph = networkx.Graph()
# Iterate over the variables and
# add them in by name as a dict.
XXXXXXXXXXfor Var in Variables:

XXXXXXXXXXName = Var.Name

XXXXXXXXXXself.Variables[Name] = Va
XXXXXXXXXXself.Graph.add_node(Name, var=Var)
# Now add the constraint pairs as
# tuples and in the graph so that
# we can find them later.
XXXXXXXXXXfor (VarA, VarB) in Constraints:
XXXXXXXXXXself.Graph.add_edge(VarA, VarB)

def isComplete(self):
"""
XXXXXXXXXXReturn True if this is complete, that is
XXXXXXXXXXeach variable has only one value remaining
XXXXXXXXXXin its cu
ent domain.
"""
XXXXXXXXXXfor Var in self.Variables:
XXXXXXXXXXif (len(self.Cu
Domain) != 1):
XXXXXXXXXXreturn(False)

XXXXXXXXXXreturn(True)

def backtrackSearch(self):
"""
XXXXXXXXXXDo the backtracking search algorithm.
"""
XXXXXXXXXXpass
def ac3(self):
"""
XXXXXXXXXXDo AC3.
"""
XXXXXXXXXXpass

# Read in the constraint code.
# =================================
VAR_START = re.compile("^VARS")
VAR_END = re.compile("^ENDVARS")
VAR_ROW = re.compile("^(?P[A-Z]+) : (?P[A-Za-z0-9 ]+)")

def readConstraintGraph(InFile):
"""
Read in the constraint graph. This
relies on the regular expressions
to match each row.
"""
with(open(InFile, "r") as Input):
XXXXXXXXXXVariables = readVars(Input)
#Constraints = readConstraints(Input)
# Now add in the construction.
pass
def readVars(InputStream):
"""
Read in the first var line then read until we
reach the endvars. Then return the variables.
"""
# Allocate storage for the vars.
Variables = []

# Read in and print the first line.
First = InputStream.readline()[:-1]
print("Reading: |{}|".format(First))
if (VAR_START.match(First) == None):
XXXXXXXXXXraise RuntimeE
or("Line Mismatch")
# Read in each of the variable lines.
NextLine = InputStream.readline()[:-1]
while(VAR_END.match(NextLine) == None):
# At this point we have a variable row so
# Generate a match.
XXXXXXXXXXprint("Reading: |{}|".format(First))
XXXXXXXXXXMatch = VAR_ROW.match(NextLine)
XXXXXXXXXXprint("Groups found: {}".format(Match.groups()))
# At this point the variable values should be split
# via string.split and then a new variable instance
# should be made. That is left to students.
# And read the next line.
XXXXXXXXXXNextLine = InputStream.readline()[:-1]


print("End vars read.")
return(Variables)



VARS
WA : XXXXXXXXXX
ID : XXXXXXXXXX
OR : XXXXXXXXXX
NV : XXXXXXXXXX
CA : XXXXXXXXXX
ND : XXXXXXXXXX
MT : XXXXXXXXXX
UT : XXXXXXXXXX
AZ : XXXXXXXXXX
WY : XXXXXXXXXX
CO : XXXXXXXXXX
NM : XXXXXXXXXX
SD : XXXXXXXXXX
NE : XXXXXXXXXX
KS : XXXXXXXXXX
OK : XXXXXXXXXX
TX : XXXXXXXXXX
MN : XXXXXXXXXX
IA : XXXXXXXXXX
MO : XXXXXXXXXX
AR : XXXXXXXXXX
LA : XXXXXXXXXX
WI : XXXXXXXXXX
IL : XXXXXXXXXX
MS : XXXXXXXXXX
MI : XXXXXXXXXX
IN : XXXXXXXXXX
KY : XXXXXXXXXX
TN : XXXXXXXXXX
AL : XXXXXXXXXX
OH : XXXXXXXXXX
WV : XXXXXXXXXX
VA : XXXXXXXXXX
GA : XXXXXXXXXX
FL : XXXXXXXXXX
PA : XXXXXXXXXX
MD : XXXXXXXXXX
DC : XXXXXXXXXX
NC : XXXXXXXXXX
SC : XXXXXXXXXX
VT : XXXXXXXXXX
NY : XXXXXXXXXX
NJ : XXXXXXXXXX
DE : XXXXXXXXXX
NH : XXXXXXXXXX
MA : XXXXXXXXXX
CT : XXXXXXXXXX
ME : XXXXXXXXXX
RI : XXXXXXXXXX
ENDVARS
CONSTRAINTS
!= WA OR
!= WA ID
!= CA OR
!= CA NV
!= CA AZ
!= OR ID
!= OR NV
!= NV ID
!= NV UT
!=
Answered 3 days After Aug 13, 2021

Solution

Sandeep Kumar answered on Aug 13 2021
156 Votes
# Read in the constraint code.
# =================================
VAR_START = re.compile("^VARS")
VAR_END = re.compile("^ENDVARS")
VAR_ROW = re.compile("^(?P[A-Z]+) : (?P[A-Za-z0-9 ]+)")
VAR_CON_START = re.compile("^CONSTRAINTS")
VAR_CON_END = re.compile("^ENDCONSTRAINTS")
VAR_CON_ROW = re.compile("^!=(?P[A-Z]+) (?P[A-Z]+)")

def readConstraintGraph(InFile):
"""
Read in the constraint graph. This
relies on the regular expressions
to match each row.
"""

with(open(InFile, "r") as Input):
Variables = readVars(Input)
Constraints = readVars(Input)
# Now add in the construction.
First = InFile.readline()[:-1]
print("Reading: |{}|".format(First))
if...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here