Datasets:

ArXiv:
File size: 7,948 Bytes
3e10edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import itertools
import os
import subprocess
from typing import Any, Dict

from wandb.apis.public import Api

# Configuration - customize these mappings
WANDB_PROJECT = "Arcade-RLC"
WANDB_ENTITY = "bolt-um"  # Optional, unless you're in a team
MAX_JOBS = 3  # Maximum number of jobs to run before terminating (useful for HPC/SLURM)
TRAIN_PATH = "/home/smorad/code/popgym_arcade/popgym_arcade/train.py"

algorithm_families = ["PQN"]
models = ["lru", "mingru", "mlp"]
seeds = [0, 1, 2]
environments_config = {
    "CartPoleEasy": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "CartPoleMedium": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "CartPoleHard": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "NoisyCartPoleEasy": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "NoisyCartPoleMedium": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "NoisyCartPoleHard": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "BattleShipEasy": {
        "PPO": int(2e7),  # Different timesteps for PPO
        "PQN": int(2e7),  # Different timesteps for PQN
        "TOTAL_TIMESTEPS_DECAY": int(2e6),  # New decay parameter for PQN
    },
    "BattleShipMedium": {
        "PPO": int(2e7),
        "PQN": int(2e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "BattleShipHard": {
        "PPO": int(2e7),
        "PQN": int(2e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "CountRecallEasy": {
        "PPO": int(2e7),
        "PQN": int(2e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "CountRecallMedium": {
        "PPO": int(2e7),
        "PQN": int(2e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "CountRecallHard": {
        "PPO": int(2e7),
        "PQN": int(2e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "NavigatorEasy": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "NavigatorMedium": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "NavigatorHard": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "MineSweeperEasy": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "MineSweeperMedium": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "MineSweeperHard": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "AutoEncodeEasy": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "AutoEncodeMedium": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
    "AutoEncodeHard": {
        "PPO": int(1e7),
        "PQN": int(1e7),
        "TOTAL_TIMESTEPS_DECAY": int(2e6),
    },
}
partial_flags = [True, False]


def is_rnn(model_str):
    return "mlp" not in model_str


def generate_experiment_key(experiment: Dict[str, Any]) -> str:
    """Create a unique key for an experiment configuration"""
    return (
        f"{experiment['algorithm']}_{experiment['model']}_"
        f"{experiment['seed']}_{experiment['environment']}_"
        f"{experiment['partial']}"
    )


def get_wandb_runs() -> set:
    """Get completed or running experiments from WandB"""
    api = Api()
    runs = (
        api.runs(f"{WANDB_ENTITY}/{WANDB_PROJECT}")
        if WANDB_ENTITY
        else api.runs(WANDB_PROJECT)
    )

    existing = set()
    for run in runs:
        config = {k: v for k, v in run.config.items() if not k.startswith("_")}
        key = generate_experiment_key(
            {
                "algorithm": config["TRAIN_TYPE"].replace("_RNN", ""),
                "model": config.get("MEMORY_TYPE", "mlp").lower(),
                "seed": config["SEED"],
                "environment": config["ENV_NAME"],
                "partial": config["PARTIAL"],
            }
        )
        if run.state in ["finished", "running"]:
            existing.add(key)
    return existing


def build_base_command(experiment: Dict[str, Any]) -> list:
    """Construct the appropriate command based on model type"""

    algo = experiment["algorithm"]
    algo += "_RNN" if is_rnn(experiment["model"]) else ""

    base_cmd = [
        "python",
        TRAIN_PATH,
        algo,
        "--PROJECT",
        WANDB_PROJECT,
        "--SEED",
        str(experiment["seed"]),
        "--ENV_NAME",
        experiment["environment"],
        "--TOTAL_TIMESTEPS",
        str(experiment["total_timesteps"]),
    ]

    base_cmd += ["--PARTIAL"] if experiment["partial"] else []

    if experiment["algorithm"] in ["PQN", "PQN_RNN"]:
        base_cmd += [
            "--TOTAL_TIMESTEPS_DECAY",
            str(experiment["total_timesteps_decay"]),
        ]

    if is_rnn(experiment["model"]):
        base_cmd += ["--MEMORY_TYPE", experiment["model"]]

    return base_cmd


def get_all_experiments():
    """Return all possible experiments"""
    all_experiments = []
    for env, config in environments_config.items():
        combinations = itertools.product(
            seeds, algorithm_families, models, partial_flags
        )
        for seed, family, model, partial in combinations:

            # Get timesteps specific to algorithm family
            total_timesteps = config[family]  # PPO or PQN

            all_experiments.append(
                {
                    "algorithm": family,
                    "model": model,
                    "total_timesteps": total_timesteps,
                    "total_timesteps_decay": config[
                        "TOTAL_TIMESTEPS_DECAY"
                    ],  # Include in config
                    "seed": seed,
                    "environment": env,
                    "partial": partial,
                }
            )
    return all_experiments


def get_pending_experiments(all_experiments):
    """Return experiments that we plan to run"""
    # Generate all possible experiment combinations

    # Get completed/running experiments from WandB
    completed_or_running = get_wandb_runs()
    # Find pending experiments
    pending_experiments = [
        exp
        for exp in all_experiments
        if generate_experiment_key(exp) not in completed_or_running
    ]
    return completed_or_running, pending_experiments


def main():
    all_experiments = get_all_experiments()

    # Run experiments sequentially
    completed_or_running, pending_experiments = get_pending_experiments(all_experiments)
    for i in range(MAX_JOBS):
        # for i, experiment in enumerate(pending_experiments):
        # print("Currently running or completed experiments:")
        # print(completed_or_running)

        if not pending_experiments:
            print("All experiments have been completed or are running!")
            break

        # Run experiment
        experiment = pending_experiments[0]
        pending_experiments = pending_experiments[1:]

        print(
            f"Found {len(pending_experiments)} pending experiments out of {len(all_experiments)} total"
        )
        print(f"\n=== Starting experiment {i + 1}/{len(pending_experiments)} ===")
        print("Configuration:", experiment)

        # Build command
        base_cmd = build_base_command(experiment)

        # Run experiment
        print("Command:", " ".join(base_cmd))

        if i + 1 == MAX_JOBS:
            print(f"Reached maximum number of jobs ({MAX_JOBS}), terminating")
            break
        i += 1


if __name__ == "__main__":
    main()