Skip to content

Commit

Permalink
ex2es01 cache feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kainoj committed Mar 30, 2018
1 parent deb1367 commit a4477d4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
57 changes: 31 additions & 26 deletions es02/ex1.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def __init__(self, rows, cols, row_desc, col_desc):
self.col_desc = col_desc # Cols description
self.nono = np.zeros((rows, cols), dtype=np.int8) # A board matrix

# self.row = [[] for _ in range(rows)]
# self.col = [[] for _ in range(cols)]
# Cache row / cols arragement cache[0] - rows, cache[1] - cols
self.cache = [[], []]

self.MAXITER = 5000 # Max. number of iterations of solve()
self.MAXITER = 20000 # Max. number of iterations of solve()

def __str__(self):
return '\n'.join([''.join(["#" if v == 1 else "." for v in row])
Expand All @@ -27,7 +27,7 @@ def __str__(self):
def __repr__(self):
return self.__str__()

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

row_desc = list(row_desc)
Expand Down Expand Up @@ -110,6 +110,17 @@ def presolve(self):
self.presolve_row()
self.transpose()

def presolveCache(self):
"""
For each row (cache[0]) and col (chache[1]) description retrun cache
list of possible row (cols) arrangement
"""
self.cache[0] = [self.genPossibleRows(row, self.c)
for row in self.row_desc]

self.cache[1] = [self.genPossibleRows(col, self.r)
for col in self.col_desc]


def badRows(self):
"""
Expand Down Expand Up @@ -175,8 +186,8 @@ def solve(self):
self.nono[rowno][colno] = (1 if self.nono[rowno][colno] == 0
else 0)

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


Expand All @@ -185,37 +196,31 @@ def solve(self):
finput = 'zad_input.txt'
foutput = 'zad_output.txt'

# finput = 'data/ex01.tst'


finput = 'data/zad1_input.tst'


lines = []
with open(finput) as f:
for line in f:
lines.append(list(map(int, line.strip('\n').split())))

r = lines[0][0]
c = lines[0][1]

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

# nono.info()
# nono.presolve()
# print(nono)

# nono = Nonogram(2, 5, [[3], [0]], [[], [], [], [2], []])
# nono.info()
# print(nono)
# nono.presolve()
nono.info()

nono.presolve()
nono.solve()
print("prechaching...")
nono.presolveCache()
print("dine")

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

# x = nono.genPossibleRows([1, 1, 2], 7)
# print(x)
# for r in nono.cache:
# print(r)
# print("----")
# nono.presolve()
# nono.solve()

# x = nono.genPossibleRowsPerm([1], 5)
# print(x)

# fout = open(foutput, "w")
# print(nono, file=fout)
27 changes: 21 additions & 6 deletions es02/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,28 @@
6. Wymuszenie użycia STDIN/STDOUT do komunikacji:
`python validator.py --stdio zad1 python rozwiazanie.py`
7. Ustawienie mnożnika dla limitów czasowych:
`python validator.py --timeout-multiplier 2.5 zad1 python rozwiazanie.py`
'''

from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

import argparse
import numpy as np
import os
import signal
import subprocess
import sys
import threading
import yaml
import time
from adodbapi.process_connect_string import process

import numpy as np

import yaml


VERBOSE = False

Expand Down Expand Up @@ -965,7 +971,8 @@
##### SS #
#S #
######################
Length= 28
out: 28
- inp: |
######################
# # SS## G #
# # SS## #
Expand Down Expand Up @@ -1420,9 +1427,10 @@ def kill_proc(process):
os.killpg(os.getpgid(process.pid), signal.SIGTERM)


def run_and_score_case(program, defaults, case_def, validator):
def run_and_score_case(program, defaults, case_def, validator, timeout_multiplier):
opts = dict(defaults)
opts.update(case_def)
opts['timeout'] *= timeout_multiplier
process_out, elapsed_time = run_case(program, **opts)
if VERBOSE:
print("Got output:")
Expand Down Expand Up @@ -1519,6 +1527,9 @@ def get_argparser():
parser.add_argument(
'--show_example', default=False, action='store_true',
help='Print a sample input/output pair.')
parser.add_argument(
'--timeout-multiplier', '-tm',
help='Multiply timeout by provided amount, e.g. 2.13')
parser.add_argument(
'--verbose', default=False, action='store_true',
help='Print more information about solutions.')
Expand Down Expand Up @@ -1590,11 +1601,12 @@ def get_cases(problem_def, cases):
for case_num, case_def in problem_cases:
print('Running case %d... ' % (case_num,), end='')
try:
timeout_multiplier = float(args.timeout_multiplier) if args.timeout_multiplier and float(args.timeout_multiplier) > 1 else 1
if args.stdio:
case_def['input_file'] = '<stdin>'
case_def['output_file'] = '<stdout>'
case_meas = run_and_score_case(
program, problem_def['defaults'], case_def, problem_validator)
program, problem_def['defaults'], case_def, problem_validator, timeout_multiplier)
ok_cases.append((case_num, case_meas))
print('OK!')
except ValidatorException as e:
Expand All @@ -1618,9 +1630,12 @@ def get_cases(problem_def, cases):
misc_opts = ''
if args.verbose:
misc_opts = ' --verbose'
if args.timeout_multiplier:
misc_opts += ' --timeout-multiplier ' + args.timeout_multiplier
if args.testset:
misc_opts = '%s --testset %s' % (
misc_opts, shellquote(args.testset),)
cases_opt = '--cases ' + ','.join([str(fc) for fc in failed_cases])
print('python validator.py%s %s %s %s' %
(misc_opts, cases_opt, args.problem, program))

0 comments on commit a4477d4

Please sign in to comment.