def dump_tabular(self):
        """
        Write all of the diagnostics from the current iteration
        """
        if (MPIUtil.is_root_proc()):
            if (self.first_row):
                self._dump_str_template = self._build_str_template()

            vals = []
            for key in self.log_headers:
                val = self.log_current_row.get(key, "")
                vals.append(val)
            
            if self.output_file is not None:
                if self.first_row:
                    header_str = self._dump_str_template.format(*self.log_headers)
                    self.output_file.write(header_str + "\n")

                val_str = self._dump_str_template.format(*map(str,vals))
                self.output_file.write(val_str + "\n")
                self.output_file.flush()

        self.log_current_row.clear()
        self.first_row=False
        return
Exemple #2
0
    def _end_record_data(self):
        if MPIUtil.is_root_proc():
            records_output_path = self._get_records_output_path()
            records_output_dir = os.path.dirname(records_output_path)
            if not os.path.exists(records_output_dir):
                os.makedirs(records_output_dir)

            trajectories = []
            record_files = glob.glob(records_output_dir + '/*.txt')
            for record_file in record_files:
                print('Read file ' + record_file)
                _trajectories = self._parse_record_data(record_file)
                trajectories += _trajectories

            states, goals, weights = [], [], []
            if self.control_type == 'speed':
                for records in trajectories:
                    for i in range(len(records)):
                        states += [list(records[i][0])]
                        goals += [list(records[i][1])]
                        weights += [list(records[i][2])]
            elif self.control_type == 'heading':
                n_previous_steps = 1
                for records in trajectories:
                    for i in range(n_previous_steps, len(records)):
                        state_curr = records[i][0]
                        goal_curr = records[i][1]
                        goal_prev = records[i-n_previous_steps][1]
                        weight_curr = records[i][2]

                        theta_curr = np.arctan2(-goal_curr[2], goal_curr[0])
                        theta_prev = np.arctan2(-goal_prev[2], goal_prev[0])
                        theta_delta = theta_curr - theta_prev
                        goal_curr_new = np.concatenate([goal_prev, [theta_delta]], axis=-1)

                        states += [list(state_curr)]
                        goals += [list(goal_curr_new)]
                        weights += [list(weight_curr)]
            else:
                Logger.print('Unsupported control type')
                assert(0)

            states = np.array(states, dtype=np.float32)
            goals = np.array(goals, dtype=np.float32)
            weights = np.array(weights, dtype=np.float32)

            output_path = self._get_output_path()
            output_dir = os.path.dirname(output_path)
            if not os.path.exists(records_output_dir):
                os.makedirs(records_output_dir)

            np.save(output_dir + '/states', states)
            np.save(output_dir + '/goals', goals)
            np.save(output_dir + '/weights', weights)

            print('Saved record data with %d samples' % (states.shape[0]))
        return
Exemple #3
0
    def _update_iter(self, iter):
        super()._update_iter(iter)

        if (self._enable_record_data and self.iter % self.record_output_iters == 0):
            records_output_path = self._get_records_output_path()
            records_output_dir = os.path.dirname(records_output_path)
            if MPIUtil.is_root_proc() and not os.path.exists(records_output_dir):
                os.makedirs(records_output_dir)
            self.save_records(records_output_path)
        return
Exemple #4
0
 def check_synced(self):
     synced = True
     if MPIUtil.is_root_proc():
         vars = np.concatenate([self.mean, self.mean_sq])
         MPIUtil.bcast(vars)
     else:
         vars_local = np.concatenate([self.mean, self.mean_sq])
         vars_root = np.empty_like(vars_local)
         MPIUtil.bcast(vars_root)
         synced = (vars_local == vars_root).all()
     return synced
    def configure_output_file(self, filename=None):
        """
        Set output directory to d, or to /tmp/somerandomnumber if d is None
        """
        self.first_row = True
        self.log_headers = []
        self.log_current_row = {}

        output_path = filename or "output/log_%i.txt"%int(time.time())

        out_dir = os.path.dirname(output_path)
        if not os.path.exists(out_dir) and MPIUtil.is_root_proc():
            os.makedirs(out_dir)

        if (MPIUtil.is_root_proc()):
            self.output_file = open(output_path, 'w')
            assert osp.exists(output_path)
            atexit.register(self.output_file.close)

            Logger.print("Logging data to " + self.output_file.name)
        return
 def print_tabular(self):
     """
     Print all of the diagnostics from the current iteration
     """
     if (MPIUtil.is_root_proc()):
         vals = []
         Logger.print("-"*37)
         for key in self.log_headers:
             val = self.log_current_row.get(key, "")
             if isinstance(val, float):
                 valstr = "%8.3g"%val
             elif isinstance(val, int):
                 valstr = str(val)
             else: 
                 valstr = val
             Logger.print("| %15s | %15s |"%(key, valstr))
             vals.append(val)
         Logger.print("-" * 37)
     return
Exemple #7
0
    def save_records(self, filename):
        def numpy_1d_array_to_string(arr):
            line = ''
            arr = list(arr)
            for i in range(0, len(arr)):
                if i < len(arr) - 1:
                    line += '{:.10}'.format(arr[i]) + ' '
                else:
                    line += '{:.10}'.format(arr[i])
            return line

        trajectories_list = MPIUtilExtend.gather(self._trajectories)
        if MPIUtil.is_root_proc():
            trajectories = trajectories_list[0]
            for i in range(1, len(trajectories_list)):
                trajectories += trajectories_list[i]

            with open(filename, 'w') as fout:
                for records in trajectories:
                    for record in records:
                        line = ''
                        for entry in record:
                            line += numpy_1d_array_to_string(entry) + '\n'
                        fout.write(line)
                    fout.write('\n')
                    self._record_smaple_count += len(records)

            _root_sample_count = np.array(self._record_smaple_count, dtype=np.float32)
            MPIUtil.bcast(_root_sample_count)
        else:
            _root_sample_count = np.array([0], dtype=np.float32)
            MPIUtil.bcast(_root_sample_count)
            self._record_smaple_count = int(_root_sample_count[0])

        Logger.print('Record Sample Count: ' + str(self._record_smaple_count))
        self._trajectories.clear()
        return
 def print(str):
     # rank == ROOT_PROC_RANK (0) is true
     if (MPIUtil.is_root_proc()):
         print(str)
     return
Exemple #9
0
 def _enable_int_output(self):
     return MPIUtil.is_root_proc() and self.int_output_dir != ""
Exemple #10
0
 def print(str):
     if (MPIUtil.is_root_proc()):
         print(str)
     return
Exemple #11
0
 def _is_root(self):
     return MPIUtil.is_root_proc()