implement multi-processing for instance solving with sat

This commit is contained in:
Maximilian Keßler 2023-03-14 00:31:21 +01:00
parent 151e15177f
commit 725d08d67b
Signed by: max
GPG key ID: BCC5A619923C0BA5

View file

@ -7,11 +7,13 @@ from download_data import export_game
from variants import num_suits, VARIANTS from variants import num_suits, VARIANTS
from alive_progress import alive_bar from alive_progress import alive_bar
from compress import decompress_deck from compress import decompress_deck
from concurrent.futures import ProcessPoolExecutor import concurrent.futures
from threading import Lock from threading import Lock
from time import sleep from time import sleep
MAX_PROCESSES=6
def update_seeds_db(): def update_seeds_db():
cur2 = conn.cursor() cur2 = conn.cursor()
with conn.cursor() as cur: with conn.cursor() as cur:
@ -100,26 +102,22 @@ def get_decks_for_all_seeds():
bar() bar()
def solve_seed(seed, num_players, deck_compressed): mutex = Lock()
print('Process {} start'.format(seed))
deck = decompress_deck(deck_compressed)
print('Process {} decompressed deck: {}'.format(seed, deck))
solvable, solution = solve(deck, num_players)
# bar()
print('Process {} finished computation'.format(seed)) def solve_seed(seed, num_players, deck_compressed):
deck = decompress_deck(deck_compressed)
solvable, solution = solve(deck, num_players)
mutex.acquire() mutex.acquire()
print('Process {} got mutex'.format(seed))
lcur = conn.cursor() lcur = conn.cursor()
print('Process {} obtained cursor'.format(seed))
lcur.execute("UPDATE seeds SET feasible = (%s) WHERE seed = (%s)", (solvable, seed)) lcur.execute("UPDATE seeds SET feasible = (%s) WHERE seed = (%s)", (solvable, seed))
conn.commit() conn.commit()
if not solvable: if not solvable:
print('Seed {} ({} players) not solvable: {}'.format(seed, num_players, deck)) with open('unsolvable_seeds', 'a') as f:
print('thread finished') f.write('{}-player, seed {}\n'.format(num_players, seed))
# bar() # print('Seed {} ({} players) not solvable: {}'.format(seed, num_players, deck))
mutex.release() mutex.release()
@ -128,20 +126,12 @@ def solve_unknown_seeds():
for var in VARIANTS: for var in VARIANTS:
cur.execute("SELECT seed, num_players, deck FROM seeds WHERE variant_id = (%s) AND feasible IS NULL AND deck IS NOT NULL ORDER BY num_players DESC", (var['id'],)) cur.execute("SELECT seed, num_players, deck FROM seeds WHERE variant_id = (%s) AND feasible IS NULL AND deck IS NOT NULL ORDER BY num_players DESC", (var['id'],))
res = cur.fetchall() res = cur.fetchall()
mutex = Lock()
# with alive_bar(len(res), title='Seed solving on {}'.format(var['name'])) as bar: with concurrent.futures.ProcessPoolExecutor(max_workers=MAX_PROCESSES) as executor:
def test(): fs = [executor.submit(solve_seed, *r) for r in res]
print("test!") with alive_bar(len(res), title='Seed solving on {}'.format(var['name'])) as bar:
sleep(5) for f in concurrent.futures.as_completed(fs):
bar()
print('Starting process pool')
with ProcessPoolExecutor(max_workers=2) as executor:
for r in res[:5]:
print('submitting task: {}, {}, {}'.format(*r))
f = executor.submit(solve_seed, *r)
sleep(.5)
executor.shutdown(wait=True)
break break