コード例 #1
0
    def run(self, n_iterations=1, min_n_workers=1, iteration_kwargs = {},):
        """
            run n_iterations of SuccessiveHalving

        Parameters
        ----------
        n_iterations: int
            number of iterations to be performed in this run
        min_n_workers: int
            minimum number of workers before starting the run
        """

        self.wait_for_workers(min_n_workers)

        iteration_kwargs.update({'result_logger': self.result_logger})

        if self.time_ref is None:
            self.time_ref = time.time()
            self.config['time_ref'] = self.time_ref

            self.logger.info('HBMASTER: starting run at %s'%(str(self.time_ref)))

        self.thread_cond.acquire()
        while True:

            self._queue_wait()

            next_run = None
            # find a new run to schedule
            for i in self.active_iterations():
                next_run = self.iterations[i].get_next_run()
                if not next_run is None: break

            if not next_run is None:
                self.logger.debug('HBMASTER: schedule new run for iteration %i'%i)
                self._submit_job(*next_run)
                continue
            else:
                if n_iterations > 0:	#we might be able to start the next iteration
                    self.iterations.append(self.get_next_iteration(len(self.iterations), iteration_kwargs))
                    n_iterations -= 1
                    continue

            # at this point there is no imediate run that can be scheduled,
            # so wait for some job to finish if there are active iterations
            if self.active_iterations():
                self.thread_cond.wait()
            else:
                break

        self.thread_cond.release()

        for i in self.warmstart_iteration:
            i.fix_timestamps(self.time_ref)

        ws_data = [i.data for i in self.warmstart_iteration]

        return Result([copy.deepcopy(i.data) for i in self.iterations] + ws_data, self.config)
コード例 #2
0
    def get_result(self):

        # mock minial HB_config to have meaningful output
        mock_HB_config = {
            'min_budget': self.max_budget,
            'max_budget': self.max_budget,
            'time_ref': self.time_ref
        }

        # get Result by pretending to be a HB-run with one iteration
        res = Result([
            self.run_data,
        ], mock_HB_config)

        return (res)
コード例 #3
0
def run_with_time(self, runtime=1, n_iterations=float("inf"), min_n_workers=1, iteration_kwargs = {},):
    """
        custom run method of Master in hpbandster submodule

        Parameters:
        -----------
        runtime: int
            time for this run in seconds
        n_iterations: int
            maximum number of iterations
        min_n_workers: int
            minimum number of workers before starting the run
    """

    self.wait_for_workers(min_n_workers)

    iteration_kwargs.update({'result_logger': self.result_logger})

    if self.time_ref is None:
        self.time_ref = time.time()
        self.config['time_ref'] = self.time_ref
    
        self.logger.info('HBMASTER: starting run at %s'%(str(self.time_ref)))

    self.thread_cond.acquire()

    start_time = time.time()

    while True:

        self._queue_wait()

        # Check if timelimit is reached
        if (runtime < time.time() - start_time):
            self.logger.info('HBMASTER: Timelimit reached: wait for remaining %i jobs'%self.num_running_jobs)
            break
        
        next_run = None
        # find a new run to schedule
        for i in self.active_iterations():
            next_run = self.iterations[i].get_next_run()
            if not next_run is None: break

        if next_run is not None:
            self.logger.debug('HBMASTER: schedule new run for iteration %i'%i)
            self._submit_job(*next_run)
            continue
        elif n_iterations > 0:
            next_HPB_iter = len(self.iterations) + (self.iterations[0].HPB_iter if len(self.iterations) > 0 else 0)
            self.iterations.append(self.get_next_iteration(next_HPB_iter, iteration_kwargs))
            n_iterations -= 1
            continue

        # at this point there is no imediate run that can be scheduled,
        # so wait for some job to finish if there are active iterations
        if self.active_iterations():
            self.thread_cond.wait()
        else:
            break

    # clean up / cancel remaining iteration runs
    next_run = True
    n_canceled = 0
    while next_run is not None:
        next_run = None
        for i in self.active_iterations():
            next_run = self.iterations[i].get_next_run()
            if not next_run is None: 
                config_id, config, budget = next_run
                job = Job(config_id, config=config, budget=budget, working_directory=self.working_directory)
                self.iterations[job.id[0]].register_result(job) # register dummy job - will be interpreted as canceled job
                n_canceled += 1
                break

    self.logger.debug('HBMASTER: Canceled %i remaining runs'%n_canceled)

    # wait for remaining jobs
    while self.num_running_jobs > 0:
        self.thread_cond.wait(60)
        self.logger.debug('HBMASTER: Job finished: wait for remaining %i jobs'%self.num_running_jobs)

    self.thread_cond.release()
    
    for i in self.warmstart_iteration:
        i.fix_timestamps(self.time_ref)
        
    ws_data = [i.data for i in self.warmstart_iteration]
    
    return Result([copy.deepcopy(i.data) for i in self.iterations] + ws_data, self.config)