Example #1
0
async def page_submit(request):
    username = request._username
    name = request.match_info['name']
    postdata = await request.post()
    proposed_input = postdata['proposed_input'].strip()
    correct_output = postdata['correct_output'].strip()

    if proposed_input != "" or correct_output != "":
        global contestant_access

        problem = problems.get_problem(name)
        # FIXME: -1000 to disable the submit check for when your score would be negative.
        # FIXME: There is no indication the the user when they failed this check
        mark_it = contestant_access == 3 or (
            contestant_access == 2 and await results.get_user_problem_total(
                username, problem.short_name) > -1000)

        print("SUBMISSION: %s %s [%s]" %
              (username, problem.short_name, "added to queue" if mark_it else [
                  "warning: impossible submission",
                  "ignored: submissions banned right now",
                  "ignored: too many points lost for this problem"
              ][contestant_access]))

        if mark_it:
            # Enqueue the task
            await database.connection.execute(ENQUE_TASK, username, name,
                                              proposed_input, correct_output)
            await asyncio.sleep(2)
    return aiohttp.web.HTTPSeeOther('/problem/' + name)
Example #2
0
def print_action(problem_id, action, solver_strs=None):
    """Print the output of an action performed on a problem.

    Get the problem with ID `problem_id` and perform `action` on it, using the
    solvers whose names match `solver_strs` and printing results to stdout.  If
    `solver_strs` is None, all of the problem's solvers are used."""

    print("== Problem {} ==".format(problem_id))

    try:
        problem = problems.get_problem(problem_id)
    except problems.ProblemImportError:
        print("Import failed.")
    except problems.WrongProblemError:
        print("The module for problem {} contains a wrong problem.".format(
            problem_id))
    else:
        solvers, unmatched_strs = find_solvers(problem, solver_strs)
        for unmatched_str in unmatched_strs:
            print(
                "There is no solver starting with {!r}.".format(unmatched_str))
        if solvers:
            action(problem, solvers)

    print()
Example #3
0
def print_action(problem_id, action, solver_strs=None):
    """Print the output of an action performed on a problem.

    Get the problem with ID `problem_id` and perform `action` on it, using the
    solvers whose names match `solver_strs` and printing results to stdout.  If
    `solver_strs` is None, all of the problem's solvers are used."""

    print("== Problem {} ==".format(problem_id))

    try:
        problem = problems.get_problem(problem_id)
    except problems.ProblemImportError:
        print("Import failed.")
    except problems.WrongProblemError:
        print("The module for problem {} contains a wrong problem."
              .format(problem_id))
    else:
        solvers, unmatched_strs = find_solvers(problem, solver_strs)
        for unmatched_str in unmatched_strs:
            print("There is no solver starting with {!r}."
                  .format(unmatched_str))
        if solvers:
            action(problem, solvers)

    print()
Example #4
0
def prepare_problem(problemname, ndim, nlive, sampler):
    loglike, grad, volume, warmup = get_problem(problemname, ndim=ndim)
    if hasattr(sampler, 'set_gradient'):
        sampler.set_gradient(grad)
    np.random.seed(1)
    us = np.random.uniform(size=(nlive, ndim))

    if ndim > 1:
        transformLayer = AffineLayer()
    else:
        transformLayer = ScalingLayer()
    transformLayer.optimize(us, us)
    region = MLFriends(us, transformLayer)
    region.maxradiussq, region.enlarge = region.compute_enlargement(
        nbootstraps=30)
    region.create_ellipsoid(minvol=1.0)

    Ls = np.array([loglike(u) for u in us])
    ncalls = 0
    nok = 0
    i = 0
    while True:
        if i % int(nlive * 0.2) == 0:
            minvol = (1 - 1. / nlive)**i
            nextTransformLayer = transformLayer.create_new(us,
                                                           region.maxradiussq,
                                                           minvol=minvol)
            nextregion = MLFriends(us, nextTransformLayer)
            nextregion.maxradiussq, nextregion.enlarge = nextregion.compute_enlargement(
                nbootstraps=30)
            if nextregion.estimate_volume() <= region.estimate_volume():
                region = nextregion
                transformLayer = region.transformLayer
            region.create_ellipsoid(minvol=minvol)

        # replace lowest likelihood point
        j = np.argmin(Ls)
        Lmin = float(Ls[j])
        while True:
            u, v, logl, nc = sampler.__next__(region, Lmin, us, Ls, transform,
                                              loglike)
            ncalls += nc
            if logl is not None:
                break

        us[j, :] = u
        region.u[j, :] = u
        region.unormed[j, :] = region.transformLayer.transform(u)
        Ls[j] = logl
        i = i + 1
        #print(i, Lmin, volume(Lmin, ndim))
        if np.isfinite(volume(Lmin, ndim)):
            nok += 1

        if nok > 2 * nlive + 1000:
            break
    return region, i, Lmin, us, Ls, transform, loglike
Example #5
0
async def page_problem_description(request):
    name = request.match_info['name']
    problem = problems.get_problem(name)
    res = await database.connection.fetch(SELECT_RESULTS, request._username,
                                          name)
    completed = await database.connection.fetch(SELECT_COMPLETED_PROBLEMS,
                                                request._username)
    completed = {r['problem'] for r in completed}
    best_score = 0
    for i in res:
        best_score = max(best_score, i['score'])

    global contestant_access

    # FIXME: -1000 to disable the submit check for when your score would be negative.
    # FIXME: There is no indication the the user when they failed this check
    mark_it = contestant_access == 3 or (
        contestant_access == 2 and await results.get_user_problem_total(
            request._username, problem.short_name) > -1000)

    return {
        'problem':
        problem,
        'code':
        pygments.highlight(
            problem.task_code if contestant_access != 0 else "\n\n\n",
            pygments.lexers.CppLexer(), pygments.formatters.HtmlFormatter()),
        'can_submit':
        mark_it,
        'show_submit':
        not contestant_access == 0
        and (problem.short_name not in completed or contestant_access == 3),
        'submit_message': [
            "", "We are currently not accepting submissions.",
            "You cannot submit any more attempts for this problem.", ""
        ][contestant_access],
        'results':
        res[::-1],
        'best_score':
        best_score,
        'username':
        request._display_name,
        'problems':
        problems.get_alphabetical(),
        'completed':
        completed,
        'is_admin':
        request._admin
    }
Example #6
0
async def run_worker(number):
    while should_run:
        jobs_to_do = await database.connection.fetch('SELECT * FROM results WHERE complete = FALSE ORDER BY id ASC')
        if len(jobs_to_do) > 0:
            job = jobs_to_do[0]
            print('Processing job -', job['id'])
            problem = problems.get_problem(job['problem'])
            score, status = await judge.run_judge(problem, job['proposed_input'], job['correct_output'])
            best_score = await results.get_user_problem_best(job['owner'], job['problem'])
            if best_score > 0 and score > 0:
                score = 0
                status = 'Code already broken!'
            await database.connection.execute(UPDATE_COMPLETED, job['id'], int(score), status)
            print('Finished -', status)
        else:
            await asyncio.sleep(0.2)
Example #7
0
def main():
    parser = argparse.ArgumentParser(description="Genetic algorithm")
    """ required arguments """
    required = parser.add_argument_group("required arguments")
    required.add_argument(
        "-problem",
        help="name of the problem at the problems.py module",
        required=True,
        choices=["maze", "even_odd", "ackley", "queens"],
    )
    required.add_argument("-gen",
                          type=int,
                          help="n of generations",
                          required=True)
    """ optional arguments """
    parser.add_argument("-lb", type=int, help="lower bound", default=0)
    parser.add_argument("-ub", type=int, help="upper bound", default=10)
    parser.add_argument("-ctax", type=float, help="crossover tax", default=0.8)
    parser.add_argument("-mtax", type=float, help="mutate tax", default=0.03)
    parser.add_argument("-csize", type=int, help="chromosome size", default=10)
    parser.add_argument("-tsize",
                        type=int,
                        help="tournment size (for tournment selection)")
    parser.add_argument("-psize", type=int, help="population size", default=10)

    args = parser.parse_args()
    problem = problems.get_problem(args.problem)

    pop = Population(
        problem,
        args.gen,
        args.psize,
        args.csize,
        args.ctax,
        args.mtax,
        args.lb,
        args.ub,
        args.tsize,
    )
    pop.evolve()
Example #8
0
    functions = {
        function_names[0]: f1,
        function_names[1]: f2,
        function_names[2]: f3,
    }
    solution_rater = solutions.SolutionRater("simulated_annealing", False)
    best_result = float("inf")

    for iterations_count in iterations_counts:
        for function_name in function_names:
            for function_arguments in functions_arguments[function_name]:
                r1 = []
                for problem_file in problem_files:
                    r2 = []
                    problem = problems.get_problem("file", problem_file)
                    for experiment in range(experiments):
                        sol_list, res_list = solutions.simulated_annealing(
                            solution_rater, problem, iterations_count,
                            functions[function_name], function_arguments)
                        if function_name == "f3" and \
                            iterations_count == 1000 and \
                            experiment == 0:
                            plt.plot(res_list)
                            plt.title(function_arguments)
                            plt.show()

                        r2.append(res_list[-1])
                    r1.append(sum(r2) / len(r2))
                result = sum(r1) / len(r1)
                if result < best_result:
Example #9
0
File: main.py Project: PRMTRG/MHE
 parser.add_argument("--problem_source", type=str)
 parser.add_argument("--random_problem_size", type=check_positive, default=5)
 parser.add_argument("--problem_file", type=str, default="")
 parser.add_argument("--solver", type=str)
 parser.add_argument("--iterations", type=check_positive, default=1000)
 parser.add_argument("--tabu_size", type=check_positive, default=100)
 parser.add_argument("--output_results_to_terminal", action="store_true")
 parser.add_argument("--output_results_to_file", action="store_true")
 parser.add_argument("--results_output_file", type=str, default="")
 parser.add_argument("--output_plot_data_to_file", action="store_true")
 parser.add_argument("--plot_data_output_file", type=str, default="")
 parser.add_argument("--plotting_step", type=check_positive, default=1)
 args = parser.parse_args()
 
 if args.problem_source == "random":
     problem = problems.get_problem("random", args.random_problem_size)
 elif args.problem_source == "file":
     problem = problems.get_problem("file", args.problem_file)
 else:
     print("Invalid problem source.")
     sys.exit()
 
 solver = solutions.Solver(args.solver, solutions.get_solver(args.solver))
 if args.solver == "bruteforce":
     solver.solve(problem,
                  output_results_to_terminal=args.output_results_to_terminal,
                  output_results_to_file=args.output_results_to_file,
                  results_output_file=args.results_output_file,
                  output_plot_data_to_file=args.output_plot_data_to_file,
                  plot_data_output_file=args.plot_data_output_file,
                  plotting_step=args.plotting_step
Example #10
0
def evaluate_warmed_sampler(problemname,
                            ndim,
                            nlive,
                            nsteps,
                            sampler,
                            seed=1,
                            region_class=RobustEllipsoidRegion):
    loglike, grad, volume, warmup = get_problem(problemname, ndim=ndim)
    if hasattr(sampler, 'set_gradient'):
        sampler.set_gradient(grad)
    np.random.seed(seed)

    def multi_loglike(xs):
        return np.asarray([loglike(x) for x in xs])

    us = np.array([warmup(ndim) for i in range(nlive)])
    Ls = np.array([loglike(u) for u in us])
    vol0 = volume(Ls.min(), ndim)
    nwarmup = 3 * nlive

    if ndim > 1:
        transformLayer = AffineLayer()
    else:
        transformLayer = ScalingLayer()
    transformLayer.optimize(us, us)
    region = region_class(us, transformLayer)
    region.maxradiussq, region.enlarge = region.compute_enlargement(
        nbootstraps=30)
    region.create_ellipsoid(minvol=vol0)
    assert region.ellipsoid_center is not None
    sampler.region_changed(Ls, region)

    Lsequence = []
    stepsequence = []
    ncalls = 0
    for i in tqdm.trange(nsteps + nwarmup):
        if i % int(nlive * 0.2) == 0:
            minvol = (1 - 1. / nlive)**i * vol0
            with warnings.catch_warnings(), np.errstate(all='raise'):
                try:
                    nextTransformLayer = transformLayer.create_new(
                        us, region.maxradiussq, minvol=minvol)
                    nextregion = region_class(us, nextTransformLayer)
                    nextregion.maxradiussq, nextregion.enlarge = nextregion.compute_enlargement(
                        nbootstraps=30)
                    if isinstance(nextregion, RobustEllipsoidRegion
                                  ) or nextregion.estimate_volume(
                                  ) <= region.estimate_volume():
                        nextregion.create_ellipsoid(minvol=minvol)
                        region = nextregion
                        transformLayer = region.transformLayer
                        assert region.ellipsoid_center is not None
                        sampler.region_changed(Ls, region)
                except Warning as w:
                    print("not updating region because: %s" % w)
                except FloatingPointError as e:
                    print("not updating region because: %s" % e)
                except np.linalg.LinAlgError as e:
                    print("not updating region because: %s" % e)

        # replace lowest likelihood point
        j = np.argmin(Ls)
        Lmin = float(Ls[j])
        while True:
            u, v, logl, nc = sampler.__next__(region, Lmin, us, Ls, transform,
                                              multi_loglike)
            if i > nwarmup:
                ncalls += nc
            if logl is not None:
                assert np.isfinite(u).all(), u
                assert np.isfinite(v).all(), v
                assert np.isfinite(logl), logl
                break

        if i > nwarmup:
            Lsequence.append(Lmin)
            stepsequence.append(quantify_step(us[sampler.starti, :], u))

        us[j, :] = u
        Ls[j] = logl

    Lsequence = np.asarray(Lsequence)
    return Lsequence, ncalls, np.array(stepsequence)
Example #11
0
def main(args):
    nlive = args.num_live_points
    ndim = args.x_dim
    nsteps = args.nsteps
    problemname = args.problem

    samplers = [
        #CubeMHSampler(nsteps=16), #CubeMHSampler(nsteps=4), CubeMHSampler(nsteps=1),
        #RegionMHSampler(nsteps=16), #RegionMHSampler(nsteps=4), RegionMHSampler(nsteps=1),
        ##DESampler(nsteps=16), DESampler(nsteps=4), #DESampler(nsteps=1),
        CubeSliceSampler(
            nsteps=2 * ndim
        ),  #CubeSliceSampler(nsteps=ndim), CubeSliceSampler(nsteps=max(1, ndim//2)),
        #RegionSliceSampler(nsteps=ndim), RegionSliceSampler(nsteps=max(1, ndim//2)),
        #RegionSliceSampler(nsteps=2), RegionSliceSampler(nsteps=4),
        #RegionSliceSampler(nsteps=ndim), RegionSliceSampler(nsteps=4*ndim),
        #RegionBallSliceSampler(nsteps=2*ndim), RegionBallSliceSampler(nsteps=ndim), RegionBallSliceSampler(nsteps=max(1, ndim//2)),
        # RegionSequentialSliceSampler(nsteps=2*ndim), RegionSequentialSliceSampler(nsteps=ndim), RegionSequentialSliceSampler(nsteps=max(1, ndim//2)),

        #SpeedVariableRegionSliceSampler([Ellipsis]*ndim), SpeedVariableRegionSliceSampler([slice(i, ndim) for i in range(ndim)]),
        #SpeedVariableRegionSliceSampler([Ellipsis]*ndim + [slice(1 + ndim//2, None)]*ndim),
    ]
    if ndim < 14:
        samplers.insert(0, MLFriendsSampler())
    colors = {}
    linestyles = {1: ':', 2: ':', 4: '--', 16: '-', 32: '-', 64: '-', -1: '-'}
    markers = {1: 'x', 2: 'x', 4: '^', 16: 'o', 32: 's', 64: 's', -1: 'o'}
    for isteps, ls, m in (max(1, ndim // 2), ':',
                          'x'), (ndim, '--',
                                 '^'), (ndim + 1, '--',
                                        '^'), (ndim * 2, '-',
                                               'o'), (ndim * 4, '-.',
                                                      '^'), (ndim * 8, '-',
                                                             'v'), (ndim * 16,
                                                                    '-', '>'):
        if isteps not in markers:
            markers[isteps] = m
        if isteps not in linestyles:
            linestyles[isteps] = ls
    Lsequence_ref = None
    label_ref = None
    axL = plt.figure('Lseq').gca()
    axS = plt.figure('shrinkage').gca()
    axspeed = plt.figure('speed').gca()
    plt.figure('stepsize', figsize=(14, 6))
    axstep1 = plt.subplot(1, 3, 1)
    axstep2 = plt.subplot(1, 3, 2)
    axstep3 = plt.subplot(1, 3, 3)
    lastspeed = None, None, None
    for sampler in samplers:
        print("evaluating sampler: %s" % sampler)
        Lsequence, ncalls, steps = evaluate_warmed_sampler(
            problemname, ndim, nlive, nsteps, sampler)

        loglike, grad, volume, warmup = get_problem(problemname, ndim=ndim)
        assert np.isfinite(Lsequence).all(), Lsequence
        vol = np.asarray([volume(Li, ndim) for Li in Lsequence])
        assert np.isfinite(vol).any(), (
            "Sampler has not reached interesting likelihoods", vol, Lsequence)
        shrinkage = 1 - (vol[np.isfinite(vol)][1:] /
                         vol[np.isfinite(vol)][:-1])**(1. / ndim)

        fullsamplername = str(sampler)
        samplername = fullsamplername.split('(')[0]

        label = fullsamplername + ' %d evals' % ncalls
        if Lsequence_ref is None:
            label_ref = label
            Lsequence_ref = Lsequence
            ls = '-'
            color = 'pink'
        else:
            color = colors.get(samplername)
            ls = '-' if sampler.adaptive_nsteps else linestyles[sampler.nsteps]
            l, = axL.plot(Lsequence_ref,
                          Lsequence,
                          label=label,
                          color=color,
                          linestyle=ls,
                          lw=1)
            colors[samplername] = l.get_color()

        # convert to a uniformly distributed variable, according to expectations
        cdf_expected = 1 - (1 - shrinkage)**(ndim * nlive)
        axS.hist(cdf_expected,
                 cumulative=True,
                 density=True,
                 histtype='step',
                 bins=np.linspace(0, 1, 4000),
                 label=label,
                 color=color,
                 ls=ls)
        print("%s shrunk %.4f, from %d shrinkage samples" %
              (fullsamplername, cdf_expected.mean(), len(shrinkage)))
        axspeed.plot(cdf_expected.mean(),
                     ncalls,
                     markers[1 if sampler.adaptive_nsteps else sampler.nsteps],
                     label=label,
                     color=color)
        if lastspeed[0] == samplername:
            axspeed.plot([lastspeed[1], cdf_expected.mean()],
                         [lastspeed[2], ncalls],
                         '-',
                         color=color)
        lastspeed = [samplername, cdf_expected.mean(), ncalls]

        stepsize, angular_step, radial_step = steps.transpose()
        assert len(stepsize) == len(Lsequence), (len(stepsize), len(Lsequence))
        # here we estimate the volume differently: from the expected shrinkage per iteration
        it = np.arange(len(stepsizesq))
        vol = (1 - 1. / nlive)**it
        assert np.isfinite(vol).all(), vol
        assert (vol > 0).all(), vol
        assert (vol <= 1).all(), vol
        relstepsize = stepsize / vol**(1. / ndim)
        relradial_step = radial_step / vol**(1. / ndim)
        axstep1.hist(relstepsize[np.isfinite(relstepsize)],
                     bins=1000,
                     cumulative=True,
                     density=True,
                     histtype='step',
                     label=label,
                     color=color,
                     ls=ls)
        axstep2.hist(angular_step,
                     bins=1000,
                     cumulative=True,
                     density=True,
                     histtype='step',
                     label=label,
                     color=color,
                     ls=ls)
        axstep3.hist(relradial_step,
                     bins=1000,
                     cumulative=True,
                     density=True,
                     histtype='step',
                     label=label,
                     color=color,
                     ls=ls)
        sampler.plot(filename='evaluate_sampling_%s_%dd_N%d_%s.png' %
                     (args.problem, ndim, nlive, samplername))

    print('range:', Lsequence_ref[0], Lsequence_ref[-1])
    axL.plot([Lsequence_ref[0], Lsequence_ref[-1]],
             [Lsequence_ref[0], Lsequence_ref[-1]],
             '-',
             color='k',
             lw=1,
             label=label_ref)
    axL.set_xlabel('logL (reference)')
    axL.set_ylabel('logL')
    lo, hi = Lsequence_ref[int(len(Lsequence_ref) * 0.1)], Lsequence_ref[-1]
    axL.set_xlim(lo, hi)
    axL.set_ylim(lo, hi)
    axL.legend(loc='best', prop=dict(size=6))
    filename = 'evaluate_sampling_%s_%dd_N%d_L.pdf' % (args.problem, ndim,
                                                       nlive)
    print("plotting to %s ..." % filename)
    plt.figure('Lseq')
    plt.savefig(filename, bbox_inches='tight')
    plt.close()

    plt.figure('shrinkage')
    plt.xlabel('Shrinkage Volume')
    plt.ylabel('Cumulative Distribution')
    plt.xlim(0, 1)
    plt.plot([0, 1], [0, 1], '--', color='k')
    plt.legend(loc='upper left', prop=dict(size=6))
    filename = 'evaluate_sampling_%s_%dd_N%d_shrinkage.pdf' % (args.problem,
                                                               ndim, nlive)
    print("plotting to %s ..." % filename)
    plt.savefig(filename, bbox_inches='tight')
    plt.close()

    plt.figure('speed')
    plt.xlabel('Bias')
    plt.ylabel('# of function evaluations')
    plt.yscale('log')
    lo, hi = plt.xlim()
    hi = max(0.5 - lo, hi - 0.5, 0.04)
    plt.xlim(0.5 - hi, 0.5 + hi)
    lo, hi = plt.ylim()
    plt.vlines(0.5, lo, hi)
    plt.ylim(lo, hi)
    plt.legend(loc='best', prop=dict(size=6), fancybox=True, framealpha=0.5)
    filename = 'evaluate_sampling_%s_%dd_N%d_speed.pdf' % (args.problem, ndim,
                                                           nlive)
    print("plotting to %s ..." % filename)
    plt.savefig(filename, bbox_inches='tight')
    plt.savefig(filename.replace('.pdf', '.png'), bbox_inches='tight')
    plt.close()

    plt.figure('stepsize')
    axstep1.set_ylabel('Cumulative Distribution')
    axstep1.set_xlabel('Euclidean distance')
    axstep1.legend(loc='lower right', prop=dict(size=6))
    axstep2.set_ylabel('Cumulative Distribution')
    axstep2.set_xlabel('Angular distance')
    #axstep2.legend(loc='best', prop=dict(size=6))
    axstep3.set_ylabel('Cumulative Distribution')
    axstep3.set_xlabel('Radial distance')
    #axstep3.legend(loc='best', prop=dict(size=6))
    filename = 'evaluate_sampling_%s_%dd_N%d_step.pdf' % (args.problem, ndim,
                                                          nlive)
    print("plotting to %s ..." % filename)
    plt.savefig(filename, bbox_inches='tight')
    plt.close()
Example #12
0
parser.add_option("--d", dest="d")
parser.add_option("--seed", dest="seed")
parser.add_option("--max_duration", dest="max_duration")
parser.add_option("--task_id", dest="task_id")
parser.add_option("--callback", dest="callback")
(options, args) = parser.parse_args()

max_calls = int(options.max_calls)
func_name = options.func_name
d = int(options.d)
seed = int(options.seed)
max_duration = options.max_duration
task_id = options.task_id
callback = options.callback

problem = problems.get_problem(options.func_name)

creator.create("FitnessMin", base.Fitness, weights=(-1.0, ) * problem.crits)
creator.create("Individual",
               array.array,
               typecode='d',
               fitness=creator.FitnessMin)

toolbox = base.Toolbox()

# The problem with its parameters is set only here.
# Problem definition
# Functions zdt1, zdt2, zdt3, zdt6 have bounds [0, 1]
## BOUND_LOW, BOUND_UP = 0.0, 1.0

# Functions zdt4 has bounds x1 = [0, 1], xn = [-5, 5], with n = 2, ..., 10