Пример #1
0
def explore_space_of_candidates(experiment, objective_function,
        working_directory, chooser,
        grid_size=1000,
        grid_seed=1,
        max_finished_jobs=100):
   
    # Build the experiment grid.
    expt_grid = ExperimentGrid(working_directory,
                               experiment.variables, grid_size, grid_seed)

    next_jobid = 0
    run_python_job = PythonRunner()

    while next_jobid < max_finished_jobs:
        best_val, best_job = expt_grid.get_best()
 
        # Gets you everything - NaN for unknown values & durations.
        grid, values, durations = expt_grid.get_grid()


        # Returns lists of indices.
        candidates = expt_grid.get_candidates()
        pending    = expt_grid.get_pending()
        complete   = expt_grid.get_complete()

        n_candidates = candidates.shape[0]
        n_pending    = pending.shape[0]
        n_complete   = complete.shape[0]
        logging.info("%d candidates   %d pending   %d complete", n_candidates, 
                n_pending, n_complete)

        if n_candidates == 0:
            logging.info("There are no candidates left.  Exiting.")
            return

        # Ask the chooser to pick the next candidate
        logging.info("Choosing next candidate... ")
        job_id = chooser.next(grid, values, durations, candidates, pending, complete)

        yield best_val, best_job, expt_grid.get_params(best_job), job_id

        # If the job_id is a tuple, then the chooser picked a new job.
        # We have to add this to our grid
        if isinstance(job_id, tuple):
            (job_id, candidate) = job_id
            job_id = expt_grid.add_to_grid(candidate)

        logging.info("selected job %d from the grid", job_id)

        expt_grid.set_submitted(job_id, next_jobid)
        expt_grid.set_running(job_id)

        start_t = time.time()
        result, memoized = run_python_job(job_id, objective_function,
                expt_grid.get_params(job_id), working_directory)
        duration = time.time() - start_t
        expt_grid.set_complete(job_id, result, duration)

        next_jobid += 1

        if memoized:
            max_finished_jobs += 1