def from_dict(self, d): m1 = BeamNGMember.from_dict(d['m1']) m2 = BeamNGMember.from_dict(d['m2']) ind = BeamNGIndividual(m1, m2, None, None) ind.members_distance = d['members_distance'] ind.name = d['name'] return ind
def generate(self, visualise=False) -> BeamNGMember: control_nodes = self.generate_control_nodes(visualise) sample_nodes = catmull_rom(control_nodes, self.num_spline_nodes) road = BeamNGMember(control_nodes, sample_nodes, self.num_spline_nodes, self.road_bbox) while not road.is_valid(): control_nodes = self.generate_control_nodes(visualise) sample_nodes = catmull_rom(control_nodes, self.num_spline_nodes) road = BeamNGMember(control_nodes, sample_nodes, self.num_spline_nodes, self.road_bbox) return road
def main(): storage = SeedStorage('short') for i in range(1, 100): member_filepath = storage.get_path_by_index(i) if not member_filepath.exists(): continue member = BeamNGMember.from_dict(storage.load_json_by_index(i)) sample_nodes = member.sample_nodes road_imagery = BeamNGRoadImagery.from_sample_nodes(sample_nodes) for extension in ['.jpg', '.svg']: image_filename = member_filepath.with_suffix(extension) print('saving', image_filename) road_imagery.save(image_filename)
try: if self.config.beamng_close_at_iteration: self._close() else: if self.brewer: self.brewer.beamng.stop_scenario() except Exception as ex: log.debug('end_iteration() failed:') traceback.print_exception(type(ex), ex, ex.__traceback__) def _close(self): if self.brewer: try: self.brewer.beamng.close() except Exception as ex: log.debug('beamng.close() failed:') traceback.print_exception(type(ex), ex, ex.__traceback__) self.brewer = None if __name__ == '__main__': config = BeamNGConfig() inst = BeamNGNvidiaOob(config) while True: seed_storage = SeedStorage('basic5') for i in range(1, 11): member = BeamNGMember.from_dict(seed_storage.load_json_by_index(i)) member.clear_evaluation() inst.evaluate([member]) log.info(member)
def initial_population_generator(path, config, problem): all_roads = [ filename for filename in glob.glob(str(path) + "\*.json", recursive=True) ] type = config.Feature_Combination shuffle(all_roads) roads = all_roads original_set = list() individuals = [] popsize = config.POPSIZE for road in roads: with open(road) as json_file: data = json.load(json_file) sample_nodes = data["road"]["nodes"] for node in sample_nodes: node[2] = -28.0 sample_nodes = np.array(sample_nodes) records = data["records"] bbox_size = (-250.0, 0.0, 250.0, 500.0) road_bbox = RoadBoundingBox(bbox_size) member = BeamNGMember(data["control_nodes"], [tuple(t) for t in sample_nodes], 20, road_bbox) member.config = config member.problem = problem simulation_id = time.strftime('%Y-%m-%d--%H-%M-%S', time.localtime()) sim_name = member.config.simulation_name.replace( '$(id)', simulation_id) simulation_data = SimulationData(sim_name) states = [] for record in records: state = VehicleState(timer=record["timer"], pos=record["pos"], dir=record["dir"], vel=record["vel"], steering=record["steering"], steering_input=record["steering_input"], brake=record["brake"], brake_input=record["brake_input"], throttle=record["throttle"], throttle_input=record["throttle_input"], wheelspeed=record["wheelspeed"], vel_kmh=record["vel_kmh"]) sim_data_record = SimulationDataRecord( **state._asdict(), is_oob=record["is_oob"], oob_counter=record["oob_counter"], max_oob_percentage=record["max_oob_percentage"], oob_distance=record["oob_distance"]) states.append(sim_data_record) simulation_data.params = SimulationParams( beamng_steps=data["params"]["beamng_steps"], delay_msec=int(data["params"]["delay_msec"])) simulation_data.road = DecalRoad.from_dict(data["road"]) simulation_data.info = SimulationInfo() simulation_data.info.start_time = data["info"]["start_time"] simulation_data.info.end_time = data["info"]["end_time"] simulation_data.info.elapsed_time = data["info"]["elapsed_time"] simulation_data.info.success = data["info"]["success"] simulation_data.info.computer_name = data["info"]["computer_name"] simulation_data.info.id = data["info"]["id"] simulation_data.states = states if len(states) > 0: member.distance_to_boundary = simulation_data.min_oob_distance() member.simulation = simulation_data individual: BeamNGIndividual = BeamNGIndividual(member, config) #individual.evaluate() b = tuple() feature_dimensions = generate_feature_dimension(type) for ft in feature_dimensions: i = feature_simulator(ft.feature_simulator, individual) b = b + (i, ) individuals.append([b, road, individual]) starting_point = choice(individuals) original_set.append(starting_point) i = 0 best_ind = individuals[0] while i < popsize - 1: max_dist = 0 for ind in individuals: dist = get_min_distance_from_set(ind, original_set) if dist > max_dist: max_dist = dist best_ind = ind original_set.append(best_ind) i += 1 base = config.initial_population_folder storage = SeedStorage(base) for index, road in enumerate(original_set): dst = storage.get_path_by_index(index + 1) ind = road[2] #copy(road[1], dst) with open(road[1]) as ff: json_file = json.load(ff) with open(dst, 'w') as f: f.write( json.dumps({ "control_nodes": json_file["control_nodes"], ind.m.simulation.f_params: ind.m.simulation.params._asdict(), ind.m.simulation.f_info: ind.m.simulation.info.__dict__, ind.m.simulation.f_road: ind.m.simulation.road.to_dict(), ind.m.simulation.f_records: [r._asdict() for r in ind.m.simulation.states] }))
def from_dict(self, d): m = BeamNGMember.from_dict(d['m']) ind = BeamNGIndividual(m, None) ind.name = d['name'] return ind
def generate_random_solution_without_sim(self): """ To ease the bootstrap of the algorithm, we can generate the first solutions in the feature space, so that we start filling the bins """ # "Generate random solution" path = self.problem._seed_pool_strategy.get_seed() with open(path) as json_file: data = json.load(json_file) sample_nodes = data["road"]["nodes"] for node in sample_nodes: node[2] = -28.0 sample_nodes = np.array(sample_nodes) records = data["records"] bbox_size = (-250.0, 0.0, 250.0, 500.0) road_bbox = RoadBoundingBox(bbox_size) member = BeamNGMember(data["control_nodes"], [tuple(t) for t in sample_nodes], len(data["control_nodes"]), road_bbox) member.config = self.config member.problem = self.problem simulation_id = time.strftime('%Y-%m-%d--%H-%M-%S', time.localtime()) sim_name = member.config.simulation_name.replace( '$(id)', simulation_id) simulation_data = SimulationData(sim_name) states = [] for record in records: state = VehicleState(timer=record["timer"], pos=record["pos"], dir=record["dir"], vel=record["vel"], steering=record["steering"], steering_input=record["steering_input"], brake=record["brake"], brake_input=record["brake_input"], throttle=record["throttle"], throttle_input=record["throttle_input"], wheelspeed=record["wheelspeed"], vel_kmh=record["vel_kmh"]) sim_data_record = SimulationDataRecord( **state._asdict(), is_oob=record["is_oob"], oob_counter=record["oob_counter"], max_oob_percentage=record["max_oob_percentage"], oob_distance=record["oob_distance"]) states.append(sim_data_record) simulation_data.params = SimulationParams( beamng_steps=data["params"]["beamng_steps"], delay_msec=int(data["params"]["delay_msec"])) simulation_data.road = DecalRoad.from_dict(data["road"]) simulation_data.info = SimulationInfo() simulation_data.info.start_time = data["info"]["start_time"] simulation_data.info.end_time = data["info"]["end_time"] simulation_data.info.elapsed_time = data["info"]["elapsed_time"] simulation_data.info.success = data["info"]["success"] simulation_data.info.computer_name = data["info"]["computer_name"] simulation_data.info.id = data["info"]["id"] simulation_data.states = states if len(states) > 0: member.distance_to_boundary = simulation_data.min_oob_distance() member.simulation = simulation_data individual: BeamNGIndividual = BeamNGIndividual( member, self.config) individual.seed = path else: print("*********Bug************") return individual