Example #1
0
 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
Example #2
0
    def save_archive(self):
        '''
        Save the archive associated with the controller class. Only occurs if the filename for the archive is not None. Saves with the format previously set.
        '''
        if self.controller_archive_filename is not None:

            try:
                self.archive_dict.update({'num_in_costs':self.num_in_costs,
                                      'num_out_params':self.num_out_params,
                                      'best_cost':self.best_cost,
                                      'best_uncer':self.best_uncer,
                                      'best_params':self.best_params,
                                      'best_index':self.best_index})
            except (ValueError, TypeError):
                self.archive_dict.update({'num_in_costs':self.num_in_costs,
                                      'num_out_params':self.num_out_params,
                                      'best_cost':self.best_cost})
                                      
                
            try:
                mlu.save_dict_to_file(self.archive_dict,self.total_archive_filename,self.controller_archive_file_type)
            except (ValueError, TypeError):
                self.log.error('Attempted to save with unknown archive file type, or some other value error.')
                raise
        else:
            self.log.debug('Did not save controller archive file.')
Example #3
0
 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')
Example #4
0
 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
Example #5
0
 def save_archive(self):
     '''
     Save the archive associated with the controller class. Only occurs if the filename for the archive is not None. Saves with the format previously set.
     '''
     if self.controller_archive_filename is not None:
         self.archive_dict.update({'num_in_costs':self.num_in_costs,
                                   'num_out_params':self.num_out_params,
                                   'best_cost':self.best_cost,
                                   'best_uncer':self.best_uncer,
                                   'best_params':self.best_params,
                                   'best_index':self.best_index})
         try:
             mlu.save_dict_to_file(self.archive_dict,self.total_archive_filename,self.controller_archive_file_type)
         except ValueError:
             self.log.error('Attempted to save with unknown archive file type, or some other value error.')
             raise
     else:
         self.log.debug('Did not save controller archive file.')
Example #6
0
    def optimize(self):
        '''
        Optimize the experiment. This code learner and interface processes/threads are launched and appropriately ended.
        Starts both threads and catches kill signals and shuts down appropriately.
        '''
        log = logging.getLogger(__name__)

        try:
            log.info('Optimization started.')
            self._start_up()
            self._optimization_routine()
            log.info('Controller finished. Closing down M-LOOP. Please wait a moment...')
        except ControllerInterrupt:
            self.log.warning('Controller ended by interruption.')
        except (KeyboardInterrupt,SystemExit):
            log.warning('!!! Do not give the interrupt signal again !!! \n M-LOOP stopped with keyboard interupt or system exit. Please wait at least 1 minute for the threads to safely shut down. \n ')
            log.warning('Closing down controller.')
        except Exception:
            self.log.warning('Controller ended due to exception of some kind. Starting shut down...')
            self._shut_down()
            self.log.warning('Safely shut down. Below are results found before exception.')
            self.print_results()
            raise
        self._shut_down()
        self.print_results()
        self.log.info('M-LOOP Done.')
        
        #create cotroller result file
        result_dict = {}
        try : 
            result_dict['status'] = 0
            result_dict['predicted_best_parameters']  = self.predicted_best_parameters
            result_dict['predicted_best_cost']        = self.predicted_best_cost
            result_dict['predicted_best_uncertainty'] = self.predicted_best_uncertainty
            #        else:
        except AttributeError:
            result_dict = {}
            result_dict['status'] = -1
            result_dict['predicted_best_parameters']  = None
            result_dict['predicted_best_cost']        = None
            result_dict['predicted_best_uncertainty'] = None
            
        mlu.save_dict_to_file(result_dict, self.total_result_filename, self.controller_result_file_type)