Skip to content

Commit

Permalink
AtCoder Heuristic Contest 001
Browse files Browse the repository at this point in the history
  • Loading branch information
kmyk committed May 20, 2021
1 parent 762faf1 commit d40f1e7
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/format-python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: format-python

on: [push, pull_request]

jobs:
format-python:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: 3.6

- name: Install dependencies
run: pip3 install -r scripts/requirements.txt

- name: Run isort
run: isort --check-only --diff scripts/*.py

- name: Run yapf
run: |
yapf --diff '--style={ COLUMN_LIMIT: 9999 }' scripts/*.py
- name: Run mypy
run: mypy scripts/*.py
35 changes: 35 additions & 0 deletions .github/workflows/measure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: measure

on: [push, pull_request]

jobs:
measure:
runs-on: ubuntu-latest

strategy:
matrix:
seed: [0, 100, 200, 300, 400, 500, 600, 700, 800, 900]

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: 3.6

- name: Install dependencies
run: pip3 install -r scripts/requirements.txt

- name: Prepare tools/
run: |
wget https://img.atcoder.jp/ahc001/ded8fd3366b4ff0b0d7d053f553cdb84.zip
unzip ded8fd3366b4ff0b0d7d053f553cdb84.zip
- name: Compile the code
run: |
g++ -std=c++17 -Wall -O2 -Iac-library main.cpp
- name: Measure the score
run: |
python3 scripts/measure.py --jobs 2 --count 100 --seed ${{ matrix.seed }}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Kimiyuki Onaka

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
83 changes: 83 additions & 0 deletions scripts/measure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import argparse
import concurrent.futures
import os
import pathlib
import subprocess
import sys
from logging import DEBUG, basicConfig, getLogger
from typing import *

logger = getLogger(__name__)


def gen(*, seeds: List[int]) -> None:
logger.info('running the generator...')
with open('seeds.txt', 'w') as fh:
for seed in seeds:
print(seed, file=fh)
with open('seeds.txt') as fh:
subprocess.check_call(['cargo', 'run', '--manifest-path', str(pathlib.Path('tools', 'Cargo.toml')), '--bin', 'gen'], stdin=fh)


def run(*, command: str, input_path: pathlib.Path, output_path: pathlib.Path, seed: int) -> None:
logger.info('running the command for seed %d...', seed)
try:
with open(input_path) as fh1:
with open(output_path, 'w') as fh2:
subprocess.check_call(command, stdin=fh1, stdout=fh2)
except subprocess.SubprocessError:
logger.exception('failed for seed = %d', seed)


def vis(*, input_path: pathlib.Path, output_path: pathlib.Path, vis_path: pathlib.Path, seed: int) -> int:
logger.info('running the visualizer for seed %d...', seed)
try:
score_str = subprocess.check_output(['cargo', 'run', '--manifest-path', str(pathlib.Path('tools', 'Cargo.toml')), '--bin', 'vis', '--', str(input_path), str(output_path)])
except subprocess.SubprocessError:
logger.exception('failed for seed = %d', seed)
return 0
os.rename('out.svg', vis_path)
return int(score_str)


def main() -> 'NoReturn':
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--command', default='./a.out')
parser.add_argument('-n', '--count', type=int, default=50)
parser.add_argument('-j', '--jobs', type=int, default=2)
parser.add_argument('--seed', type=int, default=0)
args = parser.parse_args()

basicConfig(level=DEBUG)

# gen
seeds = [args.seed + i for i in range(args.count)]
gen(seeds=seeds)

# run
pathlib.Path('out').mkdir(exist_ok=True)
with concurrent.futures.ThreadPoolExecutor(max_workers=args.jobs) as executor:
for i, seed in enumerate(seeds):
input_path = pathlib.Path('in', '%04d.txt' % i)
output_path = pathlib.Path('out', '%04d.txt' % i)
executor.submit(run, command=args.command, input_path=input_path, output_path=output_path, seed=seed)

# vis
pathlib.Path('vis').mkdir(exist_ok=True)
scores: List[int] = []
for i, seed in enumerate(seeds):
input_path = pathlib.Path('in', '%04d.txt' % i)
output_path = pathlib.Path('out', '%04d.txt' % i)
vis_path = pathlib.Path('vis', '%04d.svg' % i)
score = vis(input_path=input_path, output_path=output_path, vis_path=vis_path, seed=seed)
scores.append(score)
logger.info('seed = {}: score = {}'.format(seed, score))
logger.info('50 * average = %d', int(50 * sum(scores) / len(scores)))

if min(scores) <= 0:
sys.exit(1)
sys.exit(0)


if __name__ == '__main__':
main()
3 changes: 3 additions & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
isort==5.7.0
mypy==0.812
yapf==0.30.0

0 comments on commit d40f1e7

Please sign in to comment.