def fitness(sol, constraints): rules, nLines, nColumns, nPoints, nPopulation = constraints # Count how many rules it is following count = 0 game = nonogram.Game(nLines, nColumns, sol) board = sol # Count in lines in ascending order for lineIndex in range(nLines): rulesQtt = len(rules.lines[lineIndex]) columnIndex = 0 ruleIndex = 0 while columnIndex < nColumns or ruleIndex < rulesQtt: countSegment = 0 currRule = rules.lines[lineIndex][ ruleIndex] if ruleIndex < rulesQtt else 0 while columnIndex < nColumns and not board[lineIndex * nColumns + columnIndex]: columnIndex += 1 while columnIndex < nColumns and board[lineIndex * nColumns + columnIndex]: countSegment += 1 columnIndex += 1 count -= abs(countSegment - currRule) ruleIndex += 1 # Count in columns in ascending order for columnIndex in range(nColumns): rulesQtt = len(rules.columns[columnIndex]) lineIndex = 0 ruleIndex = 0 while lineIndex < nLines or ruleIndex < rulesQtt: countSegment = 0 currRule = rules.columns[columnIndex][ ruleIndex] if ruleIndex < rulesQtt else 0 while lineIndex < nLines and not board[lineIndex * nColumns + columnIndex]: lineIndex += 1 while lineIndex < nLines and board[lineIndex * nColumns + columnIndex]: countSegment += 1 lineIndex += 1 count -= abs(countSegment - currRule) ruleIndex += 1 return count
def myGameFake(points=False): gamePoints = [(1,i) for i in range(2,5)] gamePoints += [(3,i) for i in range(1,6)] gamePoints += [(4,0),(2,6)] gamePoints += [(2,i) for i in range(0,13) if i != 7] gamePoints += [(0,i) for i in range(0,7)]+[(4,8),(4,10),(4,12)] gamePoints += [(j,i) for j in range(5,8) for i in range(0,13) if i != 1 and i != 7] gamePoints += [(j,i) for j in range(8,12) for i in range(0,11) if (i != 1 and i != 7 and (j,i) != (10,9)) or ((j,i) == (11,1))] gamePoints += [(11,0),(12,6)] gamePoints += [(0,i) for i in range(1,6)] if points: return gamePoints game = nonogram.Game(nLines=14,nColumns=13,points=gamePoints) return game
def createPuzzle(): lines = [[2], [9], [2,4,2], [1,3,1], [1,4,2], [10], [10], [1,1,1,1], [1,1], [10]] columns = [[5,1], [2,3,1], [1,2,2], [8,1], [7,1], [6,1], [2,3,1], [1,3,1], [2,3,2], [7,1]] rules = nonogram.Rules(lines, columns) filledSquares = [(0,3),(0,4)] filledSquares += [(1,i) for i in range(1,10)] filledSquares += [(2,i) for i in range(0,10) if i != 2 and i != 7] filledSquares += [(3,0)]+[(3,i) for i in range(3,6)]+[(3,9)] filledSquares += [(4,0)]+[(4,i) for i in range(3,7)]+[(4,8),(4,9)] filledSquares += [(j,i) for i in range(0,10) for j in range(5,7)] filledSquares += [(7,1),(7,3),(7,7),(7,9)] filledSquares += [(8,2),(8,8)] filledSquares += [(9,i) for i in range(0,10)] puzzle = nonogram.Game(nColumns=10, nLines=10, points=filledSquares) return puzzle, rules
def printSol(sol, constraints): rules, nLines, nColumns, nPoints, nPopulation = constraints print(nonogram.Game(nLines, nColumns, sol.points))
def testNonogram(): filled = [(0, 0), (2,1), (3,5), (7, 7), (1,2), (5,3), (2,2)] ng = nonogram.Game(nLines=8, nColumns=8, points=filled) print(ng)