示例#1
0
    def save(self):
        self.path_root.mkdir(parents=True, exist_ok=True)
        print(self.path_root)
        with open(self.path_json, 'w') as f:
            f.write(json.dumps({
                self.f_params: self.params._asdict(),
                self.f_info: self.info.__dict__,
                self.f_road: self.road.to_dict(),
                self.f_records: [r._asdict() for r in self.states]
            }))

        with open(self.path_partial, 'w') as f:
            sep = '\t'
            f.write(sep.join(SimulationDataRecordProperties) + '\n')
            gen = (r._asdict() for r in self.states)
            gen2 = (sep.join([str(d[key]) for key in SimulationDataRecordProperties]) + '\n' for d in gen)
            f.writelines(gen2)

        road_imagery = BeamNGRoadImagery.from_sample_nodes(self.road.nodes)
        road_imagery.save(self.path_road_img.with_suffix('.jpg'))
        road_imagery.save(self.path_road_img.with_suffix('.svg'))
    def extract_results(self, i, execution_time):
        # now = datetime.now().strftime("%Y%m%d%H%M%S")
        log_dir_name = f"{self.log_dir_path}"  #/{i}"
        log_dir_path = Path(f"{log_dir_name}/archive")
        log_dir_path.mkdir(parents=True, exist_ok=True)

        # filled values
        filled = len(self.solutions)

        original_seeds = set()
        mis_seeds = set()
        Individual.COUNT_MISS = 0
        for x in enumerate(self.performances.items()):
            # enumerate function returns a tuple in the form
            # (index, (key, value)) it is a nested tuple
            # for accessing the value we do indexing x[1][1]
            original_seeds.add(self.solutions[x[1][0]].seed)
            if x[1][1] < 0:
                Individual.COUNT_MISS += 1
                mis_seeds.add(self.solutions[x[1][0]].seed)
                misbehavior = True
            else:
                misbehavior = False

            sim_folder = Path(
                f"{log_dir_path}/sim_{self.solutions[x[1][0]].m.name}_{x[1][0]}"
            )
            sim_folder.mkdir(parents=True, exist_ok=True)

            # destination_sim = f"{log_dir_path}\\simulation_{self.solutions[x[1][0]].m.name}_{x[1][0]}.tsv"
            destination_sim_json = f"{sim_folder}\\simulation.full.json"
            destination_road = f"{sim_folder}\\road.json"

            with open(destination_sim_json, 'w') as f:
                f.write(
                    json.dumps({
                        self.solutions[x[1][0]].m.simulation.f_params:
                        self.solutions[x[1][0]].m.simulation.params._asdict(),
                        self.solutions[x[1][0]].m.simulation.f_info:
                        self.solutions[x[1][0]].m.simulation.info.__dict__,
                        self.solutions[x[1][0]].m.simulation.f_road:
                        self.solutions[x[1][0]].m.simulation.road.to_dict(),
                        self.solutions[x[1][0]].m.simulation.f_records: [
                            r._asdict() for r in self.solutions[
                                x[1][0]].m.simulation.states
                        ]
                    }))

            # with open(destination_sim, 'w') as f:
            #     sep = '\t'
            #     f.write(sep.join(SimulationDataRecordProperties) + '\n')
            #     gen = (r._asdict() for r in self.solutions[x[1][0]].m.simulation.states)
            #     gen2 = (sep.join([str(d[key]) for key in SimulationDataRecordProperties]) + '\n' for d in gen)
            #     f.writelines(gen2)

            with open(destination_road, 'w') as f:
                road = {
                    'name': str(self.solutions[x[1][0]].m.name),
                    'seed': str(self.solutions[x[1][0]].seed),
                    'misbehaviour': misbehavior,
                    'performance': self.performances[x[1][0]],
                    'timestamp': str(self.solutions[x[1][0]].m.timestamp),
                    'elapsed': str(self.solutions[x[1][0]].m.elapsed),
                    'tool': "DeepHyperion",
                    'run': str(self.config.run_id),
                    'features': self.solutions[x[1][0]].m.features,
                    'rank': str(self.solutions[x[1][0]].m.rank),
                    'selected':
                    str(self.solutions[x[1][0]].m.selected_counter),
                    'placed_mutant':
                    str(self.solutions[x[1][0]].m.placed_mutant)
                }
                f.write(json.dumps(road))

            road_imagery = BeamNGRoadImagery.from_sample_nodes(
                self.solutions[x[1][0]].m.sample_nodes)
            image_path = sim_folder.joinpath(
                f"img_{self.solutions[x[1][0]].m.name}_{x[1][0]}")
            road_imagery.save(image_path.with_suffix('.jpg'))
            road_imagery.save(image_path.with_suffix('.svg'))

        feature_dict = dict()
        for ft in self.feature_dimensions:
            feature_dict.update({
                f"{ft.name}_min": ft.min,
                f"{ft.name}_max": ft.bins
            })

        _performances = {}

        # convert keys to string
        for key, value in self.performances.items():
            _key = str(key)
            _performances[_key] = str(value)

        run_time = execution_time
        report = {
            "Run time": str(run_time),
            'Covered seeds': len(original_seeds),
            'Filled cells': (filled),
            'Misclassified seeds': len(mis_seeds),
            'Misclassifications': (Individual.COUNT_MISS),
            'Performances': _performances
        }

        report.update(feature_dict)

        dst = f"{log_dir_name}/report.json"
        with open(dst, 'w') as f:
            (json.dump(report, f, sort_keys=False, indent=4))

        # Find the order of features in tuples
        b = tuple()
        for ft in self.feature_dimensions:
            i = ft.name
            b = b + (i, )

        for feature1, feature2 in itertools.combinations(
                self.feature_dimensions, 2):
            # # Create another folder insider the log one ...
            # log_dir_path = Path(f"{log_dir_name}/{feature1.name}_{feature2.name}")
            # log_dir_path.mkdir(parents=True, exist_ok=True)

            # Find the position of each feature in indexes
            x = list(b).index(feature1.name)
            y = list(b).index(feature2.name)

            # Define a new 2-D dict
            _solutions = {}
            _performances = {}

            for key, value in self.performances.items():
                _key = (key[x], key[y])
                if _key in _performances:
                    if _performances[_key] > value:
                        _performances[_key] = value
                        _solutions[_key] = self.solutions[key]
                else:
                    _performances[_key] = value
                    _solutions[_key] = self.solutions[key]

            # filled values
            filled = len(_solutions)

            original_seeds = set()
            mis_seeds = set()
            Individual.COUNT_MISS = 0
            for x in enumerate(_performances.items()):
                # enumerate function returns a tuple in the form
                # (index, (key, value)) it is a nested tuple
                # for accessing the value we do indexing x[1][1]
                original_seeds.add(_solutions[x[1][0]].seed)
                if x[1][1] < 0:
                    Individual.COUNT_MISS += 1
                    mis_seeds.add(_solutions[x[1][0]].seed)

            feature_dict = {}
            feature_dict.update({
                f"{feature1.name}_min": feature1.min,
                f"{feature1.name}_max": feature1.bins
            })

            feature_dict.update({
                f"{feature2.name}_min": feature2.min,
                f"{feature2.name}_max": feature2.bins
            })

            str_performances = {}
            # convert keys to string
            for key, value in _performances.items():
                str_performances[str(key)] = str(value)

            run_time = execution_time
            report = {
                "Run time": str(run_time),
                'Covered seeds': len(original_seeds),
                'Filled cells': (filled),
                'Misclassified seeds': len(mis_seeds),
                'Misclassifications': (Individual.COUNT_MISS),
                'Performances': str_performances
            }

            report.update(feature_dict)

            dst = f"{log_dir_name}/report_" + feature1.name + "_" + feature2.name + '.json'
            with open(dst, 'w') as f:
                (json.dump(report, f, sort_keys=False, indent=4))

            self.plot_map_of_elites(_performances, log_dir_name, feature1,
                                    feature2)
示例#3
0
 def save_road_img(member: BeamNGMember, name):
     filepath = self.folder.joinpath(name)
     # BeamNGRoadImagery.from_sample_nodes(member.sample_nodes).save(filepath.with_suffix('.jpg'))
     BeamNGRoadImagery.from_sample_nodes(member.sample_nodes).save(filepath.with_suffix('.svg'))