class_docstr: Tests for the KnowledgeBase class.
class_docstr_tok: ["Tests","for","the","KnowledgeBase","class","."]
code: def test_similarity_fn_is_used(
      self,
      storage: BaseStorageProtocol,
      tmp_path: Path,
      max_trials: int | None,
  ):
      n_previous_experiments = 2
      n_trials_per_experiment = 10
  
      n_trials_in_kb = n_previous_experiments * n_trials_per_experiment
      source_experiments = create_experiments(
          n_previous_experiments=n_previous_experiments,
          n_trials_per_experiment=n_trials_per_experiment,
          storage=storage,
      )
  
      def _similarity_metric(
          exp_a: ExperimentConfig, exp_b: ExperimentConfig
      ) -> float:
          """Some dummy similarity metric.
  
          We have experiment names like "source_{i}" and "target_{j}", so the similarity is just
          defined as 1 - abs(i - j).
          """
          a_name = exp_a["name"]
          b_name = exp_b["name"]
          _, _, a_suffix = a_name.partition("_")
          _, _, b_suffix = b_name.partition("_")
          return 1 - abs(int(a_suffix) - int(b_suffix))
  
      kb = KnowledgeBase(storage, similarity_metric=_similarity_metric)
  
      # NOTE: Here we use a number higher than the number of experiments in the KB, so that we
      # expect to get the experiments in the reverse order in which they were created.
      target_experiment = build_experiment(
          name="target_100",
          space={"x": "uniform(0, 1)"},
          storage={
              "type": "legacy",
              "database": {
                  "type": "pickleddb",
                  "host": f"{tmp_path}/foobar.pkl",
              },
          },
      )
  
      related_trials = kb.get_related_trials(
          target_experiment,
          max_trials=max_trials,
      )
      assert len(related_trials) == n_previous_experiments
      for i, (experiment, trials) in enumerate(related_trials):
          # Check that the experiments are given with a name like "source_{i}" where i is in
          # decreasing order from `n_previous_experiments-1` down to 0.
          assert (
              experiment["name"]
              == f"source_{n_previous_experiments - 1 - i}"
          )
  
file_path: orion/tests/unittests/core/worker/warm_start/test_knowledge_base.py
imports: from __future__ import annotations
  from pathlib import Path
  import pytest
  from orion.benchmark.task import RosenBrock
  from orion.client import build_experiment
  from orion.client.experiment import ExperimentClient
  from orion.core.io.database.pickleddb import PickledDB
  from orion.core.worker.experiment_config import ExperimentConfig
  from orion.core.worker.warm_start import KnowledgeBase
  from orion.storage.base import BaseStorageProtocol
  from orion.storage.legacy import Legacy
label_desc: Use image node_id 3 for calling the TestKnowledgeBase obj's underlying member method code with example usage: obj.test_similarity_fn_is_used(storage, tmp_path, max_trials) and returns: unknown