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 mutate_gene(self, index, xy_prob=0.5) -> Tuple[int, int]: gene = list(self.road.control_nodes[index]) mut_value = random.randint(self.lower_bound, self.higher_bound) if mut_value == 0: mut_value += 1 c = 0 if random.random() < xy_prob: c = 1 gene[c] += mut_value self.road.control_nodes[index] = tuple(gene) self.road.sample_nodes = catmull_rom(self.road.control_nodes, self.road.num_spline_nodes) return c, mut_value
def undo_mutation(self, index, c, mut_value): gene = list(self.road.control_nodes[index]) gene[c] -= mut_value self.road.control_nodes[index] = tuple(gene) self.road.sample_nodes = catmull_rom(self.road.control_nodes, self.road.num_spline_nodes)