Last active
March 26, 2020 08:49
-
-
Save ibmua/6d52ffcf36f84865b671c72f38c71189 to your computer and use it in GitHub Desktop.
Bulk-run TopCoder Marathon Match 116 solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python3 launcher.py 0 parallel 4 name | |
# DO CHANGE THE DIRECTORIES TO YOURS | |
# ALSO, in ' /home/i/C/marathon/116/stats" -seed ' | |
# /home/i/C/marathon/116/stats can be removed -- it's just for storing some additional statistics. | |
from multiprocessing.pool import ThreadPool | |
import os | |
import sys | |
import json | |
import random | |
import string | |
import time | |
import subprocess | |
from timeit import default_timer as timer | |
rnd_str = ''.join(random.choices(string.ascii_uppercase + string.digits, k=2)) | |
start = int(sys.argv[1]) | |
parallel = sys.argv[2] == "parallel" | |
thread_n = int(sys.argv[3]) # set to the number of workers you want (it defaults to the cpu count of your machine) | |
comment = sys.argv[4] | |
launches = thread_n* 40 | |
rnd_str = comment + "_" + rnd_str | |
seed = start | |
params = "" | |
# params = " -N 6 -P 0.8 " | |
# params = " -N 10 -P 0.25 " | |
mod = "" | |
# mod = ".2sN10P25_2O" | |
# mod = "-c00rd" | |
# mod = "-old-submission" | |
# mod = "-other" | |
# mod = "-last-sub" | |
# mod = "-final" | |
mod = "-really-pre-last" | |
# mod = "-prelast-sub" | |
# mod = "-other" | |
def work(seed): | |
# my_tool_subprocess = subprocess.Popen(["java","-jar","tester.jar","-exec",'/home/i/C/marathon/116/exec/Lossy2dCompression-' +rnd_str+ ' /home/i/C/marathon/116/outs/'+ rnd_str +' '+str(seed)+ ' /home/i/C/marathon/116/stats',"-seed",str(seed),"-novis"] , stdout=subprocess.PIPE , stderr=subprocess.PIPE) | |
# proc = subprocess.Popen('mytool {}'.format(sample),shell=True, stdout=subprocess.PIPE) | |
# proc.wait() | |
start = timer() | |
os.system( 'java -jar tester.jar -exec "/home/i/C/marathon/116/exec/Lossy2dCompression-' +rnd_str+ ' /home/i/C/marathon/116/outs/'+ rnd_str +' '+str(seed)+ ' /home/i/C/marathon/116/stats" -seed '+str(seed) + params +' -novis >> /dev/null' ) | |
print('--Done for seed', seed) | |
end = timer() | |
if end- start > 9.9: | |
print(end - start, "\033[31m"+"FUCK"+"\033[0m") | |
else: | |
print(end - start) | |
print(rnd_str, params, mod) | |
cpy = "cp /home/i/C/marathon/116/Lossy2dCompression"+mod+".cpp /home/i/C/marathon/116/exec/"+rnd_str+'.cpp' | |
comp = "g++ -std=gnu++11 -include bits/stdc++.h -O3 /home/i/C/marathon/116/Lossy2dCompression"+mod+".cpp -o /home/i/C/marathon/116/exec/Lossy2dCompression-"+rnd_str | |
print(cpy) | |
os.system( cpy ) | |
print(comp) | |
os.system( comp ) | |
print("starting at", start) | |
for i in range(start+0,start+10000): | |
if parallel: | |
tp = ThreadPool(thread_n) | |
for sd in range(1+ launches*i, 1+ launches*(i+1) ): | |
seed += 1 | |
print('++Calculating for seed', seed) | |
if parallel: | |
tp.apply_async(work, (seed,)) | |
# p.append( subprocess.Popen(["java","-jar","tester.jar","-exec",'/home/i/C/marathon/116/exec/Lossy2dCompression-' +rnd_str+ ' /home/i/C/marathon/116/outs/'+ rnd_str +' '+str(seed)+ ' /home/i/C/marathon/116/stats',"-seed",str(seed),"-novis"] , stdout=subprocess.PIPE , stderr=subprocess.PIPE) ) | |
# time.sleep(0.2) | |
else: | |
os.system( 'java -jar tester.jar -exec "/home/i/C/marathon/116/exec/Lossy2dCompression-' +rnd_str+ ' /home/i/C/marathon/116/outs/'+ rnd_str +' '+str(seed)+ ' /home/i/C/marathon/116/stats" -seed '+str(seed)+' -novis' + params ) | |
print('--Calculated for seed', seed) | |
# if parallel: | |
# for proc in p: | |
# proc.wait() | |
# (output, error) = proc.communicate() | |
# print(output.decode()) | |
# print('--') | |
# seed += 1 | |
# print('++Calculating for seed', seed) | |
# p.append( subprocess.Popen(["java","-jar","tester.jar","-exec",'/home/i/C/marathon/116/exec/Lossy2dCompression-' +rnd_str+ ' /home/i/C/marathon/116/outs/'+ rnd_str +' '+str(seed)+ ' /home/i/C/marathon/116/stats',"-seed",str(seed),"-novis"] , stdout=subprocess.PIPE , stderr=subprocess.PIPE) ) | |
print(' ', rnd_str, comment) | |
if parallel: | |
tp.close() | |
tp.join() | |
print('---') | |
mins = [1111.]*100000 | |
directory = '/home/i/C/marathon/116/outs/' | |
print( str( len(os.listdir(directory)) )) | |
for filename in os.listdir(directory): | |
with open(directory + filename, 'r') as myfile: | |
data=myfile.read() | |
#print(data) | |
lines = [ tuple(float(n) for n in x.split(' ')) for x in data.strip().split('\n') ] | |
lines.sort() | |
#print(lines) | |
for l in lines: | |
mins[ int(l[0]) ] = min( mins[ int(l[0]) ] , l[1] ) | |
#print('--') | |
SCORES = [] | |
with open(directory + rnd_str, 'r') as myfile: | |
data=myfile.read() | |
lines = [ tuple(float(n) for n in x.split(' ')) for x in data.strip().split('\n') ] | |
score = 0. | |
for l in lines: | |
score += 1./len(lines) * mins[ int(l[0]) ] / l[1] | |
print(rnd_str, score,'-', len(lines),'tests') | |
SCORES.append(score) | |
for filename in os.listdir(directory): | |
if rnd_str == filename: | |
continue | |
with open(directory + filename, 'r') as myfile: | |
data=myfile.read() | |
lines = [ tuple(float(n) for n in x.split(' ')) for x in data.strip().split('\n') ] | |
score = 0. | |
for l in lines: | |
score += 1./len(lines) * mins[ int(l[0]) ] / l[1] | |
print(filename, score,'-', len(lines),'tests') | |
SCORES.append(score) | |
SCORES[1:] = sorted(SCORES[1:], reverse=True) | |
print(SCORES) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment