Skip to content

Commit

Permalink
es2 2pc
Browse files Browse the repository at this point in the history
  • Loading branch information
kainoj committed Mar 22, 2018
1 parent e2467f0 commit deb1367
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
1 change: 1 addition & 0 deletions es02/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ clean:

ex1:
python3 -m doctest ex1.py
python validator.py zad1 python3 ex1.py
39 changes: 23 additions & 16 deletions es02/ex1.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ def __str__(self):
def __repr__(self):
return self.__str__()

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

row_desc = list(row_desc)

if len(row_desc) == 1:
zeros = row_len - row_desc[0]
Expand All @@ -51,14 +53,14 @@ def genPossibleRows(self, row_desc, row_len):

return ans

#@lru_cache(maxsize=128)
def opt_dist(self, row, row_desc, row_len):
@lru_cache(maxsize=2**20)
def opt_dist_tuples(self, row, row_desc, row_len):
"""
Given a row and its description computes opt dist...
"""
a = row #.tolist()
mins = []
for b in self.genPossibleRows(row_desc, row_len):
for b in self.genPossibleRows(tuple(row_desc), row_len):
n = len(a) + 1
m = len(b) + 1
d = np.zeros((n, m), dtype=np.int8)
Expand All @@ -75,6 +77,8 @@ def opt_dist(self, row, row_desc, row_len):
mins.append(d[n-1][m-1])
return min(mins)

def opt_dist(self, row, row_desc, row_len):
return self.opt_dist_tuples(tuple(row), tuple(row_desc), row_len)


def info(self):
Expand Down Expand Up @@ -105,6 +109,7 @@ def presolve(self):
self.transpose()
self.presolve_row()
self.transpose()


def badRows(self):
"""
Expand Down Expand Up @@ -140,7 +145,7 @@ def randDecision(self):
return random.randrange(0, 99) < 20

def solve(self):
for _ in range(self.MAXITER):
for iterno in range(self.MAXITER):

badRows = self.badRows()

Expand All @@ -151,7 +156,7 @@ def solve(self):
rowno = random.randrange(0, self.r)
else:
rowno = random.choice(badRows)

# With probability 1/5 choose a random row
if self.randDecision():
rowno = random.randrange(0, self.r)
Expand All @@ -169,17 +174,20 @@ def solve(self):
# toggle
self.nono[rowno][colno] = (1 if self.nono[rowno][colno] == 0
else 0)


# print("dupa")
# print(self)
self.solve()


if __name__ == '__main__':

finput = 'data/zad1_input.tst'
foutput = 'zad1_output.txt'
finput = 'zad_input.txt'
foutput = 'zad_output.txt'

finput = 'data/ex01.tst'
# finput = 'data/ex01.tst'

fout = open(foutput, "w")


lines = []
with open(finput) as f:
Expand All @@ -198,13 +206,12 @@ def solve(self):
# nono.info()
# print(nono)
# nono.presolve()
# nono.info()
# print(nono)
# nono.presolve()

# print(nono)
nono.presolve()
nono.solve()
print(nono)

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

# x = nono.genPossibleRows([1, 1, 2], 7)
# print(x)
Expand Down

0 comments on commit deb1367

Please sign in to comment.