def get_next_cost_dict(self,params_dict): ''' Implementation of file read in and out. Put parameters into a file and wait for a cost file to be returned. ''' self.out_file_count += 1 self.log.debug('Writing out_params to file. Count:' + repr(self.out_file_count)) self.last_params_dict = params_dict mlu.save_dict_to_file(self.last_params_dict,self.total_out_filename,self.out_file_type) while not self.end_event.is_set(): if os.path.isfile(self.total_in_filename): time.sleep(mlu.filewrite_wait) #wait for file to be written to disk try: in_dict = mlu.get_dict_from_file(self.total_in_filename, self.in_file_type) except IOError: self.log.warning('Unable to open ' + self.total_in_filename + '. Trying again.') continue except (ValueError,SyntaxError): self.log.error('There is something wrong with the syntax or type of your file:' + self.in_filename + '.' + self.in_file_type) raise os.remove(self.total_in_filename) self.in_file_count += 1 self.log.debug('Putting dict from file onto in queue. Count:' + repr(self.in_file_count)) break else: time.sleep(self.interface_wait) else: raise InterfaceInterrupt return in_dict
def run(self): ''' Implementation of file read in and out. Put parameters into a file and wait for a cost file to be returned. ''' self.log.debug('Entering FakeExperiment loop') while not self.end_event.is_set(): if os.path.isfile(self.total_in_filename): time.sleep(mlu.filewrite_wait) #wait for file to be written try: in_dict = mlu.get_dict_from_file(self.total_in_filename, self.in_file_type) except IOError: self.log.warning('Unable to open ' + self.total_in_filename + '. Trying again.') continue except (ValueError,SyntaxError): self.log.error('There is something wrong with the syntax or type of your file:' + self.in_filename + '.' + self.in_file_type) raise os.remove(self.total_in_filename) self.test_count +=1 self.log.debug('Test exp evaluating cost. Num:' + repr(self.test_count)) try: params = in_dict['params'] except KeyError as e: self.log.error('You are missing ' + repr(e.args[0]) + ' from the in params dict you provided through the queue.') raise cost_dict = self.test_landscape.get_cost_dict(params) time.sleep(self.exp_wait) mlu.save_dict_to_file(cost_dict, self.total_out_filename, self.out_file_type) else: time.sleep(self.poll_wait) self.log.debug('Ended FakeExperiment')
def __init__(self, filename, file_type ='pkl', **kwargs): self.log = logging.getLogger(__name__) self.filename = str(filename) self.file_type = str(file_type) if not mlu.check_file_type_supported(self.file_type): self.log.error('GP training file type not supported' + repr(self.file_type)) learner_dict = mlu.get_dict_from_file(self.filename, self.file_type) if 'archive_type' in learner_dict and not (learner_dict['archive_type'] == 'differential_evolution'): self.log.error('The archive appears to be the wrong type.' + repr(learner_dict['archive_type'])) raise ValueError self.archive_type = learner_dict['archive_type'] self.num_generations = int(learner_dict['generation_count']) self.num_population_members = int(learner_dict['num_population_members']) self.num_params = int(learner_dict['num_params']) self.min_boundary = np.squeeze(np.array(learner_dict['min_boundary'])) self.max_boundary = np.squeeze(np.array(learner_dict['max_boundary'])) self.params_generations = np.array(learner_dict['params_generations']) self.costs_generations = np.array(learner_dict['costs_generations']) self.finite_flag = True self.param_scaler = lambda p: (p-self.min_boundary)/(self.max_boundary - self.min_boundary) self.scaled_params_generations = np.array([[self.param_scaler(self.params_generations[inda,indb,:]) for indb in range(self.num_population_members)] for inda in range(self.num_generations)]) self.gen_numbers = np.arange(1,self.num_generations+1) self.param_colors = _color_list_from_num_of_params(self.num_params) self.gen_plot = np.array([np.full(self.num_population_members, ind, dtype=int) for ind in self.gen_numbers]).flatten()
def __init__(self, filename, file_type='pkl', **kwargs): self.log = logging.getLogger(__name__) self.filename = str(filename) self.file_type = str(file_type) if not mlu.check_file_type_supported(self.file_type): self.log.error('GP training file type not supported' + repr(self.file_type)) controller_dict = mlu.get_dict_from_file(self.filename, self.file_type) self.archive_type = controller_dict['archive_type'] if 'archive_type' in controller_dict and not ( controller_dict['archive_type'] == 'controller'): self.log.error('The archive appears to be the wrong type.') raise ValueError self.num_in_costs = int(controller_dict['num_in_costs']) self.num_out_params = int(controller_dict['num_out_params']) self.out_params = np.array(controller_dict['out_params']) self.out_type = [x.strip() for x in list(controller_dict['out_type'])] self.in_costs = np.squeeze(np.array(controller_dict['in_costs'])) self.in_uncers = np.squeeze(np.array(controller_dict['in_uncers'])) self.in_bads = np.squeeze(list(controller_dict['in_bads'])) self.best_index = int(controller_dict['best_index']) self.num_params = int(controller_dict['num_params']) self.min_boundary = np.squeeze( np.array(controller_dict['min_boundary'])) self.max_boundary = np.squeeze( np.array(controller_dict['max_boundary'])) if np.all(np.isfinite(self.min_boundary)) and np.all( np.isfinite(self.min_boundary)): self.finite_flag = True self.param_scaler = lambda p: (p - self.min_boundary) / ( self.max_boundary - self.min_boundary) self.scaled_params = np.array([ self.param_scaler(self.out_params[ind, :]) for ind in range(self.num_out_params) ]) else: self.finite_flag = False self.unique_types = set(self.out_type) self.cost_colors = [ _color_from_controller_name(x) for x in self.out_type ] self.in_numbers = np.arange(1, self.num_in_costs + 1) self.out_numbers = np.arange(1, self.num_out_params + 1) self.param_colors = _color_list_from_num_of_params(self.num_params)
def __init__(self, filename, file_type ='pkl', **kwargs): self.log = logging.getLogger(__name__) self.filename = str(filename) self.file_type = str(file_type) if not mlu.check_file_type_supported(self.file_type): self.log.error('GP training file type not supported' + repr(self.file_type)) controller_dict = mlu.get_dict_from_file(self.filename, self.file_type) self.archive_type = controller_dict['archive_type'] if 'archive_type' in controller_dict and not (controller_dict['archive_type'] == 'controller'): self.log.error('The archive appears to be the wrong type.') raise ValueError self.num_in_costs = int(controller_dict['num_in_costs']) self.num_out_params = int(controller_dict['num_out_params']) self.out_params = np.array(controller_dict['out_params']) self.out_type = [x.strip() for x in list(controller_dict['out_type'])] self.in_costs = np.squeeze(np.array(controller_dict['in_costs'])) self.in_uncers = np.squeeze(np.array(controller_dict['in_uncers'])) self.in_bads = np.squeeze(list(controller_dict['in_bads'])) self.best_index = int(controller_dict['best_index']) self.num_params = int(controller_dict['num_params']) self.min_boundary = np.squeeze(np.array(controller_dict['min_boundary'])) self.max_boundary = np.squeeze(np.array(controller_dict['max_boundary'])) if np.all(np.isfinite(self.min_boundary)) and np.all(np.isfinite(self.min_boundary)): self.finite_flag = True self.param_scaler = lambda p: (p-self.min_boundary)/(self.max_boundary - self.min_boundary) self.scaled_params = np.array([self.param_scaler(self.out_params[ind,:]) for ind in range(self.num_out_params)]) else: self.finite_flag = False self.unique_types = set(self.out_type) self.cost_colors = [_color_from_controller_name(x) for x in self.out_type] self.in_numbers = np.arange(1,self.num_in_costs+1) self.out_numbers = np.arange(1,self.num_out_params+1) self.param_colors = _color_list_from_num_of_params(self.num_params)
def __init__(self, filename, file_type='pkl', **kwargs): self.log = logging.getLogger(__name__) self.filename = str(filename) self.file_type = str(file_type) if not mlu.check_file_type_supported(self.file_type): self.log.error('GP training file type not supported' + repr(self.file_type)) learner_dict = mlu.get_dict_from_file(self.filename, self.file_type) if 'archive_type' in learner_dict and not ( learner_dict['archive_type'] == 'differential_evolution'): self.log.error('The archive appears to be the wrong type.' + repr(learner_dict['archive_type'])) raise ValueError self.archive_type = learner_dict['archive_type'] self.num_generations = int(learner_dict['generation_count']) self.num_population_members = int( learner_dict['num_population_members']) self.num_params = int(learner_dict['num_params']) self.min_boundary = np.squeeze(np.array(learner_dict['min_boundary'])) self.max_boundary = np.squeeze(np.array(learner_dict['max_boundary'])) self.params_generations = np.array(learner_dict['params_generations']) self.costs_generations = np.array(learner_dict['costs_generations']) self.finite_flag = True self.param_scaler = lambda p: (p - self.min_boundary) / ( self.max_boundary - self.min_boundary) self.scaled_params_generations = np.array([[ self.param_scaler(self.params_generations[inda, indb, :]) for indb in range(self.num_population_members) ] for inda in range(self.num_generations)]) self.gen_numbers = np.arange(1, self.num_generations + 1) self.param_colors = _color_list_from_num_of_params(self.num_params) self.gen_plot = np.array([ np.full(self.num_population_members, ind, dtype=int) for ind in self.gen_numbers ]).flatten()
def launch_from_file(config_filename, **kwargs): ''' Launch M-LOOP using a configuration file. See configuration file documentation. Args: config_filename (str): Filename of configuration file **kwargs : keywords that override the keywords in the file. Returns: controller (Controller): Controller for optimization. ''' try: file_kwargs = mlu.get_dict_from_file(config_filename, 'txt') except (IOError, OSError): print('Unable to open M-LOOP configuration file:' + repr(config_filename)) raise file_kwargs.update(kwargs) #Main run sequence #Create interface and extract unused keywords interface = mli.create_interface(**file_kwargs) file_kwargs = interface.remaining_kwargs #Create controller and extract unused keywords controller = mlc.create_controller(interface, **file_kwargs) file_kwargs = controller.remaining_kwargs #Extract keywords for post processing extras, and raise an error if any keywords were unused. extras_kwargs = _pop_extras_kwargs(file_kwargs) if file_kwargs: logging.getLogger(__name__).error('Unused extra options provided:' + repr(file_kwargs)) raise ValueError #Run the actual optimization controller.optimize() #Launch post processing extras launch_extras(controller, **extras_kwargs) return controller
def launch_from_file(config_filename, **kwargs): ''' Launch M-LOOP using a configuration file. See configuration file documentation. Args: config_filename (str): Filename of configuration file **kwargs : keywords that override the keywords in the file. Returns: controller (Controller): Controller for optimization. ''' try: file_kwargs = mlu.get_dict_from_file(config_filename,'txt') except (IOError, OSError): print('Unable to open M-LOOP configuration file:' + repr(config_filename)) raise file_kwargs.update(kwargs) #Main run sequence #Create interface and extract unused keywords interface = mli.create_interface(**file_kwargs) file_kwargs = interface.remaining_kwargs #Create controller and extract unused keywords controller = mlc.create_controller(interface, **file_kwargs) file_kwargs = controller.remaining_kwargs #Extract keywords for post processing extras, and raise an error if any keywords were unused. extras_kwargs = _pop_extras_kwargs(file_kwargs) if file_kwargs: logging.getLogger(__name__).error('Unused extra options provided:' + repr(file_kwargs)) raise ValueError #Run the actual optimization controller.optimize() #Launch post processing extras launch_extras(controller, **extras_kwargs) return controller