Example #1
0
def main():
    logging.basicConfig(
        level=logging.INFO,
        format='%(levelname).1s %(module)10.10s:%(lineno)-4d %(message)s')

    cars = {}
    logger.info('Pretending to scrape some cars...')
    for _ in range(10):
        name = random.choice('abcdefgh') + random.choice('qwerty')
        data = [random.randrange(10) for _ in range(random.randrange(5, 10))]
        logger.info(f'Found car {name!r} {data}')
        cars[name] = data

    conn = db.get_conn()
    cur = conn.cursor()
    for name, data in cars.items():
        cur.execute(
            '''
            INSERT INTO cars(name, data, invocation_id, timestamp)
            VALUES (%s, %s, %s, %s)
            ON CONFLICT DO NOTHING
            RETURNING id
            ''', [
                name,
                json.dumps(data),
                db.get_this_invocation_id(conn),
                time.time()
            ])
        res = cur.fetchall()
        if res:
            [[car_id]] = res
            logger.info(f'Adding car/{car_id}')
        else:
            logger.info(f'Car {name!r} already exists')
        conn.commit()
def put_trace(conn, problem_id: int, result: Result):
    assert result.status in ('DONE', 'PASS', 'FAIL',
                             'CHECK_FAIL'), result.status

    if result.trace is not None:
        trace_data = zlib.compress(result.trace)
    else:
        trace_data = None
    extra = json.dumps(result.extra)

    cur = conn.cursor()
    cur.execute(
        '''
        INSERT INTO traces(
            scent, status, energy, data, extra,
            problem_id, invocation_id, timestamp)
        VALUES(%s, %s, %s, %s, %s, %s, %s, %s)
        RETURNING id
        ''', [
            result.scent, result.status, result.energy, trace_data, extra,
            problem_id,
            db.get_this_invocation_id(conn),
            time.time()
        ])
    [trace_id] = cur.fetchone()
    logging.info(f'Recorded as trace/{trace_id}')
Example #3
0
def upload_current_task(conn, block):
    cur = conn.cursor()

    name = f'block-{block.number:04d}'
    data = zlib.compress(block.task.encode())
    extra = {}

    cur.execute(
        '''
    UPDATE tasks SET
        obsolete = TRUE
    WHERE name LIKE 'block-%%' AND name != %s
    ''', [name])

    cur.execute(
        '''
        INSERT INTO tasks(
            name, data, extra, invocation_id, time)
        VALUES (%s, %s, %s, %s, NOW())
        ON CONFLICT DO NOTHING
        RETURNING id
        ''', [name, data,
              json.dumps(extra),
              db.get_this_invocation_id(conn)])
    res = cur.fetchall()
    if res:
        [[task_id]] = res
        logger.info(f'Uploaded {name!r} as /task/{task_id}')
    else:
        logger.info(f'Task {name!r} already exists')
    conn.commit()
def main():
    logging.basicConfig(
        level=logging.INFO,
        format='%(levelname).1s %(module)10.10s:%(lineno)-4d %(message)s')

    conn = db.get_conn()
    cur = conn.cursor()

    legends = {}
    for part_number, part_name in (1, 'initial'), (2, 'teleports'), (3,
                                                                     'clones'):
        with ZipFile(utils.project_root() / 'tasks' /
                     f'part-{part_number}-{part_name}.zip') as z:
            with z.open(f'part-{part_number}-legend.txt') as fin:
                for line in fin:
                    name, legend = line.decode().split(' - ', maxsplit=1)
                    legends[name] = legend.rstrip()

            for filename in z.namelist():
                if filename.endswith('-legend.txt'):
                    continue
                m = re.match(r'(prob-\d{3}).desc$', filename)
                assert m, filename
                name = m.group(1)

                with z.open(filename, 'r') as fin:
                    task = fin.read().decode()

                data = zlib.compress(task.encode())
                extra = dict(legend=legends[name])

                cur.execute(
                    '''
                    INSERT INTO tasks(
                        name, data, extra, invocation_id, time)
                    VALUES (%s, %s, %s, %s, NOW())
                    ON CONFLICT DO NOTHING
                    RETURNING id
                    ''', [
                        name, data,
                        json.dumps(extra),
                        db.get_this_invocation_id(conn)
                    ])
                res = cur.fetchall()
                if res:
                    [[task_id]] = res
                    logger.info(f'Uploaded {name!r} as /task/{task_id}')
                else:
                    logger.info(f'Task {name!r} already exists')

    db.record_this_invocation(conn, status=db.Stopped())
    conn.commit()
Example #5
0
def put_fuel(conn, car_id, result):
    if result.solution is not None:
        fuel_data = json.dumps(result.solution)
    else:
        fuel_data = None
    extra = json.dumps(result.extra)

    cur = conn.cursor()
    cur.execute('''
        INSERT INTO fuels(score, data, extra, car_id, invocation_id, timestamp)
        VALUES(%s, %s, %s, %s, %s, %s)
        RETURNING id
        ''',
        [result.score, fuel_data, extra,
         car_id, db.get_this_invocation_id(conn), time.time()])
    [fuel_id] = cur.fetchone()
    logging.info(f'Recorded as fuel/{fuel_id}')
def put_solution(conn, task_id: int, result: Result):
    assert result.status in ('DONE', 'PASS', 'FAIL',
                             'CHECK_FAIL'), result.status

    if result.solution is not None:
        data = zlib.compress(result.solution.encode())
    else:
        data = None
    extra = json.dumps(result.extra)

    cur = conn.cursor()
    cur.execute(
        '''
        INSERT INTO solutions(
            scent, status, score, data, extra,
            task_id, invocation_id, time)
        VALUES(%s, %s, %s, %s, %s, %s, %s, NOW())
        RETURNING id
        ''', [
            result.scent, result.status, result.score, data, extra, task_id,
            db.get_this_invocation_id(conn)
        ])
    [solution_id] = cur.fetchone()
    logging.info(f'Recorded as sol/{solution_id}')
def main():
    logging.basicConfig(
        level=logging.INFO,
        format='%(levelname).1s %(module)10.10s:%(lineno)-4d %(message)s')

    conn = db.get_conn()
    cur = conn.cursor()

    for name in sorted(data_files.full_names()):
        if not name.startswith('FR'):
            continue
        logging.info(name)

        src_data, tgt_data = data_files.full_problem(name)
        tgt_data = None

        stats = {}
        if src_data is not None:
            m = Model.parse(src_data)
            num_full_voxels = 0
            for pos in m.enum_voxels():
                if m[pos]:
                    num_full_voxels += 1
            stats.update(R=m.R, src_size=num_full_voxels)
            src_data = zlib.compress(src_data)
        if tgt_data is not None:
            m = Model.parse(tgt_data)
            num_full_voxels = 0
            for pos in m.enum_voxels():
                if m[pos]:
                    num_full_voxels += 1
            stats.update(R=m.R, tgt_size=num_full_voxels)
            tgt_data = zlib.compress(tgt_data)

        logging.info(stats)

        name = name.replace('FR', 'ZD')
        logging.info(name)

        extra = {}

        cur.execute(
            '''
            INSERT INTO problems(
                name, src_data, tgt_data, stats, extra, invocation_id, timestamp)
            VALUES (%s, %s, %s, %s, %s, %s, %s)
            ON CONFLICT DO NOTHING
            RETURNING id
            ''', [
                name, src_data, tgt_data,
                json.dumps(stats),
                json.dumps(extra),
                db.get_this_invocation_id(conn),
                time.time()
            ])
        res = cur.fetchall()
        if res:
            [[model_id]] = res
            logger.info(f'Recorded as model/{model_id}')
        else:
            logger.info(f'Model {name!r} already exists')
        conn.commit()
def main():
    logging.basicConfig(
        level=logging.INFO,
        format='%(levelname).1s %(module)10.10s:%(lineno)-4d %(message)s')

    conn = db.get_conn()
    cur = conn.cursor()
    cur.execute('''
        SELECT
            cars.name,
            fuels.id,
            fuels.score,
            COUNT(fuel_submissions.id),
            COUNT(fuel_submissions.data)
        FROM fuels
        JOIN cars ON fuels.car_id = cars.id
        LEFT OUTER JOIN fuel_submissions
        ON fuel_submissions.fuel_id = fuels.id
        WHERE fuels.score IS NOT NULL
        GROUP BY cars.name, fuels.id
    ''')

    # For each car, take all fuels that don't have failed submissions,
    # and submit the one with the best score
    # (unless it already has a successful submission).

    fuels_by_car_name = {}
    for car_name, fuel_id, fuel_score, num_submissions, num_successful_submissions in cur:
        if num_submissions > 0 and num_successful_submissions == 0:
            continue  # failed submission
        fuels = fuels_by_car_name.setdefault(car_name, [])
        fuels.append(FuelInfo(
            id=fuel_id, score=fuel_score,
            has_successful_submission=bool(num_successful_submissions)))

    done = False
    def signal_handler(sig, frame):
        nonlocal done
        done = True
        logging.warning('Caught Ctrl-C, will finish current item and exit.')
        logging.warning('To abort immediately, hit Ctrl-C again.')
        signal.signal(signal.SIGINT, old_signal_handler)
    old_signal_handler = signal.signal(signal.SIGINT, signal_handler)

    for car_name, fuels in fuels_by_car_name.items():
        if done:
            break

        fuel = max(fuels, key=lambda f: f.score)
        if fuel.has_successful_submission:
            logging.info(f'Best fuel for car {car_name!r} already submitted')
            continue

        cur.execute('SELECT data FROM fuels WHERE id = %s', [fuel.id])
        [fuel_data] = cur.fetchone()

        submission_data, extra = submit(car_name, fuel_data)
        if submission_data is not None:
            submission_data = json.dumps(submission_data)
        extra = json.dumps(extra)
        cur.execute('''
            INSERT INTO fuel_submissions(
                data, extra, fuel_id, invocation_id, timestamp)
            VALUES (%s, %s, %s, %s, %s)
            RETURNING id
            ''',
            [submission_data, extra, fuel.id, db.get_this_invocation_id(conn), time.time()])
        [submission_id] = cur.fetchone()
        logging.info(f'Submission recorded as fuel_sub/{submission_id}')
        conn.commit()