class_docstr: Tests for the multi-task wrapper.
class_docstr_tok: ["Tests","for","the","multi-task","wrapper","."]
docstr: Test for the collisions:
  
  The wrapped algo suggests 2 trials, with different task ids, but the same other parameters:
  {"task_id": 0, "x": 1}, {"task_id": 1, "x": 1}
  The wrapper should then suggest a single trial, since it removes the task id:
  {"x": 1}
  Then, when the wrapper observes a result for that trial:
  {"x": 1} (objective = 123)
  The wrapped algo should then have both the original trials set to have that new result:
  {"task_id": 0, "x": 1} (objective=123), {"task_id": 1, "x": 1} (objective=123)
code: def test_collisions(self, n_previous_experiments: int):
      """Test for the collisions:
  
      The wrapped algo suggests 2 trials, with different task ids, but the same other parameters:
      {"task_id": 0, "x": 1}, {"task_id": 1, "x": 1}
      The wrapper should then suggest a single trial, since it removes the task id:
      {"x": 1}
      Then, when the wrapper observes a result for that trial:
      {"x": 1} (objective = 123)
      The wrapped algo should then have both the original trials set to have that new result:
      {"task_id": 0, "x": 1} (objective=123), {"task_id": 1, "x": 1} (objective=123)
      """
      # NOTE: When n_previous_experiments is 1, the `task_id` dimension is a binary logit
      # But when n_previous_experiments is >=2, the `task_id` dimension becomes categorical and
      # is split into task_id[0], task_id[1], ..., task_id[n_previous_experiments].
      kb = create_dummy_kb(
          previous_spaces=previous_spaces[:n_previous_experiments],
          n_trials_per_space=1,  # doesn't matter for this test.
      )
      wrapper = create_algo(
          FixedSuggestionAlgo,
          space=target_space,
          knowledge_base=kb,
          seed=123,
      )
      algo = wrapper.unwrapped
      assert isinstance(wrapper, MultiTaskWrapper)
      assert isinstance(algo, FixedSuggestionAlgo)
  
      assert algo.fixed_suggestion in algo.space
  
      # Create two trials with same params but different task id.
      t1 = algo.fixed_suggestion
      t2 = copy.deepcopy(algo.fixed_suggestion)
      if n_previous_experiments == 1:
          assert "task_id" in t1.params
          # The task_id should be set to 0, since we set the prior of the space to only allow
          # sampling trials with task_id of 0 (see other test).
          assert t1.params["task_id"] == 0
          # Set t2 to have the other task id.
          # NOTE: Can't change a trial's params like this. The `params` property is just a view.
          # t2.params["task_id"] = 1.0
          _set_params(t2, {"task_id": 1})
      else:
          # Same here, only the task_id[0] should have a non-zero value (of 1).
          assert t1.params["task_id[0]"] == 1
          assert all(
              t1.params[f"task_id[{i}]"] == 0
              for i in range(1, n_previous_experiments + 1)
          )
          _set_params(t2, {"task_id[0]": 0.0, "task_id[1]": 1.0})
  
      assert t1.params != t2.params
      assert t1.id != t2.id
  
      # Get the transformed version of t1 from the Wrapper.
      wrapper_t = wrapper.suggest(1)[0]
  
      algo.fixed_suggestion = t2
      new_wrapper_suggestions = wrapper.suggest(1)
      # The wrapper can't suggest anything, because the trial is a duplicate of the previous when
      # the task ids are removed.
      assert not new_wrapper_suggestions
  
      # NOTE: The collision should be recorded in this MultiTask wrapper, not in the
      # SpaceTransform wrapper!
      assert wrapper_t in wrapper.registry_mapping
      equivalent_trials = wrapper.registry_mapping[wrapper_t]
      assert len(equivalent_trials) == 2
  
      assert {t.params["task_id"] for t in equivalent_trials} == {0, 1}
  
imports: from __future__ import annotations
  import copy
  import random
  from typing import Any, Callable, Sequence
  import numpy
  import pytest
  from orion.algo.base import BaseAlgorithm
  from orion.algo.gridsearch import GridSearch
  from orion.algo.space import Space
  from orion.algo.tpe import TPE
  from orion.client import build_experiment
  from orion.client.experiment import ExperimentClient
  from orion.core.io.space_builder import SpaceBuilder
  from orion.core.worker.algo_wrappers.insist_suggest import InsistSuggest
  from orion.core.worker.algo_wrappers.space_transform import SpaceTransform
  from orion.core.worker.experiment import Experiment
  from orion.core.worker.experiment_config import ExperimentConfig
  from orion.core.worker.primary_algo import create_algo
  from orion.core.worker.trial import Trial
  from orion.core.worker.warm_start.knowledge_base import KnowledgeBase
  from orion.core.worker.warm_start.multi_task_wrapper import MultiTaskWrapper
  from orion.storage.base import BaseStorageProtocol
  from orion.testing.dummy_algo import FixedSuggestionAlgo
  from orion.algo.random import Random
  from orion.core.worker.warm_start import WarmStarteable
label_desc: Use image node_id 1 for calling the TestMultiTaskWrapper obj's underlying member method code with example usage: obj.test_collisions(n_previous_experiments) without return types