Skip to content

Commit

Permalink
es01ex01 final v1
Browse files Browse the repository at this point in the history
  • Loading branch information
kainoj committed Mar 30, 2018
1 parent 88020a7 commit 66cd782
Showing 1 changed file with 21 additions and 39 deletions.
60 changes: 21 additions & 39 deletions es02/ex1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import numpy as np
# import numpy as np

import itertools
import random
Expand Down Expand Up @@ -29,7 +29,6 @@ def __str__(self):
def __repr__(self):
return self.__str__()

# @lru_cache(maxsize=2**20)
def genPossibleRows(self, row_desc, row_len):

row_desc = list(row_desc)
Expand Down Expand Up @@ -100,29 +99,23 @@ def info(self):
print("Rows desc: {}".format(self.row_desc))
print("Cols desc: {}".format(self.col_desc))

def transpose(self):
self.nono = np.transpose(self.nono)
self.r, self.c = self.c, self.r
self.row_desc, self.col_desc = self.col_desc, self.row_desc
# self.row, self.col = self.col, self.row
# def transpose(self):
# self.nono = np.transpose(self.nono)
# self.r, self.c = self.c, self.r
# self.row_desc, self.col_desc = self.col_desc, self.row_desc
# # self.row, self.col = self.col, self.row

def presolve_row(self):
"""
>>> nono = Nonogram(2, 5, [[3], [0]], [[], [], [], [2], []])
>>> nono.presolve()
>>> nono.__str__() == '..##.\\n...#.'
True
"""
for r in range(self.r):
if len(self.row_desc[r]) == 1 and self.row_desc[r][0] > self.c / 2:
for i in range(self.c - self.row_desc[r][0], self.row_desc[r][0]):
self.nono[r][i] = 1

def presolve(self):
self.presolve_row()
self.transpose()
self.presolve_row()
self.transpose()
# self.transpose()
# self.presolve_row()
# self.transpose()

def presolveCache(self):
"""
Expand Down Expand Up @@ -164,7 +157,9 @@ def scoreColToggled(self, colno, pixno):
= 0 iff hasn't changed anything
< 0 iff made the score worse
"""
col = self.nono[:, colno]
# get column
# col = self.nono[:, colno]
col = [row[colno] for row in self.nono]
d = self.opt_dist(col, 1, colno)

col[pixno] = 1 if col[pixno] == 0 else 0
Expand All @@ -174,10 +169,8 @@ def scoreColToggled(self, colno, pixno):
return d - d2

def validateCols(self):
# print("O KURDE")
# print(self)
for c in range(self.c):
if self.opt_dist(self.nono[:, c].tolist(), 1, c) > 0:
if self.opt_dist([row[c] for row in self.nono], 1, c) > 0:
return False
return True

Expand All @@ -202,7 +195,6 @@ def solve(self):
rowno = self.badRows()

if rowno == -1 and self.validateCols():
print("iteracji: {}".format(iterno))
return

# With probability x/5 choose a random row
Expand All @@ -222,9 +214,8 @@ def solve(self):
if self.randDecision():
self.presolve()

# print("BANNG")
# print(self)
self.nono = np.zeros((self.r, self.c), dtype=np.int8)
# self.nono = np.zeros((self.r, self.c), dtype=np.int8)
self.nono = [[0 for c in range(self.c)] for r in range(self.r)]
self.presolve()
self.solve()

Expand All @@ -234,8 +225,7 @@ def solve(self):
finput = 'zad_input.txt'
foutput = 'zad_output.txt'

finput = 'data/ex01_heart.tst'

# finput = 'data/ex01_man.tst'

lines = []
with open(finput) as f:
Expand All @@ -247,20 +237,12 @@ def solve(self):

nono = Nonogram(r, c, row_desc=lines[1:r+1], col_desc=lines[r+1:])

nono.info()

print("prechaching...")
# nono.info()
nono.presolveCache()
print("done")

# for r in nono.cache:
# print(r)
# print("----")
nono.presolve()
print(nono)
nono.solve()
print(nono)

nono.solve()
# print(nono)

# fout = open(foutput, "w")
# print(nono, file=fout)
fout = open(foutput, "w")
print(nono, file=fout)

0 comments on commit 66cd782

Please sign in to comment.