def initial_pool_generator(config, problem):
    good_members_found = 0
    attempts = 0
    storage = SeedStorage('initial_pool')

    while good_members_found < config.POOLSIZE:  #40:
        path = storage.get_path_by_index(good_members_found + 1)
        # if path.exists():
        #     print('member already exists', path)
        #     good_members_found += 1
        #     continue
        attempts += 1
        print(
            f'attempts {attempts} good {good_members_found} looking for {path}'
        )
        member = problem.generate_random_member()
        member.evaluate()
        if member.distance_to_boundary <= 0:
            continue
        member = problem.member_class().from_dict(member.to_dict())
        member.config = config
        member.problem = problem
        #member.clear_evaluation()

        member.distance_to_boundary = None
        good_members_found += 1
        path.write_text(json.dumps(member.to_dict()))

    return storage.folder
Esempio n. 2
0
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)
Esempio n. 3
0
def initial_pool_generator(config, problem):
    good_members_found = 0
    attempts = 0
    storage = SeedStorage('initial_pool')
    i = 0
    while good_members_found < config.POOLSIZE:
        path = storage.get_path_by_index(good_members_found + 1)
        # if path.exists():
        #     print('member already exists', path)
        #     good_members_found += 1
        #     continue
        attempts += 1
        log.debug(
            f'attempts {attempts} good {good_members_found} looking for {path}'
        )

        max_angle = random.randint(10, 100)

        member = problem.generate_random_member(max_angle)
        member.evaluate()
        if member.distance_to_boundary <= 0:
            continue
        #member = problem.member_class().from_dict(member.to_dict())
        member.config = config
        member.problem = problem
        #member.clear_evaluation()

        #member.distance_to_boundary = None
        good_members_found += 1
        # path.write_text(json.dumps(member.to_dict()))
        path.write_text(
            json.dumps({
                "control_nodes":
                member.control_nodes,
                member.simulation.f_params:
                member.simulation.params._asdict(),
                member.simulation.f_info:
                member.simulation.info.__dict__,
                member.simulation.f_road:
                member.simulation.road.to_dict(),
                member.simulation.f_records:
                [r._asdict() for r in member.simulation.states]
            }))
    return storage.folder
Esempio n. 4
0
class SeedPoolFolder(SeedPool):
    def __init__(self, problem: Problem, folder_name):
        super().__init__(problem)
        self.storage = SeedStorage(folder_name)
        self.file_path_list = self.storage.all_files()
        assert (len(self.file_path_list)) > 0
        self.cache: Dict[str, Member] = {}

    def __len__(self):
        return len(self.file_path_list)

    def __getitem__(self, item) -> Member:
        path = self.file_path_list[item]
        result: Member = self.cache.get(path, None)
        if not result:
            result = self.problem.member_class().from_dict(
                self.storage.read(path))
            self.cache[path] = result
        result.problem = self.problem
        return result
def initial_population_generator(path, config, problem):
    all_roads = [
        filename
        for filename in glob.glob(str(path) + "\*.json", recursive=True)
    ]
    #all_roads += [filename for filename in glob.glob(path2)]

    shuffle(all_roads)

    roads = all_roads[:40]

    starting_point = choice(roads)

    original_set = list()
    original_set.append(starting_point)

    popsize = config.POPSIZE

    i = 0
    while i < popsize - 1:
        max_dist = 0
        for ind in roads:

            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):
        path = storage.get_path_by_index(index + 1)
        dst = path
        copy(road, dst)
Esempio n. 6
0
                    beamng.resume()
                    break

            # one step is 0.05 seconds (5/100)
            beamng.step(steps)

    try:
        start()
    finally:
        beamng.close()


if __name__ == '__main__':
    dataset_size = 10

    road_storage = SeedStorage('training_set')

    simulation_attempts = 5

    for idx in range(1, dataset_size + 1):
        path = road_storage.get_path_by_index(idx)

        road_generator = RoadGenerator(num_control_nodes=30, seg_length=20).generate()

        with open(path, 'w') as f:
            f.write(json.dumps(road_generator.to_dict()))

        street_1 = DecalRoad('street_1').add_4d_points(road_generator.sample_nodes)

        for attempt in range(simulation_attempts):
            try:
        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)
config_silly.keras_model_file = 'self-driving-car-4600.h5'
config_smart.beamng_close_at_iteration = True

problem_silliest = BeamNGProblem(config_silliest,
                                 SmartArchive(config_silly.ARCHIVE_THRESHOLD))
problem_silly = BeamNGProblem(config_silly,
                              SmartArchive(config_silly.ARCHIVE_THRESHOLD))
problem_smart = BeamNGProblem(config_smart,
                              SmartArchive(config_smart.ARCHIVE_THRESHOLD))

# problem = BeamNGProblem(config, SmartArchive(config.ARCHIVE_THRESHOLD))

if __name__ == '__main__':
    good_members_found = 0
    attempts = 0
    storage = SeedStorage('seeds_5_silly2')

    while good_members_found < 12:
        path = storage.get_path_by_index(good_members_found + 1)
        if path.exists():
            print('member already exists', path)
            good_members_found += 1
            continue
        attempts += 1
        print(
            f'attempts {attempts} good {good_members_found} looking for {path}'
        )

        member = problem_silliest.generate_random_member()
        member.evaluate()
        if member.distance_to_boundary <= 0:
Esempio n. 9
0
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]
                }))
config_silly.keras_model_file = 'self-driving-car-4600.h5'
config_silly.beamng_close_at_iteration = True

config_smart = BeamNGConfig()
config_smart.keras_model_file = 'self-driving-car-185-2020.h5'
config_smart.beamng_close_at_iteration = True

problem_silly = BeamNGProblem(config_silly, SmartArchive(config_silly.ARCHIVE_THRESHOLD))
problem_smart = BeamNGProblem(config_smart, SmartArchive(config_smart.ARCHIVE_THRESHOLD))

# problem = BeamNGProblem(config, SmartArchive(config.ARCHIVE_THRESHOLD))

if __name__ == '__main__':
    good_members_found = 0
    attempts = 0
    storage = SeedStorage('prova_roads')

    while good_members_found < 100:
        path = storage.get_path_by_index(good_members_found + 1)
        if path.exists():
            print('member already exists', path)
            good_members_found += 1
            continue
        attempts += 1
        print(f'attempts {attempts} good {good_members_found} looking for {path}')
        member = problem_silly.generate_random_member()
        member.evaluate()
        if member.distance_to_boundary <= 0:
            continue
        member_smart = problem_smart.member_class().from_dict(member.to_dict())
        member_smart.config = config_smart
Esempio n. 11
0
import json

from core.config import Config
from core.folder_storage import SeedStorage
from self_driving.road_generator import RoadGenerator

if __name__ == "__main__":
    config = Config()
    seed_storage = SeedStorage(config.seed_folder)
    for i in range(1, 4):
        path = seed_storage.get_path_by_index(i)
        if path.exists():
            print('file ', path, 'already exists')
        else:
            obj = RoadGenerator(
                num_control_nodes=config.num_control_nodes,
                seg_length=config.SEG_LENGTH).generate()
            print('saving', path)
            path.write_text(json.dumps(obj.to_dict()))
 def __init__(self, problem: Problem, folder_name):
     super().__init__(problem)
     self.storage = SeedStorage(folder_name)
     self.file_path_list = self.storage.all_files()
     assert (len(self.file_path_list)) > 0
     self.cache: Dict[str, Member] = {}