1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | #!/usr/bin/env python3 import sys import random import re numbers = [ int (s) for s in re.findall( '\d+' , sys.stdin.read())] if len (numbers) < = 1 : print ( "Usage:" , sys.argv[ 0 ]) print ( " Read from standard input in format:" , "n queen1x queen1y queen2x queen2y ..." ) print ( " All non-digit/whitespace characters ignored" ) sys.exit( 0 ) n = int (numbers[ 0 ]) outputComments = True rows = [ 0 for i in range (n) ] cols = [ 0 for i in range (n) ] d1 = [ 0 for i in range ( 2 * n - 1 ) ] d2 = [ 0 for i in range ( 2 * n - 1 ) ] numSatVars = 0 def readqueens(): numargs = int ( len (numbers) - 1 ) for i in range ( int (numargs / 2 )): queenx = int (numbers[i * 2 + 1 ]) queeny = int (numbers[i * 2 + 2 ]) if noattack([queenx,queeny]): rows[queenx] = 1 cols[queeny] = 1 d1[queenx + queeny] = 1 d2[queenx - queeny + n - 1 ] = 1 else : outputFalseSatInstance() def noattack(q): return (rows[q[ 0 ]] = = 0 and cols[q[ 1 ]] = = 0 and (d1[q[ 0 ] + q[ 1 ]] = = 0 ) and (d2[q[ 0 ] - q[ 1 ] + n - 1 ] = = 0 ) ) def queen2var(q): global numSatVars rawInt = q[ 0 ] * n + q[ 1 ] if rawInt not in int2var: numSatVars + = 1 qv = len (int2var) + 1 int2var[rawInt] = qv var2queen[qv] = q return int2var[rawInt] def processqueens(): global outrows global outcols global outd1 global outd2 outrows = [ [] for i in range (n) ] outcols = [ [] for i in range (n) ] outd1 = [ [] for i in range ( 2 * n - 1 ) ] outd2 = [ [] for i in range ( 2 * n - 1 ) ] openrows = [] opencols = [] for i in range (n): if rows[i] = = 0 : openrows.append(i) if cols[i] = = 0 : opencols.append(i) for i in openrows: for j in opencols: q = [i,j] if noattack(q): qv = queen2var(q) outrows[i].append(qv) outcols[j].append(qv) outd1[i + j].append(qv) outd2[i - j + n - 1 ].append(qv) for i in openrows: if outrows[i] = = []: outputComplexFalseSatInstance() for i in opencols: if outcols[i] = = []: outputComplexFalseSatInstance() int2var = {} var2queen = {} def outputFalseSatInstance(): print ( "c Unsatisfiability detected in preprocessing" ) print ( "c " ) print ( "p cnf 1 1" ) print ( "0" ) sys.exit( 0 ) def outputComplexFalseSatInstance(): print ( "c This instance contains a row or column where no queens can be placed" ) outputFalseSatInstance() #### Commander Encoding clauses = [] maxSize = 3 def varAlloc(): global numSatVars numSatVars + = 1 return numSatVars def makeCommanderClauses(varList,commander): global clauses numVars = len (varList) # if commander true then at least one is true c1 = list ( map ( lambda x: x,varList)) c1.append( - commander) clauses.append(c1) # if commander is false then all false for var in varList: clauses.append([commander, - var]) # naive at most one for vars for i in range (numVars - 1 ): for j in range (i + 1 ,numVars): clauses.append([ - varList[i], - varList[j]]) # returns variable which is equivalent to the or of variables in the varList # adds needed clauses to the global clauses list to enforce this and that at most one of varlist is true def makeCommanderFromVars(varList): if not isinstance (varList, list ): # assume that input is a single variable return varList numVars = len (varList) if numVars = = 1 : return varList[ 0 ] # no extra clauses necessary commander = varAlloc() makeCommanderClauses(varList,commander) return commander def groupVarsAndMakeCommander(varList): numVars = len (varList) if numVars = = 1 : return varList[ 0 ] newVarList = [] numGrps = (numVars + maxSize - 1 ) / / maxSize for i in range (numGrps): newVarList.append(makeCommanderFromVars(varList[i * maxSize:(i + 1 ) * maxSize])) return groupVarsAndMakeCommander(newVarList) def printClausesDimacs(commentsFlag): if commentsFlag: print ( "c n queens completion SAT translation" ) print ( "c n = " , n) print ( "c num queens placed = " , ( len (numbers) / / 2 )) print ( "c " ) print ( "c Placed Queens:" , numbers[ 1 :]) print ( "c " ) print ( "c Variable to queen Translation" ) for i in range ( len (var2queen)): print ( "c Pete " ,i + 1 , " " ,var2queen[i + 1 ][ 0 ], " " ,var2queen[i + 1 ][ 1 ]) print ( "p cnf " ,numSatVars, " " , len (clauses)) for clause in clauses: for lit in clause: print (lit,end = " " ) print ( 0 ) # do some work readqueens() processqueens() clauses = [] outrows = ( list ( filter ( lambda a: len (a) > 0 , outrows))) outcols = ( list ( filter ( lambda a: len (a) > 0 , outcols))) outd1 = ( list ( filter ( lambda a: len (a) > 1 , outd1))) outd2 = ( list ( filter ( lambda a: len (a) > 1 , outd2))) for file in outrows + outcols: fileVar = groupVarsAndMakeCommander( file ) clauses.append([fileVar]) for diag in outd1 + outd2: groupVarsAndMakeCommander(diag) # not maximally efficient as don't always need commander variable # always create variable equivalent to diag but don't use that var printClausesDimacs(outputComments) sys.exit( 0 ) |