def initialize_world_from_spec(self,
                                 situation_spec,
                                 referred_size,
                                 referred_color,
                                 referred_shape,
                                 actual_size,
                                 sample_percentage=0.5,
                                 min_other_objects=0,
                                 max_objects=12,
                                 ref_predicate=None,
                                 max_distract_objects=1):
    """Intialize the world. Return True if initialization is valid."""
    self._world.clear_situation()
    self._world.place_agent_at(situation_spec["agent_position"])
    target_shape = situation_spec["target_shape"]
    target_color = situation_spec["target_color"]
    target_size = situation_spec["target_size"]
    target_object = world_lib.Object(
        size=target_size, color=target_color, shape=target_shape)
    target_position = situation_spec["target_position"]
    self._world.place_object(target_object, target_position, target=True)
    distinct_objects, obligatory_objects = self.generate_distinct_objects(
        referred_size=self._vocabulary.translate_word(referred_size),
        referred_color=self._vocabulary.translate_word(referred_color),
        referred_shape=self._vocabulary.translate_word(referred_shape),
        actual_size=actual_size,
        actual_color=target_color)

    if ref_predicate and ref_predicate["location"]:
      is_distinct_from_target = functools.partial(
          self.is_distinct,
          referred_size=self._vocabulary.translate_word(referred_size),
          referred_color=self._vocabulary.translate_word(referred_color),
          referred_shape=self._vocabulary.translate_word(referred_shape),
          actual_object=target_object)
      # Place the reference object, which should be distinct from the target.
      ref_object, ref_position = self.place_target_reference_object(
          ref_predicate, is_distinct_from_target, target_position)
      if ref_object is None:
        return False
      ref_size, ref_color, _ = ref_object
      is_target_distinct_from_ref = self.is_distinct(
          referred_size=self._vocabulary.translate_word(ref_predicate["size"]),
          referred_color=self._vocabulary.translate_word(
              ref_predicate["color"]),
          referred_shape=self._vocabulary.translate_word(ref_predicate["noun"]),
          actual_object=ref_object,
          other_object=target_object)

      # Place distract objects and their reference objects if any. The distract
      # objects should be distinct from the target and reference object.
      distinct_ref_objects, obligatory_ref_objects = self.generate_distinct_objects(
          referred_size=self._vocabulary.translate_word(ref_predicate["size"]),
          referred_color=self._vocabulary.translate_word(
              ref_predicate["color"]),
          referred_shape=self._vocabulary.translate_word(ref_predicate["noun"]),
          actual_size=ref_size,
          actual_color=ref_color)
      distinct_ref_objects = itertools.chain(*distinct_ref_objects)
      distinct_ref_objects = self.filter_distinct_objects(
          is_distinct_from_target, distinct_ref_objects)
      filtered_obligatory_ref_objects = self.filter_distinct_objects(
          is_distinct_from_target, obligatory_ref_objects)
      # Return if there is any obligatory that is not distinct from target.
      if len(filtered_obligatory_ref_objects) < len(obligatory_ref_objects):
        return False
      # Return if there is no valid distract objects.
      if not distinct_ref_objects + obligatory_ref_objects:
        return False

      valid_distractors = self.generate_distract_objects(
          distinct_ref_objects,
          obligatory_ref_objects,
          target_object,
          ref_object,
          target_position,
          ref_position,
          ref_predicate,
          max_distract_objects=max_distract_objects,
          is_target_distinct_from_ref=is_target_distinct_from_ref)
      if not valid_distractors:
        return False

      # Remove objects that are not distinct from reference object.
      is_distinct_from_ref = functools.partial(
          self.is_distinct,
          referred_size=self._vocabulary.translate_word(ref_predicate["size"]),
          referred_color=self._vocabulary.translate_word(
              ref_predicate["color"]),
          referred_shape=self._vocabulary.translate_word(ref_predicate["noun"]),
          actual_object=ref_object)
      distinct_objects = [
          self.filter_distinct_objects(is_distinct_from_ref, obj_list)
          for obj_list in distinct_objects
      ]
      distinct_objects = [obj_list for obj_list in distinct_objects if obj_list]
      obligatory_objects = self.filter_distinct_objects(is_distinct_from_ref,
                                                        obligatory_objects)

    # Place other random objects.
    if len(distinct_objects) > 1:
      num_to_sample = int(len(distinct_objects) * sample_percentage)
    else:
      num_to_sample = len(distinct_objects)
    num_to_sample = min(max(min_other_objects, num_to_sample), max_objects)
    objects_to_place = obligatory_objects
    sampled_objects = random.sample(distinct_objects, k=num_to_sample)
    for sampled_objs in sampled_objects:
      objects_to_place.extend(sampled_objs)
    for size, color, shape in objects_to_place:
      other_position = self._world.sample_position()
      self._world.place_object(
          world_lib.Object(size=size, color=color, shape=shape),
          position=other_position)
    return True
Intialize the world. Return True if initialization is valid.
language/gscan/data/dataset.py