예제 #1
0
파일: trial.py 프로젝트: FriedSock/MLO-1
    def __init__(self, trial_no, run_name, fitness, configuration, controller,
                 run_results_folder_path):
        Thread.__init__(self)

        self.name = '{} Trial {}'.format(run_name, trial_no)
        self.run_name = run_name
        self.trial_no = trial_no
        self.fitness = fitness
        self.configuration = configuration
        self.controller = controller
        self.run_results_folder_path = run_results_folder_path
        self.status = 'Running'  # TODO - BAD

        # True if the user has selected to pause the trial
        self.wait = False

        self.graph_dictionary = {
            'rerendering': False,
            'graph_title': configuration.graph_title,
            'graph_names': configuration.graph_names,
            'all_graph_dicts': configuration.all_graph_dicts
        }
        for name in configuration.graph_names:
            self.graph_dictionary['all_graph_dicts'][name]['generate'] = True

        if configuration.plot_view != 'special':
            self.plot_view = Plot_View

        self.enable_traceback = configuration.enable_traceback
        self.GEN = configuration.max_iter
        self.generations_array = []
        self.max_fitness = configuration.max_fitness
        self.best_fitness_array = []

        self.clf = None
        self.gp_training_fitness = None
        self.gp_training_set = None

        if configuration.surrogate_type == "dummy":
            self.surrogate_model = DummySurrogateModel(fitness, configuration,
                                                       self.controller)
        else:
            self.surrogate_model = ProperSurrogateModel(fitness,
                                                        configuration,
                                                        self.controller)

        # Contains all the counter variables that may be used for visualization
        self.counter_dictionary = {}
        self.counter_dictionary['g'] = 1
        self.counter_dictionary['fit'] = 1

        # The last counter value to be visualized, 0 means none
        self.latest_counter_plot = 0

        self.controller.register_trial(self)

        self.backup = self.create_backup_manager()
예제 #2
0
파일: trial.py 프로젝트: FriedSock/MLO-1
class Trial(Thread):

    def __init__(self, trial_no, run_name, fitness, configuration, controller,
                 run_results_folder_path):
        Thread.__init__(self)

        self.name = '{} Trial {}'.format(run_name, trial_no)
        self.run_name = run_name
        self.trial_no = trial_no
        self.fitness = fitness
        self.configuration = configuration
        self.controller = controller
        self.run_results_folder_path = run_results_folder_path
        self.status = 'Running'  # TODO - BAD

        # True if the user has selected to pause the trial
        self.wait = False

        self.graph_dictionary = {
            'rerendering': False,
            'graph_title': configuration.graph_title,
            'graph_names': configuration.graph_names,
            'all_graph_dicts': configuration.all_graph_dicts
        }
        for name in configuration.graph_names:
            self.graph_dictionary['all_graph_dicts'][name]['generate'] = True

        if configuration.plot_view != 'special':
            self.plot_view = Plot_View

        self.enable_traceback = configuration.enable_traceback
        self.GEN = configuration.max_iter
        self.generations_array = []
        self.max_fitness = configuration.max_fitness
        self.best_fitness_array = []

        self.clf = None
        self.gp_training_fitness = None
        self.gp_training_set = None

        if configuration.surrogate_type == "dummy":
            self.surrogate_model = DummySurrogateModel(fitness, configuration,
                                                       self.controller)
        else:
            self.surrogate_model = ProperSurrogateModel(fitness,
                                                        configuration,
                                                        self.controller)

        # Contains all the counter variables that may be used for visualization
        self.counter_dictionary = {}
        self.counter_dictionary['g'] = 1
        self.counter_dictionary['fit'] = 1

        # The last counter value to be visualized, 0 means none
        self.latest_counter_plot = 0

        self.controller.register_trial(self)

        self.backup = self.create_backup_manager()

    def initialise(self):
        """
        Initialises the trial and returns True if everything went OK,
        False otherwise.
        """
        self.results_folder = self._create_results_folder()
        if not self.results_folder:
            # Results folder could not be created
            return False

        self.run_initialize()

        self.best = None
        self.fitness_evaluated = False
        self.gp = None
        self.model_failed = False
        self.model_retrained = True

        #TODO - maybe remove this boolean
        self.new_best_over_iteration = False
        self.M_best = self.fitness.worst_value

        self.population = None
        self.svc_training_labels = None
        self.svc_training_set = None

        self.initialize_population()

        return True

    def run(self):
        self.start_time = datetime.now().strftime('%d-%m-%Y  %H:%M:%S')
        self.view_update()

        logging.info('{} started'.format(self.get_name()))

        logging.info('Run prepared... executing')

        while self.counter_dictionary['g'] < self.GEN + 1:
            logging.info('[{}] Generation {}'.format(
                self.get_name(), self.counter_dictionary['g']))

            # Roll population
            first_pop = self.population.pop(0)
            self.population.append(first_pop)

            # Initialise termination check
            self.check = False

            if self.counter_dictionary['fit'] > self.max_fitness:
                logging.info('Fitness counter exceeded the limit... exiting')
                break

            # Train surrogate model
            while not self.surrogate_model.train(self.population):
                logging.info('Re-initializing population')
                self.initialize_population()

            code, mean, variance = \
                self.surrogate_model.model_particles(self.population)

            self.post_model_filter(self.population, code, mean, variance)
            #logging.info(self.population[0].fitness.values)

            # Iteration of meta-heuristic
            self.meta_iterate()
            self.filter_population()

            # TODO: termination condition

            #TODO: This will be done after the presentation
            '''
            if self.counter_dictionary['g'] % self.configuration.M == 0:
                if self.M_best == self.best:
                    #Return best?
                    logging.info('New best was found after M')
                    self.M_best = self.fitness.worst_value;
                    pass
                else:
                    logging.info('Perturbing things')
                    #Return perturbation?
                    pass
            '''

            # Wait until the user unpauses the trial.
            while self.wait:
                time.sleep(0)

            self.increment_counter('g')
            self.view_update()

        self.status = 'Finished'
        self.view_update()
        logging.info('{} finished'.format(self.get_name()))

    def _create_results_folder(self):
        """
        Creates a folder used for storing results.
        Returns a results folder path or None if it could not be created.
        """
        path = '{}/trial-{}'.format(self.run_results_folder_path,
                                    self.trial_no)

        try:
            os.makedirs(path)
            return path
        except OSError, e:
            # Folder already exists
            return path
        except Exception, e:
            logging.error('Could not create folder: {}, aborting'.format(path),
                          exc_info=sys.exc_info())
            return None