Example #1
0
    def _execute(self, the_test):
        # Ensure we do not execute anything longer than the time budget
        super()._execute(the_test)

        test_outcome = random.choice(["FAIL", "FAIL", "FAIL", "PASS", "PASS", "PASS", "PASS", "PASS", "ERROR"])
        description = "Mocked test results"


        sim_state = SimulationDataRecord(
            timer=3.0,
            pos= 0.0,
            dir= 0.0,
            vel= 0.0,
            steering= 0.0,
            steering_input= 0.0,
            brake= 0.0,
            brake_input= 0.0,
            throttle= 0.0,
            throttle_input= 0.0,
            wheelspeed= 0.0,
            vel_kmh = 0.0,
            is_oob= False,
            oob_counter = 0,
            max_oob_percentage = 0.0,
            oob_distance= 0.0,
        )

        execution_data = [sim_state]

        log.info("Pretend test is executing")
        time.sleep(5)
        self.total_elapsed_time += 5

        return test_outcome, description, execution_data
Example #2
0
def _load_test_data(execution_data_file):
    # Load the execution data
    with open(execution_data_file) as input_file:
        json_data = json.load(input_file)
        road_data = json_data["road_points"]
        execution_data = [SimulationDataRecord(*record) for record in json_data["execution_data"]] \
            if "execution_data" in json_data else []
    return road_data, execution_data
    def test_load_the_data_from_json(self):
        path_json = "./results/test.0001.json"
        with open(path_json, 'r') as f:
            obj = json.loads(f.read())

        states = [SimulationDataRecord(*r) for r in obj["execution_data"]]
        print("Read ", len(states), "from file", path_json)

        print("Steering: ", states[-1].steering)
Example #4
0
 def _load_test_data(self, execution_data_file):
     # Load the execution data
     with open(execution_data_file) as input_file:
         json_data = json.load(input_file)
         test_id = json_data["id"]
         road_data = json_data["road_points"]
         is_valid = json_data["is_valid"]
         test_outcome = json_data["test_outcome"]
         execution_data = [SimulationDataRecord(*record) for record in json_data["execution_data"]] \
             if is_valid else []
     return test_id, is_valid, test_outcome, road_data, execution_data
Example #5
0
    def collect_current_data(self, oob_bb=True, wrt="right"):
        """If oob_bb is True, then the out-of-bound (OOB) examples are calculated
        using the bounding box of the car."""
        self.vehicle_state_reader.update_state()
        car_state = self.vehicle_state_reader.get_state()

        is_oob, oob_counter, max_oob_percentage, oob_distance, oob_percentage = self.oob_monitor.get_oob_info(oob_bb=oob_bb, wrt=wrt)

        sim_data_record = SimulationDataRecord(**car_state._asdict(),
                                               is_oob=is_oob,
                                               oob_counter=oob_counter,
                                               max_oob_percentage=max_oob_percentage,
                                               oob_distance=oob_distance,
                                               oob_percentage=oob_percentage)
        self.states.append(sim_data_record)
Example #6
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]
                }))
Example #7
0
    def start(self, sid: SimulationID, vid: VehicleID,
              dynamic_callback: Callable[[], None]) -> None:
        from drivebuildclient.aiExchangeMessages_pb2 import SimStateResponse, DataRequest, Control
        from self_driving.simulation_data import SimulationDataRecord
        from PIL import Image
        import io
        request = DataRequest()
        request.request_ids.append("center_cam")
        simulation_data_records = []
        while True:
            sim_state = self._service.wait_for_simulator_request(sid, vid)
            dynamic_callback()
            if sim_state == SimStateResponse.SimState.RUNNING:
                data = self._service.request_data(sid, vid, request).data
                # Request camera image
                byte_im = data["center_cam"].camera.color
                image = Image.open(io.BytesIO(byte_im)).convert("RGB")

                # Determine last state
                # is_oob, oob_counter, max_oob_percentage, oob_distance = self._oob_monitor.get_oob_info(oob_bb=False, wrt="right")
                is_oob = False
                oob_counter = max_oob_percentage = oob_distance = None
                car_state = {
                    "timer": 0,
                    "damage": 0,
                    "pos": 0,
                    "dir": 0,
                    "vel": 0,
                    "gforces": 0,
                    "gforces2": 0,
                    "steering": 0,
                    "steering_input": 0,
                    "brake": 0,
                    "brake_input": 0,
                    "throttle": 0,
                    "throttle_input": 0,
                    "throttleFactor": 0,
                    "engineThrottle": 0,
                    "wheelspeed": 0,
                    "vel_kmh": data["ego_speed"].speed.speed
                }
                sim_data_record = SimulationDataRecord(
                    **car_state,
                    is_oob=is_oob,
                    oob_counter=oob_counter,
                    max_oob_percentage=max_oob_percentage,
                    oob_distance=oob_distance)
                simulation_data_records.append(sim_data_record)
                last_state = simulation_data_records[-1]
                if last_state.is_oob:
                    break

                # Compute control command
                steering_angle, throttle = self._prediction.predict(
                    image, last_state)
                control = Control.AvCommand()
                control.steering_angle = steering_angle
                control.accelerate = throttle
                self._service.control(sid, vid, control)
            else:
                break
Example #8
0
    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