def sample_entity(self, world, last_entity, combinations=None):
     if last_entity == -1:
         self.provoke_collision = random() < self.provoke_collision_rate
     elif last_entity is not None:
         self.provoke_collision = random() < self.provoke_collision_rate
     center = world.random_location(
         provoke_collision=self.provoke_collision)
     if combinations is None:
         return Entity.random_instance(
             center=center,
             rotation=self.rotation,
             size_range=self.size_range,
             distortion_range=self.distortion_range,
             shade_range=self.shade_range,
             shapes=self.selected_shapes,
             colors=self.selected_colors,
             textures=self.selected_textures)
     else:
         return Entity.random_instance(
             center=center,
             rotation=self.rotation,
             size_range=self.size_range,
             distortion_range=self.distortion_range,
             shade_range=self.shade_range,
             combinations=combinations)
示例#2
0
 def sample_entity(self, world, last_entity, combinations=None):
     if last_entity == -1:
         self.provoke_collision = random() < self.provoke_collision_rate
         self.shape_pool = list(self.shapes)
         self.color_pool = list(self.colors)
         self.texture_pool = list(self.textures)
     elif last_entity is not None:
         self.provoke_collision = random() < self.provoke_collision_rate
         reinforcement_step = randint(*self.reinforcement_range)
         for _ in range(reinforcement_step):
             self.shape_pool.append(last_entity.shape.name)
             self.color_pool.append(last_entity.color.name)
             self.texture_pool.append(last_entity.texture.name)
     center = world.random_location(
         provoke_collision=self.provoke_collision)
     if combinations is None:
         return Entity.random_instance(
             center=center,
             rotation=self.rotation,
             size_range=self.size_range,
             distortion_range=self.distortion_range,
             shade_range=self.shade_range,
             shapes=self.shape_pool,
             colors=self.color_pool,
             textures=self.texture_pool)
     else:
         return Entity.random_instance(
             center=center,
             rotation=self.rotation,
             size_range=self.size_range,
             distortion_range=self.distortion_range,
             shade_range=self.shade_range,
             combinations=combinations)
 def sample_entity(self, world, last_entity, combinations=None):
     if last_entity == -1:
         while True:
             self.clusters = list()
             for _ in range(self.num_clusters * self.__class__.MAX_ATTEMPTS):
                 r = random()
                 if r < 0.25:
                     cluster_center = Point(0.125 + r * 3.0, 0.15)
                 elif r < 0.5:
                     cluster_center = Point(0.125 + (r - 0.25) * 3.0, 0.85)
                 elif r < 0.75:
                     cluster_center = Point(0.15, 0.125 + (r - 0.5) * 3.0)
                 else:
                     cluster_center = Point(0.85, 0.125 + (r - 0.75) * 3.0)
                 # cluster_center = world.random_location()
                 if all(cluster_center.distance(cluster) > self.__class__.MIN_CLUSTERS_DISTANCE for cluster in self.clusters):
                     self.clusters.append(cluster_center)
                     if len(self.clusters) == self.num_clusters:
                         break
             else:
                 print(self.clusters)
                 continue
             break
     elif last_entity is not None:
         pass
     self.selected_cluster = choice(self.clusters)
     angle = Point.from_angle(angle=random())
     center = self.selected_cluster + angle * random() * self.__class__.MAX_CLUSTER_CENTER_DISTANCE
     if combinations is None:
         return Entity.random_instance(center=center, rotation=self.rotation, size_range=self.size_range, distortion_range=self.distortion_range, shade_range=self.shade_range, shapes=self.shapes, colors=self.colors, textures=self.textures)
     else:
         return Entity.random_instance(center=center, rotation=self.rotation, size_range=self.size_range, distortion_range=self.distortion_range, shade_range=self.shade_range, combinations=combinations)
示例#4
0
 def generate_validation_world(self):
     world = World(self.world_size, self.world_color, self.noise_range)
     n_entity = choice(self.validation_entity_counts)
     if n_entity == 0:
         return world
     shapes = GenericGenerator.choose(self.validation_shapes,
                                      self.shapes_range)
     colors = GenericGenerator.choose(self.validation_colors,
                                      self.colors_range)
     textures = GenericGenerator.choose(self.validation_textures,
                                        self.textures_range)
     if self.validation_combinations:
         for _ in range(GenericGenerator.MAX_ATTEMPTS):
             entity = Entity.random_instance(
                 center=world.random_location(),
                 rotation=self.rotation,
                 size_range=self.size_range,
                 distortion_range=self.distortion_range,
                 shade_range=self.shade_range,
                 combinations=self.validation_combinations)
             if world.add_entity(
                     entity,
                     boundary_tolerance=self.boundary_tolerance,
                     collision_tolerance=self.collision_tolerance):
                 break
         else:
             return None
         n = 1
     else:
         n = 0
     for _ in range(n_entity * GenericGenerator.MAX_ATTEMPTS):
         entity = Entity.random_instance(
             center=world.random_location(),
             shapes=shapes,
             size_range=self.size_range,
             distortion_range=self.distortion_range,
             rotation=self.rotation,
             colors=colors,
             shade_range=self.shade_range,
             textures=textures)
         n += world.add_entity(entity,
                               boundary_tolerance=self.boundary_tolerance,
                               collision_tolerance=self.collision_tolerance)
         if n >= n_entity:
             break
     else:
         return None
     if self.collision_tolerance:
         world.sort_entities()
     return world
示例#5
0
 def generate_test_world(self):
     world = World(self.world_size, self.world_color)
     if self.num_entities == 0:
         return world
     if self.test_combinations:
         provoke_collision = random() < self.provoke_collision_rate
         while True:
             center = world.random_location(
                 provoke_collision=provoke_collision)
             entity = Entity.random_instance(
                 center=center,
                 rotation=self.rotation,
                 size_range=self.size_range,
                 distortion_range=self.distortion_range,
                 shade_range=self.shade_range,
                 combinations=self.test_combinations)
             if world.add_entity(
                     entity,
                     boundary_tolerance=self.boundary_tolerance,
                     collision_tolerance=self.collision_tolerance):
                 break
         n = 1
     else:
         n = 0
     provoke_collision = random() < self.provoke_collision_rate
     for _ in range(self.num_entities * self.__class__.MAX_ATTEMPTS):
         center = world.random_location(provoke_collision=provoke_collision)
         entity = Entity.random_instance(
             center=center,
             shapes=self.selected_shapes,
             size_range=self.size_range,
             distortion_range=self.distortion_range,
             rotation=self.rotation,
             colors=self.selected_colors,
             shade_range=self.shade_range,
             textures=self.selected_textures)
         if world.add_entity(entity,
                             boundary_tolerance=self.boundary_tolerance,
                             collision_tolerance=self.collision_tolerance):
             n += 1
             provoke_collision = random() < self.provoke_collision_rate
         if n == self.num_entities:
             break
     else:
         return None
     if self.collision_tolerance:
         world.sort_entities()
     return world
示例#6
0
 def from_model(model):
     world = World(size=model['size'], color=model['color']['name'])
     for entity_model in model['entities']:
         world.entities.append(Entity.from_model(entity_model))
     if 'meta' in model:
         world.meta.update(model['meta'])
     return world
示例#7
0
 def from_model(model):
     world = World(size=model['size'], color=Color.from_model(model['color']))
     for entity_model in model['entities']:
         world.entities.append(Entity.from_model(entity_model))
     return world