colosseum.benchmark.run

  1import os
  2import shutil
  3from typing import TYPE_CHECKING, Dict, List, Type, Tuple, Iterable
  4
  5from colosseum import config
  6from colosseum.benchmark.benchmark import ColosseumBenchmark
  7from colosseum.benchmark.utils import (
  8    instantiate_benchmark_folder,
  9    instantiate_agent_configs,
 10)
 11from colosseum.experiment.experiment_instance import ExperimentInstance
 12from colosseum.experiment.experiment_instances import (
 13    get_experiment_instances_from_folder,
 14)
 15from colosseum.utils import ensure_folder
 16
 17if TYPE_CHECKING:
 18    from colosseum.agent.agents.base import BaseAgent
 19
 20
 21def instantiate_agents_and_benchmark(
 22    agents_configs: Dict[Type["BaseAgent"], str],
 23    benchmark: ColosseumBenchmark,
 24    overwrite_previous_experiment: bool = False,
 25    experiment_folder: str = None,
 26) -> str:
 27    """
 28    instantiate the benchmark and the agents configs locally.
 29
 30    Parameters
 31    ----------
 32    agents_configs : Dict[Type["BaseAgent"], str]
 33        The dictionary associates agent classes to their gin config files.
 34    benchmark : ColosseumBenchmark
 35        The benchmark to be instantiated.
 36    overwrite_previous_experiment : bool
 37        If True the destination folder is cleared before instantiating the MDP configs. If False, it raises an error if
 38        it finds a different set of agents configs, MDP configs, or `ExperimentalConfig` in the destination folder.
 39    experiment_folder : str
 40        The folder where to instantiate the benchmark and the agents configs. By default, it is taken from the package
 41        configurations.
 42
 43    Returns
 44    -------
 45    str
 46        The folder where the benchmark and the agents configs have been instantiated.
 47    """
 48
 49    # Avoid mixing episodic and continuous settings
 50    assert all(
 51        agent_class.is_episodic() == list(agents_configs)[0].is_episodic()
 52        for agent_class in agents_configs
 53    )
 54    assert all(
 55        mdp_configs.is_episodic() == list(agents_configs)[0].is_episodic()
 56        for mdp_configs in benchmark.mdps_gin_configs
 57    )
 58
 59    # Set the current experiment folder of the given benchmark
 60    benchmark_folder = (
 61        config.get_experiments_folder()
 62        if experiment_folder is None
 63        else ensure_folder(experiment_folder)
 64    ) + benchmark.name
 65
 66    if overwrite_previous_experiment:
 67        # Remove any previous experiment directory
 68        shutil.rmtree(benchmark_folder, ignore_errors=True)
 69        os.makedirs(benchmark_folder)
 70
 71    # Instantiate the mdp configs
 72    instantiate_benchmark_folder(benchmark, benchmark_folder)
 73
 74    # Instantiate the agent configs
 75    instantiate_agent_configs(agents_configs, benchmark_folder)
 76
 77    return benchmark_folder
 78
 79
 80def instantiate_and_get_exp_instances_from_benchmark(
 81    agents_configs: Dict[Type["BaseAgent"], str],
 82    benchmark: ColosseumBenchmark,
 83    overwrite_previous_experiment: bool = False,
 84    experiment_folder: str = None,
 85) -> List[ExperimentInstance]:
 86    """
 87    instantiate the benchmark and the agents configs locally, and creates the corresponding `ExperimentInstance`.
 88
 89    Parameters
 90    ----------
 91    agents_configs : Dict[Type["BaseAgent"], str]
 92        The dictionary associates agent classes to their gin config files.
 93    benchmark : ColosseumBenchmark
 94        The benchmark to be instantiated.
 95    overwrite_previous_experiment : bool
 96        If True the destination folder is cleared before instantiating the MDP configs. If False, it raises an error if
 97        it finds a different set of agents configs, MDP configs, or `ExperimentalConfig` in the destination folder.
 98    experiment_folder : str
 99        The folder where to instantiate the benchmark and the agents configs. By default, it is taken from the package
100        configurations.
101
102    Returns
103    -------
104    List[ExperimentInstance]
105        The `ExperimentInstance`s corresponding to the benchmark and the agents configs.
106    """
107
108    # Instantiate the local directories for the benchmark
109    benchmark_folder = instantiate_agents_and_benchmark(
110        agents_configs, benchmark, overwrite_previous_experiment, experiment_folder
111    )
112
113    # Create the ExperimentInstance objects
114    return get_experiment_instances_from_folder(benchmark_folder)
115
116
117def instantiate_and_get_exp_instances_from_agents_and_benchmarks(
118    agents_and_benchmarks: Iterable[
119        Tuple[Dict[Type["BaseAgent"], str], ColosseumBenchmark]
120    ],
121    overwrite_previous_experiment: bool = False,
122    experiment_folder: str = None,
123) -> List[ExperimentInstance]:
124    """
125    instantiate the benchmarks_and_agents and the agents configs locally, and creates the corresponding `ExperimentInstance`.
126
127    Parameters
128    ----------
129    agents_and_benchmarks : Iterable[Tuple[Dict[Type["BaseAgent"], str], ColosseumBenchmark]]
130        The agent configs and benchmarks_and_agents to be instantiated.
131    overwrite_previous_experiment : bool
132        If True the destination folder is cleared before instantiating the MDP configs. If False, it raises an error if
133        it finds a different set of agents configs, MDP configs, or `ExperimentalConfig` in the destination folder.
134    experiment_folder : str
135        The folder where to instantiate the benchmark and the agents configs. By default, it is taken from the package
136        configurations.
137
138    Returns
139    -------
140    List[ExperimentInstance]
141        The `ExperimentInstance`s corresponding to the benchmarks_and_agents and the agents configs.
142    """
143
144    experiment_instances = []
145    for agents_configs, benchmark in agents_and_benchmarks:
146        experiment_instances += instantiate_and_get_exp_instances_from_benchmark(
147            agents_configs, benchmark, overwrite_previous_experiment, experiment_folder
148        )
149    return experiment_instances
150
151
152def instantiate_and_get_exp_instances_from_agents_and_benchmarks_for_hyperopt(
153    agents_and_benchmarks: Iterable[
154        Tuple[Dict[Type["BaseAgent"], str], ColosseumBenchmark]
155    ],
156    overwrite_previous_experiment: bool = False,
157) -> List[ExperimentInstance]:
158    """
159    instantiate the benchmarks_and_agents and the agents configs for the parameters optimization locally (by
160    checking the parameters optimization folder in the package configurations), and creates the corresponding
161    `ExperimentInstance`.
162
163    Parameters
164    ----------
165    agents_and_benchmarks : Iterable[Tuple[Dict[Type["BaseAgent"], str], ColosseumBenchmark]]
166        The agent configs and benchmarks_and_agents to be instantiated.
167    overwrite_previous_experiment : bool
168        If True the destination folder is cleared before instantiating the MDP configs. If False, it raises an error if
169        it finds a different set of agents configs, MDP configs, or `ExperimentalConfig` in the destination folder.
170
171    Returns
172    -------
173    List[ExperimentInstance]
174        The `ExperimentInstance`s corresponding to the benchmarks_and_agents and the agents configs.
175    """
176    return instantiate_and_get_exp_instances_from_agents_and_benchmarks(
177        agents_and_benchmarks,
178        overwrite_previous_experiment,
179        config.get_hyperopt_folder(),
180    )
def instantiate_agents_and_benchmark( agents_configs: Dict[Type[colosseum.agent.agents.base.BaseAgent], str], benchmark: colosseum.benchmark.benchmark.ColosseumBenchmark, overwrite_previous_experiment: bool = False, experiment_folder: str = None) -> str:
22def instantiate_agents_and_benchmark(
23    agents_configs: Dict[Type["BaseAgent"], str],
24    benchmark: ColosseumBenchmark,
25    overwrite_previous_experiment: bool = False,
26    experiment_folder: str = None,
27) -> str:
28    """
29    instantiate the benchmark and the agents configs locally.
30
31    Parameters
32    ----------
33    agents_configs : Dict[Type["BaseAgent"], str]
34        The dictionary associates agent classes to their gin config files.
35    benchmark : ColosseumBenchmark
36        The benchmark to be instantiated.
37    overwrite_previous_experiment : bool
38        If True the destination folder is cleared before instantiating the MDP configs. If False, it raises an error if
39        it finds a different set of agents configs, MDP configs, or `ExperimentalConfig` in the destination folder.
40    experiment_folder : str
41        The folder where to instantiate the benchmark and the agents configs. By default, it is taken from the package
42        configurations.
43
44    Returns
45    -------
46    str
47        The folder where the benchmark and the agents configs have been instantiated.
48    """
49
50    # Avoid mixing episodic and continuous settings
51    assert all(
52        agent_class.is_episodic() == list(agents_configs)[0].is_episodic()
53        for agent_class in agents_configs
54    )
55    assert all(
56        mdp_configs.is_episodic() == list(agents_configs)[0].is_episodic()
57        for mdp_configs in benchmark.mdps_gin_configs
58    )
59
60    # Set the current experiment folder of the given benchmark
61    benchmark_folder = (
62        config.get_experiments_folder()
63        if experiment_folder is None
64        else ensure_folder(experiment_folder)
65    ) + benchmark.name
66
67    if overwrite_previous_experiment:
68        # Remove any previous experiment directory
69        shutil.rmtree(benchmark_folder, ignore_errors=True)
70        os.makedirs(benchmark_folder)
71
72    # Instantiate the mdp configs
73    instantiate_benchmark_folder(benchmark, benchmark_folder)
74
75    # Instantiate the agent configs
76    instantiate_agent_configs(agents_configs, benchmark_folder)
77
78    return benchmark_folder

instantiate the benchmark and the agents configs locally.

Parameters
  • agents_configs (Dict[Type["BaseAgent"], str]): The dictionary associates agent classes to their gin config files.
  • benchmark (ColosseumBenchmark): The benchmark to be instantiated.
  • overwrite_previous_experiment (bool): If True the destination folder is cleared before instantiating the MDP configs. If False, it raises an error if it finds a different set of agents configs, MDP configs, or ExperimentalConfig in the destination folder.
  • experiment_folder (str): The folder where to instantiate the benchmark and the agents configs. By default, it is taken from the package configurations.
Returns
  • str: The folder where the benchmark and the agents configs have been instantiated.
def instantiate_and_get_exp_instances_from_benchmark( agents_configs: Dict[Type[colosseum.agent.agents.base.BaseAgent], str], benchmark: colosseum.benchmark.benchmark.ColosseumBenchmark, overwrite_previous_experiment: bool = False, experiment_folder: str = None) -> List[colosseum.experiment.experiment_instance.ExperimentInstance]:
 81def instantiate_and_get_exp_instances_from_benchmark(
 82    agents_configs: Dict[Type["BaseAgent"], str],
 83    benchmark: ColosseumBenchmark,
 84    overwrite_previous_experiment: bool = False,
 85    experiment_folder: str = None,
 86) -> List[ExperimentInstance]:
 87    """
 88    instantiate the benchmark and the agents configs locally, and creates the corresponding `ExperimentInstance`.
 89
 90    Parameters
 91    ----------
 92    agents_configs : Dict[Type["BaseAgent"], str]
 93        The dictionary associates agent classes to their gin config files.
 94    benchmark : ColosseumBenchmark
 95        The benchmark to be instantiated.
 96    overwrite_previous_experiment : bool
 97        If True the destination folder is cleared before instantiating the MDP configs. If False, it raises an error if
 98        it finds a different set of agents configs, MDP configs, or `ExperimentalConfig` in the destination folder.
 99    experiment_folder : str
100        The folder where to instantiate the benchmark and the agents configs. By default, it is taken from the package
101        configurations.
102
103    Returns
104    -------
105    List[ExperimentInstance]
106        The `ExperimentInstance`s corresponding to the benchmark and the agents configs.
107    """
108
109    # Instantiate the local directories for the benchmark
110    benchmark_folder = instantiate_agents_and_benchmark(
111        agents_configs, benchmark, overwrite_previous_experiment, experiment_folder
112    )
113
114    # Create the ExperimentInstance objects
115    return get_experiment_instances_from_folder(benchmark_folder)

instantiate the benchmark and the agents configs locally, and creates the corresponding ExperimentInstance.

Parameters
  • agents_configs (Dict[Type["BaseAgent"], str]): The dictionary associates agent classes to their gin config files.
  • benchmark (ColosseumBenchmark): The benchmark to be instantiated.
  • overwrite_previous_experiment (bool): If True the destination folder is cleared before instantiating the MDP configs. If False, it raises an error if it finds a different set of agents configs, MDP configs, or ExperimentalConfig in the destination folder.
  • experiment_folder (str): The folder where to instantiate the benchmark and the agents configs. By default, it is taken from the package configurations.
Returns
  • List[ExperimentInstance]: The ExperimentInstances corresponding to the benchmark and the agents configs.
def instantiate_and_get_exp_instances_from_agents_and_benchmarks( agents_and_benchmarks: Iterable[Tuple[Dict[Type[colosseum.agent.agents.base.BaseAgent], str], colosseum.benchmark.benchmark.ColosseumBenchmark]], overwrite_previous_experiment: bool = False, experiment_folder: str = None) -> List[colosseum.experiment.experiment_instance.ExperimentInstance]:
118def instantiate_and_get_exp_instances_from_agents_and_benchmarks(
119    agents_and_benchmarks: Iterable[
120        Tuple[Dict[Type["BaseAgent"], str], ColosseumBenchmark]
121    ],
122    overwrite_previous_experiment: bool = False,
123    experiment_folder: str = None,
124) -> List[ExperimentInstance]:
125    """
126    instantiate the benchmarks_and_agents and the agents configs locally, and creates the corresponding `ExperimentInstance`.
127
128    Parameters
129    ----------
130    agents_and_benchmarks : Iterable[Tuple[Dict[Type["BaseAgent"], str], ColosseumBenchmark]]
131        The agent configs and benchmarks_and_agents to be instantiated.
132    overwrite_previous_experiment : bool
133        If True the destination folder is cleared before instantiating the MDP configs. If False, it raises an error if
134        it finds a different set of agents configs, MDP configs, or `ExperimentalConfig` in the destination folder.
135    experiment_folder : str
136        The folder where to instantiate the benchmark and the agents configs. By default, it is taken from the package
137        configurations.
138
139    Returns
140    -------
141    List[ExperimentInstance]
142        The `ExperimentInstance`s corresponding to the benchmarks_and_agents and the agents configs.
143    """
144
145    experiment_instances = []
146    for agents_configs, benchmark in agents_and_benchmarks:
147        experiment_instances += instantiate_and_get_exp_instances_from_benchmark(
148            agents_configs, benchmark, overwrite_previous_experiment, experiment_folder
149        )
150    return experiment_instances

instantiate the benchmarks_and_agents and the agents configs locally, and creates the corresponding ExperimentInstance.

Parameters
  • agents_and_benchmarks (Iterable[Tuple[Dict[Type["BaseAgent"], str], ColosseumBenchmark]]): The agent configs and benchmarks_and_agents to be instantiated.
  • overwrite_previous_experiment (bool): If True the destination folder is cleared before instantiating the MDP configs. If False, it raises an error if it finds a different set of agents configs, MDP configs, or ExperimentalConfig in the destination folder.
  • experiment_folder (str): The folder where to instantiate the benchmark and the agents configs. By default, it is taken from the package configurations.
Returns
  • List[ExperimentInstance]: The ExperimentInstances corresponding to the benchmarks_and_agents and the agents configs.
def instantiate_and_get_exp_instances_from_agents_and_benchmarks_for_hyperopt( agents_and_benchmarks: Iterable[Tuple[Dict[Type[colosseum.agent.agents.base.BaseAgent], str], colosseum.benchmark.benchmark.ColosseumBenchmark]], overwrite_previous_experiment: bool = False) -> List[colosseum.experiment.experiment_instance.ExperimentInstance]:
153def instantiate_and_get_exp_instances_from_agents_and_benchmarks_for_hyperopt(
154    agents_and_benchmarks: Iterable[
155        Tuple[Dict[Type["BaseAgent"], str], ColosseumBenchmark]
156    ],
157    overwrite_previous_experiment: bool = False,
158) -> List[ExperimentInstance]:
159    """
160    instantiate the benchmarks_and_agents and the agents configs for the parameters optimization locally (by
161    checking the parameters optimization folder in the package configurations), and creates the corresponding
162    `ExperimentInstance`.
163
164    Parameters
165    ----------
166    agents_and_benchmarks : Iterable[Tuple[Dict[Type["BaseAgent"], str], ColosseumBenchmark]]
167        The agent configs and benchmarks_and_agents to be instantiated.
168    overwrite_previous_experiment : bool
169        If True the destination folder is cleared before instantiating the MDP configs. If False, it raises an error if
170        it finds a different set of agents configs, MDP configs, or `ExperimentalConfig` in the destination folder.
171
172    Returns
173    -------
174    List[ExperimentInstance]
175        The `ExperimentInstance`s corresponding to the benchmarks_and_agents and the agents configs.
176    """
177    return instantiate_and_get_exp_instances_from_agents_and_benchmarks(
178        agents_and_benchmarks,
179        overwrite_previous_experiment,
180        config.get_hyperopt_folder(),
181    )

instantiate the benchmarks_and_agents and the agents configs for the parameters optimization locally (by checking the parameters optimization folder in the package configurations), and creates the corresponding ExperimentInstance.

Parameters
  • agents_and_benchmarks (Iterable[Tuple[Dict[Type["BaseAgent"], str], ColosseumBenchmark]]): The agent configs and benchmarks_and_agents to be instantiated.
  • overwrite_previous_experiment (bool): If True the destination folder is cleared before instantiating the MDP configs. If False, it raises an error if it finds a different set of agents configs, MDP configs, or ExperimentalConfig in the destination folder.
Returns
  • List[ExperimentInstance]: The ExperimentInstances corresponding to the benchmarks_and_agents and the agents configs.