Datasets:
Publish WeightProgramBench: 317 training games and 5 held-out MinAtar tasks
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- README.md +183 -0
- TASK_ORIGINS.json +0 -0
- THIRD_PARTY_NOTICES.md +76 -0
- data/test.jsonl +5 -0
- data/train.jsonl +0 -0
- runtime/LICENSE-Crisp +54 -0
- runtime/LICENSE-MinAtar +674 -0
- runtime/NOTICE.txt +28 -0
- runtime/minatar_synth/games/accel_b.py +133 -0
- runtime/minatar_synth/games/adventure.py +199 -0
- runtime/minatar_synth/games/aerial_bar.py +129 -0
- runtime/minatar_synth/games/alien.py +116 -0
- runtime/minatar_synth/games/amidar.py +133 -0
- runtime/minatar_synth/games/ant_lion.py +118 -0
- runtime/minatar_synth/games/arcfire.py +122 -0
- runtime/minatar_synth/games/assault.py +190 -0
- runtime/minatar_synth/games/asteroids.py +167 -0
- runtime/minatar_synth/games/atlantis.py +158 -0
- runtime/minatar_synth/games/attack_chain.py +118 -0
- runtime/minatar_synth/games/b_blast.py +121 -0
- runtime/minatar_synth/games/b_cannon.py +159 -0
- runtime/minatar_synth/games/b_math.py +98 -0
- runtime/minatar_synth/games/b_walls.py +118 -0
- runtime/minatar_synth/games/balance.py +111 -0
- runtime/minatar_synth/games/ball_tour.py +108 -0
- runtime/minatar_synth/games/balloon.py +124 -0
- runtime/minatar_synth/games/balls_bombs.py +129 -0
- runtime/minatar_synth/games/bamboo.py +108 -0
- runtime/minatar_synth/games/bank_heist.py +197 -0
- runtime/minatar_synth/games/barge_hop.py +161 -0
- runtime/minatar_synth/games/baroll.py +103 -0
- runtime/minatar_synth/games/baserunner_dash.py +113 -0
- runtime/minatar_synth/games/battle_zone.py +223 -0
- runtime/minatar_synth/games/beam_rider.py +215 -0
- runtime/minatar_synth/games/berzerk.py +246 -0
- runtime/minatar_synth/games/blade_dancer.py +130 -0
- runtime/minatar_synth/games/blast_buoy.py +155 -0
- runtime/minatar_synth/games/blaze_thrust.py +131 -0
- runtime/minatar_synth/games/block_stacker.py +123 -0
- runtime/minatar_synth/games/boarding.py +150 -0
- runtime/minatar_synth/games/bola_thread.py +138 -0
- runtime/minatar_synth/games/bomb_up.py +165 -0
- runtime/minatar_synth/games/bore_comet.py +150 -0
- runtime/minatar_synth/games/bottop.py +92 -0
- runtime/minatar_synth/games/bowling.py +164 -0
- runtime/minatar_synth/games/boxing.py +147 -0
- runtime/minatar_synth/games/breed_c.py +138 -0
- runtime/minatar_synth/games/bridge_cross.py +113 -0
- runtime/minatar_synth/games/bs_fish.py +111 -0
- runtime/minatar_synth/games/c_nodes.py +165 -0
README.md
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
pretty_name: WeightProgramBench
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
task_categories:
|
| 6 |
+
- text-generation
|
| 7 |
+
size_categories:
|
| 8 |
+
- n<1K
|
| 9 |
+
tags:
|
| 10 |
+
- reinforcement-learning
|
| 11 |
+
- weight-space-programming
|
| 12 |
+
- games
|
| 13 |
+
- benchmark
|
| 14 |
+
configs:
|
| 15 |
+
- config_name: default
|
| 16 |
+
data_files:
|
| 17 |
+
- split: train
|
| 18 |
+
path: data/train.jsonl
|
| 19 |
+
- split: test
|
| 20 |
+
path: data/test.jsonl
|
| 21 |
+
---
|
| 22 |
+
|
| 23 |
+
# WeightProgramBench
|
| 24 |
+
|
| 25 |
+
Write the weights of a small game-playing neural network in one response,
|
| 26 |
+
without tools. Each task provides the game rules, observation layout, game
|
| 27 |
+
source, and network format. A CPU scorer reads numeric tensor assignments and
|
| 28 |
+
plays the resulting policy; it never executes model-written code or trains the
|
| 29 |
+
submitted network. Reinforcement learning can train the language model between
|
| 30 |
+
submissions.
|
| 31 |
+
|
| 32 |
+
This dataset preserves the expanded MinAtar Synth v4 corpus: **317 training
|
| 33 |
+
games and five held-out original MinAtar games**. It contains task prompts,
|
| 34 |
+
game implementations, the frozen scorer, and source attribution. It does not
|
| 35 |
+
contain reference solutions, model weights, or model-generated trajectories.
|
| 36 |
+
|
| 37 |
+
## Splits
|
| 38 |
+
|
| 39 |
+
| Split | Games | Family |
|
| 40 |
+
| --- | ---: | --- |
|
| 41 |
+
| Train | 51 | Atari-style synthetic Python conversions |
|
| 42 |
+
| Train | 266 | Crisp-inspired synthetic Python conversions |
|
| 43 |
+
| Test | 5 | Original MinAtar: Asterix, Breakout, Freeway, Seaquest, Space Invaders |
|
| 44 |
+
|
| 45 |
+
Training environments are compact Python adaptations, not Atari ROM emulation
|
| 46 |
+
or native Crisp gameplay. All use `10 × 10 × 6` binary observations. The five
|
| 47 |
+
original MinAtar games have their observation channels mapped into the same
|
| 48 |
+
six-channel input. The split holds out game identities; it does not establish
|
| 49 |
+
that every test mechanic is absent from training.
|
| 50 |
+
|
| 51 |
+
## Load the tasks
|
| 52 |
+
|
| 53 |
+
```python
|
| 54 |
+
from datasets import load_dataset
|
| 55 |
+
|
| 56 |
+
tasks = load_dataset("neilkale/WeightProgramBench")
|
| 57 |
+
print(len(tasks["train"]), len(tasks["test"])) # 317, 5
|
| 58 |
+
task = tasks["test"][0]
|
| 59 |
+
messages = [
|
| 60 |
+
{"role": "system", "content": task["system_prompt"]},
|
| 61 |
+
{"role": "user", "content": task["prompt"]},
|
| 62 |
+
]
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
Each row preserves the original fields `name`, `split`, `game`, `source`,
|
| 66 |
+
`prompt`, `system_prompt`, and `episodes`. Additional columns are:
|
| 67 |
+
|
| 68 |
+
- `task_id`: zero-based index in the original 322-row task file. These IDs are
|
| 69 |
+
not renumbered within splits.
|
| 70 |
+
- `family`: `atari_synthetic`, `crisp_synthetic`, or `minatar_original`.
|
| 71 |
+
- `upstream_source_url` and `upstream_revision`: implementation provenance.
|
| 72 |
+
- `native_source_url`: pinned native Crisp source, where applicable.
|
| 73 |
+
|
| 74 |
+
For reproducible experiments, pin the same dataset commit using `revision=`
|
| 75 |
+
when loading tasks and downloading runtime files.
|
| 76 |
+
|
| 77 |
+
## Policy and scoring
|
| 78 |
+
|
| 79 |
+
Every task uses a **600 → 32 → 32 → 6** network with **20,486 float32
|
| 80 |
+
parameters**, two tanh hidden layers, and softmax-sampled actions. All parameters
|
| 81 |
+
start at zero. The model supplies numeric tensor assignments in a fenced
|
| 82 |
+
`edits` block, following the task's format. The scorer validates assignments
|
| 83 |
+
with Python's AST and literal parser; it does not execute the response as Python.
|
| 84 |
+
|
| 85 |
+
Each submitted policy plays **1,000 episodes**:
|
| 86 |
+
|
| 87 |
+
- Environment seed: `1729 + episode_index`, covering `1729–2728`.
|
| 88 |
+
- Independent action RNG seed: `7919 + episode_index`, covering `7919–8918`.
|
| 89 |
+
- Sticky-action probability: `0.1`.
|
| 90 |
+
- Episode limit: termination or **10,000 actions**.
|
| 91 |
+
- Original MinAtar difficulty ramping: disabled.
|
| 92 |
+
|
| 93 |
+
Seeds are fixed across games and submissions. The reward is
|
| 94 |
+
`clip(mean_game_score, 0, game_score_cap) / game_score_cap`. Each synthetic game
|
| 95 |
+
declares its own cap. Original MinAtar caps are 1,000, except Freeway's cap of
|
| 96 |
+
100. These scales do not make games equally difficult. Invalid submissions
|
| 97 |
+
receive zero. Keep raw scores alongside normalized rewards when comparing
|
| 98 |
+
policies.
|
| 99 |
+
|
| 100 |
+
The frozen runtime uses **NumPy 2.3.4** and **MinAtar 1.0.15**. Policy inference
|
| 101 |
+
and replay run on CPU. One policy's 1,000 episodes are one language-model
|
| 102 |
+
sample, not 1,000 independent model generations.
|
| 103 |
+
|
| 104 |
+
## Score a saved response locally
|
| 105 |
+
|
| 106 |
+
Install the replay dependencies; installing MinAtar without dependencies avoids
|
| 107 |
+
adding optional visualization and training packages:
|
| 108 |
+
|
| 109 |
+
```bash
|
| 110 |
+
python -m pip install huggingface_hub 'numpy==2.3.4'
|
| 111 |
+
python -m pip install --no-deps 'MinAtar==1.0.15'
|
| 112 |
+
```
|
| 113 |
+
|
| 114 |
+
Save the model's complete response, including its `edits` block, in `answer.txt`.
|
| 115 |
+
Then evaluate it against the matching game:
|
| 116 |
+
|
| 117 |
+
```python
|
| 118 |
+
import json
|
| 119 |
+
from pathlib import Path
|
| 120 |
+
import sys
|
| 121 |
+
from huggingface_hub import snapshot_download
|
| 122 |
+
|
| 123 |
+
snapshot = Path(snapshot_download("neilkale/WeightProgramBench", repo_type="dataset"))
|
| 124 |
+
runtime = snapshot / "runtime"
|
| 125 |
+
sys.path.insert(0, str(runtime))
|
| 126 |
+
from verify import apply_edits, evaluate, score_cap
|
| 127 |
+
|
| 128 |
+
tasks = [json.loads(line) for line in (runtime / "tasks.jsonl").read_text().splitlines()]
|
| 129 |
+
task = next(task for task in tasks if task["game"] == "breakout" and task["source"] == "original")
|
| 130 |
+
answer = Path("answer.txt").read_text()
|
| 131 |
+
weights, edit_count = apply_edits(answer)
|
| 132 |
+
raw_score, mean_episode_length = evaluate(task["game"], task["source"], weights, task["episodes"])
|
| 133 |
+
cap = score_cap(task["game"], task["source"])
|
| 134 |
+
reward = min(max(raw_score, 0.0), cap) / cap
|
| 135 |
+
print({"raw_score": raw_score, "reward": reward, "mean_episode_length": mean_episode_length})
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
This example raises on malformed responses so local debugging is explicit.
|
| 139 |
+
For the benchmark's invalid-submission handling, use
|
| 140 |
+
`verify.grade_answer(game, source, answer, episodes)`, which returns a reward
|
| 141 |
+
and grading metadata. Full 1,000-episode replay can take several minutes,
|
| 142 |
+
depending on the game and CPU.
|
| 143 |
+
|
| 144 |
+
The Trajectory integration lives in the
|
| 145 |
+
[WeightProgramBench cookbook PR](https://github.com/Trajectorylabs/trajectory-cookbook/pull/104).
|
| 146 |
+
Model inference and training are separate from the local scorer.
|
| 147 |
+
|
| 148 |
+
## Files and integrity
|
| 149 |
+
|
| 150 |
+
- `data/train.jsonl`, `data/test.jsonl`: Hugging Face task splits with provenance
|
| 151 |
+
columns added.
|
| 152 |
+
- `runtime/tasks.jsonl`: byte-identical frozen task records in their original
|
| 153 |
+
order, without added columns.
|
| 154 |
+
- `runtime/minatar_synth/games/`: the 317 frozen Python training environments.
|
| 155 |
+
- `runtime/verify.py`: frozen policy parser and CPU scorer.
|
| 156 |
+
- `runtime/source.json`: SHA-256 digests for the original task file, scorer,
|
| 157 |
+
and all 317 game files.
|
| 158 |
+
- `TASK_ORIGINS.json`: per-game source mappings, pinned revisions, and prompt
|
| 159 |
+
and implementation digests.
|
| 160 |
+
- `THIRD_PARTY_NOTICES.md`, `runtime/NOTICE.txt`, and `runtime/LICENSE-*`:
|
| 161 |
+
credits and applicable upstream notices.
|
| 162 |
+
|
| 163 |
+
Synthetic source revision: `4038830175c4201ca10e004ed4441985f2acddf2`.
|
| 164 |
+
Frozen task-file SHA-256:
|
| 165 |
+
`cc2d728fc3c820f217ecf43a4fdc0c9e2a5e292fcdd8b4afb0cb1d34774691e6`.
|
| 166 |
+
|
| 167 |
+
## Credits and licensing
|
| 168 |
+
|
| 169 |
+
The weight-space programming setup follows
|
| 170 |
+
[Rikiya Takehi, Jayden Teoh, Alex Zhang, and Omar Khattab's experiments](https://rikiyat.github.io/blog/weight-space-programming/),
|
| 171 |
+
particularly Experiment 5. The expanded training environments come from
|
| 172 |
+
[Trajectory Labs, Jerry Chan, and contributors to MinAtar Synth](https://github.com/Trajectorylabs/minatar-synth/tree/4038830175c4201ca10e004ed4441985f2acddf2).
|
| 173 |
+
Crisp game designs and sources are credited to **ABA Games** and contributors,
|
| 174 |
+
and the portable SDL/C versions to **Joyrider3774** and contributors.
|
| 175 |
+
Original **MinAtar** is by **Kenny Young and Tian Tian**.
|
| 176 |
+
|
| 177 |
+
Licensing differs by component: original MinAtar source is GPL-3.0, the cited
|
| 178 |
+
native Crisp source collections carry MIT notices, and the original experiment's
|
| 179 |
+
prompt/log materials are attributed under CC BY 4.0. The pinned MinAtar Synth
|
| 180 |
+
repository does not declare a license for its converted implementations. This
|
| 181 |
+
dataset does not assert a blanket license for those files. See
|
| 182 |
+
[THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md) for the exact scopes and source
|
| 183 |
+
links; the original MinAtar source embedded in prompts retains its GPL scope.
|
TASK_ORIGINS.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
THIRD_PARTY_NOTICES.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Sources and credits
|
| 2 |
+
|
| 3 |
+
WeightProgramBench packages the existing MinAtar Synth v4 task corpus without
|
| 4 |
+
changing its game implementations, prompts, parser, or scoring contract. Its
|
| 5 |
+
322 tasks comprise 51 Atari-style synthetic training games, 266 Crisp-inspired
|
| 6 |
+
synthetic training games, and five original MinAtar held-out games. These are
|
| 7 |
+
compact Python adaptations, not Atari ROM emulation or native Crisp gameplay.
|
| 8 |
+
|
| 9 |
+
[`TASK_ORIGINS.json`](TASK_ORIGINS.json) records every task's family, source,
|
| 10 |
+
revision or package version, source digest, and prompt digest. It also retains
|
| 11 |
+
the exact pinned native-source link from each Crisp conversion's documentation.
|
| 12 |
+
|
| 13 |
+
## MinAtar Synth implementations
|
| 14 |
+
|
| 15 |
+
Credit Trajectory Labs, Jerry Chan, and the contributors to
|
| 16 |
+
[MinAtar Synth](https://github.com/Trajectorylabs/minatar-synth/tree/4038830175c4201ca10e004ed4441985f2acddf2).
|
| 17 |
+
The 317 Python training environments are pinned to commit
|
| 18 |
+
`4038830175c4201ca10e004ed4441985f2acddf2`. The five original MinAtar environments
|
| 19 |
+
are a separate dependency. The extra `pong.py` in the source repository is not
|
| 20 |
+
part of this benchmark's task roster.
|
| 21 |
+
|
| 22 |
+
The pinned MinAtar Synth repository does not declare a license. No blanket license
|
| 23 |
+
is asserted for the converted implementations; the notices below describe the
|
| 24 |
+
respective upstream components and do not supply that missing declaration.
|
| 25 |
+
|
| 26 |
+
The 51 Atari-style conversions document mechanics and deliberate simplifications
|
| 27 |
+
from historical Atari 2600 games and their manuals. Their documentation's
|
| 28 |
+
historical-source notes are retained per task in `TASK_ORIGINS.json`. They use
|
| 29 |
+
NumPy and the Python standard library, not Atari ROMs or the original MinAtar
|
| 30 |
+
game modules. This describes their implementation and provenance, not a
|
| 31 |
+
conclusion about the license of the converted code.
|
| 32 |
+
|
| 33 |
+
## Crisp game designs and source
|
| 34 |
+
|
| 35 |
+
Credit **ABA Games** and the authors and contributors to its Crisp games, and
|
| 36 |
+
**Joyrider3774** and contributors for the portable SDL/C versions. The 266
|
| 37 |
+
conversions cite three pinned source collections:
|
| 38 |
+
|
| 39 |
+
| Source | Conversions | Pinned revision |
|
| 40 |
+
| --- | ---: | --- |
|
| 41 |
+
| [crisp-game-lib-games](https://github.com/abagames/crisp-game-lib-games) | 136 | `cfb39d2f988feb5918eb83697145b5a35adabf58` |
|
| 42 |
+
| [crisp-game-lib-11-games](https://github.com/abagames/crisp-game-lib-11-games) | 8 | `6fea4eefe2deaa42706051d96a04ec9e36104696` |
|
| 43 |
+
| [crisp-game-lib-portable-sdl](https://github.com/joyrider3774/crisp-game-lib-portable-sdl) | 122 | `04bc2c4cfc2853628eda25a46190bb7ac6fe216a` |
|
| 44 |
+
|
| 45 |
+
These source collections carry MIT licenses, Copyright 2021 or 2022 ABA Games.
|
| 46 |
+
Their exact license texts and source links are retained in
|
| 47 |
+
[`LICENSE-Crisp`](runtime/LICENSE-Crisp). Keep the applicable copyright and permission
|
| 48 |
+
notices with redistributed copies or substantial portions of those sources.
|
| 49 |
+
These source licenses are distinct from the undeclared license of the converted
|
| 50 |
+
MinAtar Synth implementation.
|
| 51 |
+
|
| 52 |
+
## Original MinAtar games
|
| 53 |
+
|
| 54 |
+
Credit **Kenny Young and Tian Tian**, authors of
|
| 55 |
+
[MinAtar](https://github.com/kenjyoung/MinAtar). The five held-out tasks are
|
| 56 |
+
`asterix`, `breakout`, `freeway`, `seaquest`, and `space_invaders`, using
|
| 57 |
+
**MinAtar 1.0.15**. Their original source is included in their task prompts with
|
| 58 |
+
its author headers. The package version, source-file paths, and source digests
|
| 59 |
+
are recorded rather than asserting an unverified Git release commit.
|
| 60 |
+
|
| 61 |
+
MinAtar is licensed under **GNU GPL version 3**. Its complete license text is
|
| 62 |
+
included as [`LICENSE-MinAtar`](runtime/LICENSE-MinAtar); preserve the source notices and
|
| 63 |
+
applicable GPL terms when distributing this material. This notice does not
|
| 64 |
+
relicense MinAtar under the cookbook's license.
|
| 65 |
+
|
| 66 |
+
## Weight-space programming experiment
|
| 67 |
+
|
| 68 |
+
Credit **Rikiya Takehi, Jayden Teoh, Alex Zhang, and Omar Khattab** for the
|
| 69 |
+
[weight-space programming experiments](https://rikiyat.github.io/blog/weight-space-programming/),
|
| 70 |
+
especially [Experiment 5: writing policy weights from scratch](https://github.com/RikiyaT/lm_understand-logs/tree/4d09ba6d6a7e891dd8063974dfe81b3d78e3494f/exp5_write_from_scratch).
|
| 71 |
+
The benchmark follows that one-response, no-tools weight-programming setup.
|
| 72 |
+
The experiment's prompt/log materials are attributed under
|
| 73 |
+
[CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), separately from the
|
| 74 |
+
GPL-licensed MinAtar source embedded in those prompts. The expanded game corpus
|
| 75 |
+
and model-training experiments are subsequent adaptations, not results from
|
| 76 |
+
the original experiment.
|
data/test.jsonl
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{"name": "space_invaders", "split": "test", "game": "space_invaders", "source": "original", "prompt": "Write from scratch the weights of a policy network that plays Space Invaders as well as possible.\n\nThere is no training, data, loss, gradients, fine-tuning, code execution, calculator, or environment feedback. This is one response. Briefly describe the strategy and hidden units, then provide the weights in the final `edits` code block.\n\nMove the cannon, shoot alien waves, and avoid aliens and enemy bullets.\n\nActions are sampled from softmax logits and have sticky-action probability 0.1.\n\nExact MinAtar 1.0.15 source (`minatar/environments/space_invaders.py`):\n```python\n################################################################################################################\n# Authors: #\n# Kenny Young (kjyoung@ualberta.ca) #\n# Tian Tian (ttian@ualberta.ca) #\n################################################################################################################\nimport numpy as np\n\n\n#####################################################################################################################\n# Constants\n#\n#####################################################################################################################\nshot_cool_down = 5\nenemy_move_interval = 12\nenemy_shot_interval = 10\n\n\n#####################################################################################################################\n# Env\n#\n# The player controls a cannon at the bottom of the screen and can shoot bullets upward at a cluster of aliens above.\n# The aliens move across the screen until one of them hits the edge, at which point they all move down and switch\n# directions. The current alien direction is indicated by 2 channels (one for left and one for right) one of which is\n# active at the location of each alien. A reward of +1 is given each time an alien is shot, and that alien is also\n# removed. The aliens will also shoot bullets back at the player. When few aliens are left, alien speed will begin to\n# increase. When only one alien is left, it will move at one cell per frame. When a wave of aliens is fully cleared a\n# new one will spawn which moves at a slightly faster speed than the last. Termination occurs when an alien or bullet\n# hits the player.\n#\n#####################################################################################################################\nclass Env:\n def __init__(self, ramping=True):\n self.channels ={\n 'cannon':0,\n 'alien':1,\n 'alien_left':2,\n 'alien_right':3,\n 'friendly_bullet':4,\n 'enemy_bullet':5\n }\n self.action_map = ['n','l','u','r','d','f']\n self.ramping = ramping\n self.random = np.random.RandomState()\n self.reset()\n\n # Update environment according to agent action\n def act(self, a):\n r = 0\n if(self.terminal):\n return r, self.terminal\n\n a = self.action_map[a]\n\n # Resolve player action\n if(a=='f' and self.shot_timer == 0):\n self.f_bullet_map[9,self.pos]=1\n self.shot_timer = shot_cool_down\n elif(a=='l'):\n self.pos = max(0, self.pos-1)\n elif(a=='r'):\n self.pos = min(9, self.pos+1)\n\n # Update Friendly Bullets\n self.f_bullet_map = np.roll(self.f_bullet_map, -1, axis=0)\n self.f_bullet_map[9,:] = 0\n\n # Update Enemy Bullets\n self.e_bullet_map = np.roll(self.e_bullet_map, 1, axis=0)\n self.e_bullet_map[0,:] = 0\n if(self.e_bullet_map[9,self.pos]):\n self.terminal = True\n\n # Update aliens\n if(self.alien_map[9,self.pos]):\n self.terminal = True\n if(self.alien_move_timer==0):\n self.alien_move_timer = min(np.count_nonzero(self.alien_map),self.enemy_move_interval)\n if((np.sum(self.alien_map[:,0])>0 and self.alien_dir<0) or (np.sum(self.alien_map[:,9])>0 and self.alien_dir>0)):\n self.alien_dir = -self.alien_dir\n if(np.sum(self.alien_map[9,:])>0):\n self.terminal = True\n self.alien_map = np.roll(self.alien_map, 1, axis=0)\n else:\n self.alien_map = np.roll(self.alien_map, self.alien_dir, axis=1)\n if(self.alien_map[9,self.pos]):\n self.terminal = True\n if(self.alien_shot_timer==0):\n self.alien_shot_timer = enemy_shot_interval\n nearest_alien = self._nearest_alien(self.pos)\n self.e_bullet_map[nearest_alien[0], nearest_alien[1]] = 1\n\n kill_locations = np.logical_and(self.alien_map,self.alien_map==self.f_bullet_map)\n\n r+=np.sum(kill_locations)\n self.alien_map[kill_locations] = self.f_bullet_map[kill_locations] = 0\n\n\n # Update various timers\n self.shot_timer -= self.shot_timer>0\n self.alien_move_timer-=1\n self.alien_shot_timer-=1\n if(np.count_nonzero(self.alien_map)==0):\n if(self.enemy_move_interval>6 and self.ramping):\n self.enemy_move_interval-=1\n self.ramp_index+=1\n self.alien_map[0:4,2:8] = 1\n return r, self.terminal\n\n # Find the alien closest to player in manhattan distance, currently used to decide which alien shoots\n def _nearest_alien(self, pos):\n search_order = [i for i in range(10)]\n search_order.sort(key=lambda x: abs(x-pos))\n for i in search_order:\n if(np.sum(self.alien_map[:,i])>0):\n return [np.max(np.where(self.alien_map[:,i]==1)),i]\n return None\n\n # Query the current level of the difficulty ramp, could be used as additional input to agent for example\n def difficulty_ramp(self):\n return self.ramp_index\n\n # Process the game-state into the 10x10xn state provided to the agent and return\n def state(self):\n state = np.zeros((10,10,len(self.channels)),dtype=bool)\n state[9,self.pos,self.channels['cannon']] = 1\n state[:,:, self.channels['alien']] = self.alien_map\n if(self.alien_dir<0):\n state[:,:, self.channels['alien_left']] = self.alien_map\n else:\n state[:,:, self.channels['alien_right']] = self.alien_map\n state[:,:, self.channels['friendly_bullet']] = self.f_bullet_map\n state[:,:, self.channels['enemy_bullet']] = self.e_bullet_map\n return state\n\n # Reset to start state for new episode\n def reset(self):\n self.pos = 5\n self.f_bullet_map = np.zeros((10,10))\n self.e_bullet_map = np.zeros((10,10))\n self.alien_map = np.zeros((10,10))\n self.alien_map[0:4,2:8] = 1\n self.alien_dir = -1\n self.enemy_move_interval = enemy_move_interval\n self.alien_move_timer = self.enemy_move_interval\n self.alien_shot_timer = enemy_shot_interval\n self.ramp_index = 0\n self.shot_timer = 0\n self.terminal = False\n\n # Dimensionality of the game-state (10x10xn)\n def state_shape(self):\n return [10,10,len(self.channels)]\n\n # Subset of actions that actually have a unique impact in this environment\n def minimal_action_set(self):\n minimal_actions = ['n','l','r','f']\n return [self.action_map.index(x) for x in minimal_actions]\n\n```\n\nInput: a row-major 10x10x6 binary grid. Index `(row * 10 + column) * 6 + channel`. Channels: player=cannon; danger=alien|enemy_bullet; reward=alien; projectile=friendly_bullet; trail=alien_left|alien_right; gauge=empty.\n\nNetwork: `h1=tanh(x@W1+b1)`, `h2=tanh(h1@W2+b2)`, `logits=h2@W3+b3`. Shapes are W1 600x32, b1 32, W2 32x32, b2 32, W3 32x6, b3 6. All 20,486 values start at zero.\n\nThe hidden evaluator runs 1,000 fixed-seed episodes, stopping each after 10,000 steps. It divides mean game score by the game's fixed cap and clips the result to [0, 1].\n\nThe final fenced block must start with `edits`. It may contain numeric assignments to indexed entries or slices of W1, b1, W2, b2, W3, and b3. Half-open Python slices and numeric lists are supported, as are `*=`, `+=`, and `-=`. Values must be finite and have absolute value at most 1e6. Example:\n```edits\nW1[12, 0] = 2.5\nb1[0:4] = -1\nW2[0:2, 0:3] = [[1, -1, 0.5], [0, 2, 0]]\nW3[:, 2] *= 2\n```\n", "system_prompt": "You directly program neural-network weights. Follow the task constraints exactly.", "episodes": 1000, "task_id": 185, "family": "minatar_original", "upstream_source_url": "https://pypi.org/project/MinAtar/1.0.15/", "upstream_revision": "MinAtar==1.0.15", "native_source_url": null}
|
| 2 |
+
{"name": "breakout", "split": "test", "game": "breakout", "source": "original", "prompt": "Write from scratch the weights of a policy network that plays Breakout as well as possible.\n\nThere is no training, data, loss, gradients, fine-tuning, code execution, calculator, or environment feedback. This is one response. Briefly describe the strategy and hidden units, then provide the weights in the final `edits` code block.\n\nMove the bottom-row paddle left or right to bounce the diagonal ball and break bricks.\n\nActions are sampled from softmax logits and have sticky-action probability 0.1.\n\nExact MinAtar 1.0.15 source (`minatar/environments/breakout.py`):\n```python\n################################################################################################################\n# Authors: #\n# Kenny Young (kjyoung@ualberta.ca) #\n# Tian Tian (ttian@ualberta.ca) #\n################################################################################################################\nimport numpy as np\n\n\n#####################################################################################################################\n# Env\n#\n# The player controls a paddle on the bottom of the screen and must bounce a ball tobreak 3 rows of bricks along the\n# top of the screen. A reward of +1 is given for each brick broken by the ball. When all bricks are cleared another 3\n# rows are added. The ball travels only along diagonals, when it hits the paddle it is bounced either to the left or\n# right depending on the side of the paddle hit, when it hits a wall or brick it is reflected. Termination occurs when\n# the ball hits the bottom of the screen. The balls direction is indicated by a trail channel.\n#\n#####################################################################################################################\nclass Env:\n def __init__(self, ramping=None):\n self.channels ={\n 'paddle':0,\n 'ball':1,\n 'trail':2,\n 'brick':3,\n }\n self.action_map = ['n','l','u','r','d','f']\n self.random = np.random.RandomState()\n self.reset()\n\n # Update environment according to agent action\n def act(self, a):\n r = 0\n if(self.terminal):\n return r, self.terminal\n\n a = self.action_map[a]\n\n # Resolve player action\n if(a=='l'):\n self.pos = max(0, self.pos-1)\n elif(a=='r'):\n self.pos = min(9,self.pos+1)\n\n # Update ball position\n self.last_x = self.ball_x\n self.last_y = self.ball_y\n if(self.ball_dir == 0):\n new_x = self.ball_x-1\n new_y = self.ball_y-1\n elif(self.ball_dir == 1):\n new_x = self.ball_x+1\n new_y = self.ball_y-1\n elif(self.ball_dir == 2):\n new_x = self.ball_x+1\n new_y = self.ball_y+1\n elif(self.ball_dir == 3):\n new_x = self.ball_x-1\n new_y = self.ball_y+1\n\n strike_toggle = False\n if(new_x<0 or new_x>9):\n if(new_x<0):\n new_x = 0\n if(new_x>9):\n new_x=9\n self.ball_dir=[1,0,3,2][self.ball_dir]\n if(new_y<0):\n new_y = 0\n self.ball_dir=[3,2,1,0][self.ball_dir]\n elif(self.brick_map[new_y,new_x]==1):\n strike_toggle = True\n if(not self.strike):\n r+=1\n self.strike = True\n self.brick_map[new_y,new_x]=0\n new_y = self.last_y\n self.ball_dir=[3,2,1,0][self.ball_dir]\n elif(new_y == 9):\n if(np.count_nonzero(self.brick_map)==0):\n self.brick_map[1:4,:] = 1\n if(self.ball_x == self.pos):\n self.ball_dir=[3,2,1,0][self.ball_dir]\n new_y = self.last_y\n elif(new_x == self.pos):\n self.ball_dir=[2,3,0,1][self.ball_dir]\n new_y = self.last_y\n else:\n self.terminal = True\n\n if(not strike_toggle):\n self.strike = False\n\n self.ball_x = new_x\n self.ball_y = new_y\n return r, self.terminal\n\n # Query the current level of the difficulty ramp, difficulty does not ramp in this game, so return None\n def difficulty_ramp(self):\n return None\n\n # Process the game-state into the 10x10xn state provided to the agent and return\n def state(self):\n state = np.zeros((10,10,len(self.channels)),dtype=bool)\n state[self.ball_y,self.ball_x,self.channels['ball']] = 1\n state[9,self.pos, self.channels['paddle']] = 1\n state[self.last_y,self.last_x,self.channels['trail']] = 1\n state[:,:,self.channels['brick']] = self.brick_map\n return state\n\n # Reset to start state for new episode\n def reset(self):\n self.ball_y = 3\n ball_start = self.random.randint(2)\n self.ball_x, self.ball_dir = [(0,2),(9,3)][ball_start]\n self.pos = 4\n self.brick_map = np.zeros((10,10))\n self.brick_map[1:4,:] = 1\n self.strike = False\n self.last_x = self.ball_x\n self.last_y = self.ball_y\n self.terminal = False\n\n # Dimensionality of the game-state (10x10xn)\n def state_shape(self):\n return [10,10,len(self.channels)]\n\n # Subset of actions that actually have a unique impact in this environment\n def minimal_action_set(self):\n minimal_actions = ['n','l','r']\n return [self.action_map.index(x) for x in minimal_actions]\n\n```\n\nInput: a row-major 10x10x6 binary grid. Index `(row * 10 + column) * 6 + channel`. Channels: player=paddle; danger=empty; reward=brick; projectile=ball; trail=trail; gauge=empty.\n\nNetwork: `h1=tanh(x@W1+b1)`, `h2=tanh(h1@W2+b2)`, `logits=h2@W3+b3`. Shapes are W1 600x32, b1 32, W2 32x32, b2 32, W3 32x6, b3 6. All 20,486 values start at zero.\n\nThe hidden evaluator runs 1,000 fixed-seed episodes, stopping each after 10,000 steps. It divides mean game score by the game's fixed cap and clips the result to [0, 1].\n\nThe final fenced block must start with `edits`. It may contain numeric assignments to indexed entries or slices of W1, b1, W2, b2, W3, and b3. Half-open Python slices and numeric lists are supported, as are `*=`, `+=`, and `-=`. Values must be finite and have absolute value at most 1e6. Example:\n```edits\nW1[12, 0] = 2.5\nb1[0:4] = -1\nW2[0:2, 0:3] = [[1, -1, 0.5], [0, 2, 0]]\nW3[:, 2] *= 2\n```\n", "system_prompt": "You directly program neural-network weights. Follow the task constraints exactly.", "episodes": 1000, "task_id": 241, "family": "minatar_original", "upstream_source_url": "https://pypi.org/project/MinAtar/1.0.15/", "upstream_revision": "MinAtar==1.0.15", "native_source_url": null}
|
| 3 |
+
{"name": "freeway", "split": "test", "game": "freeway", "source": "original", "prompt": "Write from scratch the weights of a policy network that plays Freeway as well as possible.\n\nThere is no training, data, loss, gradients, fine-tuning, code execution, calculator, or environment feedback. This is one response. Briefly describe the strategy and hidden units, then provide the weights in the final `edits` code block.\n\nMove the chicken upward through traffic and score each time it reaches the top.\n\nActions are sampled from softmax logits and have sticky-action probability 0.1.\n\nExact MinAtar 1.0.15 source (`minatar/environments/freeway.py`):\n```python\n################################################################################################################\n# Authors: #\n# Kenny Young (kjyoung@ualberta.ca) #\n# Tian Tian (ttian@ualberta.ca) #\n################################################################################################################\nimport numpy as np\n\n\n#####################################################################################################################\n# Constants\n#\n#####################################################################################################################\nplayer_speed = 3\ntime_limit = 2500\n\n\n#####################################################################################################################\n# Env\n#\n# The player begins at the bottom of the screen and motion is restricted to traveling up and down. Player speed is\n# also restricted such that the player can only move every 3 frames. A reward of +1 is given when the player reaches\n# the top of the screen, at which point the player is returned to the bottom. Cars travel horizontally on the screen\n# and teleport to the other side when the edge is reached. When hit by a car, the player is returned to the bottom of\n# the screen. Car direction and speed is indicated by 5 trail channels, the location of the trail gives direction\n# while the specific channel indicates how frequently the car moves (from once every frame to once every 5 frames).\n# Each time the player successfully reaches the top of the screen, the car speeds are randomized. Termination occurs\n# after 2500 frames have elapsed.\n#\n#####################################################################################################################\nclass Env:\n def __init__(self, ramping=None):\n self.channels ={\n 'chicken':0,\n 'car':1,\n 'speed1':2,\n 'speed2':3,\n 'speed3':4,\n 'speed4':5,\n 'speed5':6,\n }\n self.action_map = ['n','l','u','r','d','f']\n self.random = np.random.RandomState()\n self.reset()\n\n # Update environment according to agent action\n def act(self, a):\n r = 0\n if(self.terminal):\n return r, self.terminal\n\n a = self.action_map[a]\n\n if(a=='u' and self.move_timer==0):\n self.move_timer = player_speed\n self.pos = max(0, self.pos-1)\n elif(a=='d' and self.move_timer==0):\n self.move_timer = player_speed\n self.pos = min(9, self.pos+1)\n\n # Win condition\n if(self.pos==0):\n r+=1\n self._randomize_cars(initialize=False)\n self.pos = 9\n\n # Update cars\n for car in self.cars:\n if(car[0:2]==[4,self.pos]):\n self.pos = 9\n if(car[2]==0):\n car[2]=abs(car[3])\n car[0]+=1 if car[3]>0 else -1\n if(car[0]<0):\n car[0]=9\n elif(car[0]>9):\n car[0]=0\n if(car[0:2]==[4,self.pos]):\n self.pos = 9\n else:\n car[2]-=1\n\n # Update various timers\n self.move_timer-=self.move_timer>0\n self.terminate_timer-=1\n if(self.terminate_timer<0):\n self.terminal = True\n return r, self.terminal\n\n # Query the current level of the difficulty ramp, difficulty does not ramp in this game, so return None\n def difficulty_ramp(self):\n return None\n\n # Process the game-state into the 10x10xn state provided to the agent and return\n def state(self):\n state = np.zeros((10,10,len(self.channels)),dtype=bool)\n state[self.pos,4,self.channels['chicken']] = 1\n for car in self.cars:\n state[car[1],car[0], self.channels['car']] = 1\n back_x = car[0]-1 if car[3]>0 else car[0]+1\n if(back_x<0):\n back_x=9\n elif(back_x>9):\n back_x=0\n if(abs(car[3])==1):\n trail = self.channels['speed1']\n elif(abs(car[3])==2):\n trail = self.channels['speed2']\n elif(abs(car[3])==3):\n trail = self.channels['speed3']\n elif(abs(car[3])==4):\n trail = self.channels['speed4']\n elif(abs(car[3])==5):\n trail = self.channels['speed5']\n state[car[1],back_x, trail] = 1\n return state\n\n # Randomize car speeds and directions, also reset their position if initialize=True\n def _randomize_cars(self, initialize=False):\n speeds = self.random.randint(1,6,8)\n directions = np.sign(self.random.rand(8) - 0.5).astype(int)\n speeds*=directions\n if(initialize):\n self.cars = []\n for i in range(8):\n self.cars+=[[0,i+1,abs(speeds[i]),speeds[i]]]\n else:\n for i in range(8):\n self.cars[i][2:4]=[abs(speeds[i]),speeds[i]]\n\n # Reset to start state for new episode\n def reset(self):\n self._randomize_cars(initialize=True)\n self.pos = 9\n self.move_timer = player_speed\n self.terminate_timer = time_limit\n self.terminal = False\n\n # Dimensionality of the game-state (10x10xn)\n def state_shape(self):\n return [10,10,len(self.channels)]\n\n # Subset of actions that actually have a unique impact in this environment\n def minimal_action_set(self):\n minimal_actions = ['n','u','d']\n return [self.action_map.index(x) for x in minimal_actions]\n\n```\n\nInput: a row-major 10x10x6 binary grid. Index `(row * 10 + column) * 6 + channel`. Channels: player=chicken; danger=car; reward=empty; projectile=empty; trail=speed1|speed2|speed3|speed4|speed5; gauge=empty.\n\nNetwork: `h1=tanh(x@W1+b1)`, `h2=tanh(h1@W2+b2)`, `logits=h2@W3+b3`. Shapes are W1 600x32, b1 32, W2 32x32, b2 32, W3 32x6, b3 6. All 20,486 values start at zero.\n\nThe hidden evaluator runs 1,000 fixed-seed episodes, stopping each after 10,000 steps. It divides mean game score by the game's fixed cap and clips the result to [0, 1].\n\nThe final fenced block must start with `edits`. It may contain numeric assignments to indexed entries or slices of W1, b1, W2, b2, W3, and b3. Half-open Python slices and numeric lists are supported, as are `*=`, `+=`, and `-=`. Values must be finite and have absolute value at most 1e6. Example:\n```edits\nW1[12, 0] = 2.5\nb1[0:4] = -1\nW2[0:2, 0:3] = [[1, -1, 0.5], [0, 2, 0]]\nW3[:, 2] *= 2\n```\n", "system_prompt": "You directly program neural-network weights. Follow the task constraints exactly.", "episodes": 1000, "task_id": 251, "family": "minatar_original", "upstream_source_url": "https://pypi.org/project/MinAtar/1.0.15/", "upstream_revision": "MinAtar==1.0.15", "native_source_url": null}
|
| 4 |
+
{"name": "seaquest", "split": "test", "game": "seaquest", "source": "original", "prompt": "Write from scratch the weights of a policy network that plays Seaquest as well as possible.\n\nThere is no training, data, loss, gradients, fine-tuning, code execution, calculator, or environment feedback. This is one response. Briefly describe the strategy and hidden units, then provide the weights in the final `edits` code block.\n\nControl the submarine, shoot enemies, rescue divers, surface with divers, and manage oxygen.\n\nActions are sampled from softmax logits and have sticky-action probability 0.1.\n\nExact MinAtar 1.0.15 source (`minatar/environments/seaquest.py`):\n```python\n################################################################################################################\n# Authors: #\n# Kenny Young (kjyoung@ualberta.ca) #\n# Tian Tian (ttian@ualberta.ca) #\n################################################################################################################\nimport numpy as np\n\n\n#####################################################################################################################\n# Constants\n#\n#####################################################################################################################\nramp_interval = 100\nmax_oxygen = 200\ninit_spawn_speed = 20\ndiver_spawn_speed = 30\ninit_move_interval = 5\nshot_cool_down = 5\nenemy_shot_interval = 10\nenemy_move_interval = 5\ndiver_move_interval = 5\n\n\n#####################################################################################################################\n# Env\n#\n# The player controls a submarine consisting of two cells, front and back, to allow direction to be determined. The\n# player can also fire bullets from the front of the submarine. Enemies consist of submarines and fish, distinguished\n# by the fact that submarines shoot bullets and fish do not. A reward of +1 is given each time an enemy is struck by\n# one of the player's bullets, at which point the enemy is also removed. There are also divers which the player can\n# move onto to pick up, doing so increments a bar indicated by another channel along the bottom of the screen. The\n# player also has a limited supply of oxygen indicated by another bar in another channel. Oxygen degrades over time,\n# and is replenished whenever the player moves to the top of the screen as long as the player has at least one rescued\n# diver on board. The player can carry a maximum of 6 divers. When surfacing with less than 6, one diver is removed.\n# When surfacing with 6, all divers are removed and a reward is given for each active cell in the oxygen bar. Each\n# time the player surfaces the difficulty is increased by increasing the spawn rate and movement speed of enemies.\n# Termination occurs when the player is hit by an enemy fish, sub or bullet; or when oxygen reached 0; or when the\n# player attempts to surface with no rescued divers. Enemy and diver directions are indicated by a trail channel\n# active in their previous location to reduce partial observability.\n#\n#####################################################################################################################\nclass Env:\n def __init__(self, ramping=True):\n self.channels ={\n 'sub_front':0,\n 'sub_back':1,\n 'friendly_bullet':2,\n 'trail':3,\n 'enemy_bullet':4,\n 'enemy_fish':5,\n 'enemy_sub':6,\n 'oxygen_guage':7,\n 'diver_guage':8,\n 'diver':9\n }\n self.action_map = ['n','l','u','r','d','f']\n self.ramping = ramping\n self.random = np.random.RandomState()\n self.reset()\n\n # Update environment according to agent action\n def act(self, a):\n r = 0\n if(self.terminal):\n return r, self.terminal\n\n a = self.action_map[a]\n\n # Spawn enemy if timer is up\n if(self.e_spawn_timer==0):\n self._spawn_enemy()\n self.e_spawn_timer = self.e_spawn_speed\n\n if(self.d_spawn_timer==0):\n self._spawn_diver()\n self.d_spawn_timer = diver_spawn_speed\n\n # Resolve player action\n if(a=='f' and self.shot_timer == 0):\n self.f_bullets+=[[self.sub_x, self.sub_y, self.sub_or]]\n self.shot_timer = shot_cool_down\n elif(a=='l'):\n self.sub_x = max(0, self.sub_x-1)\n self.sub_or = False\n elif(a=='r'):\n self.sub_x = min(9, self.sub_x+1)\n self.sub_or = True\n elif(a=='u'):\n self.sub_y = max(0, self.sub_y-1)\n elif(a=='d'):\n self.sub_y = min(8, self.sub_y+1)\n\n # Update friendly Bullets\n for bullet in reversed(self.f_bullets):\n bullet[0]+=1 if bullet[2] else -1\n if(bullet[0]<0 or bullet[0]>9):\n self.f_bullets.remove(bullet)\n else:\n removed = False\n for x in self.e_fish:\n if(bullet[0:2]==x[0:2]):\n self.e_fish.remove(x)\n self.f_bullets.remove(bullet)\n r+=1\n removed = True\n break\n if(not removed):\n for x in self.e_subs:\n if(bullet[0:2]==x[0:2]):\n self.e_subs.remove(x)\n self.f_bullets.remove(bullet)\n r+=1\n break\n\n # Update divers\n for diver in reversed(self.divers):\n if(diver[0:2]==[self.sub_x,self.sub_y] and self.diver_count<6):\n self.divers.remove(diver)\n self.diver_count+=1\n else:\n if(diver[3]==0):\n diver[3]=diver_move_interval\n diver[0]+=1 if diver[2] else -1\n if(diver[0]<0 or diver[0]>9):\n self.divers.remove(diver)\n elif(diver[0:2]==[self.sub_x,self.sub_y] and self.diver_count<6):\n self.divers.remove(diver)\n self.diver_count+=1\n else:\n diver[3]-=1\n\n # Update enemy subs\n for sub in reversed(self.e_subs):\n if(sub[0:2]==[self.sub_x,self.sub_y]):\n self.terminal = True\n if(sub[3]==0):\n sub[3]=self.move_speed\n sub[0]+=1 if sub[2] else -1\n if(sub[0]<0 or sub[0]>9):\n self.e_subs.remove(sub)\n elif(sub[0:2]==[self.sub_x,self.sub_y]):\n self.terminal = True\n else:\n for x in self.f_bullets:\n if(sub[0:2]==x[0:2]):\n self.e_subs.remove(sub)\n self.f_bullets.remove(x)\n r+=1\n break\n else:\n sub[3]-=1\n if(sub[4]==0):\n sub[4]=enemy_shot_interval\n self.e_bullets+=[[sub[0] if sub[2] else sub[0], sub[1], sub[2]]]\n else:\n sub[4]-=1\n\n # Update enemy bullets\n for bullet in reversed(self.e_bullets):\n if(bullet[0:2]==[self.sub_x,self.sub_y]):\n self.terminal = True\n bullet[0]+=1 if bullet[2] else -1\n if(bullet[0]<0 or bullet[0]>9):\n self.e_bullets.remove(bullet)\n else:\n if(bullet[0:2]==[self.sub_x,self.sub_y]):\n self.terminal = True\n\n # Update enemy fish\n for fish in reversed(self.e_fish):\n if(fish[0:2]==[self.sub_x,self.sub_y]):\n self.terminal = True\n if(fish[3]==0):\n fish[3]=self.move_speed\n fish[0]+=1 if fish[2] else -1\n if(fish[0]<0 or fish[0]>9):\n self.e_fish.remove(fish)\n elif(fish[0:2]==[self.sub_x,self.sub_y]):\n self.terminal = True\n else:\n for x in self.f_bullets:\n if(fish[0:2]==x[0:2]):\n self.e_fish.remove(fish)\n self.f_bullets.remove(x)\n r+=1\n break\n else:\n fish[3]-=1\n\n # Update various timers\n self.e_spawn_timer -= self.e_spawn_timer>0\n self.d_spawn_timer -= self.d_spawn_timer>0\n self.shot_timer -= self.shot_timer>0\n if(self.oxygen<=0):\n self.terminal = True\n if(self.sub_y>0):\n self.oxygen-=1\n self.surface = False\n else:\n if(not self.surface):\n if(self.diver_count == 0):\n self.terminal = True\n else:\n r+=self._surface()\n return r, self.terminal\n\n # Called when player hits surface (top row) if they have no divers, this ends the game,\n # if they have 6 divers this gives reward proportional to the remaining oxygen and restores full oxygen\n # otherwise this reduces the number of divers and restores full oxygen\n def _surface(self):\n self.surface = True\n if(self.diver_count == 6):\n self.diver_count = 0\n r = self.oxygen*10//max_oxygen\n else:\n r = 0\n self.oxygen = max_oxygen\n self.diver_count -= 1\n if self.ramping and (self.e_spawn_speed>1 or self.move_speed>2):\n if(self.move_speed>2 and self.ramp_index%2):\n self.move_speed-=1\n if(self.e_spawn_speed>1):\n self.e_spawn_speed-=1\n self.ramp_index+=1\n return r\n\n # Spawn an enemy fish or submarine in random row and random direction,\n # if the resulting row and direction would lead to a collision, do nothing instead\n def _spawn_enemy(self):\n lr = self.random.rand() < 1/2\n is_sub = self.random.rand() < 1/3\n x = 0 if lr else 9\n y = self.random.randint(low=1, high=9)\n\n # Do not spawn in same row an opposite direction as existing\n if(any([z[1]==y and z[2]!=lr for z in self.e_subs+self.e_fish])):\n return\n if(is_sub):\n self.e_subs+=[[x,y,lr,self.move_speed,enemy_shot_interval]]\n else:\n self.e_fish+=[[x,y,lr,self.move_speed]]\n\n # Spawn a diver in random row with random direction\n def _spawn_diver(self):\n lr = self.random.rand() < 1/2\n x = 0 if lr else 9\n y = self.random.randint(low=1, high=9)\n self.divers+=[[x,y,lr,diver_move_interval]]\n\n # Query the current level of the difficulty ramp, could be used as additional input to agent for example\n def difficulty_ramp(self):\n return self.ramp_index\n\n # Process the game-state into the 10x10xn state provided to the agent and return\n def state(self):\n state = np.zeros((10,10,len(self.channels)),dtype=bool)\n state[self.sub_y,self.sub_x,self.channels['sub_front']] = 1\n back_x = self.sub_x-1 if self.sub_or else self.sub_x+1\n state[self.sub_y,back_x,self.channels['sub_back']] = 1\n state[9,0:max(0,self.oxygen)*10//max_oxygen, self.channels['oxygen_guage']] = 1\n state[9,9-self.diver_count:9, self.channels['diver_guage']] = 1\n for bullet in self.f_bullets:\n state[bullet[1],bullet[0], self.channels['friendly_bullet']] = 1\n for bullet in self.e_bullets:\n state[bullet[1],bullet[0], self.channels['enemy_bullet']] = 1\n for fish in self.e_fish:\n state[fish[1],fish[0], self.channels['enemy_fish']] = 1\n back_x = fish[0]-1 if fish[2] else fish[0]+1\n if(back_x>=0 and back_x<=9):\n state[fish[1],back_x, self.channels['trail']] = 1\n for sub in self.e_subs:\n state[sub[1],sub[0], self.channels['enemy_sub']] = 1\n back_x = sub[0]-1 if sub[2] else sub[0]+1\n if(back_x>=0 and back_x<=9):\n state[sub[1],back_x, self.channels['trail']] = 1\n for diver in self.divers:\n state[diver[1],diver[0], self.channels['diver']] = 1\n back_x = diver[0]-1 if diver[2] else diver[0]+1\n if(back_x>=0 and back_x<=9):\n state[diver[1],back_x, self.channels['trail']] = 1\n\n return state\n\n # Reset to start state for new episode\n def reset(self):\n self.oxygen = max_oxygen\n self.diver_count = 0\n self.sub_x = 5\n self.sub_y = 0\n # 0=left, 1=right\n self.sub_or = False\n self.f_bullets = []\n self.e_bullets = []\n self.e_fish = []\n self.e_subs = []\n self.divers = []\n self.e_spawn_speed = init_spawn_speed\n self.e_spawn_timer = self.e_spawn_speed\n self.d_spawn_timer = diver_spawn_speed\n self.move_speed = init_move_interval\n self.ramp_index = 0\n self.shot_timer = 0\n self.surface = True\n self.terminal = False\n\n # Dimensionality of the game-state (10x10xn)\n def state_shape(self):\n return [10,10,len(self.channels)]\n\n # Subset of actions that actually have a unique impact in this environment\n def minimal_action_set(self):\n minimal_actions = ['n','l','u','r','d','f']\n return [self.action_map.index(x) for x in minimal_actions]\n\n```\n\nInput: a row-major 10x10x6 binary grid. Index `(row * 10 + column) * 6 + channel`. Channels: player=sub_front|sub_back; danger=enemy_fish|enemy_sub|enemy_bullet; reward=enemy_fish|enemy_sub|diver; projectile=friendly_bullet; trail=trail|sub_back; gauge=oxygen_guage|diver_guage.\n\nNetwork: `h1=tanh(x@W1+b1)`, `h2=tanh(h1@W2+b2)`, `logits=h2@W3+b3`. Shapes are W1 600x32, b1 32, W2 32x32, b2 32, W3 32x6, b3 6. All 20,486 values start at zero.\n\nThe hidden evaluator runs 1,000 fixed-seed episodes, stopping each after 10,000 steps. It divides mean game score by the game's fixed cap and clips the result to [0, 1].\n\nThe final fenced block must start with `edits`. It may contain numeric assignments to indexed entries or slices of W1, b1, W2, b2, W3, and b3. Half-open Python slices and numeric lists are supported, as are `*=`, `+=`, and `-=`. Values must be finite and have absolute value at most 1e6. Example:\n```edits\nW1[12, 0] = 2.5\nb1[0:4] = -1\nW2[0:2, 0:3] = [[1, -1, 0.5], [0, 2, 0]]\nW3[:, 2] *= 2\n```\n", "system_prompt": "You directly program neural-network weights. Follow the task constraints exactly.", "episodes": 1000, "task_id": 270, "family": "minatar_original", "upstream_source_url": "https://pypi.org/project/MinAtar/1.0.15/", "upstream_revision": "MinAtar==1.0.15", "native_source_url": null}
|
| 5 |
+
{"name": "asterix", "split": "test", "game": "asterix", "source": "original", "prompt": "Write from scratch the weights of a policy network that plays Asterix as well as possible.\n\nThere is no training, data, loss, gradients, fine-tuning, code execution, calculator, or environment feedback. This is one response. Briefly describe the strategy and hidden units, then provide the weights in the final `edits` code block.\n\nMove in four directions to collect treasure while avoiding horizontally moving enemies.\n\nActions are sampled from softmax logits and have sticky-action probability 0.1.\n\nExact MinAtar 1.0.15 source (`minatar/environments/asterix.py`):\n```python\n################################################################################################################\n# Authors: #\n# Kenny Young (kjyoung@ualberta.ca) #\n# Tian Tian (ttian@ualberta.ca) #\n################################################################################################################\nimport numpy as np\n\n\n#####################################################################################################################\n# Constants\n#\n#####################################################################################################################\nramp_interval = 100\ninit_spawn_speed = 10\ninit_move_interval = 5\nshot_cool_down = 5\n\n\n#####################################################################################################################\n# Env\n#\n# The player can move freely along the 4 cardinal directions. Enemies and treasure spawn from the sides. A reward of\n# +1 is given for picking up treasure. Termination occurs if the player makes contact with an enemy. Enemy and\n# treasure direction are indicated by a trail channel. Difficulty is periodically increased by increasing the speed\n# and spawn rate of enemies and treasure.\n#\n#####################################################################################################################\nclass Env:\n def __init__(self, ramping=True):\n self.channels ={\n 'player':0,\n 'enemy':1,\n 'trail':2,\n 'gold':3\n }\n self.action_map = ['n','l','u','r','d','f']\n self.ramping = ramping\n self.random = np.random.RandomState()\n self.reset()\n\n # Update environment according to agent action\n def act(self, a):\n r = 0\n if(self.terminal):\n return r, self.terminal\n\n a = self.action_map[a]\n\n # Spawn enemy if timer is up\n if(self.spawn_timer==0):\n self._spawn_entity()\n self.spawn_timer = self.spawn_speed\n\n # Resolve player action\n if(a=='l'):\n self.player_x = max(0, self.player_x-1)\n elif(a=='r'):\n self.player_x = min(9, self.player_x+1)\n elif(a=='u'):\n self.player_y = max(1, self.player_y-1)\n elif(a=='d'):\n self.player_y = min(8, self.player_y+1)\n\n # Update entities\n for i in range(len(self.entities)):\n x = self.entities[i]\n if(x is not None):\n if(x[0:2]==[self.player_x,self.player_y]):\n if(self.entities[i][3]):\n self.entities[i] = None\n r+=1\n else:\n self.terminal = True\n if(self.move_timer==0):\n self.move_timer = self.move_speed\n for i in range(len(self.entities)):\n x = self.entities[i]\n if(x is not None):\n x[0]+=1 if x[2] else -1\n if(x[0]<0 or x[0]>9):\n self.entities[i] = None\n if(x[0:2]==[self.player_x,self.player_y]):\n if(self.entities[i][3]):\n self.entities[i] = None\n r+=1\n else:\n self.terminal = True\n\n # Update various timers\n self.spawn_timer -= 1\n self.move_timer -= 1\n\n\n #Ramp difficulty if interval has elapsed\n if self.ramping and (self.spawn_speed>1 or self.move_speed>1):\n if(self.ramp_timer>=0):\n self.ramp_timer-=1\n else:\n if(self.move_speed>1 and self.ramp_index%2):\n self.move_speed-=1\n if(self.spawn_speed>1):\n self.spawn_speed-=1\n self.ramp_index+=1\n self.ramp_timer=ramp_interval\n return r, self.terminal\n\n # Spawn a new enemy or treasure at a random location with random direction (if all rows are filled do nothing)\n def _spawn_entity(self):\n lr = self.random.rand() < 1/2\n is_gold = self.random.rand() < 1/3\n x = 0 if lr else 9\n slot_options = [i for i in range(len(self.entities)) if self.entities[i]==None]\n if(not slot_options):\n return\n slot = slot_options[self.random.randint(len(slot_options))]\n self.entities[slot] = [x,slot+1,lr,is_gold]\n\n # Query the current level of the difficulty ramp, could be used as additional input to agent for example\n def difficulty_ramp(self):\n return self.ramp_index\n\n # Process the game-state into the 10x10xn state provided to the agent and return\n def state(self):\n state = np.zeros((10,10,len(self.channels)),dtype=bool)\n state[self.player_y,self.player_x,self.channels['player']] = 1\n for x in self.entities:\n if(x is not None):\n c = self.channels['gold'] if x[3] else self.channels['enemy']\n state[x[1], x[0],c] = 1\n back_x = x[0]-1 if x[2] else x[0]+1\n if(back_x>=0 and back_x<=9):\n state[x[1], back_x, self.channels['trail']] = 1\n return state\n\n # Reset to start state for new episode\n def reset(self):\n self.player_x = 5\n self.player_y = 5\n self.entities = [None]*8\n self.shot_timer = 0\n self.spawn_speed = init_spawn_speed\n self.spawn_timer = self.spawn_speed\n self.move_speed = init_move_interval\n self.move_timer = self.move_speed\n self.ramp_timer = ramp_interval\n self.ramp_index = 0\n self.terminal = False\n\n # Dimensionality of the game-state (10x10xn)\n def state_shape(self):\n return [10,10,len(self.channels)]\n\n # Subset of actions that actually have a unique impact in this environment\n def minimal_action_set(self):\n minimal_actions = ['n','l','u','r','d']\n return [self.action_map.index(x) for x in minimal_actions]\n\n```\n\nInput: a row-major 10x10x6 binary grid. Index `(row * 10 + column) * 6 + channel`. Channels: player=player; danger=enemy; reward=gold; projectile=empty; trail=trail; gauge=empty.\n\nNetwork: `h1=tanh(x@W1+b1)`, `h2=tanh(h1@W2+b2)`, `logits=h2@W3+b3`. Shapes are W1 600x32, b1 32, W2 32x32, b2 32, W3 32x6, b3 6. All 20,486 values start at zero.\n\nThe hidden evaluator runs 1,000 fixed-seed episodes, stopping each after 10,000 steps. It divides mean game score by the game's fixed cap and clips the result to [0, 1].\n\nThe final fenced block must start with `edits`. It may contain numeric assignments to indexed entries or slices of W1, b1, W2, b2, W3, and b3. Half-open Python slices and numeric lists are supported, as are `*=`, `+=`, and `-=`. Values must be finite and have absolute value at most 1e6. Example:\n```edits\nW1[12, 0] = 2.5\nb1[0:4] = -1\nW2[0:2, 0:3] = [[1, -1, 0.5], [0, 2, 0]]\nW3[:, 2] *= 2\n```\n", "system_prompt": "You directly program neural-network weights. Follow the task constraints exactly.", "episodes": 1000, "task_id": 278, "family": "minatar_original", "upstream_source_url": "https://pypi.org/project/MinAtar/1.0.15/", "upstream_revision": "MinAtar==1.0.15", "native_source_url": null}
|
data/train.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
runtime/LICENSE-Crisp
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Crisp source notices
|
| 2 |
+
===================
|
| 3 |
+
|
| 4 |
+
The following 2021 MIT notice is reproduced verbatim from:
|
| 5 |
+
https://github.com/abagames/crisp-game-lib-games/blob/cfb39d2f988feb5918eb83697145b5a35adabf58/LICENSE.txt
|
| 6 |
+
|
| 7 |
+
MIT License
|
| 8 |
+
|
| 9 |
+
Copyright (c) 2021 ABA Games
|
| 10 |
+
|
| 11 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 12 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 13 |
+
in the Software without restriction, including without limitation the rights
|
| 14 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 15 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 16 |
+
furnished to do so, subject to the following conditions:
|
| 17 |
+
|
| 18 |
+
The above copyright notice and this permission notice shall be included in all
|
| 19 |
+
copies or substantial portions of the Software.
|
| 20 |
+
|
| 21 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 22 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 23 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 24 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 25 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 26 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 27 |
+
SOFTWARE.
|
| 28 |
+
|
| 29 |
+
The following 2022 MIT notice is reproduced verbatim. Both pinned sources
|
| 30 |
+
below carry this same notice:
|
| 31 |
+
https://github.com/abagames/crisp-game-lib-11-games/blob/6fea4eefe2deaa42706051d96a04ec9e36104696/LICENSE.txt
|
| 32 |
+
https://github.com/joyrider3774/crisp-game-lib-portable-sdl/blob/04bc2c4cfc2853628eda25a46190bb7ac6fe216a/LICENSE.txt
|
| 33 |
+
|
| 34 |
+
MIT License
|
| 35 |
+
|
| 36 |
+
Copyright (c) 2022 ABA Games
|
| 37 |
+
|
| 38 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 39 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 40 |
+
in the Software without restriction, including without limitation the rights
|
| 41 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 42 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 43 |
+
furnished to do so, subject to the following conditions:
|
| 44 |
+
|
| 45 |
+
The above copyright notice and this permission notice shall be included in all
|
| 46 |
+
copies or substantial portions of the Software.
|
| 47 |
+
|
| 48 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 49 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 50 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 51 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 52 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 53 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 54 |
+
SOFTWARE.
|
runtime/LICENSE-MinAtar
ADDED
|
@@ -0,0 +1,674 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
GNU GENERAL PUBLIC LICENSE
|
| 2 |
+
Version 3, 29 June 2007
|
| 3 |
+
|
| 4 |
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
| 5 |
+
Everyone is permitted to copy and distribute verbatim copies
|
| 6 |
+
of this license document, but changing it is not allowed.
|
| 7 |
+
|
| 8 |
+
Preamble
|
| 9 |
+
|
| 10 |
+
The GNU General Public License is a free, copyleft license for
|
| 11 |
+
software and other kinds of works.
|
| 12 |
+
|
| 13 |
+
The licenses for most software and other practical works are designed
|
| 14 |
+
to take away your freedom to share and change the works. By contrast,
|
| 15 |
+
the GNU General Public License is intended to guarantee your freedom to
|
| 16 |
+
share and change all versions of a program--to make sure it remains free
|
| 17 |
+
software for all its users. We, the Free Software Foundation, use the
|
| 18 |
+
GNU General Public License for most of our software; it applies also to
|
| 19 |
+
any other work released this way by its authors. You can apply it to
|
| 20 |
+
your programs, too.
|
| 21 |
+
|
| 22 |
+
When we speak of free software, we are referring to freedom, not
|
| 23 |
+
price. Our General Public Licenses are designed to make sure that you
|
| 24 |
+
have the freedom to distribute copies of free software (and charge for
|
| 25 |
+
them if you wish), that you receive source code or can get it if you
|
| 26 |
+
want it, that you can change the software or use pieces of it in new
|
| 27 |
+
free programs, and that you know you can do these things.
|
| 28 |
+
|
| 29 |
+
To protect your rights, we need to prevent others from denying you
|
| 30 |
+
these rights or asking you to surrender the rights. Therefore, you have
|
| 31 |
+
certain responsibilities if you distribute copies of the software, or if
|
| 32 |
+
you modify it: responsibilities to respect the freedom of others.
|
| 33 |
+
|
| 34 |
+
For example, if you distribute copies of such a program, whether
|
| 35 |
+
gratis or for a fee, you must pass on to the recipients the same
|
| 36 |
+
freedoms that you received. You must make sure that they, too, receive
|
| 37 |
+
or can get the source code. And you must show them these terms so they
|
| 38 |
+
know their rights.
|
| 39 |
+
|
| 40 |
+
Developers that use the GNU GPL protect your rights with two steps:
|
| 41 |
+
(1) assert copyright on the software, and (2) offer you this License
|
| 42 |
+
giving you legal permission to copy, distribute and/or modify it.
|
| 43 |
+
|
| 44 |
+
For the developers' and authors' protection, the GPL clearly explains
|
| 45 |
+
that there is no warranty for this free software. For both users' and
|
| 46 |
+
authors' sake, the GPL requires that modified versions be marked as
|
| 47 |
+
changed, so that their problems will not be attributed erroneously to
|
| 48 |
+
authors of previous versions.
|
| 49 |
+
|
| 50 |
+
Some devices are designed to deny users access to install or run
|
| 51 |
+
modified versions of the software inside them, although the manufacturer
|
| 52 |
+
can do so. This is fundamentally incompatible with the aim of
|
| 53 |
+
protecting users' freedom to change the software. The systematic
|
| 54 |
+
pattern of such abuse occurs in the area of products for individuals to
|
| 55 |
+
use, which is precisely where it is most unacceptable. Therefore, we
|
| 56 |
+
have designed this version of the GPL to prohibit the practice for those
|
| 57 |
+
products. If such problems arise substantially in other domains, we
|
| 58 |
+
stand ready to extend this provision to those domains in future versions
|
| 59 |
+
of the GPL, as needed to protect the freedom of users.
|
| 60 |
+
|
| 61 |
+
Finally, every program is threatened constantly by software patents.
|
| 62 |
+
States should not allow patents to restrict development and use of
|
| 63 |
+
software on general-purpose computers, but in those that do, we wish to
|
| 64 |
+
avoid the special danger that patents applied to a free program could
|
| 65 |
+
make it effectively proprietary. To prevent this, the GPL assures that
|
| 66 |
+
patents cannot be used to render the program non-free.
|
| 67 |
+
|
| 68 |
+
The precise terms and conditions for copying, distribution and
|
| 69 |
+
modification follow.
|
| 70 |
+
|
| 71 |
+
TERMS AND CONDITIONS
|
| 72 |
+
|
| 73 |
+
0. Definitions.
|
| 74 |
+
|
| 75 |
+
"This License" refers to version 3 of the GNU General Public License.
|
| 76 |
+
|
| 77 |
+
"Copyright" also means copyright-like laws that apply to other kinds of
|
| 78 |
+
works, such as semiconductor masks.
|
| 79 |
+
|
| 80 |
+
"The Program" refers to any copyrightable work licensed under this
|
| 81 |
+
License. Each licensee is addressed as "you". "Licensees" and
|
| 82 |
+
"recipients" may be individuals or organizations.
|
| 83 |
+
|
| 84 |
+
To "modify" a work means to copy from or adapt all or part of the work
|
| 85 |
+
in a fashion requiring copyright permission, other than the making of an
|
| 86 |
+
exact copy. The resulting work is called a "modified version" of the
|
| 87 |
+
earlier work or a work "based on" the earlier work.
|
| 88 |
+
|
| 89 |
+
A "covered work" means either the unmodified Program or a work based
|
| 90 |
+
on the Program.
|
| 91 |
+
|
| 92 |
+
To "propagate" a work means to do anything with it that, without
|
| 93 |
+
permission, would make you directly or secondarily liable for
|
| 94 |
+
infringement under applicable copyright law, except executing it on a
|
| 95 |
+
computer or modifying a private copy. Propagation includes copying,
|
| 96 |
+
distribution (with or without modification), making available to the
|
| 97 |
+
public, and in some countries other activities as well.
|
| 98 |
+
|
| 99 |
+
To "convey" a work means any kind of propagation that enables other
|
| 100 |
+
parties to make or receive copies. Mere interaction with a user through
|
| 101 |
+
a computer network, with no transfer of a copy, is not conveying.
|
| 102 |
+
|
| 103 |
+
An interactive user interface displays "Appropriate Legal Notices"
|
| 104 |
+
to the extent that it includes a convenient and prominently visible
|
| 105 |
+
feature that (1) displays an appropriate copyright notice, and (2)
|
| 106 |
+
tells the user that there is no warranty for the work (except to the
|
| 107 |
+
extent that warranties are provided), that licensees may convey the
|
| 108 |
+
work under this License, and how to view a copy of this License. If
|
| 109 |
+
the interface presents a list of user commands or options, such as a
|
| 110 |
+
menu, a prominent item in the list meets this criterion.
|
| 111 |
+
|
| 112 |
+
1. Source Code.
|
| 113 |
+
|
| 114 |
+
The "source code" for a work means the preferred form of the work
|
| 115 |
+
for making modifications to it. "Object code" means any non-source
|
| 116 |
+
form of a work.
|
| 117 |
+
|
| 118 |
+
A "Standard Interface" means an interface that either is an official
|
| 119 |
+
standard defined by a recognized standards body, or, in the case of
|
| 120 |
+
interfaces specified for a particular programming language, one that
|
| 121 |
+
is widely used among developers working in that language.
|
| 122 |
+
|
| 123 |
+
The "System Libraries" of an executable work include anything, other
|
| 124 |
+
than the work as a whole, that (a) is included in the normal form of
|
| 125 |
+
packaging a Major Component, but which is not part of that Major
|
| 126 |
+
Component, and (b) serves only to enable use of the work with that
|
| 127 |
+
Major Component, or to implement a Standard Interface for which an
|
| 128 |
+
implementation is available to the public in source code form. A
|
| 129 |
+
"Major Component", in this context, means a major essential component
|
| 130 |
+
(kernel, window system, and so on) of the specific operating system
|
| 131 |
+
(if any) on which the executable work runs, or a compiler used to
|
| 132 |
+
produce the work, or an object code interpreter used to run it.
|
| 133 |
+
|
| 134 |
+
The "Corresponding Source" for a work in object code form means all
|
| 135 |
+
the source code needed to generate, install, and (for an executable
|
| 136 |
+
work) run the object code and to modify the work, including scripts to
|
| 137 |
+
control those activities. However, it does not include the work's
|
| 138 |
+
System Libraries, or general-purpose tools or generally available free
|
| 139 |
+
programs which are used unmodified in performing those activities but
|
| 140 |
+
which are not part of the work. For example, Corresponding Source
|
| 141 |
+
includes interface definition files associated with source files for
|
| 142 |
+
the work, and the source code for shared libraries and dynamically
|
| 143 |
+
linked subprograms that the work is specifically designed to require,
|
| 144 |
+
such as by intimate data communication or control flow between those
|
| 145 |
+
subprograms and other parts of the work.
|
| 146 |
+
|
| 147 |
+
The Corresponding Source need not include anything that users
|
| 148 |
+
can regenerate automatically from other parts of the Corresponding
|
| 149 |
+
Source.
|
| 150 |
+
|
| 151 |
+
The Corresponding Source for a work in source code form is that
|
| 152 |
+
same work.
|
| 153 |
+
|
| 154 |
+
2. Basic Permissions.
|
| 155 |
+
|
| 156 |
+
All rights granted under this License are granted for the term of
|
| 157 |
+
copyright on the Program, and are irrevocable provided the stated
|
| 158 |
+
conditions are met. This License explicitly affirms your unlimited
|
| 159 |
+
permission to run the unmodified Program. The output from running a
|
| 160 |
+
covered work is covered by this License only if the output, given its
|
| 161 |
+
content, constitutes a covered work. This License acknowledges your
|
| 162 |
+
rights of fair use or other equivalent, as provided by copyright law.
|
| 163 |
+
|
| 164 |
+
You may make, run and propagate covered works that you do not
|
| 165 |
+
convey, without conditions so long as your license otherwise remains
|
| 166 |
+
in force. You may convey covered works to others for the sole purpose
|
| 167 |
+
of having them make modifications exclusively for you, or provide you
|
| 168 |
+
with facilities for running those works, provided that you comply with
|
| 169 |
+
the terms of this License in conveying all material for which you do
|
| 170 |
+
not control copyright. Those thus making or running the covered works
|
| 171 |
+
for you must do so exclusively on your behalf, under your direction
|
| 172 |
+
and control, on terms that prohibit them from making any copies of
|
| 173 |
+
your copyrighted material outside their relationship with you.
|
| 174 |
+
|
| 175 |
+
Conveying under any other circumstances is permitted solely under
|
| 176 |
+
the conditions stated below. Sublicensing is not allowed; section 10
|
| 177 |
+
makes it unnecessary.
|
| 178 |
+
|
| 179 |
+
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
| 180 |
+
|
| 181 |
+
No covered work shall be deemed part of an effective technological
|
| 182 |
+
measure under any applicable law fulfilling obligations under article
|
| 183 |
+
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
| 184 |
+
similar laws prohibiting or restricting circumvention of such
|
| 185 |
+
measures.
|
| 186 |
+
|
| 187 |
+
When you convey a covered work, you waive any legal power to forbid
|
| 188 |
+
circumvention of technological measures to the extent such circumvention
|
| 189 |
+
is effected by exercising rights under this License with respect to
|
| 190 |
+
the covered work, and you disclaim any intention to limit operation or
|
| 191 |
+
modification of the work as a means of enforcing, against the work's
|
| 192 |
+
users, your or third parties' legal rights to forbid circumvention of
|
| 193 |
+
technological measures.
|
| 194 |
+
|
| 195 |
+
4. Conveying Verbatim Copies.
|
| 196 |
+
|
| 197 |
+
You may convey verbatim copies of the Program's source code as you
|
| 198 |
+
receive it, in any medium, provided that you conspicuously and
|
| 199 |
+
appropriately publish on each copy an appropriate copyright notice;
|
| 200 |
+
keep intact all notices stating that this License and any
|
| 201 |
+
non-permissive terms added in accord with section 7 apply to the code;
|
| 202 |
+
keep intact all notices of the absence of any warranty; and give all
|
| 203 |
+
recipients a copy of this License along with the Program.
|
| 204 |
+
|
| 205 |
+
You may charge any price or no price for each copy that you convey,
|
| 206 |
+
and you may offer support or warranty protection for a fee.
|
| 207 |
+
|
| 208 |
+
5. Conveying Modified Source Versions.
|
| 209 |
+
|
| 210 |
+
You may convey a work based on the Program, or the modifications to
|
| 211 |
+
produce it from the Program, in the form of source code under the
|
| 212 |
+
terms of section 4, provided that you also meet all of these conditions:
|
| 213 |
+
|
| 214 |
+
a) The work must carry prominent notices stating that you modified
|
| 215 |
+
it, and giving a relevant date.
|
| 216 |
+
|
| 217 |
+
b) The work must carry prominent notices stating that it is
|
| 218 |
+
released under this License and any conditions added under section
|
| 219 |
+
7. This requirement modifies the requirement in section 4 to
|
| 220 |
+
"keep intact all notices".
|
| 221 |
+
|
| 222 |
+
c) You must license the entire work, as a whole, under this
|
| 223 |
+
License to anyone who comes into possession of a copy. This
|
| 224 |
+
License will therefore apply, along with any applicable section 7
|
| 225 |
+
additional terms, to the whole of the work, and all its parts,
|
| 226 |
+
regardless of how they are packaged. This License gives no
|
| 227 |
+
permission to license the work in any other way, but it does not
|
| 228 |
+
invalidate such permission if you have separately received it.
|
| 229 |
+
|
| 230 |
+
d) If the work has interactive user interfaces, each must display
|
| 231 |
+
Appropriate Legal Notices; however, if the Program has interactive
|
| 232 |
+
interfaces that do not display Appropriate Legal Notices, your
|
| 233 |
+
work need not make them do so.
|
| 234 |
+
|
| 235 |
+
A compilation of a covered work with other separate and independent
|
| 236 |
+
works, which are not by their nature extensions of the covered work,
|
| 237 |
+
and which are not combined with it such as to form a larger program,
|
| 238 |
+
in or on a volume of a storage or distribution medium, is called an
|
| 239 |
+
"aggregate" if the compilation and its resulting copyright are not
|
| 240 |
+
used to limit the access or legal rights of the compilation's users
|
| 241 |
+
beyond what the individual works permit. Inclusion of a covered work
|
| 242 |
+
in an aggregate does not cause this License to apply to the other
|
| 243 |
+
parts of the aggregate.
|
| 244 |
+
|
| 245 |
+
6. Conveying Non-Source Forms.
|
| 246 |
+
|
| 247 |
+
You may convey a covered work in object code form under the terms
|
| 248 |
+
of sections 4 and 5, provided that you also convey the
|
| 249 |
+
machine-readable Corresponding Source under the terms of this License,
|
| 250 |
+
in one of these ways:
|
| 251 |
+
|
| 252 |
+
a) Convey the object code in, or embodied in, a physical product
|
| 253 |
+
(including a physical distribution medium), accompanied by the
|
| 254 |
+
Corresponding Source fixed on a durable physical medium
|
| 255 |
+
customarily used for software interchange.
|
| 256 |
+
|
| 257 |
+
b) Convey the object code in, or embodied in, a physical product
|
| 258 |
+
(including a physical distribution medium), accompanied by a
|
| 259 |
+
written offer, valid for at least three years and valid for as
|
| 260 |
+
long as you offer spare parts or customer support for that product
|
| 261 |
+
model, to give anyone who possesses the object code either (1) a
|
| 262 |
+
copy of the Corresponding Source for all the software in the
|
| 263 |
+
product that is covered by this License, on a durable physical
|
| 264 |
+
medium customarily used for software interchange, for a price no
|
| 265 |
+
more than your reasonable cost of physically performing this
|
| 266 |
+
conveying of source, or (2) access to copy the
|
| 267 |
+
Corresponding Source from a network server at no charge.
|
| 268 |
+
|
| 269 |
+
c) Convey individual copies of the object code with a copy of the
|
| 270 |
+
written offer to provide the Corresponding Source. This
|
| 271 |
+
alternative is allowed only occasionally and noncommercially, and
|
| 272 |
+
only if you received the object code with such an offer, in accord
|
| 273 |
+
with subsection 6b.
|
| 274 |
+
|
| 275 |
+
d) Convey the object code by offering access from a designated
|
| 276 |
+
place (gratis or for a charge), and offer equivalent access to the
|
| 277 |
+
Corresponding Source in the same way through the same place at no
|
| 278 |
+
further charge. You need not require recipients to copy the
|
| 279 |
+
Corresponding Source along with the object code. If the place to
|
| 280 |
+
copy the object code is a network server, the Corresponding Source
|
| 281 |
+
may be on a different server (operated by you or a third party)
|
| 282 |
+
that supports equivalent copying facilities, provided you maintain
|
| 283 |
+
clear directions next to the object code saying where to find the
|
| 284 |
+
Corresponding Source. Regardless of what server hosts the
|
| 285 |
+
Corresponding Source, you remain obligated to ensure that it is
|
| 286 |
+
available for as long as needed to satisfy these requirements.
|
| 287 |
+
|
| 288 |
+
e) Convey the object code using peer-to-peer transmission, provided
|
| 289 |
+
you inform other peers where the object code and Corresponding
|
| 290 |
+
Source of the work are being offered to the general public at no
|
| 291 |
+
charge under subsection 6d.
|
| 292 |
+
|
| 293 |
+
A separable portion of the object code, whose source code is excluded
|
| 294 |
+
from the Corresponding Source as a System Library, need not be
|
| 295 |
+
included in conveying the object code work.
|
| 296 |
+
|
| 297 |
+
A "User Product" is either (1) a "consumer product", which means any
|
| 298 |
+
tangible personal property which is normally used for personal, family,
|
| 299 |
+
or household purposes, or (2) anything designed or sold for incorporation
|
| 300 |
+
into a dwelling. In determining whether a product is a consumer product,
|
| 301 |
+
doubtful cases shall be resolved in favor of coverage. For a particular
|
| 302 |
+
product received by a particular user, "normally used" refers to a
|
| 303 |
+
typical or common use of that class of product, regardless of the status
|
| 304 |
+
of the particular user or of the way in which the particular user
|
| 305 |
+
actually uses, or expects or is expected to use, the product. A product
|
| 306 |
+
is a consumer product regardless of whether the product has substantial
|
| 307 |
+
commercial, industrial or non-consumer uses, unless such uses represent
|
| 308 |
+
the only significant mode of use of the product.
|
| 309 |
+
|
| 310 |
+
"Installation Information" for a User Product means any methods,
|
| 311 |
+
procedures, authorization keys, or other information required to install
|
| 312 |
+
and execute modified versions of a covered work in that User Product from
|
| 313 |
+
a modified version of its Corresponding Source. The information must
|
| 314 |
+
suffice to ensure that the continued functioning of the modified object
|
| 315 |
+
code is in no case prevented or interfered with solely because
|
| 316 |
+
modification has been made.
|
| 317 |
+
|
| 318 |
+
If you convey an object code work under this section in, or with, or
|
| 319 |
+
specifically for use in, a User Product, and the conveying occurs as
|
| 320 |
+
part of a transaction in which the right of possession and use of the
|
| 321 |
+
User Product is transferred to the recipient in perpetuity or for a
|
| 322 |
+
fixed term (regardless of how the transaction is characterized), the
|
| 323 |
+
Corresponding Source conveyed under this section must be accompanied
|
| 324 |
+
by the Installation Information. But this requirement does not apply
|
| 325 |
+
if neither you nor any third party retains the ability to install
|
| 326 |
+
modified object code on the User Product (for example, the work has
|
| 327 |
+
been installed in ROM).
|
| 328 |
+
|
| 329 |
+
The requirement to provide Installation Information does not include a
|
| 330 |
+
requirement to continue to provide support service, warranty, or updates
|
| 331 |
+
for a work that has been modified or installed by the recipient, or for
|
| 332 |
+
the User Product in which it has been modified or installed. Access to a
|
| 333 |
+
network may be denied when the modification itself materially and
|
| 334 |
+
adversely affects the operation of the network or violates the rules and
|
| 335 |
+
protocols for communication across the network.
|
| 336 |
+
|
| 337 |
+
Corresponding Source conveyed, and Installation Information provided,
|
| 338 |
+
in accord with this section must be in a format that is publicly
|
| 339 |
+
documented (and with an implementation available to the public in
|
| 340 |
+
source code form), and must require no special password or key for
|
| 341 |
+
unpacking, reading or copying.
|
| 342 |
+
|
| 343 |
+
7. Additional Terms.
|
| 344 |
+
|
| 345 |
+
"Additional permissions" are terms that supplement the terms of this
|
| 346 |
+
License by making exceptions from one or more of its conditions.
|
| 347 |
+
Additional permissions that are applicable to the entire Program shall
|
| 348 |
+
be treated as though they were included in this License, to the extent
|
| 349 |
+
that they are valid under applicable law. If additional permissions
|
| 350 |
+
apply only to part of the Program, that part may be used separately
|
| 351 |
+
under those permissions, but the entire Program remains governed by
|
| 352 |
+
this License without regard to the additional permissions.
|
| 353 |
+
|
| 354 |
+
When you convey a copy of a covered work, you may at your option
|
| 355 |
+
remove any additional permissions from that copy, or from any part of
|
| 356 |
+
it. (Additional permissions may be written to require their own
|
| 357 |
+
removal in certain cases when you modify the work.) You may place
|
| 358 |
+
additional permissions on material, added by you to a covered work,
|
| 359 |
+
for which you have or can give appropriate copyright permission.
|
| 360 |
+
|
| 361 |
+
Notwithstanding any other provision of this License, for material you
|
| 362 |
+
add to a covered work, you may (if authorized by the copyright holders of
|
| 363 |
+
that material) supplement the terms of this License with terms:
|
| 364 |
+
|
| 365 |
+
a) Disclaiming warranty or limiting liability differently from the
|
| 366 |
+
terms of sections 15 and 16 of this License; or
|
| 367 |
+
|
| 368 |
+
b) Requiring preservation of specified reasonable legal notices or
|
| 369 |
+
author attributions in that material or in the Appropriate Legal
|
| 370 |
+
Notices displayed by works containing it; or
|
| 371 |
+
|
| 372 |
+
c) Prohibiting misrepresentation of the origin of that material, or
|
| 373 |
+
requiring that modified versions of such material be marked in
|
| 374 |
+
reasonable ways as different from the original version; or
|
| 375 |
+
|
| 376 |
+
d) Limiting the use for publicity purposes of names of licensors or
|
| 377 |
+
authors of the material; or
|
| 378 |
+
|
| 379 |
+
e) Declining to grant rights under trademark law for use of some
|
| 380 |
+
trade names, trademarks, or service marks; or
|
| 381 |
+
|
| 382 |
+
f) Requiring indemnification of licensors and authors of that
|
| 383 |
+
material by anyone who conveys the material (or modified versions of
|
| 384 |
+
it) with contractual assumptions of liability to the recipient, for
|
| 385 |
+
any liability that these contractual assumptions directly impose on
|
| 386 |
+
those licensors and authors.
|
| 387 |
+
|
| 388 |
+
All other non-permissive additional terms are considered "further
|
| 389 |
+
restrictions" within the meaning of section 10. If the Program as you
|
| 390 |
+
received it, or any part of it, contains a notice stating that it is
|
| 391 |
+
governed by this License along with a term that is a further
|
| 392 |
+
restriction, you may remove that term. If a license document contains
|
| 393 |
+
a further restriction but permits relicensing or conveying under this
|
| 394 |
+
License, you may add to a covered work material governed by the terms
|
| 395 |
+
of that license document, provided that the further restriction does
|
| 396 |
+
not survive such relicensing or conveying.
|
| 397 |
+
|
| 398 |
+
If you add terms to a covered work in accord with this section, you
|
| 399 |
+
must place, in the relevant source files, a statement of the
|
| 400 |
+
additional terms that apply to those files, or a notice indicating
|
| 401 |
+
where to find the applicable terms.
|
| 402 |
+
|
| 403 |
+
Additional terms, permissive or non-permissive, may be stated in the
|
| 404 |
+
form of a separately written license, or stated as exceptions;
|
| 405 |
+
the above requirements apply either way.
|
| 406 |
+
|
| 407 |
+
8. Termination.
|
| 408 |
+
|
| 409 |
+
You may not propagate or modify a covered work except as expressly
|
| 410 |
+
provided under this License. Any attempt otherwise to propagate or
|
| 411 |
+
modify it is void, and will automatically terminate your rights under
|
| 412 |
+
this License (including any patent licenses granted under the third
|
| 413 |
+
paragraph of section 11).
|
| 414 |
+
|
| 415 |
+
However, if you cease all violation of this License, then your
|
| 416 |
+
license from a particular copyright holder is reinstated (a)
|
| 417 |
+
provisionally, unless and until the copyright holder explicitly and
|
| 418 |
+
finally terminates your license, and (b) permanently, if the copyright
|
| 419 |
+
holder fails to notify you of the violation by some reasonable means
|
| 420 |
+
prior to 60 days after the cessation.
|
| 421 |
+
|
| 422 |
+
Moreover, your license from a particular copyright holder is
|
| 423 |
+
reinstated permanently if the copyright holder notifies you of the
|
| 424 |
+
violation by some reasonable means, this is the first time you have
|
| 425 |
+
received notice of violation of this License (for any work) from that
|
| 426 |
+
copyright holder, and you cure the violation prior to 30 days after
|
| 427 |
+
your receipt of the notice.
|
| 428 |
+
|
| 429 |
+
Termination of your rights under this section does not terminate the
|
| 430 |
+
licenses of parties who have received copies or rights from you under
|
| 431 |
+
this License. If your rights have been terminated and not permanently
|
| 432 |
+
reinstated, you do not qualify to receive new licenses for the same
|
| 433 |
+
material under section 10.
|
| 434 |
+
|
| 435 |
+
9. Acceptance Not Required for Having Copies.
|
| 436 |
+
|
| 437 |
+
You are not required to accept this License in order to receive or
|
| 438 |
+
run a copy of the Program. Ancillary propagation of a covered work
|
| 439 |
+
occurring solely as a consequence of using peer-to-peer transmission
|
| 440 |
+
to receive a copy likewise does not require acceptance. However,
|
| 441 |
+
nothing other than this License grants you permission to propagate or
|
| 442 |
+
modify any covered work. These actions infringe copyright if you do
|
| 443 |
+
not accept this License. Therefore, by modifying or propagating a
|
| 444 |
+
covered work, you indicate your acceptance of this License to do so.
|
| 445 |
+
|
| 446 |
+
10. Automatic Licensing of Downstream Recipients.
|
| 447 |
+
|
| 448 |
+
Each time you convey a covered work, the recipient automatically
|
| 449 |
+
receives a license from the original licensors, to run, modify and
|
| 450 |
+
propagate that work, subject to this License. You are not responsible
|
| 451 |
+
for enforcing compliance by third parties with this License.
|
| 452 |
+
|
| 453 |
+
An "entity transaction" is a transaction transferring control of an
|
| 454 |
+
organization, or substantially all assets of one, or subdividing an
|
| 455 |
+
organization, or merging organizations. If propagation of a covered
|
| 456 |
+
work results from an entity transaction, each party to that
|
| 457 |
+
transaction who receives a copy of the work also receives whatever
|
| 458 |
+
licenses to the work the party's predecessor in interest had or could
|
| 459 |
+
give under the previous paragraph, plus a right to possession of the
|
| 460 |
+
Corresponding Source of the work from the predecessor in interest, if
|
| 461 |
+
the predecessor has it or can get it with reasonable efforts.
|
| 462 |
+
|
| 463 |
+
You may not impose any further restrictions on the exercise of the
|
| 464 |
+
rights granted or affirmed under this License. For example, you may
|
| 465 |
+
not impose a license fee, royalty, or other charge for exercise of
|
| 466 |
+
rights granted under this License, and you may not initiate litigation
|
| 467 |
+
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
| 468 |
+
any patent claim is infringed by making, using, selling, offering for
|
| 469 |
+
sale, or importing the Program or any portion of it.
|
| 470 |
+
|
| 471 |
+
11. Patents.
|
| 472 |
+
|
| 473 |
+
A "contributor" is a copyright holder who authorizes use under this
|
| 474 |
+
License of the Program or a work on which the Program is based. The
|
| 475 |
+
work thus licensed is called the contributor's "contributor version".
|
| 476 |
+
|
| 477 |
+
A contributor's "essential patent claims" are all patent claims
|
| 478 |
+
owned or controlled by the contributor, whether already acquired or
|
| 479 |
+
hereafter acquired, that would be infringed by some manner, permitted
|
| 480 |
+
by this License, of making, using, or selling its contributor version,
|
| 481 |
+
but do not include claims that would be infringed only as a
|
| 482 |
+
consequence of further modification of the contributor version. For
|
| 483 |
+
purposes of this definition, "control" includes the right to grant
|
| 484 |
+
patent sublicenses in a manner consistent with the requirements of
|
| 485 |
+
this License.
|
| 486 |
+
|
| 487 |
+
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
| 488 |
+
patent license under the contributor's essential patent claims, to
|
| 489 |
+
make, use, sell, offer for sale, import and otherwise run, modify and
|
| 490 |
+
propagate the contents of its contributor version.
|
| 491 |
+
|
| 492 |
+
In the following three paragraphs, a "patent license" is any express
|
| 493 |
+
agreement or commitment, however denominated, not to enforce a patent
|
| 494 |
+
(such as an express permission to practice a patent or covenant not to
|
| 495 |
+
sue for patent infringement). To "grant" such a patent license to a
|
| 496 |
+
party means to make such an agreement or commitment not to enforce a
|
| 497 |
+
patent against the party.
|
| 498 |
+
|
| 499 |
+
If you convey a covered work, knowingly relying on a patent license,
|
| 500 |
+
and the Corresponding Source of the work is not available for anyone
|
| 501 |
+
to copy, free of charge and under the terms of this License, through a
|
| 502 |
+
publicly available network server or other readily accessible means,
|
| 503 |
+
then you must either (1) cause the Corresponding Source to be so
|
| 504 |
+
available, or (2) arrange to deprive yourself of the benefit of the
|
| 505 |
+
patent license for this particular work, or (3) arrange, in a manner
|
| 506 |
+
consistent with the requirements of this License, to extend the patent
|
| 507 |
+
license to downstream recipients. "Knowingly relying" means you have
|
| 508 |
+
actual knowledge that, but for the patent license, your conveying the
|
| 509 |
+
covered work in a country, or your recipient's use of the covered work
|
| 510 |
+
in a country, would infringe one or more identifiable patents in that
|
| 511 |
+
country that you have reason to believe are valid.
|
| 512 |
+
|
| 513 |
+
If, pursuant to or in connection with a single transaction or
|
| 514 |
+
arrangement, you convey, or propagate by procuring conveyance of, a
|
| 515 |
+
covered work, and grant a patent license to some of the parties
|
| 516 |
+
receiving the covered work authorizing them to use, propagate, modify
|
| 517 |
+
or convey a specific copy of the covered work, then the patent license
|
| 518 |
+
you grant is automatically extended to all recipients of the covered
|
| 519 |
+
work and works based on it.
|
| 520 |
+
|
| 521 |
+
A patent license is "discriminatory" if it does not include within
|
| 522 |
+
the scope of its coverage, prohibits the exercise of, or is
|
| 523 |
+
conditioned on the non-exercise of one or more of the rights that are
|
| 524 |
+
specifically granted under this License. You may not convey a covered
|
| 525 |
+
work if you are a party to an arrangement with a third party that is
|
| 526 |
+
in the business of distributing software, under which you make payment
|
| 527 |
+
to the third party based on the extent of your activity of conveying
|
| 528 |
+
the work, and under which the third party grants, to any of the
|
| 529 |
+
parties who would receive the covered work from you, a discriminatory
|
| 530 |
+
patent license (a) in connection with copies of the covered work
|
| 531 |
+
conveyed by you (or copies made from those copies), or (b) primarily
|
| 532 |
+
for and in connection with specific products or compilations that
|
| 533 |
+
contain the covered work, unless you entered into that arrangement,
|
| 534 |
+
or that patent license was granted, prior to 28 March 2007.
|
| 535 |
+
|
| 536 |
+
Nothing in this License shall be construed as excluding or limiting
|
| 537 |
+
any implied license or other defenses to infringement that may
|
| 538 |
+
otherwise be available to you under applicable patent law.
|
| 539 |
+
|
| 540 |
+
12. No Surrender of Others' Freedom.
|
| 541 |
+
|
| 542 |
+
If conditions are imposed on you (whether by court order, agreement or
|
| 543 |
+
otherwise) that contradict the conditions of this License, they do not
|
| 544 |
+
excuse you from the conditions of this License. If you cannot convey a
|
| 545 |
+
covered work so as to satisfy simultaneously your obligations under this
|
| 546 |
+
License and any other pertinent obligations, then as a consequence you may
|
| 547 |
+
not convey it at all. For example, if you agree to terms that obligate you
|
| 548 |
+
to collect a royalty for further conveying from those to whom you convey
|
| 549 |
+
the Program, the only way you could satisfy both those terms and this
|
| 550 |
+
License would be to refrain entirely from conveying the Program.
|
| 551 |
+
|
| 552 |
+
13. Use with the GNU Affero General Public License.
|
| 553 |
+
|
| 554 |
+
Notwithstanding any other provision of this License, you have
|
| 555 |
+
permission to link or combine any covered work with a work licensed
|
| 556 |
+
under version 3 of the GNU Affero General Public License into a single
|
| 557 |
+
combined work, and to convey the resulting work. The terms of this
|
| 558 |
+
License will continue to apply to the part which is the covered work,
|
| 559 |
+
but the special requirements of the GNU Affero General Public License,
|
| 560 |
+
section 13, concerning interaction through a network will apply to the
|
| 561 |
+
combination as such.
|
| 562 |
+
|
| 563 |
+
14. Revised Versions of this License.
|
| 564 |
+
|
| 565 |
+
The Free Software Foundation may publish revised and/or new versions of
|
| 566 |
+
the GNU General Public License from time to time. Such new versions will
|
| 567 |
+
be similar in spirit to the present version, but may differ in detail to
|
| 568 |
+
address new problems or concerns.
|
| 569 |
+
|
| 570 |
+
Each version is given a distinguishing version number. If the
|
| 571 |
+
Program specifies that a certain numbered version of the GNU General
|
| 572 |
+
Public License "or any later version" applies to it, you have the
|
| 573 |
+
option of following the terms and conditions either of that numbered
|
| 574 |
+
version or of any later version published by the Free Software
|
| 575 |
+
Foundation. If the Program does not specify a version number of the
|
| 576 |
+
GNU General Public License, you may choose any version ever published
|
| 577 |
+
by the Free Software Foundation.
|
| 578 |
+
|
| 579 |
+
If the Program specifies that a proxy can decide which future
|
| 580 |
+
versions of the GNU General Public License can be used, that proxy's
|
| 581 |
+
public statement of acceptance of a version permanently authorizes you
|
| 582 |
+
to choose that version for the Program.
|
| 583 |
+
|
| 584 |
+
Later license versions may give you additional or different
|
| 585 |
+
permissions. However, no additional obligations are imposed on any
|
| 586 |
+
author or copyright holder as a result of your choosing to follow a
|
| 587 |
+
later version.
|
| 588 |
+
|
| 589 |
+
15. Disclaimer of Warranty.
|
| 590 |
+
|
| 591 |
+
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
| 592 |
+
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
| 593 |
+
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
| 594 |
+
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
| 595 |
+
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
| 596 |
+
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
| 597 |
+
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
| 598 |
+
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
| 599 |
+
|
| 600 |
+
16. Limitation of Liability.
|
| 601 |
+
|
| 602 |
+
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
| 603 |
+
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
| 604 |
+
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
| 605 |
+
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
| 606 |
+
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
| 607 |
+
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
| 608 |
+
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
| 609 |
+
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
| 610 |
+
SUCH DAMAGES.
|
| 611 |
+
|
| 612 |
+
17. Interpretation of Sections 15 and 16.
|
| 613 |
+
|
| 614 |
+
If the disclaimer of warranty and limitation of liability provided
|
| 615 |
+
above cannot be given local legal effect according to their terms,
|
| 616 |
+
reviewing courts shall apply local law that most closely approximates
|
| 617 |
+
an absolute waiver of all civil liability in connection with the
|
| 618 |
+
Program, unless a warranty or assumption of liability accompanies a
|
| 619 |
+
copy of the Program in return for a fee.
|
| 620 |
+
|
| 621 |
+
END OF TERMS AND CONDITIONS
|
| 622 |
+
|
| 623 |
+
How to Apply These Terms to Your New Programs
|
| 624 |
+
|
| 625 |
+
If you develop a new program, and you want it to be of the greatest
|
| 626 |
+
possible use to the public, the best way to achieve this is to make it
|
| 627 |
+
free software which everyone can redistribute and change under these terms.
|
| 628 |
+
|
| 629 |
+
To do so, attach the following notices to the program. It is safest
|
| 630 |
+
to attach them to the start of each source file to most effectively
|
| 631 |
+
state the exclusion of warranty; and each file should have at least
|
| 632 |
+
the "copyright" line and a pointer to where the full notice is found.
|
| 633 |
+
|
| 634 |
+
<one line to give the program's name and a brief idea of what it does.>
|
| 635 |
+
Copyright (C) <year> <name of author>
|
| 636 |
+
|
| 637 |
+
This program is free software: you can redistribute it and/or modify
|
| 638 |
+
it under the terms of the GNU General Public License as published by
|
| 639 |
+
the Free Software Foundation, either version 3 of the License, or
|
| 640 |
+
(at your option) any later version.
|
| 641 |
+
|
| 642 |
+
This program is distributed in the hope that it will be useful,
|
| 643 |
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 644 |
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 645 |
+
GNU General Public License for more details.
|
| 646 |
+
|
| 647 |
+
You should have received a copy of the GNU General Public License
|
| 648 |
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 649 |
+
|
| 650 |
+
Also add information on how to contact you by electronic and paper mail.
|
| 651 |
+
|
| 652 |
+
If the program does terminal interaction, make it output a short
|
| 653 |
+
notice like this when it starts in an interactive mode:
|
| 654 |
+
|
| 655 |
+
<program> Copyright (C) <year> <name of author>
|
| 656 |
+
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
| 657 |
+
This is free software, and you are welcome to redistribute it
|
| 658 |
+
under certain conditions; type `show c' for details.
|
| 659 |
+
|
| 660 |
+
The hypothetical commands `show w' and `show c' should show the appropriate
|
| 661 |
+
parts of the General Public License. Of course, your program's commands
|
| 662 |
+
might be different; for a GUI interface, you would use an "about box".
|
| 663 |
+
|
| 664 |
+
You should also get your employer (if you work as a programmer) or school,
|
| 665 |
+
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
| 666 |
+
For more information on this, and how to apply and follow the GNU GPL, see
|
| 667 |
+
<http://www.gnu.org/licenses/>.
|
| 668 |
+
|
| 669 |
+
The GNU General Public License does not permit incorporating your program
|
| 670 |
+
into proprietary programs. If your program is a subroutine library, you
|
| 671 |
+
may consider it more useful to permit linking proprietary applications with
|
| 672 |
+
the library. If this is what you want to do, use the GNU Lesser General
|
| 673 |
+
Public License instead of this License. But first, please read
|
| 674 |
+
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
|
runtime/NOTICE.txt
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
WeightProgramBench source notices
|
| 2 |
+
|
| 3 |
+
The 317 synthetic game implementations come from Trajectory Labs, Jerry Chan,
|
| 4 |
+
and contributors to https://github.com/Trajectorylabs/minatar-synth at commit
|
| 5 |
+
4038830175c4201ca10e004ed4441985f2acddf2. That pinned repository does not declare a
|
| 6 |
+
license for the converted implementations; this notice grants none.
|
| 7 |
+
|
| 8 |
+
266 games adapt designs and source credited to ABA Games, with portable SDL/C
|
| 9 |
+
versions credited to Joyrider3774 and contributors. The original collections
|
| 10 |
+
are https://github.com/abagames/crisp-game-lib-games,
|
| 11 |
+
https://github.com/abagames/crisp-game-lib-11-games, and
|
| 12 |
+
https://github.com/joyrider3774/crisp-game-lib-portable-sdl. Their applicable MIT
|
| 13 |
+
copyright and permission notices are preserved in LICENSE-Crisp.
|
| 14 |
+
|
| 15 |
+
The five original MinAtar games use MinAtar 1.0.15, by Kenny Young and Tian Tian:
|
| 16 |
+
https://github.com/kenjyoung/MinAtar. GPL-3.0 MinAtar source is embedded in the
|
| 17 |
+
original-game prompts as well as installed as a dependency. LICENSE-MinAtar
|
| 18 |
+
contains the full GPL text. This material is not relicensed by the cookbook.
|
| 19 |
+
|
| 20 |
+
The weight-space programming experiment is credited to Rikiya Takehi,
|
| 21 |
+
Jayden Teoh, Alex Zhang, and Omar Khattab:
|
| 22 |
+
https://rikiyat.github.io/blog/weight-space-programming/
|
| 23 |
+
Experiment prompt/log materials are attributed under CC BY 4.0:
|
| 24 |
+
https://creativecommons.org/licenses/by/4.0/
|
| 25 |
+
The expanded corpus and subsequent model training are separate adaptations.
|
| 26 |
+
|
| 27 |
+
The example's THIRD_PARTY_NOTICES.md and TASK_ORIGINS.json provide the complete
|
| 28 |
+
source mapping, pinned revisions, and prompt and implementation digests.
|
runtime/minatar_synth/games/accel_b.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
HORIZON = 400
|
| 7 |
+
MIN_SPAWN_STEPS = 4
|
| 8 |
+
MAX_MULTIPLIER = 64
|
| 9 |
+
PLAYER_ROW = 4
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@dataclass
|
| 13 |
+
class Target:
|
| 14 |
+
row: int
|
| 15 |
+
column: int
|
| 16 |
+
fire_in: int = 0
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@dataclass
|
| 20 |
+
class HomingShot:
|
| 21 |
+
row: int
|
| 22 |
+
column: int
|
| 23 |
+
target: Target
|
| 24 |
+
age: int = 0
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class AccelBEnvironment:
|
| 28 |
+
score_cap = (HORIZON // MIN_SPAWN_STEPS) * 2 * MAX_MULTIPLIER
|
| 29 |
+
|
| 30 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 31 |
+
self.sticky_action_prob = sticky_action_prob
|
| 32 |
+
self.horizon = min(horizon, HORIZON)
|
| 33 |
+
self.seed(0)
|
| 34 |
+
self.reset()
|
| 35 |
+
|
| 36 |
+
def seed(self, seed: int) -> None:
|
| 37 |
+
self.random = np.random.default_rng(seed)
|
| 38 |
+
|
| 39 |
+
def reset(self) -> None:
|
| 40 |
+
self.steps = self.last_action = self.throttle = 0
|
| 41 |
+
self.spawn_in = MIN_SPAWN_STEPS
|
| 42 |
+
self.multiplier = 1
|
| 43 |
+
self.enemies: list[Target] = []
|
| 44 |
+
self.hostile: list[Target] = []
|
| 45 |
+
self.shots: list[HomingShot] = []
|
| 46 |
+
self.blasts: list[tuple[int, int, int]] = []
|
| 47 |
+
self.terminal = False
|
| 48 |
+
|
| 49 |
+
@property
|
| 50 |
+
def column(self) -> int:
|
| 51 |
+
return 1 + self.throttle // 2
|
| 52 |
+
|
| 53 |
+
@classmethod
|
| 54 |
+
def normalize_score(cls, score: float) -> float:
|
| 55 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 56 |
+
|
| 57 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 58 |
+
if self.terminal:
|
| 59 |
+
return 0.0, True
|
| 60 |
+
if self.random.random() < self.sticky_action_prob:
|
| 61 |
+
action = self.last_action
|
| 62 |
+
self.steps += 1
|
| 63 |
+
pressed = action == 5 and self.last_action != 5
|
| 64 |
+
self.last_action = action
|
| 65 |
+
self.throttle = min(6, self.throttle + 1) if action == 5 else max(0, self.throttle - 1)
|
| 66 |
+
if pressed and not (self.shots or self.blasts):
|
| 67 |
+
self.multiplier = 1
|
| 68 |
+
self.shots = [HomingShot(PLAYER_ROW, self.column, target)
|
| 69 |
+
for target in (*self.enemies, *self.hostile)]
|
| 70 |
+
self._advance_targets()
|
| 71 |
+
self._advance_salvo()
|
| 72 |
+
reward = 0
|
| 73 |
+
for targets in (self.enemies, self.hostile):
|
| 74 |
+
remaining = []
|
| 75 |
+
for target in targets:
|
| 76 |
+
if any(abs(target.row - row) <= 1 and abs(target.column - column) <= 1
|
| 77 |
+
for row, column, _ in self.blasts):
|
| 78 |
+
reward += self.multiplier
|
| 79 |
+
self.multiplier = min(MAX_MULTIPLIER, 2 * self.multiplier)
|
| 80 |
+
elif target.column >= 0:
|
| 81 |
+
remaining.append(target)
|
| 82 |
+
targets[:] = remaining
|
| 83 |
+
self.terminal = any((missile.row, missile.column) == (PLAYER_ROW, self.column)
|
| 84 |
+
for missile in self.hostile) or self.steps >= self.horizon
|
| 85 |
+
self.spawn_in -= 1
|
| 86 |
+
if self.spawn_in == 0 and not self.terminal:
|
| 87 |
+
self.enemies.append(Target(int(self.random.choice((1, 2, 3, 5, 6, 7, 8))), 9, 3))
|
| 88 |
+
self.spawn_in = max(MIN_SPAWN_STEPS, 10 - self.throttle)
|
| 89 |
+
return float(reward), self.terminal
|
| 90 |
+
|
| 91 |
+
def _advance_targets(self) -> None:
|
| 92 |
+
for enemy in self.enemies:
|
| 93 |
+
if self.throttle >= 3 or self.steps % 3 == 0:
|
| 94 |
+
enemy.column -= 1
|
| 95 |
+
enemy.fire_in -= 1
|
| 96 |
+
if enemy.fire_in == 0 and enemy.column >= 0:
|
| 97 |
+
self.hostile.append(Target(enemy.row, enemy.column))
|
| 98 |
+
for missile in self.hostile:
|
| 99 |
+
missile.column -= 1
|
| 100 |
+
if self.steps % 2 == 0:
|
| 101 |
+
missile.row += int(np.sign(PLAYER_ROW - missile.row))
|
| 102 |
+
|
| 103 |
+
def _advance_salvo(self) -> None:
|
| 104 |
+
self.blasts = [(row, column, timer - 1)
|
| 105 |
+
for row, column, timer in self.blasts if timer > 1]
|
| 106 |
+
remaining = []
|
| 107 |
+
for shot in self.shots:
|
| 108 |
+
shot.age += 1
|
| 109 |
+
shot.column += 1 if shot.age == 1 else int(np.sign(shot.target.column - shot.column))
|
| 110 |
+
if shot.age > 1:
|
| 111 |
+
shot.row += int(np.sign(shot.target.row - shot.row))
|
| 112 |
+
distance = max(abs(shot.row - shot.target.row), abs(shot.column - shot.target.column))
|
| 113 |
+
if distance <= 1 or shot.age >= 12:
|
| 114 |
+
self.blasts.append((shot.row, max(0, min(9, shot.column)), 3))
|
| 115 |
+
else:
|
| 116 |
+
remaining.append(shot)
|
| 117 |
+
self.shots = remaining
|
| 118 |
+
|
| 119 |
+
def state(self) -> np.ndarray:
|
| 120 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 121 |
+
state[PLAYER_ROW, self.column, 0] = True
|
| 122 |
+
for channel, targets in ((1, self.hostile), (2, self.enemies), (3, self.shots)):
|
| 123 |
+
for target in targets:
|
| 124 |
+
if 0 <= target.column < 10:
|
| 125 |
+
state[target.row, target.column, channel] = True
|
| 126 |
+
for row, column, _ in self.blasts:
|
| 127 |
+
state[max(0, row - 1):row + 2, max(0, column - 1):column + 2, 4] = True
|
| 128 |
+
values = (self.steps, self.throttle, self.spawn_in, self.multiplier,
|
| 129 |
+
int(self.last_action == 5), int(not (self.shots or self.blasts)))
|
| 130 |
+
for row, value in enumerate(values):
|
| 131 |
+
for bit in range(9):
|
| 132 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 133 |
+
return state
|
runtime/minatar_synth/games/adventure.py
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class AdventureEnvironment:
|
| 5 |
+
"""Compact Adventure: rooms, keys, carried objects, dragon, bat, and chalice return."""
|
| 6 |
+
|
| 7 |
+
score_cap = 1.0
|
| 8 |
+
|
| 9 |
+
@classmethod
|
| 10 |
+
def normalize_score(cls, raw_score: float) -> float:
|
| 11 |
+
return min(max(raw_score, 0.0), cls.score_cap) / cls.score_cap
|
| 12 |
+
|
| 13 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = 1_000):
|
| 14 |
+
self.sticky_action_prob = sticky_action_prob
|
| 15 |
+
self.horizon = horizon
|
| 16 |
+
self.random = np.random.RandomState()
|
| 17 |
+
self.seed(0)
|
| 18 |
+
self.reset()
|
| 19 |
+
|
| 20 |
+
def seed(self, seed: int) -> None:
|
| 21 |
+
self.random.seed(seed)
|
| 22 |
+
|
| 23 |
+
def reset(self) -> None:
|
| 24 |
+
self.room = 0
|
| 25 |
+
self.player = [8, 5]
|
| 26 |
+
self.held = None
|
| 27 |
+
self.items = {
|
| 28 |
+
"gold_key": [0, 5, 7], "black_key": [2, 7, 7], "white_key": [4, 2, 2],
|
| 29 |
+
"sword": [1, 7, 2], "chalice": [5, 2, 7], "bridge": [3, 6, 2],
|
| 30 |
+
"magnet": [5, 7, 2],
|
| 31 |
+
}
|
| 32 |
+
self.dragons = [[2, 5, 5], [3, 3, 7], [5, 6, 6]]
|
| 33 |
+
self.previous_dragons = [dragon.copy() for dragon in self.dragons]
|
| 34 |
+
self.bat = [1, 2, 8]
|
| 35 |
+
self.previous_bat = self.bat.copy()
|
| 36 |
+
self.castles = {
|
| 37 |
+
0: {"key": "gold_key", "open": False},
|
| 38 |
+
2: {"key": "black_key", "open": False},
|
| 39 |
+
4: {"key": "white_key", "open": False},
|
| 40 |
+
}
|
| 41 |
+
self.dragon_alive = [True, True, True]
|
| 42 |
+
self.last_action = 0
|
| 43 |
+
self.steps = 0
|
| 44 |
+
self.terminal = False
|
| 45 |
+
|
| 46 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 47 |
+
if self.terminal:
|
| 48 |
+
return 0.0, True
|
| 49 |
+
if self.random.rand() < self.sticky_action_prob:
|
| 50 |
+
action = self.last_action
|
| 51 |
+
self.last_action = action
|
| 52 |
+
dropped = False
|
| 53 |
+
if action in (1, 2, 3, 4):
|
| 54 |
+
dy, dx = {1: (0, -1), 2: (-1, 0), 3: (0, 1), 4: (1, 0)}[action]
|
| 55 |
+
target = [min(9, max(0, self.player[0] + dy)), min(9, max(0, self.player[1] + dx))]
|
| 56 |
+
if self._can_move(target):
|
| 57 |
+
self.player = target
|
| 58 |
+
self._change_room()
|
| 59 |
+
elif action == 5 and self.held is not None:
|
| 60 |
+
self.items[self.held] = [self.room, *self.player]
|
| 61 |
+
self.held = None
|
| 62 |
+
dropped = True
|
| 63 |
+
|
| 64 |
+
if not dropped:
|
| 65 |
+
self._collect_item()
|
| 66 |
+
self._use_magnet()
|
| 67 |
+
self.previous_dragons = [dragon.copy() for dragon in self.dragons]
|
| 68 |
+
self.previous_bat = self.bat.copy()
|
| 69 |
+
self._move_dragons()
|
| 70 |
+
self._move_bat()
|
| 71 |
+
castle = self.castles.get(self.room)
|
| 72 |
+
if castle and self.player[0] <= 1 and self.held == castle["key"]:
|
| 73 |
+
castle["open"] = True
|
| 74 |
+
if self.room == 0 and self.player[0] <= 1 and self.held == "chalice" and self.castles[0]["open"]:
|
| 75 |
+
self.terminal = True
|
| 76 |
+
return 1.0, True
|
| 77 |
+
self.steps += 1
|
| 78 |
+
if self.steps >= self.horizon:
|
| 79 |
+
self.terminal = True
|
| 80 |
+
return 0.0, self.terminal
|
| 81 |
+
|
| 82 |
+
def _can_move(self, target: list[int]) -> bool:
|
| 83 |
+
if self.room != 3 or {self.player[0], target[0]} != {4, 5}:
|
| 84 |
+
return True
|
| 85 |
+
bridge = self.items.get("bridge")
|
| 86 |
+
bridge_column = bridge[2] if bridge is not None and bridge[0] == 3 else None
|
| 87 |
+
return self.held == "bridge" or bridge_column == target[1]
|
| 88 |
+
|
| 89 |
+
def _change_room(self) -> None:
|
| 90 |
+
if self.player[1] == 0 and self.room % 2 == 1:
|
| 91 |
+
self.room -= 1
|
| 92 |
+
self.player[1] = 9
|
| 93 |
+
elif self.player[1] == 9 and self.room % 2 == 0:
|
| 94 |
+
self.room += 1
|
| 95 |
+
self.player[1] = 0
|
| 96 |
+
elif self.player[0] == 0 and self.room >= 2:
|
| 97 |
+
self.room -= 2
|
| 98 |
+
self.player[0] = 9
|
| 99 |
+
elif self.player[0] == 9 and self.room < 4:
|
| 100 |
+
self.room += 2
|
| 101 |
+
self.player[0] = 0
|
| 102 |
+
|
| 103 |
+
def _collect_item(self) -> None:
|
| 104 |
+
for name, location in list(self.items.items()):
|
| 105 |
+
if location == [self.room, *self.player] and self.held is None:
|
| 106 |
+
self.held = name
|
| 107 |
+
del self.items[name]
|
| 108 |
+
castle = self.castles.get(self.room)
|
| 109 |
+
if castle and self.held == castle["key"] and self.player[0] <= 1:
|
| 110 |
+
castle["open"] = True
|
| 111 |
+
|
| 112 |
+
def _move_dragons(self) -> None:
|
| 113 |
+
for index, dragon in enumerate(self.dragons):
|
| 114 |
+
if not self.dragon_alive[index] or dragon[0] != self.room:
|
| 115 |
+
continue
|
| 116 |
+
_, row, column = dragon
|
| 117 |
+
row += int(np.sign(self.player[0] - row))
|
| 118 |
+
column += int(np.sign(self.player[1] - column))
|
| 119 |
+
dragon[1:] = [row, column]
|
| 120 |
+
if [row, column] == self.player:
|
| 121 |
+
if self.held == "sword":
|
| 122 |
+
self.dragon_alive[index] = False
|
| 123 |
+
else:
|
| 124 |
+
self.terminal = True
|
| 125 |
+
|
| 126 |
+
def _use_magnet(self) -> None:
|
| 127 |
+
if self.held != "magnet":
|
| 128 |
+
return
|
| 129 |
+
candidates = [(name, location) for name, location in self.items.items() if location[0] == self.room]
|
| 130 |
+
if not candidates:
|
| 131 |
+
return
|
| 132 |
+
_, location = min(candidates, key=lambda item: abs(item[1][1] - self.player[0]) + abs(item[1][2] - self.player[1]))
|
| 133 |
+
location[1] += int(np.sign(self.player[0] - location[1]))
|
| 134 |
+
location[2] += int(np.sign(self.player[1] - location[2]))
|
| 135 |
+
|
| 136 |
+
def _move_bat(self) -> None:
|
| 137 |
+
if self.random.rand() >= 0.15:
|
| 138 |
+
return
|
| 139 |
+
self.bat[0] = int(self.random.randint(6))
|
| 140 |
+
self.bat[1] = int(self.random.randint(10))
|
| 141 |
+
self.bat[2] = int(self.random.randint(10))
|
| 142 |
+
if self.bat == [self.room, *self.player] and self.held is not None:
|
| 143 |
+
name = self.held
|
| 144 |
+
self.held = None
|
| 145 |
+
self.items[name] = self.bat.copy()
|
| 146 |
+
|
| 147 |
+
def state(self) -> np.ndarray:
|
| 148 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 149 |
+
state[*self.player, 0] = True
|
| 150 |
+
for name, (room, row, column) in self.items.items():
|
| 151 |
+
if room == self.room:
|
| 152 |
+
if name == "chalice":
|
| 153 |
+
state[row, column, 2] = True
|
| 154 |
+
else:
|
| 155 |
+
state[row, column, 3] = True
|
| 156 |
+
marker = {
|
| 157 |
+
"gold_key": 0,
|
| 158 |
+
"black_key": 1,
|
| 159 |
+
"white_key": 2,
|
| 160 |
+
"sword": 3,
|
| 161 |
+
"bridge": 4,
|
| 162 |
+
"magnet": 5,
|
| 163 |
+
}[name]
|
| 164 |
+
state[row, marker, 5] = True
|
| 165 |
+
for alive, dragon in zip(self.dragon_alive, self.dragons, strict=True):
|
| 166 |
+
if alive and dragon[0] == self.room:
|
| 167 |
+
state[dragon[1], dragon[2], 1] = True
|
| 168 |
+
for previous, alive in zip(self.previous_dragons, self.dragon_alive, strict=True):
|
| 169 |
+
if alive and previous[0] == self.room:
|
| 170 |
+
state[previous[1], previous[2], 4] = True
|
| 171 |
+
if self.bat[0] == self.room:
|
| 172 |
+
state[self.bat[1], self.bat[2], 1] = True
|
| 173 |
+
if self.previous_bat[0] == self.room:
|
| 174 |
+
state[self.previous_bat[1], self.previous_bat[2], 4] = True
|
| 175 |
+
if self.room == 3:
|
| 176 |
+
state[4, :, 1] = True
|
| 177 |
+
bridge = self.items.get("bridge")
|
| 178 |
+
if self.held == "bridge":
|
| 179 |
+
state[4, self.player[1], 1] = False
|
| 180 |
+
elif bridge is not None and bridge[0] == 3:
|
| 181 |
+
state[4, bridge[2], 1] = False
|
| 182 |
+
if self.room in self.castles:
|
| 183 |
+
state[0, 4:7, 1] = not self.castles[self.room]["open"]
|
| 184 |
+
if self.held is not None:
|
| 185 |
+
held_marker = {
|
| 186 |
+
"gold_key": 0,
|
| 187 |
+
"black_key": 1,
|
| 188 |
+
"white_key": 2,
|
| 189 |
+
"sword": 3,
|
| 190 |
+
"chalice": 4,
|
| 191 |
+
"bridge": 5,
|
| 192 |
+
"magnet": 6,
|
| 193 |
+
}[self.held]
|
| 194 |
+
state[8, held_marker, 5] = True
|
| 195 |
+
state[9, self.room, 5] = True
|
| 196 |
+
for castle_room, castle in self.castles.items():
|
| 197 |
+
if castle["open"]:
|
| 198 |
+
state[7, castle_room, 5] = True
|
| 199 |
+
return state
|
runtime/minatar_synth/games/aerial_bar.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from math import cos, pi, sin
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
UNIT = 8
|
| 9 |
+
WATER = 72
|
| 10 |
+
PERIOD = 32
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@dataclass
|
| 14 |
+
class Bar:
|
| 15 |
+
anchor: int
|
| 16 |
+
length: int
|
| 17 |
+
phase: int
|
| 18 |
+
used: bool = False
|
| 19 |
+
|
| 20 |
+
def tip(self, ceiling: int) -> tuple[int, int]:
|
| 21 |
+
angle = 0.85 * sin(self.phase * 2 * pi / PERIOD)
|
| 22 |
+
return self.anchor + round(self.length * sin(angle)), ceiling + round(self.length * cos(angle))
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class AerialBarEnvironment:
|
| 26 |
+
score_cap = HORIZON
|
| 27 |
+
|
| 28 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 29 |
+
self.sticky_action_prob = sticky_action_prob
|
| 30 |
+
self.horizon = min(horizon, HORIZON)
|
| 31 |
+
self.seed(0)
|
| 32 |
+
self.reset()
|
| 33 |
+
|
| 34 |
+
def seed(self, seed: int) -> None:
|
| 35 |
+
self.random = np.random.default_rng(seed)
|
| 36 |
+
|
| 37 |
+
def reset(self) -> None:
|
| 38 |
+
self.steps = self.last_action = self.flight = 0
|
| 39 |
+
self.ceiling = 8
|
| 40 |
+
self.attached: Bar | None = Bar(32, 38, 0, True)
|
| 41 |
+
self.bars = [self.attached, Bar(66, int(self.random.integers(25, 34)), 0)]
|
| 42 |
+
self.x, self.y = self.attached.tip(self.ceiling)
|
| 43 |
+
self.vx = self.vy = 0
|
| 44 |
+
self.bounced = False
|
| 45 |
+
self.terminal = False
|
| 46 |
+
|
| 47 |
+
@classmethod
|
| 48 |
+
def normalize_score(cls, score: float) -> float:
|
| 49 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 50 |
+
|
| 51 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 52 |
+
if self.terminal:
|
| 53 |
+
return 0.0, True
|
| 54 |
+
if self.random.random() < self.sticky_action_prob:
|
| 55 |
+
action = self.last_action
|
| 56 |
+
pressed = action == 5 and self.last_action != 5
|
| 57 |
+
self.last_action = action
|
| 58 |
+
self.steps += 1
|
| 59 |
+
scroll = int(self.steps % 8 == 0) + max(0, (self.x - 40) // 8)
|
| 60 |
+
self.x -= scroll
|
| 61 |
+
for bar in self.bars:
|
| 62 |
+
bar.anchor -= scroll
|
| 63 |
+
bar.phase = (bar.phase + 1) % PERIOD
|
| 64 |
+
if self.bars[-1].anchor < 55:
|
| 65 |
+
self.bars.append(Bar(self.bars[-1].anchor + int(self.random.integers(26, 42)),
|
| 66 |
+
int(self.random.integers(18, 39)), int(self.random.integers(PERIOD))))
|
| 67 |
+
|
| 68 |
+
reward = 0
|
| 69 |
+
if self.attached is not None:
|
| 70 |
+
bar = self.attached
|
| 71 |
+
self.x, self.y = bar.tip(self.ceiling)
|
| 72 |
+
if pressed:
|
| 73 |
+
future = Bar(bar.anchor, bar.length, (bar.phase + 1) % PERIOD)
|
| 74 |
+
next_x, next_y = future.tip(self.ceiling)
|
| 75 |
+
self.vx, self.vy = next_x - self.x, next_y - self.y - 1
|
| 76 |
+
self.attached = None
|
| 77 |
+
self.flight = 0
|
| 78 |
+
self.bounced = False
|
| 79 |
+
else:
|
| 80 |
+
self.flight += 1
|
| 81 |
+
self.x += self.vx
|
| 82 |
+
self.y += self.vy
|
| 83 |
+
if action != 5 or self.steps % 5 == 0:
|
| 84 |
+
self.vy += 1
|
| 85 |
+
if action != 5 and self.steps % 4 == 0:
|
| 86 |
+
self.vx -= int(np.sign(self.vx))
|
| 87 |
+
if self.y >= WATER:
|
| 88 |
+
self.y = WATER - 1
|
| 89 |
+
self.vy = -max(1, self.vy * 3 // 2)
|
| 90 |
+
self.ceiling += 8
|
| 91 |
+
self.bounced = True
|
| 92 |
+
for bar in self.bars:
|
| 93 |
+
bar.used = False
|
| 94 |
+
if self.y <= self.ceiling:
|
| 95 |
+
self.y = self.ceiling
|
| 96 |
+
self.vy = max(0, -self.vy // 2)
|
| 97 |
+
for bar in self.bars:
|
| 98 |
+
x, y = bar.tip(self.ceiling)
|
| 99 |
+
if not bar.used and abs(self.x - x) <= 5 and abs(self.y - y) <= 5:
|
| 100 |
+
self.attached = bar
|
| 101 |
+
bar.used = True
|
| 102 |
+
reward = 0 if self.bounced else self.flight
|
| 103 |
+
self.ceiling = max(8, self.ceiling - 4)
|
| 104 |
+
self.x, self.y = bar.tip(self.ceiling)
|
| 105 |
+
break
|
| 106 |
+
self.bars = [bar for bar in self.bars if bar.anchor >= -24 or bar is self.attached]
|
| 107 |
+
self.terminal = (
|
| 108 |
+
(self.attached.anchor < 0 if self.attached is not None else self.x < 0)
|
| 109 |
+
or any(bar.tip(self.ceiling)[1] >= WATER for bar in self.bars)
|
| 110 |
+
or self.steps >= self.horizon
|
| 111 |
+
)
|
| 112 |
+
return float(reward), self.terminal
|
| 113 |
+
|
| 114 |
+
def state(self) -> np.ndarray:
|
| 115 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 116 |
+
state[min(9, self.y // UNIT), max(0, min(9, self.x // UNIT)), 0] = True
|
| 117 |
+
state[WATER // UNIT:, :, 1] = True
|
| 118 |
+
for bar in self.bars:
|
| 119 |
+
x, y = bar.tip(self.ceiling)
|
| 120 |
+
if 0 <= x < 80 and 0 <= y < 80:
|
| 121 |
+
state[y // UNIT, x // UNIT, 3 if bar.used else 2] = True
|
| 122 |
+
state[:min(10, self.ceiling // UNIT), :, 4] = True
|
| 123 |
+
values = (self.steps, self.flight, self.ceiling, self.x + 32, self.y,
|
| 124 |
+
self.vx + 16, self.vy + 64, int(self.attached is not None),
|
| 125 |
+
int(self.bounced), int(self.last_action == 5))
|
| 126 |
+
for row, value in enumerate(values):
|
| 127 |
+
for bit in range(10):
|
| 128 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 129 |
+
return state
|
runtime/minatar_synth/games/alien.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
MAZE = np.array([
|
| 5 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
| 6 |
+
[1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
|
| 7 |
+
[1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
|
| 8 |
+
[1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
|
| 9 |
+
[1, 0, 1, 1, 1, 1, 0, 1, 0, 1],
|
| 10 |
+
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
| 11 |
+
[1, 1, 0, 1, 1, 0, 1, 1, 0, 1],
|
| 12 |
+
[1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
|
| 13 |
+
[1, 0, 1, 0, 0, 0, 1, 0, 0, 1],
|
| 14 |
+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
| 15 |
+
], dtype=bool)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class AlienEnvironment:
|
| 19 |
+
"""Maze pursuit with eggs, pulsars, and three vulnerable Alien enemies."""
|
| 20 |
+
|
| 21 |
+
score_cap = 35.0
|
| 22 |
+
|
| 23 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = 500):
|
| 24 |
+
self.sticky_action_prob = sticky_action_prob
|
| 25 |
+
self.horizon = horizon
|
| 26 |
+
self.random = np.random.RandomState()
|
| 27 |
+
self.seed(0)
|
| 28 |
+
self.reset()
|
| 29 |
+
|
| 30 |
+
@classmethod
|
| 31 |
+
def normalize_score(cls, raw_score: float) -> float:
|
| 32 |
+
return min(max(raw_score, 0.0), cls.score_cap) / cls.score_cap
|
| 33 |
+
|
| 34 |
+
def seed(self, seed: int) -> None:
|
| 35 |
+
self.random.seed(seed)
|
| 36 |
+
|
| 37 |
+
def reset(self) -> None:
|
| 38 |
+
self.player = [8, 5]
|
| 39 |
+
self.previous_player = self.player.copy()
|
| 40 |
+
self.aliens = [[1, 1], [1, 8], [7, 1]]
|
| 41 |
+
self.previous_aliens = [position.copy() for position in self.aliens]
|
| 42 |
+
passages = [tuple(position) for position in np.argwhere(~MAZE)]
|
| 43 |
+
reserved = {tuple(self.player), *(tuple(position) for position in self.aliens)}
|
| 44 |
+
self.pulsars = {(3, 3), (5, 8), (8, 1)}
|
| 45 |
+
self.eggs = {position for position in passages if position not in reserved | self.pulsars}
|
| 46 |
+
self.eggs = set(sorted(self.eggs)[:20])
|
| 47 |
+
self.power_timer = 0
|
| 48 |
+
self.last_action = 0
|
| 49 |
+
self.steps = 0
|
| 50 |
+
self.terminal = False
|
| 51 |
+
|
| 52 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 53 |
+
if self.terminal:
|
| 54 |
+
return 0.0, True
|
| 55 |
+
if self.random.rand() < self.sticky_action_prob:
|
| 56 |
+
action = self.last_action
|
| 57 |
+
self.last_action = action
|
| 58 |
+
self.previous_player = self.player.copy()
|
| 59 |
+
delta = {1: (0, -1), 2: (-1, 0), 3: (0, 1), 4: (1, 0)}.get(action, (0, 0))
|
| 60 |
+
target = [self.player[0] + delta[0], self.player[1] + delta[1]]
|
| 61 |
+
if not MAZE[*target]:
|
| 62 |
+
self.player = target
|
| 63 |
+
|
| 64 |
+
reward = 0.0
|
| 65 |
+
position = tuple(self.player)
|
| 66 |
+
if position in self.eggs:
|
| 67 |
+
self.eggs.remove(position)
|
| 68 |
+
reward += 1.0
|
| 69 |
+
if position in self.pulsars:
|
| 70 |
+
self.pulsars.remove(position)
|
| 71 |
+
self.power_timer = 30
|
| 72 |
+
|
| 73 |
+
self.previous_aliens = [alien.copy() for alien in self.aliens]
|
| 74 |
+
for alien in self.aliens:
|
| 75 |
+
self._move_alien(alien)
|
| 76 |
+
defeated = []
|
| 77 |
+
for alien in self.aliens:
|
| 78 |
+
if alien == self.player:
|
| 79 |
+
if self.power_timer > 0:
|
| 80 |
+
reward += 5.0
|
| 81 |
+
defeated.append(alien)
|
| 82 |
+
else:
|
| 83 |
+
self.terminal = True
|
| 84 |
+
for alien in defeated:
|
| 85 |
+
self.aliens.remove(alien)
|
| 86 |
+
self.power_timer = max(0, self.power_timer - 1)
|
| 87 |
+
self.steps += 1
|
| 88 |
+
if not self.eggs:
|
| 89 |
+
self.terminal = True
|
| 90 |
+
if self.steps >= self.horizon:
|
| 91 |
+
self.terminal = True
|
| 92 |
+
return reward, self.terminal
|
| 93 |
+
|
| 94 |
+
def _move_alien(self, alien: list[int]) -> None:
|
| 95 |
+
candidates = []
|
| 96 |
+
for row_delta, column_delta in ((0, -1), (-1, 0), (0, 1), (1, 0)):
|
| 97 |
+
candidate = [alien[0] + row_delta, alien[1] + column_delta]
|
| 98 |
+
if not MAZE[*candidate]:
|
| 99 |
+
distance = abs(candidate[0] - self.player[0]) + abs(candidate[1] - self.player[1])
|
| 100 |
+
candidates.append((distance, self.random.rand(), candidate))
|
| 101 |
+
if candidates:
|
| 102 |
+
alien[:] = min(candidates)[2]
|
| 103 |
+
|
| 104 |
+
def state(self) -> np.ndarray:
|
| 105 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 106 |
+
state[*self.player, 0] = True
|
| 107 |
+
for alien in self.aliens:
|
| 108 |
+
state[*alien, 1] = True
|
| 109 |
+
for egg in self.eggs:
|
| 110 |
+
state[*egg, 2] = True
|
| 111 |
+
for pulsar in self.pulsars:
|
| 112 |
+
state[*pulsar, 3] = True
|
| 113 |
+
for previous in self.previous_aliens:
|
| 114 |
+
state[*previous, 4] = True
|
| 115 |
+
state[9, : min(10, (self.power_timer + 2) // 3), 5] = True
|
| 116 |
+
return state
|
runtime/minatar_synth/games/amidar.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
ACTIONS = {
|
| 5 |
+
1: (0, -1),
|
| 6 |
+
2: (-1, 0),
|
| 7 |
+
3: (0, 1),
|
| 8 |
+
4: (1, 0),
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class AmidarEnvironment:
|
| 13 |
+
"""Trace a rectilinear maze while avoiding deterministic patrol enemies."""
|
| 14 |
+
|
| 15 |
+
score_cap = 9.0
|
| 16 |
+
|
| 17 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = 600):
|
| 18 |
+
self.sticky_action_prob = sticky_action_prob
|
| 19 |
+
self.horizon = horizon
|
| 20 |
+
self.random = np.random.RandomState()
|
| 21 |
+
self.seed(0)
|
| 22 |
+
self.reset()
|
| 23 |
+
|
| 24 |
+
@classmethod
|
| 25 |
+
def normalize_score(cls, raw_score: float) -> float:
|
| 26 |
+
return min(max(raw_score, 0.0), cls.score_cap) / cls.score_cap
|
| 27 |
+
|
| 28 |
+
def seed(self, seed: int) -> None:
|
| 29 |
+
self.random.seed(seed)
|
| 30 |
+
|
| 31 |
+
def reset(self) -> None:
|
| 32 |
+
self.player = [9, 0]
|
| 33 |
+
self.previous_player = self.player.copy()
|
| 34 |
+
self.enemies = [[0, 0], [0, 9], [9, 9]]
|
| 35 |
+
self.previous_enemies = [position.copy() for position in self.enemies]
|
| 36 |
+
self.enemy_directions = [4, 1, 2]
|
| 37 |
+
self.painted_edges: set[tuple[tuple[int, int], tuple[int, int]]] = set()
|
| 38 |
+
self.completed_boxes: set[tuple[int, int]] = set()
|
| 39 |
+
self.shadow_timer = 0
|
| 40 |
+
self.shadow_uses = 3
|
| 41 |
+
self.last_action = 0
|
| 42 |
+
self.steps = 0
|
| 43 |
+
self.terminal = False
|
| 44 |
+
|
| 45 |
+
@staticmethod
|
| 46 |
+
def _on_path(position: list[int] | tuple[int, int]) -> bool:
|
| 47 |
+
row, column = position
|
| 48 |
+
return 0 <= row < 10 and 0 <= column < 10 and (row % 3 == 0 or column % 3 == 0)
|
| 49 |
+
|
| 50 |
+
@staticmethod
|
| 51 |
+
def _edge(first: list[int], second: list[int]) -> tuple[tuple[int, int], tuple[int, int]]:
|
| 52 |
+
return tuple(sorted((tuple(first), tuple(second))))
|
| 53 |
+
|
| 54 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 55 |
+
if self.terminal:
|
| 56 |
+
return 0.0, True
|
| 57 |
+
if self.random.rand() < self.sticky_action_prob:
|
| 58 |
+
action = self.last_action
|
| 59 |
+
self.last_action = action
|
| 60 |
+
self.previous_player = self.player.copy()
|
| 61 |
+
if action == 5 and self.shadow_uses > 0 and self.shadow_timer == 0:
|
| 62 |
+
self.shadow_uses -= 1
|
| 63 |
+
self.shadow_timer = 12
|
| 64 |
+
elif action in ACTIONS:
|
| 65 |
+
row_delta, column_delta = ACTIONS[action]
|
| 66 |
+
target = [self.player[0] + row_delta, self.player[1] + column_delta]
|
| 67 |
+
if self._on_path(target):
|
| 68 |
+
self.player = target
|
| 69 |
+
if self.player != self.previous_player:
|
| 70 |
+
self.painted_edges.add(self._edge(self.previous_player, self.player))
|
| 71 |
+
|
| 72 |
+
reward = self._complete_boxes()
|
| 73 |
+
self.previous_enemies = [position.copy() for position in self.enemies]
|
| 74 |
+
if self.shadow_timer == 0:
|
| 75 |
+
for index in range(len(self.enemies)):
|
| 76 |
+
self._move_enemy(index)
|
| 77 |
+
if self.shadow_timer == 0 and any(enemy == self.player for enemy in self.enemies):
|
| 78 |
+
self.terminal = True
|
| 79 |
+
self.shadow_timer = max(0, self.shadow_timer - 1)
|
| 80 |
+
self.steps += 1
|
| 81 |
+
if len(self.completed_boxes) == 9 or self.steps >= self.horizon:
|
| 82 |
+
self.terminal = True
|
| 83 |
+
return reward, self.terminal
|
| 84 |
+
|
| 85 |
+
def _move_enemy(self, index: int) -> None:
|
| 86 |
+
direction = self.enemy_directions[index]
|
| 87 |
+
preferences = (direction, {1: 2, 2: 3, 3: 4, 4: 1}[direction], {1: 4, 2: 1, 3: 2, 4: 3}[direction])
|
| 88 |
+
for candidate_direction in preferences:
|
| 89 |
+
row_delta, column_delta = ACTIONS[candidate_direction]
|
| 90 |
+
target = [self.enemies[index][0] + row_delta, self.enemies[index][1] + column_delta]
|
| 91 |
+
if self._on_path(target):
|
| 92 |
+
self.enemies[index] = target
|
| 93 |
+
self.enemy_directions[index] = candidate_direction
|
| 94 |
+
return
|
| 95 |
+
|
| 96 |
+
def _complete_boxes(self) -> float:
|
| 97 |
+
reward = 0.0
|
| 98 |
+
for box_row in range(3):
|
| 99 |
+
for box_column in range(3):
|
| 100 |
+
box = (box_row, box_column)
|
| 101 |
+
if box in self.completed_boxes:
|
| 102 |
+
continue
|
| 103 |
+
top = box_row * 3
|
| 104 |
+
left = box_column * 3
|
| 105 |
+
required = set()
|
| 106 |
+
for offset in range(3):
|
| 107 |
+
required.add(self._edge([top, left + offset], [top, left + offset + 1]))
|
| 108 |
+
required.add(self._edge([top + 3, left + offset], [top + 3, left + offset + 1]))
|
| 109 |
+
required.add(self._edge([top + offset, left], [top + offset + 1, left]))
|
| 110 |
+
required.add(self._edge([top + offset, left + 3], [top + offset + 1, left + 3]))
|
| 111 |
+
if required <= self.painted_edges:
|
| 112 |
+
self.completed_boxes.add(box)
|
| 113 |
+
reward += 1.0
|
| 114 |
+
return reward
|
| 115 |
+
|
| 116 |
+
def state(self) -> np.ndarray:
|
| 117 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 118 |
+
state[*self.player, 0] = True
|
| 119 |
+
for enemy in self.enemies:
|
| 120 |
+
channel = 3 if self.shadow_timer > 0 else 1
|
| 121 |
+
state[*enemy, channel] = True
|
| 122 |
+
for box_row in range(3):
|
| 123 |
+
for box_column in range(3):
|
| 124 |
+
center = (min(9, box_row * 3 + 1), min(9, box_column * 3 + 1))
|
| 125 |
+
if (box_row, box_column) not in self.completed_boxes:
|
| 126 |
+
state[*center, 2] = True
|
| 127 |
+
for first, second in self.painted_edges:
|
| 128 |
+
state[*first, 4] = True
|
| 129 |
+
state[*second, 4] = True
|
| 130 |
+
state[9, : self.shadow_uses, 5] = True
|
| 131 |
+
if self.shadow_timer > 0:
|
| 132 |
+
state[8, : min(10, self.shadow_timer), 5] = True
|
| 133 |
+
return state
|
runtime/minatar_synth/games/ant_lion.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from math import cos, pi, sin
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
ANGLES = 32
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@dataclass
|
| 12 |
+
class EscapeBar:
|
| 13 |
+
angle: int
|
| 14 |
+
inner: int
|
| 15 |
+
outer: int
|
| 16 |
+
speed: int = 2
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class AntLionEnvironment:
|
| 20 |
+
score_cap = HORIZON * (HORIZON + 1)
|
| 21 |
+
|
| 22 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 23 |
+
self.sticky_action_prob = sticky_action_prob
|
| 24 |
+
self.horizon = min(horizon, HORIZON)
|
| 25 |
+
self.seed(0)
|
| 26 |
+
self.reset()
|
| 27 |
+
|
| 28 |
+
def seed(self, seed: int) -> None:
|
| 29 |
+
self.random = np.random.default_rng(seed)
|
| 30 |
+
|
| 31 |
+
def reset(self) -> None:
|
| 32 |
+
self.angle, self.radius = 8, 32
|
| 33 |
+
self.steps = self.last_action = self.velocity = self.multiplier = 0
|
| 34 |
+
self.spawn_in = 1
|
| 35 |
+
self.bars: list[EscapeBar] = []
|
| 36 |
+
self.attached: EscapeBar | None = None
|
| 37 |
+
self.offset = 0
|
| 38 |
+
self.terminal = False
|
| 39 |
+
|
| 40 |
+
@classmethod
|
| 41 |
+
def normalize_score(cls, score: float) -> float:
|
| 42 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 43 |
+
|
| 44 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 45 |
+
if self.terminal:
|
| 46 |
+
return 0.0, True
|
| 47 |
+
if self.random.random() < self.sticky_action_prob:
|
| 48 |
+
action = self.last_action
|
| 49 |
+
self.last_action = action
|
| 50 |
+
self.steps += 1
|
| 51 |
+
if self.attached is None:
|
| 52 |
+
self.velocity = max(-2, min(2, self.velocity + (1 if action == 5 else -1)))
|
| 53 |
+
self.angle = (self.angle + self.velocity) % ANGLES
|
| 54 |
+
self.radius -= 2 if self.radius > 40 else 1
|
| 55 |
+
if self.radius <= 3:
|
| 56 |
+
self.terminal = True
|
| 57 |
+
return 0.0, True
|
| 58 |
+
self.spawn_in -= 1
|
| 59 |
+
if not self.spawn_in:
|
| 60 |
+
self.bars.append(EscapeBar(int(self.random.integers(ANGLES)), 42,
|
| 61 |
+
42 + int(self.random.integers(8, 17))))
|
| 62 |
+
self.spawn_in = max(8, int(self.random.integers(14, 20)) - self.steps // 80)
|
| 63 |
+
remaining = []
|
| 64 |
+
for bar in self.bars:
|
| 65 |
+
if self.steps % 4 == 0:
|
| 66 |
+
bar.angle = (bar.angle - 1) % ANGLES
|
| 67 |
+
movement = bar.speed + int(bar.inner > 40)
|
| 68 |
+
bar.inner = max(0, bar.inner - movement)
|
| 69 |
+
bar.outer -= movement
|
| 70 |
+
if bar.outer >= 0:
|
| 71 |
+
remaining.append(bar)
|
| 72 |
+
else:
|
| 73 |
+
self.multiplier = max(1, self.multiplier - 1) if self.multiplier else 0
|
| 74 |
+
if self.attached is bar:
|
| 75 |
+
self.attached = None
|
| 76 |
+
self.bars = remaining
|
| 77 |
+
if self.attached is not None:
|
| 78 |
+
bar = self.attached
|
| 79 |
+
self.offset += 3
|
| 80 |
+
if self.steps % 5 == 0:
|
| 81 |
+
bar.speed = min(3, bar.speed + 1)
|
| 82 |
+
self.angle, self.radius = bar.angle, bar.inner + self.offset
|
| 83 |
+
if self.radius > bar.outer + 3:
|
| 84 |
+
self.attached = None
|
| 85 |
+
reward = 0
|
| 86 |
+
if self.attached is None and self.radius > 3:
|
| 87 |
+
for bar in self.bars:
|
| 88 |
+
separation = (self.angle - bar.angle) % ANGLES
|
| 89 |
+
if min(separation, ANGLES - separation) <= 1 and bar.inner <= self.radius <= bar.outer:
|
| 90 |
+
self.attached = bar
|
| 91 |
+
self.offset = self.radius - bar.inner
|
| 92 |
+
self.multiplier += 2
|
| 93 |
+
reward = self.multiplier
|
| 94 |
+
break
|
| 95 |
+
self.terminal = self.radius <= 3 or self.steps >= self.horizon
|
| 96 |
+
return float(reward), self.terminal
|
| 97 |
+
|
| 98 |
+
@staticmethod
|
| 99 |
+
def _cell(angle: int, radius: int) -> tuple[int, int]:
|
| 100 |
+
radians = angle * 2 * pi / ANGLES
|
| 101 |
+
return round(4.5 + sin(radians) * radius / 9), round(4.5 + cos(radians) * radius / 9)
|
| 102 |
+
|
| 103 |
+
def state(self) -> np.ndarray:
|
| 104 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 105 |
+
row, column = self._cell(self.angle, self.radius)
|
| 106 |
+
state[max(0, min(9, row)), max(0, min(9, column)), 0] = True
|
| 107 |
+
state[4:6, 4:6, 1] = True
|
| 108 |
+
for bar in self.bars:
|
| 109 |
+
for radius in range(bar.inner, bar.outer + 1):
|
| 110 |
+
row, column = self._cell(bar.angle, radius)
|
| 111 |
+
if 0 <= row < 10 and 0 <= column < 10:
|
| 112 |
+
state[row, column, 3 if bar is self.attached else 2] = True
|
| 113 |
+
values = (self.angle, self.radius, self.velocity + 2, self.offset,
|
| 114 |
+
self.multiplier, self.steps, self.spawn_in, self.last_action)
|
| 115 |
+
for row, value in enumerate(values):
|
| 116 |
+
for bit in range(10):
|
| 117 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 118 |
+
return state
|
runtime/minatar_synth/games/arcfire.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from math import atan2, cos, hypot, pi, sin, sqrt
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
HORIZON = 400
|
| 7 |
+
SECTORS = 24
|
| 8 |
+
MAX_CHARGE = 12
|
| 9 |
+
SPAWN_INTERVAL = 6
|
| 10 |
+
CENTER = (5, 5)
|
| 11 |
+
MAX_ENEMIES = (HORIZON + SPAWN_INTERVAL - 1) // SPAWN_INTERVAL
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class ArcfireEnvironment:
|
| 15 |
+
score_cap = MAX_ENEMIES * (MAX_ENEMIES + 1) // 2
|
| 16 |
+
|
| 17 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 18 |
+
self.sticky_action_prob = sticky_action_prob
|
| 19 |
+
self.horizon = min(horizon, HORIZON)
|
| 20 |
+
self.seed(0)
|
| 21 |
+
self.reset()
|
| 22 |
+
|
| 23 |
+
def seed(self, seed: int) -> None:
|
| 24 |
+
self.random = np.random.default_rng(seed)
|
| 25 |
+
|
| 26 |
+
def reset(self) -> None:
|
| 27 |
+
self.player = CENTER
|
| 28 |
+
self.steps = self.last_action = self.aim = self.charge = self.arc_start = 0
|
| 29 |
+
self.multiplier = 1
|
| 30 |
+
self.spawn_index = int(self.random.integers(36))
|
| 31 |
+
self.enemies: list[tuple[int, int]] = []
|
| 32 |
+
self.shot: tuple[int, int, int, int] | None = None
|
| 33 |
+
self.terminal = False
|
| 34 |
+
|
| 35 |
+
@classmethod
|
| 36 |
+
def normalize_score(cls, score: float) -> float:
|
| 37 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 38 |
+
|
| 39 |
+
@staticmethod
|
| 40 |
+
def _range(width: int) -> int:
|
| 41 |
+
return max(3, round(9 / sqrt(width)))
|
| 42 |
+
|
| 43 |
+
def _in_arc(self, cell: tuple[int, int], start: int, width: int, radius: int) -> bool:
|
| 44 |
+
dy, dx = cell[0] - self.player[0], cell[1] - self.player[1]
|
| 45 |
+
direction = atan2(dy, dx) * SECTORS / (2 * pi)
|
| 46 |
+
return (direction - start) % SECTORS <= width and abs(hypot(dx, dy) - radius) <= 0.75
|
| 47 |
+
|
| 48 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 49 |
+
if self.terminal:
|
| 50 |
+
return 0.0, True
|
| 51 |
+
if self.random.random() < self.sticky_action_prob:
|
| 52 |
+
action = self.last_action
|
| 53 |
+
self.steps += 1
|
| 54 |
+
self.aim = (self.aim + 1) % SECTORS
|
| 55 |
+
if action == 5 and self.last_action != 5:
|
| 56 |
+
self.charge, self.arc_start, self.multiplier = 1, self.aim, 1
|
| 57 |
+
elif action == 5 and self.charge:
|
| 58 |
+
self.charge += 1
|
| 59 |
+
if self.charge > MAX_CHARGE:
|
| 60 |
+
self.charge = 0
|
| 61 |
+
elif self.charge:
|
| 62 |
+
width = self.charge
|
| 63 |
+
reach = self._range(width)
|
| 64 |
+
if self.shot is None:
|
| 65 |
+
self.shot = (self.arc_start, width, reach, 1)
|
| 66 |
+
angle = (self.arc_start + width / 2) * 2 * pi / SECTORS
|
| 67 |
+
stride = reach / 2
|
| 68 |
+
row, column = self.player
|
| 69 |
+
self.player = (max(1, min(8, row + round(stride * sin(angle)))),
|
| 70 |
+
max(1, min(8, column + round(stride * cos(angle)))))
|
| 71 |
+
self.charge = 0
|
| 72 |
+
self.last_action = action
|
| 73 |
+
|
| 74 |
+
if self.steps % SPAWN_INTERVAL == 1:
|
| 75 |
+
if self.random.random() < 0.1:
|
| 76 |
+
self.spawn_index = int(self.random.integers(36))
|
| 77 |
+
else:
|
| 78 |
+
self.spawn_index = (self.spawn_index + int(self.random.integers(-1, 2))) % 36
|
| 79 |
+
side, offset = divmod(self.spawn_index, 9)
|
| 80 |
+
self.enemies.append(((0, offset), (offset, 9), (9, 9 - offset), (9 - offset, 0))[side])
|
| 81 |
+
if self.steps % 3 == 0:
|
| 82 |
+
self.enemies = [(row + int(np.sign(CENTER[0] - row)), column + int(np.sign(CENTER[1] - column)))
|
| 83 |
+
for row, column in self.enemies]
|
| 84 |
+
|
| 85 |
+
reward = 0
|
| 86 |
+
if self.shot is not None:
|
| 87 |
+
start, width, reach, radius = self.shot
|
| 88 |
+
survivors = []
|
| 89 |
+
for cell in self.enemies:
|
| 90 |
+
if self._in_arc(cell, start, width, radius):
|
| 91 |
+
reward += self.multiplier
|
| 92 |
+
self.multiplier += 1
|
| 93 |
+
else:
|
| 94 |
+
survivors.append(cell)
|
| 95 |
+
self.enemies = survivors
|
| 96 |
+
self.shot = (start, width, reach, radius + 1) if radius < reach else None
|
| 97 |
+
self.terminal = any(cell in (CENTER, self.player) for cell in self.enemies)
|
| 98 |
+
self.terminal |= self.steps >= self.horizon
|
| 99 |
+
return float(reward), self.terminal
|
| 100 |
+
|
| 101 |
+
def state(self) -> np.ndarray:
|
| 102 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 103 |
+
state[*self.player, 0] = True
|
| 104 |
+
for cell in self.enemies:
|
| 105 |
+
state[*cell, 1] = True
|
| 106 |
+
state[*CENTER, 2] = True
|
| 107 |
+
for row in range(10):
|
| 108 |
+
for column in range(10):
|
| 109 |
+
cell = row, column
|
| 110 |
+
if self.shot is not None:
|
| 111 |
+
start, width, _, radius = self.shot
|
| 112 |
+
state[row, column, 3] = self._in_arc(cell, start, width, radius)
|
| 113 |
+
if self.charge:
|
| 114 |
+
state[row, column, 4] = self._in_arc(cell, self.arc_start, self.charge, self._range(self.charge))
|
| 115 |
+
values = (self.steps, self.aim, self.charge, self.arc_start, self.multiplier,
|
| 116 |
+
self.spawn_index, int(self.last_action == 5), *(self.shot or (0, 0, 0, 0)))
|
| 117 |
+
offset = 0
|
| 118 |
+
for value, width in zip(values, (9, 5, 4, 5, 7, 6, 1, 5, 4, 4, 4), strict=True):
|
| 119 |
+
for bit in range(width):
|
| 120 |
+
state[offset // 10, offset % 10, 5] = bool(value & (1 << bit))
|
| 121 |
+
offset += 1
|
| 122 |
+
return state
|
runtime/minatar_synth/games/assault.py
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class AssaultEnvironment:
|
| 5 |
+
"""Fixed-cannon wave defense with vertical/lateral fire and weapon heat."""
|
| 6 |
+
|
| 7 |
+
score_cap = 75.0
|
| 8 |
+
|
| 9 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = 1_500):
|
| 10 |
+
self.sticky_action_prob = sticky_action_prob
|
| 11 |
+
self.horizon = horizon
|
| 12 |
+
self.random = np.random.RandomState()
|
| 13 |
+
self.seed(0)
|
| 14 |
+
self.reset()
|
| 15 |
+
|
| 16 |
+
@classmethod
|
| 17 |
+
def normalize_score(cls, raw_score: float) -> float:
|
| 18 |
+
return min(max(raw_score, 0.0), cls.score_cap) / cls.score_cap
|
| 19 |
+
|
| 20 |
+
def seed(self, seed: int) -> None:
|
| 21 |
+
self.random.seed(seed)
|
| 22 |
+
|
| 23 |
+
def reset(self) -> None:
|
| 24 |
+
self.player_column = 5
|
| 25 |
+
self.facing = 1
|
| 26 |
+
self.mothership_column = 0
|
| 27 |
+
self.mothership_direction = 1
|
| 28 |
+
self.aliens: list[dict[str, int | bool]] = []
|
| 29 |
+
self.friendly_shots: list[list[int]] = []
|
| 30 |
+
self.hostile_shots: list[list[int]] = []
|
| 31 |
+
self.fireballs: list[list[int]] = []
|
| 32 |
+
self.previous_aliens: list[list[int]] = []
|
| 33 |
+
self.previous_hostiles: list[list[int]] = []
|
| 34 |
+
self.wave = 0
|
| 35 |
+
self.spawned = 0
|
| 36 |
+
self.lives = 3
|
| 37 |
+
self.heat = 0
|
| 38 |
+
self.steps = 0
|
| 39 |
+
self.last_action = 0
|
| 40 |
+
self.terminal = False
|
| 41 |
+
|
| 42 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 43 |
+
if self.terminal:
|
| 44 |
+
return 0.0, True
|
| 45 |
+
if self.random.rand() < self.sticky_action_prob:
|
| 46 |
+
action = self.last_action
|
| 47 |
+
self.last_action = action
|
| 48 |
+
fired = False
|
| 49 |
+
if action == 1:
|
| 50 |
+
self.player_column = max(0, self.player_column - 1)
|
| 51 |
+
self.facing = -1
|
| 52 |
+
elif action == 3:
|
| 53 |
+
self.player_column = min(9, self.player_column + 1)
|
| 54 |
+
self.facing = 1
|
| 55 |
+
elif action == 2 and self.heat < 9:
|
| 56 |
+
self.friendly_shots.append([8, self.player_column, -1, 0])
|
| 57 |
+
fired = True
|
| 58 |
+
elif action == 5 and self.heat < 9:
|
| 59 |
+
self.friendly_shots.append([9, self.player_column, 0, self.facing])
|
| 60 |
+
fired = True
|
| 61 |
+
self.heat = min(10, self.heat + 2) if fired else max(0, self.heat - 1)
|
| 62 |
+
if self.heat >= 10:
|
| 63 |
+
self._lose_life()
|
| 64 |
+
|
| 65 |
+
self._move_mothership()
|
| 66 |
+
self._spawn_alien()
|
| 67 |
+
self.previous_aliens = [[int(alien["row"]), int(alien["column"])] for alien in self.aliens]
|
| 68 |
+
self.previous_hostiles = [shot[:2] for shot in self.hostile_shots + self.fireballs]
|
| 69 |
+
self._move_aliens()
|
| 70 |
+
self._move_projectiles()
|
| 71 |
+
reward = self._resolve_friendly_hits()
|
| 72 |
+
self._resolve_hostile_hits()
|
| 73 |
+
self._launch_hostile_weapon()
|
| 74 |
+
self.steps += 1
|
| 75 |
+
if self.spawned == 10 and not self.aliens:
|
| 76 |
+
self.wave += 1
|
| 77 |
+
if self.wave == 5:
|
| 78 |
+
self.terminal = True
|
| 79 |
+
else:
|
| 80 |
+
self.spawned = 0
|
| 81 |
+
if self.steps >= self.horizon:
|
| 82 |
+
self.terminal = True
|
| 83 |
+
return reward, self.terminal
|
| 84 |
+
|
| 85 |
+
def _move_mothership(self) -> None:
|
| 86 |
+
self.mothership_column += self.mothership_direction
|
| 87 |
+
if self.mothership_column in (0, 9):
|
| 88 |
+
self.mothership_direction *= -1
|
| 89 |
+
|
| 90 |
+
def _spawn_alien(self) -> None:
|
| 91 |
+
if self.spawned >= 10 or self.steps % 4 != 0:
|
| 92 |
+
return
|
| 93 |
+
self.aliens.append({
|
| 94 |
+
"row": 1,
|
| 95 |
+
"column": self.mothership_column,
|
| 96 |
+
"direction": self.mothership_direction,
|
| 97 |
+
"split": self.spawned % 2 == 1,
|
| 98 |
+
})
|
| 99 |
+
self.spawned += 1
|
| 100 |
+
|
| 101 |
+
def _move_aliens(self) -> None:
|
| 102 |
+
for alien in self.aliens:
|
| 103 |
+
direction = int(alien["direction"])
|
| 104 |
+
column = int(alien["column"]) + direction
|
| 105 |
+
if column < 0 or column > 9:
|
| 106 |
+
direction *= -1
|
| 107 |
+
column = min(9, max(0, column))
|
| 108 |
+
alien["column"] = column
|
| 109 |
+
alien["direction"] = direction
|
| 110 |
+
if self.wave % 3 == 1 and self.steps % 3 == 0:
|
| 111 |
+
alien["row"] = min(7, int(alien["row"]) + 1)
|
| 112 |
+
elif self.wave % 3 == 2 and self.random.rand() < 0.2:
|
| 113 |
+
alien["row"] = int(self.random.randint(1, 7))
|
| 114 |
+
alien["column"] = int(self.random.randint(10))
|
| 115 |
+
|
| 116 |
+
def _move_projectiles(self) -> None:
|
| 117 |
+
for shot in self.friendly_shots:
|
| 118 |
+
shot[0] += shot[2]
|
| 119 |
+
shot[1] += shot[3]
|
| 120 |
+
self.friendly_shots = [shot for shot in self.friendly_shots if 0 <= shot[0] < 10 and 0 <= shot[1] < 10]
|
| 121 |
+
for shot in self.hostile_shots:
|
| 122 |
+
shot[0] += 1
|
| 123 |
+
reached_base = [shot for shot in self.hostile_shots if shot[0] >= 9]
|
| 124 |
+
self.hostile_shots = [shot for shot in self.hostile_shots if shot[0] < 9]
|
| 125 |
+
for shot in reached_base:
|
| 126 |
+
direction = int(np.sign(self.player_column - shot[1])) or 1
|
| 127 |
+
self.fireballs.append([9, shot[1], 0, direction])
|
| 128 |
+
for fireball in self.fireballs:
|
| 129 |
+
fireball[1] += fireball[3]
|
| 130 |
+
self.fireballs = [shot for shot in self.fireballs if 0 <= shot[1] < 10]
|
| 131 |
+
|
| 132 |
+
def _resolve_friendly_hits(self) -> float:
|
| 133 |
+
reward = 0.0
|
| 134 |
+
remaining_shots = []
|
| 135 |
+
for shot in self.friendly_shots:
|
| 136 |
+
hit_fireball = next((fireball for fireball in self.fireballs if fireball[:2] == shot[:2]), None)
|
| 137 |
+
if hit_fireball is not None:
|
| 138 |
+
self.fireballs.remove(hit_fireball)
|
| 139 |
+
continue
|
| 140 |
+
hit = next((alien for alien in self.aliens if [alien["row"], alien["column"]] == shot[:2]), None)
|
| 141 |
+
if hit is None:
|
| 142 |
+
remaining_shots.append(shot)
|
| 143 |
+
continue
|
| 144 |
+
self.aliens.remove(hit)
|
| 145 |
+
if bool(hit["split"]):
|
| 146 |
+
column = int(hit["column"])
|
| 147 |
+
row = int(hit["row"])
|
| 148 |
+
for direction in (-1, 1):
|
| 149 |
+
self.aliens.append({"row": row, "column": column, "direction": direction, "split": False})
|
| 150 |
+
else:
|
| 151 |
+
reward += 1.0
|
| 152 |
+
self.friendly_shots = remaining_shots
|
| 153 |
+
return reward
|
| 154 |
+
|
| 155 |
+
def _resolve_hostile_hits(self) -> None:
|
| 156 |
+
if any(shot[:2] == [9, self.player_column] for shot in self.fireballs):
|
| 157 |
+
self.fireballs = [shot for shot in self.fireballs if shot[:2] != [9, self.player_column]]
|
| 158 |
+
self._lose_life()
|
| 159 |
+
|
| 160 |
+
def _launch_hostile_weapon(self) -> None:
|
| 161 |
+
if self.steps % 12 != 0 or not self.aliens:
|
| 162 |
+
return
|
| 163 |
+
lowest = max(self.aliens, key=lambda alien: int(alien["row"]))
|
| 164 |
+
self.hostile_shots.append([int(lowest["row"]) + 1, int(lowest["column"])])
|
| 165 |
+
|
| 166 |
+
def _lose_life(self) -> None:
|
| 167 |
+
self.lives -= 1
|
| 168 |
+
self.heat = 0
|
| 169 |
+
self.friendly_shots.clear()
|
| 170 |
+
self.hostile_shots.clear()
|
| 171 |
+
self.fireballs.clear()
|
| 172 |
+
if self.lives <= 0:
|
| 173 |
+
self.terminal = True
|
| 174 |
+
|
| 175 |
+
def state(self) -> np.ndarray:
|
| 176 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 177 |
+
state[9, self.player_column, 0] = True
|
| 178 |
+
state[0, self.mothership_column, 1] = True
|
| 179 |
+
for shot in self.hostile_shots + self.fireballs:
|
| 180 |
+
state[shot[0], shot[1], 1] = True
|
| 181 |
+
for alien in self.aliens:
|
| 182 |
+
state[int(alien["row"]), int(alien["column"]), 2] = True
|
| 183 |
+
for shot in self.friendly_shots:
|
| 184 |
+
state[shot[0], shot[1], 3] = True
|
| 185 |
+
for row, column in self.previous_aliens + self.previous_hostiles:
|
| 186 |
+
state[row, column, 4] = True
|
| 187 |
+
state[9, : self.heat, 5] = True
|
| 188 |
+
state[8, : self.lives, 5] = True
|
| 189 |
+
state[7, : self.wave + 1, 5] = True
|
| 190 |
+
return state
|
runtime/minatar_synth/games/asteroids.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
DIRECTIONS = (
|
| 5 |
+
(-1, 0),
|
| 6 |
+
(-1, 1),
|
| 7 |
+
(0, 1),
|
| 8 |
+
(1, 1),
|
| 9 |
+
(1, 0),
|
| 10 |
+
(1, -1),
|
| 11 |
+
(0, -1),
|
| 12 |
+
(-1, -1),
|
| 13 |
+
)
|
| 14 |
+
POINTS = {3: 20.0, 2: 50.0, 1: 100.0}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class AsteroidsEnvironment:
|
| 18 |
+
"""Momentum, wraparound, fragmentation, and risky hyperspace on a torus."""
|
| 19 |
+
|
| 20 |
+
score_cap = 6_240.0
|
| 21 |
+
|
| 22 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = 1_500):
|
| 23 |
+
self.sticky_action_prob = sticky_action_prob
|
| 24 |
+
self.horizon = horizon
|
| 25 |
+
self.random = np.random.RandomState()
|
| 26 |
+
self.seed(0)
|
| 27 |
+
self.reset()
|
| 28 |
+
|
| 29 |
+
@classmethod
|
| 30 |
+
def normalize_score(cls, raw_score: float) -> float:
|
| 31 |
+
return min(max(raw_score, 0.0), cls.score_cap) / cls.score_cap
|
| 32 |
+
|
| 33 |
+
def seed(self, seed: int) -> None:
|
| 34 |
+
self.random.seed(seed)
|
| 35 |
+
|
| 36 |
+
def reset(self) -> None:
|
| 37 |
+
self.ship = [5, 5]
|
| 38 |
+
self.previous_ship = self.ship.copy()
|
| 39 |
+
self.velocity = [0, 0]
|
| 40 |
+
self.direction = 0
|
| 41 |
+
self.shots: list[list[int]] = []
|
| 42 |
+
self.wave = 0
|
| 43 |
+
self.lives = 4
|
| 44 |
+
self.steps = 0
|
| 45 |
+
self.last_action = 0
|
| 46 |
+
self.terminal = False
|
| 47 |
+
self._start_wave()
|
| 48 |
+
|
| 49 |
+
def _start_wave(self) -> None:
|
| 50 |
+
starts = ((0, 0, 1, 1), (0, 9, 1, -1), (9, 0, -1, 1), (9, 9, -1, -1))
|
| 51 |
+
self.asteroids = [
|
| 52 |
+
{"row": row, "column": column, "dr": dr, "dc": dc, "size": 3}
|
| 53 |
+
for row, column, dr, dc in starts
|
| 54 |
+
]
|
| 55 |
+
self.previous_asteroids = [[int(asteroid["row"]), int(asteroid["column"])] for asteroid in self.asteroids]
|
| 56 |
+
|
| 57 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 58 |
+
if self.terminal:
|
| 59 |
+
return 0.0, True
|
| 60 |
+
if self.random.rand() < self.sticky_action_prob:
|
| 61 |
+
action = self.last_action
|
| 62 |
+
self.last_action = action
|
| 63 |
+
self.previous_ship = self.ship.copy()
|
| 64 |
+
if action == 1:
|
| 65 |
+
self.direction = (self.direction - 1) % 8
|
| 66 |
+
elif action == 3:
|
| 67 |
+
self.direction = (self.direction + 1) % 8
|
| 68 |
+
elif action == 2:
|
| 69 |
+
thrust = DIRECTIONS[self.direction]
|
| 70 |
+
self.velocity[0] = int(np.clip(self.velocity[0] + thrust[0], -1, 1))
|
| 71 |
+
self.velocity[1] = int(np.clip(self.velocity[1] + thrust[1], -1, 1))
|
| 72 |
+
elif action == 4:
|
| 73 |
+
self.ship = [int(self.random.randint(10)), int(self.random.randint(10))]
|
| 74 |
+
self.velocity = [0, 0]
|
| 75 |
+
elif action == 5:
|
| 76 |
+
dr, dc = DIRECTIONS[self.direction]
|
| 77 |
+
self.shots.append([self.ship[0], self.ship[1], dr, dc, 12])
|
| 78 |
+
|
| 79 |
+
if action != 4:
|
| 80 |
+
self.ship[0] = (self.ship[0] + self.velocity[0]) % 10
|
| 81 |
+
self.ship[1] = (self.ship[1] + self.velocity[1]) % 10
|
| 82 |
+
self.previous_asteroids = [[int(asteroid["row"]), int(asteroid["column"])] for asteroid in self.asteroids]
|
| 83 |
+
self._move_asteroids()
|
| 84 |
+
self._move_shots()
|
| 85 |
+
reward = self._resolve_shots()
|
| 86 |
+
if any([asteroid["row"], asteroid["column"]] == self.ship for asteroid in self.asteroids):
|
| 87 |
+
self._lose_ship()
|
| 88 |
+
self.steps += 1
|
| 89 |
+
if not self.asteroids:
|
| 90 |
+
self.wave += 1
|
| 91 |
+
if self.wave == 3:
|
| 92 |
+
self.terminal = True
|
| 93 |
+
else:
|
| 94 |
+
self._start_wave()
|
| 95 |
+
if self.steps >= self.horizon:
|
| 96 |
+
self.terminal = True
|
| 97 |
+
return reward, self.terminal
|
| 98 |
+
|
| 99 |
+
def _move_asteroids(self) -> None:
|
| 100 |
+
for asteroid in self.asteroids:
|
| 101 |
+
asteroid["row"] = (int(asteroid["row"]) + int(asteroid["dr"])) % 10
|
| 102 |
+
asteroid["column"] = (int(asteroid["column"]) + int(asteroid["dc"])) % 10
|
| 103 |
+
|
| 104 |
+
def _move_shots(self) -> None:
|
| 105 |
+
for shot in self.shots:
|
| 106 |
+
shot[0] = (shot[0] + shot[2]) % 10
|
| 107 |
+
shot[1] = (shot[1] + shot[3]) % 10
|
| 108 |
+
shot[4] -= 1
|
| 109 |
+
self.shots = [shot for shot in self.shots if shot[4] > 0]
|
| 110 |
+
|
| 111 |
+
def _resolve_shots(self) -> float:
|
| 112 |
+
reward = 0.0
|
| 113 |
+
remaining_shots = []
|
| 114 |
+
for shot in self.shots:
|
| 115 |
+
hit = next(
|
| 116 |
+
(asteroid for asteroid in self.asteroids if [asteroid["row"], asteroid["column"]] == shot[:2]),
|
| 117 |
+
None,
|
| 118 |
+
)
|
| 119 |
+
if hit is None:
|
| 120 |
+
remaining_shots.append(shot)
|
| 121 |
+
continue
|
| 122 |
+
self.asteroids.remove(hit)
|
| 123 |
+
size = int(hit["size"])
|
| 124 |
+
reward += POINTS[size]
|
| 125 |
+
if size > 1:
|
| 126 |
+
for turn in (-1, 1):
|
| 127 |
+
direction = DIRECTIONS.index((int(hit["dr"]), int(hit["dc"])))
|
| 128 |
+
dr, dc = DIRECTIONS[(direction + turn) % 8]
|
| 129 |
+
self.asteroids.append({
|
| 130 |
+
"row": int(hit["row"]),
|
| 131 |
+
"column": int(hit["column"]),
|
| 132 |
+
"dr": dr,
|
| 133 |
+
"dc": dc,
|
| 134 |
+
"size": size - 1,
|
| 135 |
+
})
|
| 136 |
+
self.shots = remaining_shots
|
| 137 |
+
return reward
|
| 138 |
+
|
| 139 |
+
def _lose_ship(self) -> None:
|
| 140 |
+
self.lives -= 1
|
| 141 |
+
self.ship = [5, 5]
|
| 142 |
+
self.previous_ship = self.ship.copy()
|
| 143 |
+
self.velocity = [0, 0]
|
| 144 |
+
self.shots.clear()
|
| 145 |
+
if self.lives <= 0:
|
| 146 |
+
self.terminal = True
|
| 147 |
+
|
| 148 |
+
def state(self) -> np.ndarray:
|
| 149 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 150 |
+
state[*self.ship, 0] = True
|
| 151 |
+
for asteroid in self.asteroids:
|
| 152 |
+
row, column = int(asteroid["row"]), int(asteroid["column"])
|
| 153 |
+
state[row, column, 1] = True
|
| 154 |
+
state[row, column, 2] = True
|
| 155 |
+
size = int(asteroid["size"])
|
| 156 |
+
state[row, (column + size - 1) % 10, 5] = True
|
| 157 |
+
for shot in self.shots:
|
| 158 |
+
state[shot[0], shot[1], 3] = True
|
| 159 |
+
for row, column in self.previous_asteroids:
|
| 160 |
+
state[row, column, 4] = True
|
| 161 |
+
state[*self.previous_ship, 4] = True
|
| 162 |
+
state[9, : self.lives, 5] = True
|
| 163 |
+
state[8, : self.wave + 1, 5] = True
|
| 164 |
+
velocity_marker = (self.velocity[0] + 1) * 3 + self.velocity[1] + 1
|
| 165 |
+
state[7, velocity_marker, 5] = True
|
| 166 |
+
state[6, self.direction, 5] = True
|
| 167 |
+
return state
|
runtime/minatar_synth/games/atlantis.py
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class AtlantisEnvironment:
|
| 5 |
+
"""Three fixed defense posts protect seven installations from descending formations."""
|
| 6 |
+
|
| 7 |
+
score_cap = 35_500.0
|
| 8 |
+
|
| 9 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = 1_500):
|
| 10 |
+
self.sticky_action_prob = sticky_action_prob
|
| 11 |
+
self.horizon = horizon
|
| 12 |
+
self.random = np.random.RandomState()
|
| 13 |
+
self.seed(0)
|
| 14 |
+
self.reset()
|
| 15 |
+
|
| 16 |
+
@classmethod
|
| 17 |
+
def normalize_score(cls, raw_score: float) -> float:
|
| 18 |
+
return min(max(raw_score, 0.0), cls.score_cap) / cls.score_cap
|
| 19 |
+
|
| 20 |
+
def seed(self, seed: int) -> None:
|
| 21 |
+
self.random.seed(seed)
|
| 22 |
+
|
| 23 |
+
def reset(self) -> None:
|
| 24 |
+
self.installations = [True] * 7
|
| 25 |
+
self.shots: list[list[int | str]] = []
|
| 26 |
+
self.wave = 0
|
| 27 |
+
self.wave_step = 0
|
| 28 |
+
self.steps = 0
|
| 29 |
+
self.total_score = 0.0
|
| 30 |
+
self.restorations_used = 0
|
| 31 |
+
self.last_action = 0
|
| 32 |
+
self.terminal = False
|
| 33 |
+
self.escaped = False
|
| 34 |
+
self._start_wave()
|
| 35 |
+
|
| 36 |
+
def _start_wave(self) -> None:
|
| 37 |
+
self.enemies = [
|
| 38 |
+
{"row": index // 5, "column": (index * 2) % 10, "direction": 1, "bandit": index == 8}
|
| 39 |
+
for index in range(9)
|
| 40 |
+
]
|
| 41 |
+
self.previous_enemies = [[int(enemy["row"]), int(enemy["column"])] for enemy in self.enemies]
|
| 42 |
+
self.wave_step = 0
|
| 43 |
+
|
| 44 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 45 |
+
if self.terminal:
|
| 46 |
+
return 0.0, True
|
| 47 |
+
if self.random.rand() < self.sticky_action_prob:
|
| 48 |
+
action = self.last_action
|
| 49 |
+
self.last_action = action
|
| 50 |
+
if action == 1:
|
| 51 |
+
self.shots.append([8, 1, -1, 1, "sentry"])
|
| 52 |
+
elif action == 3:
|
| 53 |
+
self.shots.append([8, 8, -1, -1, "sentry"])
|
| 54 |
+
elif action == 5 and self.installations[0]:
|
| 55 |
+
self.shots.append([8, 5, -1, 0, "command"])
|
| 56 |
+
|
| 57 |
+
self.previous_enemies = [[int(enemy["row"]), int(enemy["column"])] for enemy in self.enemies]
|
| 58 |
+
self._move_enemies()
|
| 59 |
+
self._move_shots()
|
| 60 |
+
reward = self._resolve_hits()
|
| 61 |
+
self._resolve_deathrays()
|
| 62 |
+
self.wave_step += 1
|
| 63 |
+
self.steps += 1
|
| 64 |
+
if not any(self.installations):
|
| 65 |
+
self.escaped = True
|
| 66 |
+
self.terminal = True
|
| 67 |
+
elif not self.enemies:
|
| 68 |
+
bonus = 500.0 * sum(self.installations)
|
| 69 |
+
reward += bonus
|
| 70 |
+
self.total_score += bonus
|
| 71 |
+
self._restore_installations()
|
| 72 |
+
self.wave += 1
|
| 73 |
+
if self.wave == 5:
|
| 74 |
+
self.terminal = True
|
| 75 |
+
else:
|
| 76 |
+
self._start_wave()
|
| 77 |
+
if self.steps >= self.horizon:
|
| 78 |
+
self.terminal = True
|
| 79 |
+
return reward, self.terminal
|
| 80 |
+
|
| 81 |
+
def _move_enemies(self) -> None:
|
| 82 |
+
if self.wave_step % max(1, 4 - min(self.wave, 2)) != 0:
|
| 83 |
+
return
|
| 84 |
+
for enemy in self.enemies:
|
| 85 |
+
column = int(enemy["column"]) + int(enemy["direction"])
|
| 86 |
+
if column < 0 or column > 9:
|
| 87 |
+
enemy["direction"] = -int(enemy["direction"])
|
| 88 |
+
enemy["row"] = int(enemy["row"]) + 1
|
| 89 |
+
column = min(9, max(0, column))
|
| 90 |
+
enemy["column"] = column
|
| 91 |
+
|
| 92 |
+
def _move_shots(self) -> None:
|
| 93 |
+
for shot in self.shots:
|
| 94 |
+
shot[0] = int(shot[0]) + int(shot[2])
|
| 95 |
+
shot[1] = int(shot[1]) + int(shot[3])
|
| 96 |
+
self.shots = [shot for shot in self.shots if 0 <= int(shot[0]) < 10 and 0 <= int(shot[1]) < 10]
|
| 97 |
+
|
| 98 |
+
def _resolve_hits(self) -> float:
|
| 99 |
+
reward = 0.0
|
| 100 |
+
remaining_shots = []
|
| 101 |
+
for shot in self.shots:
|
| 102 |
+
hit = next(
|
| 103 |
+
(enemy for enemy in self.enemies if [enemy["row"], enemy["column"]] == shot[:2]),
|
| 104 |
+
None,
|
| 105 |
+
)
|
| 106 |
+
if hit is None:
|
| 107 |
+
remaining_shots.append(shot)
|
| 108 |
+
continue
|
| 109 |
+
sentry = shot[4] == "sentry"
|
| 110 |
+
if bool(hit["bandit"]):
|
| 111 |
+
reward += 2_000.0 if sentry else 1_000.0
|
| 112 |
+
self.enemies.clear()
|
| 113 |
+
remaining_shots = []
|
| 114 |
+
break
|
| 115 |
+
reward += 200.0 if sentry else 100.0
|
| 116 |
+
self.enemies.remove(hit)
|
| 117 |
+
self.shots = remaining_shots
|
| 118 |
+
self.total_score += reward
|
| 119 |
+
return reward
|
| 120 |
+
|
| 121 |
+
def _resolve_deathrays(self) -> None:
|
| 122 |
+
attackers = [enemy for enemy in self.enemies if int(enemy["row"]) >= 8]
|
| 123 |
+
for enemy in attackers:
|
| 124 |
+
live = [index for index, intact in enumerate(self.installations) if intact]
|
| 125 |
+
if live:
|
| 126 |
+
target = 0 if self.installations[0] else live[int(self.random.randint(len(live)))]
|
| 127 |
+
self.installations[target] = False
|
| 128 |
+
self.enemies.remove(enemy)
|
| 129 |
+
|
| 130 |
+
def _restore_installations(self) -> None:
|
| 131 |
+
available = int(self.total_score // 10_000) - self.restorations_used
|
| 132 |
+
while available > 0 and not all(self.installations):
|
| 133 |
+
target = next(index for index, intact in enumerate(self.installations) if not intact)
|
| 134 |
+
self.installations[target] = True
|
| 135 |
+
self.restorations_used += 1
|
| 136 |
+
available -= 1
|
| 137 |
+
|
| 138 |
+
def state(self) -> np.ndarray:
|
| 139 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 140 |
+
state[9, 0, 0] = True
|
| 141 |
+
state[9, 9, 0] = True
|
| 142 |
+
if self.installations[0]:
|
| 143 |
+
state[9, 5, 0] = True
|
| 144 |
+
for enemy in self.enemies:
|
| 145 |
+
row, column = int(enemy["row"]), int(enemy["column"])
|
| 146 |
+
state[row, column, 1] = True
|
| 147 |
+
state[row, column, 2] = True
|
| 148 |
+
if bool(enemy["bandit"]):
|
| 149 |
+
state[row, column, 5] = True
|
| 150 |
+
for shot in self.shots:
|
| 151 |
+
state[int(shot[0]), int(shot[1]), 3] = True
|
| 152 |
+
for row, column in self.previous_enemies:
|
| 153 |
+
if 0 <= row < 10 and 0 <= column < 10:
|
| 154 |
+
state[row, column, 4] = True
|
| 155 |
+
for index, intact in enumerate(self.installations):
|
| 156 |
+
state[9, index + 1, 5] = intact
|
| 157 |
+
state[8, : self.wave + 1, 5] = True
|
| 158 |
+
return state
|
runtime/minatar_synth/games/attack_chain.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from math import sqrt
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
HORIZON = 400
|
| 7 |
+
CARDS = 6
|
| 8 |
+
CHAINS_PER_DEAL = 7
|
| 9 |
+
RED, GREEN, BLUE = range(3)
|
| 10 |
+
PRESSURE_LIMIT = 300
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class AttackChainEnvironment:
|
| 14 |
+
# One chain per two steps at most, and no card reaches 200 damage.
|
| 15 |
+
score_cap = (HORIZON // 2) * (99 + 250) * (1 + HORIZON // 2)
|
| 16 |
+
|
| 17 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 18 |
+
self.sticky_action_prob = sticky_action_prob
|
| 19 |
+
self.horizon = min(horizon, HORIZON)
|
| 20 |
+
self.seed(0)
|
| 21 |
+
self.reset()
|
| 22 |
+
|
| 23 |
+
def seed(self, seed: int) -> None:
|
| 24 |
+
self.random = np.random.default_rng(seed)
|
| 25 |
+
|
| 26 |
+
def reset(self) -> None:
|
| 27 |
+
self.steps = self.pressure = self.total = self.turn = self.chains = 0
|
| 28 |
+
self.cooldown = self.pending = 0
|
| 29 |
+
self.last_type = -1
|
| 30 |
+
self.last_action = 0
|
| 31 |
+
self.multiplier = 1
|
| 32 |
+
self.terminal = False
|
| 33 |
+
self._deal()
|
| 34 |
+
|
| 35 |
+
def _deal(self) -> None:
|
| 36 |
+
green_left = 2
|
| 37 |
+
self.types = []
|
| 38 |
+
for _ in range(CARDS):
|
| 39 |
+
if green_left and self.random.random() < 0.2:
|
| 40 |
+
self.types.append(GREEN)
|
| 41 |
+
green_left -= 1
|
| 42 |
+
else:
|
| 43 |
+
self.types.append(int(self.random.choice((RED, BLUE))))
|
| 44 |
+
scale = 4 + sqrt(min(20, self.multiplier))
|
| 45 |
+
self.values = [int(value * scale) for value in self.random.integers(3, 7, size=CARDS)]
|
| 46 |
+
self.available = [True] * CARDS
|
| 47 |
+
|
| 48 |
+
@classmethod
|
| 49 |
+
def normalize_score(cls, score: float) -> float:
|
| 50 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 51 |
+
|
| 52 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 53 |
+
if self.terminal:
|
| 54 |
+
return 0.0, True
|
| 55 |
+
if self.random.random() < self.sticky_action_prob:
|
| 56 |
+
action = self.last_action
|
| 57 |
+
self.last_action = action
|
| 58 |
+
self.steps += 1
|
| 59 |
+
self.pressure += 1
|
| 60 |
+
reward = 0
|
| 61 |
+
if self.cooldown:
|
| 62 |
+
self.cooldown = 0
|
| 63 |
+
self.total += self.pending
|
| 64 |
+
self.pending = 0
|
| 65 |
+
if self.total >= 100 or not any(self.available):
|
| 66 |
+
reward = self._settle()
|
| 67 |
+
elif self.pressure >= PRESSURE_LIMIT:
|
| 68 |
+
self.terminal = True
|
| 69 |
+
elif self.available[action]:
|
| 70 |
+
self.pressure += 4
|
| 71 |
+
self.available[action] = False
|
| 72 |
+
value, color = self.values[action], self.types[action]
|
| 73 |
+
if color == RED and self.turn == 0:
|
| 74 |
+
value = value * 5 // 4
|
| 75 |
+
elif color == GREEN:
|
| 76 |
+
value = min(value, 99 - self.total)
|
| 77 |
+
self.pending = value
|
| 78 |
+
self.cooldown = 1
|
| 79 |
+
self.last_type = color
|
| 80 |
+
self.turn += 1
|
| 81 |
+
self.values[action] = self.values[action] * 11 // 10 + 10
|
| 82 |
+
self.terminal |= self.steps >= self.horizon
|
| 83 |
+
return float(reward), self.terminal
|
| 84 |
+
|
| 85 |
+
def _settle(self) -> int:
|
| 86 |
+
reward = self.total * self.multiplier
|
| 87 |
+
excess = max(0, self.total - 115)
|
| 88 |
+
self.pressure = max(0, self.pressure - int(excess * sqrt(excess / self.multiplier)))
|
| 89 |
+
returns = sum(self.total >= threshold for threshold in (100, 150, 200))
|
| 90 |
+
unavailable = [index for index, ready in enumerate(self.available) if not ready]
|
| 91 |
+
if returns and unavailable and self.last_type == BLUE:
|
| 92 |
+
best = max(unavailable, key=self.values.__getitem__)
|
| 93 |
+
self.available[best] = True
|
| 94 |
+
unavailable.remove(best)
|
| 95 |
+
returns -= 1
|
| 96 |
+
for index in self.random.permutation(unavailable)[:returns]:
|
| 97 |
+
self.available[index] = True
|
| 98 |
+
self.chains += 1
|
| 99 |
+
if not any(self.available) or self.chains == CHAINS_PER_DEAL:
|
| 100 |
+
self.chains = 0
|
| 101 |
+
self.multiplier += 1
|
| 102 |
+
self._deal()
|
| 103 |
+
self.total = self.turn = 0
|
| 104 |
+
return reward
|
| 105 |
+
|
| 106 |
+
def state(self) -> np.ndarray:
|
| 107 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 108 |
+
for index, (color, value, ready) in enumerate(zip(self.types, self.values, self.available, strict=True)):
|
| 109 |
+
state[9, index, color] = True
|
| 110 |
+
state[9, index, 3] = ready
|
| 111 |
+
for bit in range(8):
|
| 112 |
+
state[bit, index, 4] = bool(value & (1 << bit))
|
| 113 |
+
values = (self.pressure, self.total, self.turn, self.chains, self.multiplier,
|
| 114 |
+
self.pending, self.cooldown, self.last_type + 1, self.steps, self.last_action)
|
| 115 |
+
for row, value in enumerate(values):
|
| 116 |
+
for bit in range(10):
|
| 117 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 118 |
+
return state
|
runtime/minatar_synth/games/b_blast.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
HORIZON = 300
|
| 7 |
+
RED_INTERVAL = 12
|
| 8 |
+
ORDINARY_INTERVAL = 4
|
| 9 |
+
MAX_RED = (HORIZON + RED_INTERVAL - 1) // RED_INTERVAL
|
| 10 |
+
MAX_BALLS = HORIZON // ORDINARY_INTERVAL + 10 * MAX_RED
|
| 11 |
+
MOVES = {1: (0, -1), 2: (-1, 0), 3: (0, 1), 4: (1, 0)}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@dataclass
|
| 15 |
+
class Ball:
|
| 16 |
+
row: int
|
| 17 |
+
column: int
|
| 18 |
+
red: bool = False
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class BBlastEnvironment:
|
| 22 |
+
score_cap = MAX_BALLS * (MAX_BALLS + 1) // 2 + 10 * MAX_RED
|
| 23 |
+
|
| 24 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 25 |
+
self.sticky_action_prob = sticky_action_prob
|
| 26 |
+
self.horizon = min(horizon, HORIZON)
|
| 27 |
+
self.seed(0)
|
| 28 |
+
self.reset()
|
| 29 |
+
|
| 30 |
+
def seed(self, seed: int) -> None:
|
| 31 |
+
self.random = np.random.default_rng(seed)
|
| 32 |
+
|
| 33 |
+
def reset(self) -> None:
|
| 34 |
+
self.cursor = (5, 5)
|
| 35 |
+
self.center = self.cursor
|
| 36 |
+
self.steps = self.last_action = self.charge = self.cooldown = 0
|
| 37 |
+
self.balls: list[Ball] = []
|
| 38 |
+
self.terminal = False
|
| 39 |
+
|
| 40 |
+
@classmethod
|
| 41 |
+
def normalize_score(cls, score: float) -> float:
|
| 42 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 43 |
+
|
| 44 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 45 |
+
if self.terminal:
|
| 46 |
+
return 0.0, True
|
| 47 |
+
if self.random.random() < self.sticky_action_prob:
|
| 48 |
+
action = self.last_action
|
| 49 |
+
self.steps += 1
|
| 50 |
+
self.cooldown = max(0, self.cooldown - 1)
|
| 51 |
+
reward = 0
|
| 52 |
+
if action == 5:
|
| 53 |
+
if not self.charge:
|
| 54 |
+
self.center = self.cursor
|
| 55 |
+
self.charge = 1
|
| 56 |
+
elif not self.cooldown or self.steps % 3 == 0:
|
| 57 |
+
self.charge = min(12, self.charge + 1)
|
| 58 |
+
else:
|
| 59 |
+
if self.charge:
|
| 60 |
+
reward += self._blast()
|
| 61 |
+
if action in MOVES:
|
| 62 |
+
dr, dc = MOVES[action]
|
| 63 |
+
row, column = self.cursor
|
| 64 |
+
self.cursor = max(0, min(9, row + dr)), max(0, min(9, column + dc))
|
| 65 |
+
self.last_action = action
|
| 66 |
+
if self.steps % RED_INTERVAL == 1:
|
| 67 |
+
self.balls.append(Ball(1, int(self.random.integers(2, 8)), True))
|
| 68 |
+
if self.steps % ORDINARY_INTERVAL == 0:
|
| 69 |
+
self.balls.append(Ball(int(self.random.integers(2, 8)), int(self.random.integers(1, 9))))
|
| 70 |
+
if self.steps % 2 == 0:
|
| 71 |
+
reward += self._settle()
|
| 72 |
+
self.terminal |= self.steps >= self.horizon
|
| 73 |
+
return float(reward), self.terminal
|
| 74 |
+
|
| 75 |
+
def _blast(self) -> int:
|
| 76 |
+
row, column = self.center
|
| 77 |
+
hit = [ball for ball in self.balls
|
| 78 |
+
if (ball.row - row) ** 2 + (ball.column - column) ** 2 <= (self.charge / 2 + 0.5) ** 2]
|
| 79 |
+
removed = {id(ball) for ball in hit}
|
| 80 |
+
self.balls = [ball for ball in self.balls if id(ball) not in removed]
|
| 81 |
+
for ball in hit:
|
| 82 |
+
if ball.red:
|
| 83 |
+
self.balls.extend(Ball(max(0, min(9, ball.row + dr)), max(0, min(9, ball.column + dc)))
|
| 84 |
+
for dr in (-1, 0, 1) for dc in (-1, 0, 1))
|
| 85 |
+
self.charge = 0
|
| 86 |
+
self.cooldown = 6
|
| 87 |
+
return len(hit) * (len(hit) + 1) // 2
|
| 88 |
+
|
| 89 |
+
def _settle(self) -> int:
|
| 90 |
+
reward = 0
|
| 91 |
+
occupied: set[tuple[int, int]] = set()
|
| 92 |
+
for ball in sorted(self.balls, key=lambda item: -item.row):
|
| 93 |
+
options = [(ball.row + 1, ball.column), (ball.row, ball.column),
|
| 94 |
+
(ball.row, ball.column - 1), (ball.row, ball.column + 1),
|
| 95 |
+
(ball.row - 1, ball.column)]
|
| 96 |
+
for row, column in options:
|
| 97 |
+
if 0 <= row < 10 and 0 <= column < 10 and (row, column) not in occupied:
|
| 98 |
+
ball.row, ball.column = row, column
|
| 99 |
+
occupied.add((row, column))
|
| 100 |
+
break
|
| 101 |
+
if ball.red and ball.row == 9:
|
| 102 |
+
reward += 10
|
| 103 |
+
ball.red = False
|
| 104 |
+
if ball.row == 0:
|
| 105 |
+
self.terminal = True
|
| 106 |
+
return reward
|
| 107 |
+
|
| 108 |
+
def state(self) -> np.ndarray:
|
| 109 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 110 |
+
state[*self.cursor, 0] = True
|
| 111 |
+
for ball in self.balls:
|
| 112 |
+
state[ball.row, ball.column, 1 if ball.red else 2] = True
|
| 113 |
+
if self.charge:
|
| 114 |
+
state[*self.center, 3] = True
|
| 115 |
+
rows, columns = np.ogrid[:10, :10]
|
| 116 |
+
state[:, :, 4] = ((rows - self.center[0]) ** 2 + (columns - self.center[1]) ** 2
|
| 117 |
+
<= (self.charge / 2 + 0.5) ** 2)
|
| 118 |
+
for row, value in enumerate((self.steps, self.charge, self.cooldown, self.last_action)):
|
| 119 |
+
for bit in range(9):
|
| 120 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 121 |
+
return state
|
runtime/minatar_synth/games/b_cannon.py
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
HORIZON = 400
|
| 7 |
+
WAVE_DELAY = 8
|
| 8 |
+
MAX_WAVE = 4
|
| 9 |
+
PERIMETER = (
|
| 10 |
+
tuple((9, x) for x in range(10))
|
| 11 |
+
+ tuple((y, 9) for y in range(8, -1, -1))
|
| 12 |
+
+ tuple((0, x) for x in range(8, -1, -1))
|
| 13 |
+
+ tuple((y, 0) for y in range(1, 9))
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@dataclass
|
| 18 |
+
class Ball:
|
| 19 |
+
row: int
|
| 20 |
+
column: int
|
| 21 |
+
dy: int
|
| 22 |
+
dx: int
|
| 23 |
+
size: int = 3
|
| 24 |
+
immunity: int = 0
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class BCannonEnvironment:
|
| 28 |
+
score_cap = HORIZON // WAVE_DELAY * MAX_WAVE * 8 * 99
|
| 29 |
+
|
| 30 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 31 |
+
self.sticky_action_prob = sticky_action_prob
|
| 32 |
+
self.horizon = min(horizon, HORIZON)
|
| 33 |
+
self.seed(0)
|
| 34 |
+
self.reset()
|
| 35 |
+
|
| 36 |
+
def seed(self, seed: int) -> None:
|
| 37 |
+
self.random = np.random.default_rng(seed)
|
| 38 |
+
|
| 39 |
+
def reset(self) -> None:
|
| 40 |
+
self.steps = self.last_action = self.held = self.wave_size = self.ball_index = 0
|
| 41 |
+
self.perimeter_index, self.direction = 5, 1
|
| 42 |
+
self.wave_max, self.ball_period, self.wave_wait = 2, 3, WAVE_DELAY
|
| 43 |
+
self.balls: list[Ball] = []
|
| 44 |
+
self.rope: list[tuple[int, int]] = []
|
| 45 |
+
self.rope_length = 0
|
| 46 |
+
self.terminal = False
|
| 47 |
+
|
| 48 |
+
@property
|
| 49 |
+
def player(self) -> tuple[int, int]:
|
| 50 |
+
return PERIMETER[self.perimeter_index]
|
| 51 |
+
|
| 52 |
+
@classmethod
|
| 53 |
+
def normalize_score(cls, score: float) -> float:
|
| 54 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 55 |
+
|
| 56 |
+
def _aim(self) -> tuple[int, int] | None:
|
| 57 |
+
if not self.balls:
|
| 58 |
+
return None
|
| 59 |
+
row, column = self.player
|
| 60 |
+
ball = min(self.balls, key=lambda item: (item.row - row) ** 2 + (item.column - column) ** 2)
|
| 61 |
+
return max(1, min(8, ball.row + ball.dy)), max(1, min(8, ball.column + ball.dx))
|
| 62 |
+
|
| 63 |
+
def _fire(self, target: tuple[int, int]) -> None:
|
| 64 |
+
row, column = self.player
|
| 65 |
+
dy, dx = target[0] - row, target[1] - column
|
| 66 |
+
scale = max(abs(dy), abs(dx))
|
| 67 |
+
self.rope = []
|
| 68 |
+
for distance in range(1, 11):
|
| 69 |
+
cell = row + round(dy * distance / scale), column + round(dx * distance / scale)
|
| 70 |
+
if not all(0 < coordinate < 9 for coordinate in cell):
|
| 71 |
+
break
|
| 72 |
+
self.rope.append(cell)
|
| 73 |
+
self.rope_length = 1
|
| 74 |
+
|
| 75 |
+
def _spawn_wave(self) -> None:
|
| 76 |
+
self.wave_size += 1
|
| 77 |
+
if self.wave_size > self.wave_max:
|
| 78 |
+
self.wave_size = 1
|
| 79 |
+
self.wave_max = min(MAX_WAVE, self.wave_max + 1)
|
| 80 |
+
self.ball_period = max(1, self.ball_period - 1)
|
| 81 |
+
start = int(self.random.integers(1, 3))
|
| 82 |
+
self.balls = [Ball(3, start + 2 * index, 1, 0) for index in range(self.wave_size)]
|
| 83 |
+
self.ball_index = (self.ball_index + self.wave_size) % 100
|
| 84 |
+
self.wave_wait = WAVE_DELAY
|
| 85 |
+
|
| 86 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 87 |
+
if self.terminal:
|
| 88 |
+
return 0.0, True
|
| 89 |
+
if self.random.random() < self.sticky_action_prob:
|
| 90 |
+
action = self.last_action
|
| 91 |
+
if action == 5 and self.last_action != 5:
|
| 92 |
+
self.direction *= -1
|
| 93 |
+
self.last_action = action
|
| 94 |
+
self.steps += 1
|
| 95 |
+
if action == 5:
|
| 96 |
+
if self.balls:
|
| 97 |
+
self.held += 1
|
| 98 |
+
target = self._aim()
|
| 99 |
+
if self.held == 3 and target is not None:
|
| 100 |
+
self._fire(target)
|
| 101 |
+
else:
|
| 102 |
+
self.held = 0
|
| 103 |
+
self.perimeter_index = (self.perimeter_index + self.direction) % len(PERIMETER)
|
| 104 |
+
reward = self._advance_balls()
|
| 105 |
+
if self.rope:
|
| 106 |
+
self.rope_length += 1
|
| 107 |
+
if self.rope_length > len(self.rope) + 1:
|
| 108 |
+
self.rope.clear()
|
| 109 |
+
self.rope_length = 0
|
| 110 |
+
if not self.balls:
|
| 111 |
+
self.wave_wait -= 1
|
| 112 |
+
if self.wave_wait == 0:
|
| 113 |
+
self._spawn_wave()
|
| 114 |
+
self.terminal |= self.steps >= self.horizon
|
| 115 |
+
return float(reward), self.terminal
|
| 116 |
+
|
| 117 |
+
def _advance_balls(self) -> int:
|
| 118 |
+
reward = 0
|
| 119 |
+
remaining = []
|
| 120 |
+
rope = set(self.rope[:self.rope_length])
|
| 121 |
+
for ball in self.balls:
|
| 122 |
+
if self.steps % self.ball_period == 0:
|
| 123 |
+
if not 0 <= ball.row + ball.dy < 10:
|
| 124 |
+
ball.dy *= -1
|
| 125 |
+
if not 0 <= ball.column + ball.dx < 10:
|
| 126 |
+
ball.dx *= -1
|
| 127 |
+
ball.row += ball.dy
|
| 128 |
+
ball.column += ball.dx
|
| 129 |
+
ball.immunity = max(0, ball.immunity - 1)
|
| 130 |
+
cell = ball.row, ball.column
|
| 131 |
+
if cell in rope and not ball.immunity:
|
| 132 |
+
if ball.size == 0:
|
| 133 |
+
reward += self.ball_index
|
| 134 |
+
else:
|
| 135 |
+
remaining.extend(Ball(*cell, -ball.dy or 1, dx, ball.size - 1, 3) for dx in (-1, 1))
|
| 136 |
+
self.ball_index = (self.ball_index + 2) % 100
|
| 137 |
+
else:
|
| 138 |
+
self.terminal |= cell == self.player
|
| 139 |
+
remaining.append(ball)
|
| 140 |
+
self.balls = remaining
|
| 141 |
+
return reward
|
| 142 |
+
|
| 143 |
+
def state(self) -> np.ndarray:
|
| 144 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 145 |
+
state[*self.player, 0] = True
|
| 146 |
+
for ball in self.balls:
|
| 147 |
+
state[ball.row, ball.column, 1] = True
|
| 148 |
+
state[ball.row, ball.column, 2] |= ball.immunity > 0
|
| 149 |
+
for cell in self.rope[:self.rope_length]:
|
| 150 |
+
state[*cell, 3] = True
|
| 151 |
+
target = self._aim()
|
| 152 |
+
if target is not None:
|
| 153 |
+
state[*target, 4] = True
|
| 154 |
+
values = (self.steps, self.held, self.wave_size, self.wave_max, self.wave_wait,
|
| 155 |
+
self.ball_period, self.ball_index, int(self.direction > 0), int(self.last_action == 5))
|
| 156 |
+
for row, value in enumerate(values):
|
| 157 |
+
for bit in range(9):
|
| 158 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 159 |
+
return state
|
runtime/minatar_synth/games/b_math.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
HORIZON = 400
|
| 5 |
+
FULL_TIME = 60
|
| 6 |
+
FEEDBACK_STEPS = 3
|
| 7 |
+
MAX_ANSWERS = (HORIZON + FEEDBACK_STEPS) // (FEEDBACK_STEPS + 1)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class BMathEnvironment:
|
| 11 |
+
score_cap = 57 * MAX_ANSWERS * (MAX_ANSWERS + 1) // 2
|
| 12 |
+
|
| 13 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 14 |
+
self.sticky_action_prob = sticky_action_prob
|
| 15 |
+
self.horizon = min(horizon, HORIZON)
|
| 16 |
+
self.seed(0)
|
| 17 |
+
self.reset()
|
| 18 |
+
|
| 19 |
+
def seed(self, seed: int) -> None:
|
| 20 |
+
self.random = np.random.default_rng(seed)
|
| 21 |
+
|
| 22 |
+
def reset(self) -> None:
|
| 23 |
+
self.steps = self.questions = self.feedback = self.last_action = 0
|
| 24 |
+
self.time = FULL_TIME
|
| 25 |
+
self.multiplier = 1
|
| 26 |
+
self.correct = False
|
| 27 |
+
self.selected = 0
|
| 28 |
+
self.terminal = False
|
| 29 |
+
self._question()
|
| 30 |
+
|
| 31 |
+
def _question(self) -> None:
|
| 32 |
+
self.operation = int(self.random.integers(min(4, 1 + self.questions // 5)))
|
| 33 |
+
if self.operation < 2:
|
| 34 |
+
total = int(self.random.integers(10, 100))
|
| 35 |
+
part = int(self.random.integers(1, total))
|
| 36 |
+
self.left, self.right, self.answer = (
|
| 37 |
+
(part, total - part, total) if self.operation == 0 else (total, part, total - part)
|
| 38 |
+
)
|
| 39 |
+
else:
|
| 40 |
+
factor = int(self.random.integers(2, 10))
|
| 41 |
+
quotient = int(self.random.integers(2, 99 // factor + 1))
|
| 42 |
+
product = factor * quotient
|
| 43 |
+
self.left, self.right, self.answer = (
|
| 44 |
+
(quotient, factor, product) if self.operation == 2 else (product, factor, quotient)
|
| 45 |
+
)
|
| 46 |
+
distractors = [value for value in range(max(1, self.answer - 7), min(99, self.answer + 7) + 1)
|
| 47 |
+
if value != self.answer]
|
| 48 |
+
choices = self.random.choice(distractors, size=4, replace=False)
|
| 49 |
+
self.options = self.random.permutation([self.answer, *choices]).tolist()
|
| 50 |
+
|
| 51 |
+
@classmethod
|
| 52 |
+
def normalize_score(cls, score: float) -> float:
|
| 53 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 54 |
+
|
| 55 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 56 |
+
if self.terminal:
|
| 57 |
+
return 0.0, True
|
| 58 |
+
if self.random.random() < self.sticky_action_prob:
|
| 59 |
+
action = self.last_action
|
| 60 |
+
self.last_action = action
|
| 61 |
+
self.steps += 1
|
| 62 |
+
reward = 0
|
| 63 |
+
if self.feedback:
|
| 64 |
+
self.feedback -= 1
|
| 65 |
+
if not self.feedback:
|
| 66 |
+
self._question()
|
| 67 |
+
elif 1 <= action <= 5:
|
| 68 |
+
reward = (57 * self.time * self.multiplier + FULL_TIME - 1) // FULL_TIME
|
| 69 |
+
self.correct = self.options[action - 1] == self.answer
|
| 70 |
+
self.selected = action
|
| 71 |
+
if self.correct:
|
| 72 |
+
self.time += 30
|
| 73 |
+
if self.time >= FULL_TIME:
|
| 74 |
+
self.multiplier += 1
|
| 75 |
+
else:
|
| 76 |
+
self.time -= 15
|
| 77 |
+
self.multiplier = max(1, self.multiplier - 1)
|
| 78 |
+
self.time = min(FULL_TIME, self.time)
|
| 79 |
+
self.feedback = FEEDBACK_STEPS
|
| 80 |
+
self.questions += 1
|
| 81 |
+
self.time = max(0, self.time - 1)
|
| 82 |
+
self.terminal = self.time == 0 or self.steps >= self.horizon
|
| 83 |
+
return float(reward), self.terminal
|
| 84 |
+
|
| 85 |
+
def state(self) -> np.ndarray:
|
| 86 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 87 |
+
state[self.operation, 0, 0] = True
|
| 88 |
+
for row, value in enumerate((self.left, self.right, *self.options)):
|
| 89 |
+
for bit in range(8):
|
| 90 |
+
state[row, bit, 1] = bool(value & (1 << bit))
|
| 91 |
+
if self.feedback:
|
| 92 |
+
state[0, self.selected, 2] = True
|
| 93 |
+
state[int(self.correct), 0, 3] = True
|
| 94 |
+
for row, value in enumerate((self.time, self.multiplier, self.questions,
|
| 95 |
+
self.feedback, self.steps, self.last_action)):
|
| 96 |
+
for bit in range(9):
|
| 97 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 98 |
+
return state
|
runtime/minatar_synth/games/b_walls.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
HORIZON = 400
|
| 7 |
+
COLUMN = 5
|
| 8 |
+
PLAYER_ROW = 9
|
| 9 |
+
PHASE_LENGTH = 6
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@dataclass
|
| 13 |
+
class Wall:
|
| 14 |
+
row: int
|
| 15 |
+
phase: int
|
| 16 |
+
width: int
|
| 17 |
+
period: int
|
| 18 |
+
direction: int
|
| 19 |
+
|
| 20 |
+
def blocks(self, column: int) -> bool:
|
| 21 |
+
return (column - self.phase) % self.period < self.width
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class BWallsEnvironment:
|
| 25 |
+
score_cap = HORIZON * 2 ** (PLAYER_ROW - 1)
|
| 26 |
+
|
| 27 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 28 |
+
self.sticky_action_prob = sticky_action_prob
|
| 29 |
+
self.horizon = min(horizon, HORIZON)
|
| 30 |
+
self.seed(0)
|
| 31 |
+
self.reset()
|
| 32 |
+
|
| 33 |
+
def seed(self, seed: int) -> None:
|
| 34 |
+
self.random = np.random.default_rng(seed)
|
| 35 |
+
|
| 36 |
+
def reset(self) -> None:
|
| 37 |
+
self.steps = self.wall_clock = self.last_action = self.travel = 0
|
| 38 |
+
self.multiplier = 1
|
| 39 |
+
self.shot: int | None = None
|
| 40 |
+
self.terminal = False
|
| 41 |
+
self.walls: list[Wall] = []
|
| 42 |
+
self._spawn(3)
|
| 43 |
+
self._spawn(0)
|
| 44 |
+
|
| 45 |
+
def _spawn(self, row: int) -> None:
|
| 46 |
+
width = int(self.random.integers(1, 3))
|
| 47 |
+
period = width + int(self.random.integers(2, 5))
|
| 48 |
+
self.walls.append(Wall(row, int(self.random.integers(period)), width, period,
|
| 49 |
+
int(self.random.choice((-1, 1)))))
|
| 50 |
+
|
| 51 |
+
@classmethod
|
| 52 |
+
def normalize_score(cls, score: float) -> float:
|
| 53 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 54 |
+
|
| 55 |
+
@property
|
| 56 |
+
def stopped(self) -> bool:
|
| 57 |
+
return self.wall_clock // PHASE_LENGTH % 2 == 1
|
| 58 |
+
|
| 59 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 60 |
+
if self.terminal:
|
| 61 |
+
return 0.0, True
|
| 62 |
+
if self.random.random() < self.sticky_action_prob:
|
| 63 |
+
action = self.last_action
|
| 64 |
+
if action == 5 and self.last_action != 5 and self.shot is None:
|
| 65 |
+
self.shot = PLAYER_ROW
|
| 66 |
+
self.multiplier = 1
|
| 67 |
+
self.last_action = action
|
| 68 |
+
self.steps += 1
|
| 69 |
+
moving = not self.stopped
|
| 70 |
+
if moving:
|
| 71 |
+
for wall in self.walls:
|
| 72 |
+
wall.phase = (wall.phase + wall.direction) % wall.period
|
| 73 |
+
|
| 74 |
+
near_launch = self.shot is not None and self.shot >= 7
|
| 75 |
+
if near_launch or self.steps % (5 if self.steps < 200 else 4) == 0:
|
| 76 |
+
self.travel += 1
|
| 77 |
+
for wall in self.walls:
|
| 78 |
+
wall.row += 1
|
| 79 |
+
if self.travel % 3 == 0:
|
| 80 |
+
self._spawn(0)
|
| 81 |
+
|
| 82 |
+
reward = 0
|
| 83 |
+
if self.shot is not None:
|
| 84 |
+
self.shot -= 1
|
| 85 |
+
if self.shot < 0 or any(wall.row == self.shot and wall.blocks(COLUMN) for wall in self.walls):
|
| 86 |
+
self.shot = None
|
| 87 |
+
else:
|
| 88 |
+
for wall in self.walls:
|
| 89 |
+
if wall.row - 1 == self.shot:
|
| 90 |
+
reward = self.multiplier
|
| 91 |
+
self.multiplier *= 2
|
| 92 |
+
self.walls.remove(wall)
|
| 93 |
+
break
|
| 94 |
+
if moving and self.shot < max((wall.row for wall in self.walls), default=-2) + 1:
|
| 95 |
+
self.shot = None
|
| 96 |
+
if self.shot is None:
|
| 97 |
+
self.wall_clock += 1
|
| 98 |
+
self.terminal = any(wall.row == PLAYER_ROW and wall.blocks(COLUMN) for wall in self.walls)
|
| 99 |
+
self.walls = [wall for wall in self.walls if wall.row <= PLAYER_ROW]
|
| 100 |
+
self.terminal |= self.steps >= self.horizon
|
| 101 |
+
return float(reward), self.terminal
|
| 102 |
+
|
| 103 |
+
def state(self) -> np.ndarray:
|
| 104 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 105 |
+
state[PLAYER_ROW, COLUMN, 0] = True
|
| 106 |
+
if self.shot is not None:
|
| 107 |
+
state[self.shot, COLUMN, 3] = True
|
| 108 |
+
for wall in self.walls:
|
| 109 |
+
for column in range(10):
|
| 110 |
+
state[wall.row, column, 1] = wall.blocks(column)
|
| 111 |
+
state[wall.row, column, 4] = (column - wall.phase - wall.direction) % wall.period < wall.width
|
| 112 |
+
if wall.row > 0:
|
| 113 |
+
state[wall.row - 1, COLUMN, 2] = True
|
| 114 |
+
for row, value in enumerate((self.steps, self.wall_clock % (2 * PHASE_LENGTH),
|
| 115 |
+
self.travel % 3, self.multiplier, self.last_action)):
|
| 116 |
+
for bit in range(10):
|
| 117 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 118 |
+
return state
|
runtime/minatar_synth/games/balance.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from math import ceil, cos, sin
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
HORIZON = 400
|
| 7 |
+
SPAWN_INTERVAL = 6
|
| 8 |
+
FLOOR = 9
|
| 9 |
+
MIN_LENGTH = 20
|
| 10 |
+
MAX_LENGTH = 64
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class BalanceEnvironment:
|
| 14 |
+
score_cap = ((HORIZON + SPAWN_INTERVAL - 1) // SPAWN_INTERVAL) * 3 * MAX_LENGTH
|
| 15 |
+
|
| 16 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 17 |
+
self.sticky_action_prob = sticky_action_prob
|
| 18 |
+
self.horizon = min(horizon, HORIZON)
|
| 19 |
+
self.seed(0)
|
| 20 |
+
self.reset()
|
| 21 |
+
|
| 22 |
+
def seed(self, seed: int) -> None:
|
| 23 |
+
self.random = np.random.default_rng(seed)
|
| 24 |
+
|
| 25 |
+
def reset(self) -> None:
|
| 26 |
+
self.steps = self.last_action = 0
|
| 27 |
+
self.base = self.cursor = 4.5
|
| 28 |
+
self.velocity = self.angle = self.angular_velocity = self.wind = 0.0
|
| 29 |
+
self.length = MIN_LENGTH
|
| 30 |
+
self.coin_column = float(self.random.choice((2, 7)))
|
| 31 |
+
self.coins: list[tuple[float, float, float]] = []
|
| 32 |
+
self.terminal = False
|
| 33 |
+
|
| 34 |
+
@classmethod
|
| 35 |
+
def normalize_score(cls, score: float) -> float:
|
| 36 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 37 |
+
|
| 38 |
+
@property
|
| 39 |
+
def tip(self) -> tuple[float, float]:
|
| 40 |
+
return FLOOR - cos(self.angle) * self.length / 8, self.base + sin(self.angle) * self.length / 8
|
| 41 |
+
|
| 42 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 43 |
+
if self.terminal:
|
| 44 |
+
return 0.0, True
|
| 45 |
+
if self.random.random() < self.sticky_action_prob:
|
| 46 |
+
action = self.last_action
|
| 47 |
+
self.last_action = action
|
| 48 |
+
self.steps += 1
|
| 49 |
+
self.cursor = max(0.0, min(9.0, self.cursor + (1 if action == 3 else -1 if action == 1 else 0)))
|
| 50 |
+
self.wind = 0.94 * (self.wind + float(self.random.uniform(-0.025, 0.025)))
|
| 51 |
+
self.velocity = 0.90 * (self.velocity + 0.08 * (self.cursor - self.base) - self.wind)
|
| 52 |
+
self.base += self.velocity
|
| 53 |
+
self.angular_velocity = 0.88 * (self.angular_velocity - 0.16 * self.velocity
|
| 54 |
+
+ self.angle * self.length / 800)
|
| 55 |
+
self.angle += self.angular_velocity
|
| 56 |
+
row, column = self.tip
|
| 57 |
+
shift = (4.5 - column) * 0.18
|
| 58 |
+
self.base += shift
|
| 59 |
+
self.cursor = max(0.0, min(9.0, self.cursor + shift))
|
| 60 |
+
if row >= FLOOR - 0.25:
|
| 61 |
+
self.length /= 2
|
| 62 |
+
self.angular_velocity *= -0.5
|
| 63 |
+
if self.length < MIN_LENGTH:
|
| 64 |
+
self.terminal = True
|
| 65 |
+
return 0.0, True
|
| 66 |
+
self.angle /= 2
|
| 67 |
+
if self.steps % SPAWN_INTERVAL == 1:
|
| 68 |
+
self.coins.append((0.0, self.coin_column, float(self.random.uniform(-0.15, 0.15))))
|
| 69 |
+
self.coin_column = (self.coin_column + shift + float(self.random.uniform(-0.6, 0.6))) % 10
|
| 70 |
+
|
| 71 |
+
row, column = self.tip
|
| 72 |
+
reward = 0
|
| 73 |
+
remaining = []
|
| 74 |
+
for y, x, drift in self.coins:
|
| 75 |
+
y += 0.5
|
| 76 |
+
drift = 0.96 * (drift + self.wind / 2)
|
| 77 |
+
x += drift + shift
|
| 78 |
+
tip_hit = abs(y - row) <= 0.45 and abs(x - column) <= 0.55
|
| 79 |
+
fraction = (FLOOR - y) / max(0.01, FLOOR - row)
|
| 80 |
+
shaft_x = self.base + (column - self.base) * fraction
|
| 81 |
+
if tip_hit or (0 <= fraction <= 1 and abs(x - shaft_x) <= 0.45):
|
| 82 |
+
for _ in range(3 if tip_hit else 1):
|
| 83 |
+
reward += ceil(self.length)
|
| 84 |
+
self.length = min(MAX_LENGTH, self.length + 1)
|
| 85 |
+
elif y < FLOOR:
|
| 86 |
+
remaining.append((y, x, drift))
|
| 87 |
+
self.coins = remaining
|
| 88 |
+
self.terminal = self.steps >= self.horizon
|
| 89 |
+
return float(reward), self.terminal
|
| 90 |
+
|
| 91 |
+
def state(self) -> np.ndarray:
|
| 92 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 93 |
+
row, column = self.tip
|
| 94 |
+
state[min(9, max(0, round(row))), min(9, max(0, round(column))), 0] = True
|
| 95 |
+
state[FLOOR, :, 1] = True
|
| 96 |
+
for y, x, _ in self.coins:
|
| 97 |
+
if 0 <= round(x) < 10:
|
| 98 |
+
state[round(y), round(x), 2] = True
|
| 99 |
+
for fraction in np.linspace(0, 1, 16):
|
| 100 |
+
x = round(self.base + fraction * (column - self.base))
|
| 101 |
+
y = round(FLOOR + fraction * (row - FLOOR))
|
| 102 |
+
if 0 <= x < 10 and 0 <= y < 10:
|
| 103 |
+
state[y, x, 3] = True
|
| 104 |
+
state[FLOOR, round(self.cursor), 4] = True
|
| 105 |
+
values = (self.steps, round(self.length * 4), round(self.angle * 64) + 256,
|
| 106 |
+
round(self.angular_velocity * 128) + 256, round(self.velocity * 32) + 256,
|
| 107 |
+
round(self.wind * 512) + 256, self.last_action)
|
| 108 |
+
for row, value in enumerate(values):
|
| 109 |
+
for bit in range(10):
|
| 110 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 111 |
+
return state
|
runtime/minatar_synth/games/ball_tour.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
ORBIT = (5, 6, 7, 8, 8, 8, 7, 6, 5, 4, 3, 2, 2, 2, 3, 4)
|
| 5 |
+
HORIZON = 500
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class BallTourEnvironment:
|
| 9 |
+
# A ball is spawned no more often than every three columns of travel.
|
| 10 |
+
score_cap = 999 * ((HORIZON + 2) // 3)
|
| 11 |
+
|
| 12 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 13 |
+
self.sticky_action_prob = sticky_action_prob
|
| 14 |
+
self.horizon = min(horizon, HORIZON)
|
| 15 |
+
self.seed(0)
|
| 16 |
+
self.reset()
|
| 17 |
+
|
| 18 |
+
def seed(self, seed: int) -> None:
|
| 19 |
+
self.random = np.random.default_rng(seed)
|
| 20 |
+
|
| 21 |
+
def reset(self) -> None:
|
| 22 |
+
self.steps = self.distance = self.scroll_fraction = self.last_action = 0
|
| 23 |
+
self.next_spike = 1
|
| 24 |
+
self.next_ball = 2
|
| 25 |
+
self.multiplier = 100
|
| 26 |
+
self.spikes: list[tuple[int, int, int]] = []
|
| 27 |
+
self.balls: list[tuple[int, int]] = []
|
| 28 |
+
self.terminal = False
|
| 29 |
+
|
| 30 |
+
@classmethod
|
| 31 |
+
def normalize_score(cls, score: float) -> float:
|
| 32 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 33 |
+
|
| 34 |
+
@property
|
| 35 |
+
def row(self) -> int:
|
| 36 |
+
return ORBIT[self.steps % len(ORBIT)]
|
| 37 |
+
|
| 38 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 39 |
+
if self.terminal:
|
| 40 |
+
return 0.0, True
|
| 41 |
+
if self.random.random() < self.sticky_action_prob:
|
| 42 |
+
action = self.last_action
|
| 43 |
+
self.last_action = action
|
| 44 |
+
self.steps += 1
|
| 45 |
+
self.scroll_fraction += 10 if action == 5 else 1
|
| 46 |
+
advance, self.scroll_fraction = divmod(self.scroll_fraction, 10)
|
| 47 |
+
self.distance += advance
|
| 48 |
+
moved_spikes = []
|
| 49 |
+
for row, column, velocity in self.spikes:
|
| 50 |
+
column += advance
|
| 51 |
+
if column > 9:
|
| 52 |
+
continue
|
| 53 |
+
if velocity and self.steps % 4 == 0:
|
| 54 |
+
if not 1 <= row + velocity <= 8:
|
| 55 |
+
velocity = -velocity
|
| 56 |
+
row += velocity
|
| 57 |
+
moved_spikes.append((row, column, velocity))
|
| 58 |
+
self.spikes = moved_spikes
|
| 59 |
+
if self.distance == self.next_spike:
|
| 60 |
+
row = int(self.random.integers(1, 9))
|
| 61 |
+
velocity = int(self.random.choice((-1, 1))) if self.random.random() < 0.2 else 0
|
| 62 |
+
self.spikes.append((row, 0, velocity))
|
| 63 |
+
self.next_spike += int(self.random.integers(1, 6))
|
| 64 |
+
self.terminal = any(column == 9 and row == self.row - 1 for row, column, _ in self.spikes)
|
| 65 |
+
if self.terminal:
|
| 66 |
+
return 0.0, True
|
| 67 |
+
reward = 0
|
| 68 |
+
remaining = []
|
| 69 |
+
for row, column in self.balls:
|
| 70 |
+
column += advance
|
| 71 |
+
if column == 9 and row in (self.row - 1, self.row):
|
| 72 |
+
reward += self.multiplier // 100
|
| 73 |
+
self.multiplier = min(99_900, self.multiplier + 1_000)
|
| 74 |
+
elif column <= 9:
|
| 75 |
+
remaining.append((row, column))
|
| 76 |
+
self.balls = remaining
|
| 77 |
+
if self.distance == self.next_ball:
|
| 78 |
+
row = int(self.random.integers(2, 8))
|
| 79 |
+
if any(spike_row == row and column == 0 for spike_row, column, _ in self.spikes):
|
| 80 |
+
self.next_ball += 1
|
| 81 |
+
else:
|
| 82 |
+
self.balls.append((row, 0))
|
| 83 |
+
self.next_ball += int(self.random.integers(3, 8))
|
| 84 |
+
self.multiplier = max(100, self.multiplier - 2)
|
| 85 |
+
self.terminal = self.steps >= self.horizon
|
| 86 |
+
return float(reward), self.terminal
|
| 87 |
+
|
| 88 |
+
def state(self) -> np.ndarray:
|
| 89 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 90 |
+
state[self.row, 9, 0] = True
|
| 91 |
+
state[self.row - 1, 9, 3] = True
|
| 92 |
+
state[ORBIT[(self.steps + 1) % len(ORBIT)], 9, 4] = True
|
| 93 |
+
for row, column in self.balls:
|
| 94 |
+
state[row, column, 2] = True
|
| 95 |
+
for row, column, velocity in self.spikes:
|
| 96 |
+
state[row, column, 1] = True
|
| 97 |
+
state[row, column, 4] = velocity == 1
|
| 98 |
+
state[row, column, 5] = velocity == -1
|
| 99 |
+
gauges = ((self.steps, 9), (self.scroll_fraction, 4), (self.multiplier, 17),
|
| 100 |
+
(self.next_spike - self.distance, 3), (self.next_ball - self.distance, 3),
|
| 101 |
+
(int(self.last_action == 5), 1))
|
| 102 |
+
offset = 0
|
| 103 |
+
for value, width in gauges:
|
| 104 |
+
for bit in range(width):
|
| 105 |
+
index = offset + bit
|
| 106 |
+
state[index // 9, index % 9, 3] = bool(value & (1 << bit))
|
| 107 |
+
offset += width
|
| 108 |
+
return state
|
runtime/minatar_synth/games/balloon.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from math import hypot
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
BONUS_INTERVAL = 8
|
| 9 |
+
BALLOON_INTERVAL = 28
|
| 10 |
+
MAX_BONUSES = (HORIZON + BONUS_INTERVAL - 1) // BONUS_INTERVAL
|
| 11 |
+
MOVES = {1: (0, -1), 2: (-1, 0), 3: (0, 1), 4: (1, 0)}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@dataclass
|
| 15 |
+
class Balloon:
|
| 16 |
+
row: float
|
| 17 |
+
column: float
|
| 18 |
+
drift: float
|
| 19 |
+
rise: float = -0.24
|
| 20 |
+
radius: float = 1.8
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@dataclass
|
| 24 |
+
class Bonus:
|
| 25 |
+
row: float
|
| 26 |
+
column: float
|
| 27 |
+
balloon: Balloon | None = None
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class BalloonEnvironment:
|
| 31 |
+
score_cap = MAX_BONUSES * (MAX_BONUSES + 1) // 2
|
| 32 |
+
|
| 33 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 34 |
+
self.sticky_action_prob = sticky_action_prob
|
| 35 |
+
self.horizon = min(horizon, HORIZON)
|
| 36 |
+
self.seed(0)
|
| 37 |
+
self.reset()
|
| 38 |
+
|
| 39 |
+
def seed(self, seed: int) -> None:
|
| 40 |
+
self.random = np.random.default_rng(seed)
|
| 41 |
+
|
| 42 |
+
def reset(self) -> None:
|
| 43 |
+
self.steps = self.last_action = 0
|
| 44 |
+
self.cursor = (5, 5)
|
| 45 |
+
self.wind = (0.0, 0.0)
|
| 46 |
+
self.balloons: list[Balloon] = []
|
| 47 |
+
self.bonuses: list[Bonus] = []
|
| 48 |
+
self.terminal = False
|
| 49 |
+
|
| 50 |
+
@classmethod
|
| 51 |
+
def normalize_score(cls, score: float) -> float:
|
| 52 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 53 |
+
|
| 54 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 55 |
+
if self.terminal:
|
| 56 |
+
return 0.0, True
|
| 57 |
+
if self.random.random() < self.sticky_action_prob:
|
| 58 |
+
action = self.last_action
|
| 59 |
+
self.last_action = action
|
| 60 |
+
self.steps += 1
|
| 61 |
+
row, column = self.cursor
|
| 62 |
+
dr, dc = MOVES.get(action, (0, 0))
|
| 63 |
+
self.cursor = max(0, min(9, row + dr)), max(0, min(9, column + dc))
|
| 64 |
+
self.wind = (0.5 * self.wind[0] + 0.25 * (self.cursor[0] - row),
|
| 65 |
+
0.5 * self.wind[1] + 0.25 * (self.cursor[1] - column))
|
| 66 |
+
if self.steps % BALLOON_INTERVAL == 1:
|
| 67 |
+
self.balloons.append(Balloon(9.5, float(self.random.uniform(2, 7)),
|
| 68 |
+
float(self.random.uniform(-0.12, 0.12))))
|
| 69 |
+
if self.steps % BONUS_INTERVAL == 1:
|
| 70 |
+
self.bonuses.append(Bonus(0.0, float(self.random.uniform(1, 8))))
|
| 71 |
+
for balloon in self.balloons:
|
| 72 |
+
if hypot(balloon.row - self.cursor[0], balloon.column - self.cursor[1]) < balloon.radius:
|
| 73 |
+
balloon.rise += self.wind[0]
|
| 74 |
+
balloon.drift += self.wind[1]
|
| 75 |
+
balloon.row += balloon.rise
|
| 76 |
+
balloon.column += balloon.drift
|
| 77 |
+
if not balloon.radius <= balloon.column <= 9 - balloon.radius:
|
| 78 |
+
balloon.column = max(balloon.radius, min(9 - balloon.radius, balloon.column))
|
| 79 |
+
balloon.drift *= -0.7
|
| 80 |
+
if balloon.row > 10 - balloon.radius and balloon.rise > 0:
|
| 81 |
+
balloon.rise *= -0.7
|
| 82 |
+
balloon.drift *= 0.9
|
| 83 |
+
balloon.rise += 0.15 * (-0.24 - balloon.rise)
|
| 84 |
+
balloon.radius -= 0.015 * hypot(balloon.drift, balloon.rise)
|
| 85 |
+
self.balloons = [balloon for balloon in self.balloons
|
| 86 |
+
if balloon.row >= balloon.radius and balloon.radius >= 0.8]
|
| 87 |
+
alive = {id(balloon) for balloon in self.balloons}
|
| 88 |
+
collected = 0
|
| 89 |
+
remaining = []
|
| 90 |
+
for bonus in self.bonuses:
|
| 91 |
+
if bonus.balloon is None:
|
| 92 |
+
bonus.row += 0.22
|
| 93 |
+
for balloon in self.balloons:
|
| 94 |
+
if hypot(balloon.row - bonus.row, balloon.column - bonus.column) < 1.3 * balloon.radius:
|
| 95 |
+
bonus.balloon = balloon
|
| 96 |
+
break
|
| 97 |
+
if bonus.balloon is None and bonus.row >= 10:
|
| 98 |
+
self.terminal = True
|
| 99 |
+
if bonus.balloon is not None:
|
| 100 |
+
if id(bonus.balloon) not in alive:
|
| 101 |
+
collected += 1
|
| 102 |
+
continue
|
| 103 |
+
bonus.row, bonus.column = bonus.balloon.row, bonus.balloon.column
|
| 104 |
+
remaining.append(bonus)
|
| 105 |
+
self.bonuses = remaining
|
| 106 |
+
self.terminal |= self.steps >= self.horizon
|
| 107 |
+
return float(collected * (collected + 1) // 2), self.terminal
|
| 108 |
+
|
| 109 |
+
def state(self) -> np.ndarray:
|
| 110 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 111 |
+
state[*self.cursor, 0] = True
|
| 112 |
+
rows, columns = np.ogrid[:10, :10]
|
| 113 |
+
for balloon in self.balloons:
|
| 114 |
+
state[:, :, 1] |= (rows - balloon.row) ** 2 + (columns - balloon.column) ** 2 <= balloon.radius ** 2
|
| 115 |
+
state[max(0, min(9, round(balloon.row + balloon.rise))),
|
| 116 |
+
max(0, min(9, round(balloon.column + balloon.drift))), 4] = True
|
| 117 |
+
for bonus in self.bonuses:
|
| 118 |
+
state[max(0, min(9, round(bonus.row))), round(bonus.column), 3 if bonus.balloon else 2] = True
|
| 119 |
+
values = (self.steps, round(self.wind[0] * 64) + 32,
|
| 120 |
+
round(self.wind[1] * 64) + 32, self.last_action)
|
| 121 |
+
for row, value in enumerate(values):
|
| 122 |
+
for bit in range(9):
|
| 123 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 124 |
+
return state
|
runtime/minatar_synth/games/balls_bombs.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
HORIZON = 400
|
| 7 |
+
GROUND = 32
|
| 8 |
+
UNIT = 4
|
| 9 |
+
SPAWN_INTERVAL = 5
|
| 10 |
+
MAX_BALLS = (HORIZON + SPAWN_INTERVAL - 1) // SPAWN_INTERVAL
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@dataclass
|
| 14 |
+
class Ball:
|
| 15 |
+
x: int
|
| 16 |
+
y: int
|
| 17 |
+
vx: int
|
| 18 |
+
vy: int = 0
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class BallsBombsEnvironment:
|
| 22 |
+
score_cap = MAX_BALLS * (MAX_BALLS + 1) // 2
|
| 23 |
+
|
| 24 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 25 |
+
self.sticky_action_prob = sticky_action_prob
|
| 26 |
+
self.horizon = min(horizon, HORIZON)
|
| 27 |
+
self.seed(0)
|
| 28 |
+
self.reset()
|
| 29 |
+
|
| 30 |
+
def seed(self, seed: int) -> None:
|
| 31 |
+
self.random = np.random.default_rng(seed)
|
| 32 |
+
|
| 33 |
+
def reset(self) -> None:
|
| 34 |
+
self.steps = self.last_action = self.scroll = 0
|
| 35 |
+
self.player = 2
|
| 36 |
+
self.next_bomb = 6
|
| 37 |
+
self.bomb: int | None = None
|
| 38 |
+
self.armed = False
|
| 39 |
+
self.blast: tuple[int, int] | None = None
|
| 40 |
+
self.multiplier = 1
|
| 41 |
+
self.balls: list[Ball] = []
|
| 42 |
+
self.terminal = False
|
| 43 |
+
|
| 44 |
+
@classmethod
|
| 45 |
+
def normalize_score(cls, score: float) -> float:
|
| 46 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 47 |
+
|
| 48 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 49 |
+
if self.terminal:
|
| 50 |
+
return 0.0, True
|
| 51 |
+
if self.random.random() < self.sticky_action_prob:
|
| 52 |
+
action = self.last_action
|
| 53 |
+
self.last_action = action
|
| 54 |
+
self.steps += 1
|
| 55 |
+
if action == 5:
|
| 56 |
+
self.player += 1
|
| 57 |
+
self.scroll = max(0, self.player - 4)
|
| 58 |
+
if not self.armed and self.bomb is None and self.player >= self.next_bomb:
|
| 59 |
+
self.bomb = self.scroll + 9
|
| 60 |
+
if self.bomb == self.player:
|
| 61 |
+
self.armed = True
|
| 62 |
+
self.bomb = None
|
| 63 |
+
self.next_bomb = self.player + int(self.random.integers(9, 13))
|
| 64 |
+
if self.blast is not None:
|
| 65 |
+
center, radius = self.blast
|
| 66 |
+
self.blast = (center, radius + 1) if radius < 6 else None
|
| 67 |
+
if self.blast is None:
|
| 68 |
+
self.multiplier = 1
|
| 69 |
+
if self.steps % SPAWN_INTERVAL == 1:
|
| 70 |
+
from_left = self.random.random() < 0.1
|
| 71 |
+
self.balls.append(Ball(self.scroll + (0 if from_left else 9),
|
| 72 |
+
int(self.random.integers(5, 17)), 1 if from_left else -1))
|
| 73 |
+
reward = 0
|
| 74 |
+
survivors = []
|
| 75 |
+
for ball in self.balls:
|
| 76 |
+
ball.vy += 1
|
| 77 |
+
ball.y += ball.vy
|
| 78 |
+
if ball.y >= GROUND:
|
| 79 |
+
ball.y = GROUND
|
| 80 |
+
ball.vy = -ball.vy
|
| 81 |
+
if self.steps % 2 == 0:
|
| 82 |
+
ball.x += ball.vx
|
| 83 |
+
if ball.x <= self.scroll and ball.vx < 0:
|
| 84 |
+
ball.vx = 2
|
| 85 |
+
elif ball.x >= self.scroll + 9 and ball.vx > 0:
|
| 86 |
+
ball.vx = -1
|
| 87 |
+
if self._in_blast(ball.y // UNIT, ball.x):
|
| 88 |
+
reward += self.multiplier
|
| 89 |
+
self.multiplier += 1
|
| 90 |
+
elif ball.x == self.player and ball.y // UNIT == GROUND // UNIT:
|
| 91 |
+
if self.armed:
|
| 92 |
+
self.armed = False
|
| 93 |
+
self.blast = self.player, 1
|
| 94 |
+
else:
|
| 95 |
+
self.terminal = True
|
| 96 |
+
elif ball.x >= self.scroll - 2:
|
| 97 |
+
survivors.append(ball)
|
| 98 |
+
self.balls = survivors
|
| 99 |
+
self.terminal |= self.steps >= self.horizon
|
| 100 |
+
return float(reward), self.terminal
|
| 101 |
+
|
| 102 |
+
def _in_blast(self, row: int, x: int) -> bool:
|
| 103 |
+
if self.blast is None:
|
| 104 |
+
return False
|
| 105 |
+
center, radius = self.blast
|
| 106 |
+
distance_squared = (x - center) ** 2 + (GROUND // UNIT - row) ** 2
|
| 107 |
+
return (radius - 0.75) ** 2 <= distance_squared <= (radius + 0.75) ** 2
|
| 108 |
+
|
| 109 |
+
def state(self) -> np.ndarray:
|
| 110 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 111 |
+
state[GROUND // UNIT, self.player - self.scroll, 0] = True
|
| 112 |
+
for ball in self.balls:
|
| 113 |
+
column = ball.x - self.scroll
|
| 114 |
+
if 0 <= column < 10:
|
| 115 |
+
row = max(0, ball.y // UNIT)
|
| 116 |
+
state[row, column, 1] = True
|
| 117 |
+
state[row, column, 4] |= ball.vy < 0
|
| 118 |
+
if self.bomb is not None and 0 <= self.bomb - self.scroll < 10:
|
| 119 |
+
state[GROUND // UNIT, self.bomb - self.scroll, 2] = True
|
| 120 |
+
if self.blast is not None:
|
| 121 |
+
for row in range(9):
|
| 122 |
+
for column in range(10):
|
| 123 |
+
state[row, column, 3] = self._in_blast(row, column + self.scroll)
|
| 124 |
+
values = (self.steps, self.multiplier, int(self.armed), self.player,
|
| 125 |
+
self.next_bomb, self.last_action, 0 if self.blast is None else self.blast[1])
|
| 126 |
+
for row, value in enumerate(values):
|
| 127 |
+
for bit in range(9):
|
| 128 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 129 |
+
return state
|
runtime/minatar_synth/games/bamboo.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
FRAMES_PER_STEP = 4
|
| 9 |
+
MIN_SPAWN_INTERVAL = 70
|
| 10 |
+
MAX_HARVEST = 100
|
| 11 |
+
TRACK_WIDTH = 200
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@dataclass
|
| 15 |
+
class Stalk:
|
| 16 |
+
x: int
|
| 17 |
+
height: float = 0
|
| 18 |
+
speed: float = 1
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class BambooEnvironment:
|
| 22 |
+
score_cap = math.ceil(HORIZON * FRAMES_PER_STEP / MIN_SPAWN_INTERVAL) * MAX_HARVEST
|
| 23 |
+
|
| 24 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 25 |
+
self.sticky_action_prob = sticky_action_prob
|
| 26 |
+
self.horizon = min(horizon, HORIZON)
|
| 27 |
+
self.seed(0)
|
| 28 |
+
self.reset()
|
| 29 |
+
|
| 30 |
+
def seed(self, seed: int) -> None:
|
| 31 |
+
self.random = np.random.default_rng(seed)
|
| 32 |
+
|
| 33 |
+
def reset(self) -> None:
|
| 34 |
+
self.steps = self.spawn_timer = 0
|
| 35 |
+
self.pressed = self.terminal = False
|
| 36 |
+
self.x = 190.0
|
| 37 |
+
self.direction = 1
|
| 38 |
+
self.boost = 0.0
|
| 39 |
+
self.stalks: list[Stalk] = []
|
| 40 |
+
|
| 41 |
+
@classmethod
|
| 42 |
+
def normalize_score(cls, score: float) -> float:
|
| 43 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 44 |
+
|
| 45 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 46 |
+
if self.terminal:
|
| 47 |
+
return 0.0, True
|
| 48 |
+
pressed = self.pressed if self.random.random() < self.sticky_action_prob else action == 5
|
| 49 |
+
if pressed and not self.pressed:
|
| 50 |
+
self.direction *= -1
|
| 51 |
+
self.pressed = pressed
|
| 52 |
+
self.steps += 1
|
| 53 |
+
reward = 0
|
| 54 |
+
for _ in range(FRAMES_PER_STEP):
|
| 55 |
+
reward += self._advance_frame()
|
| 56 |
+
if self.terminal:
|
| 57 |
+
break
|
| 58 |
+
self.terminal |= self.steps >= self.horizon
|
| 59 |
+
return float(reward), self.terminal
|
| 60 |
+
|
| 61 |
+
def _advance_frame(self) -> int:
|
| 62 |
+
self.x = (self.x + self.direction * (1 + self.boost)) % TRACK_WIDTH
|
| 63 |
+
self.boost *= 0.9
|
| 64 |
+
if self.spawn_timer == 0:
|
| 65 |
+
self.stalks.append(Stalk(int(self.random.integers(5, 195))))
|
| 66 |
+
self.spawn_timer = int(self.random.integers(MIN_SPAWN_INTERVAL, 101))
|
| 67 |
+
self.spawn_timer -= 1
|
| 68 |
+
|
| 69 |
+
reward = 0
|
| 70 |
+
survivors = []
|
| 71 |
+
for stalk in self.stalks:
|
| 72 |
+
stalk.height += 0.14 * stalk.speed
|
| 73 |
+
distance = abs(self.x - stalk.x)
|
| 74 |
+
if not self.pressed and min(distance, TRACK_WIDTH - distance) <= 6:
|
| 75 |
+
if 5 <= stalk.height <= 25:
|
| 76 |
+
grade = math.ceil((stalk.height - 5) / 3)
|
| 77 |
+
reward += MAX_HARVEST if grade == 7 else grade**2
|
| 78 |
+
continue
|
| 79 |
+
if stalk.height > 25:
|
| 80 |
+
stalk.height *= 0.7
|
| 81 |
+
stalk.speed *= 0.6
|
| 82 |
+
self.direction *= -1
|
| 83 |
+
self.boost = min(5, self.boost + 1)
|
| 84 |
+
if stalk.height <= 25:
|
| 85 |
+
continue
|
| 86 |
+
if stalk.height > 50:
|
| 87 |
+
stalk.speed *= 0.997
|
| 88 |
+
if stalk.height >= 89:
|
| 89 |
+
self.terminal = True
|
| 90 |
+
survivors.append(stalk)
|
| 91 |
+
self.stalks = survivors
|
| 92 |
+
return reward
|
| 93 |
+
|
| 94 |
+
def state(self) -> np.ndarray:
|
| 95 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 96 |
+
column = int(self.x) // 20
|
| 97 |
+
state[8, column, 0] = True
|
| 98 |
+
state[8, column, 1] = self.direction < 0
|
| 99 |
+
for stalk in self.stalks:
|
| 100 |
+
column = stalk.x // 20
|
| 101 |
+
height = max(1, math.ceil(stalk.height / 10))
|
| 102 |
+
state[9 - height:9, column, 2] = True
|
| 103 |
+
state[9 - height, column, 3] |= 5 <= stalk.height <= 25
|
| 104 |
+
state[9 - height, column, 4] |= 23 < stalk.height <= 25
|
| 105 |
+
state[9, 0, 5] = self.pressed
|
| 106 |
+
for bit in range(9):
|
| 107 |
+
state[9, bit + 1, 5] = bool(self.steps & (1 << bit))
|
| 108 |
+
return state
|
runtime/minatar_synth/games/bank_heist.py
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
ROADS = np.array([
|
| 5 |
+
[1, 1, 1, 0, 0, 0, 1, 1, 1, 1],
|
| 6 |
+
[1, 0, 1, 1, 1, 1, 1, 0, 1, 0],
|
| 7 |
+
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
|
| 8 |
+
[1, 1, 1, 0, 1, 1, 1, 1, 1, 1],
|
| 9 |
+
[0, 0, 1, 0, 0, 0, 1, 0, 1, 0],
|
| 10 |
+
[1, 1, 1, 1, 1, 0, 1, 0, 1, 0],
|
| 11 |
+
[1, 0, 0, 0, 1, 0, 1, 1, 1, 0],
|
| 12 |
+
[1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
|
| 13 |
+
[1, 0, 1, 0, 0, 0, 1, 0, 1, 0],
|
| 14 |
+
[1, 1, 1, 1, 1, 1, 1, 0, 1, 0],
|
| 15 |
+
], dtype=bool)
|
| 16 |
+
BANK_LOCATIONS = ((1, 2), (1, 6), (3, 4), (3, 8), (5, 2), (5, 6), (7, 4), (7, 8), (9, 2))
|
| 17 |
+
TOWN_BONUSES = (93.0, 186.0, 279.0, 372.0)
|
| 18 |
+
DELTAS = {1: (0, -1), 2: (-1, 0), 3: (0, 1), 4: (1, 0)}
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class BankHeistEnvironment:
|
| 22 |
+
"""Four-town robbery route with fuel, pursuing cops, and tailpipe dynamite."""
|
| 23 |
+
|
| 24 |
+
score_cap = 4_530.0
|
| 25 |
+
|
| 26 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = 1_500):
|
| 27 |
+
self.sticky_action_prob = sticky_action_prob
|
| 28 |
+
self.horizon = horizon
|
| 29 |
+
self.random = np.random.RandomState()
|
| 30 |
+
self.seed(0)
|
| 31 |
+
self.reset()
|
| 32 |
+
|
| 33 |
+
@classmethod
|
| 34 |
+
def normalize_score(cls, raw_score: float) -> float:
|
| 35 |
+
return min(max(raw_score, 0.0), cls.score_cap) / cls.score_cap
|
| 36 |
+
|
| 37 |
+
def seed(self, seed: int) -> None:
|
| 38 |
+
self.random.seed(seed)
|
| 39 |
+
|
| 40 |
+
def reset(self) -> None:
|
| 41 |
+
self.town = 0
|
| 42 |
+
self.cars = 5
|
| 43 |
+
self.steps = 0
|
| 44 |
+
self.last_action = 0
|
| 45 |
+
self.terminal = False
|
| 46 |
+
self._start_town(full_fuel=True)
|
| 47 |
+
|
| 48 |
+
def _start_town(self, full_fuel: bool) -> None:
|
| 49 |
+
previous_fuel = getattr(self, "fuel", 100)
|
| 50 |
+
self.fuel = 100 if full_fuel else max(previous_fuel, 50)
|
| 51 |
+
self.player = [9, 0]
|
| 52 |
+
self.previous_player = self.player.copy()
|
| 53 |
+
self.direction = 3
|
| 54 |
+
self.cops: list[dict[str, object]] = []
|
| 55 |
+
self.previous_cops: list[list[int]] = []
|
| 56 |
+
self.dynamite: list[dict[str, int]] = []
|
| 57 |
+
self.robbed = 0
|
| 58 |
+
self.spawned_banks = 3
|
| 59 |
+
self.banks = set(BANK_LOCATIONS[:3])
|
| 60 |
+
|
| 61 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 62 |
+
if self.terminal:
|
| 63 |
+
return 0.0, True
|
| 64 |
+
if self.random.rand() < self.sticky_action_prob:
|
| 65 |
+
action = self.last_action
|
| 66 |
+
self.last_action = action
|
| 67 |
+
self.previous_player = self.player.copy()
|
| 68 |
+
if action in DELTAS:
|
| 69 |
+
self.direction = action
|
| 70 |
+
self._move_player(action)
|
| 71 |
+
elif action == 5 and self.fuel > 5:
|
| 72 |
+
row_delta, column_delta = DELTAS[self.direction]
|
| 73 |
+
self.dynamite.append({
|
| 74 |
+
"row": (self.player[0] - row_delta) % 10,
|
| 75 |
+
"column": (self.player[1] - column_delta) % 10,
|
| 76 |
+
"timer": 2,
|
| 77 |
+
})
|
| 78 |
+
self.fuel -= 5
|
| 79 |
+
|
| 80 |
+
reward = self._rob_bank()
|
| 81 |
+
self.previous_cops = [list(cop["position"]) for cop in self.cops]
|
| 82 |
+
self._move_cops()
|
| 83 |
+
reward += self._detonate_dynamite()
|
| 84 |
+
if any(list(cop["position"]) == self.player for cop in self.cops):
|
| 85 |
+
self._lose_car()
|
| 86 |
+
self.fuel -= 1
|
| 87 |
+
if self.fuel <= 0:
|
| 88 |
+
self._lose_car()
|
| 89 |
+
if self.player == [0, 9]:
|
| 90 |
+
reward += self._leave_town()
|
| 91 |
+
self.steps += 1
|
| 92 |
+
if self.steps >= self.horizon:
|
| 93 |
+
self.terminal = True
|
| 94 |
+
return reward, self.terminal
|
| 95 |
+
|
| 96 |
+
def _move_player(self, action: int) -> None:
|
| 97 |
+
row_delta, column_delta = DELTAS[action]
|
| 98 |
+
target = [self.player[0] + row_delta, self.player[1] + column_delta]
|
| 99 |
+
if self.player[0] in (3, 7) and target[1] < 0:
|
| 100 |
+
target[1] = 9
|
| 101 |
+
elif self.player[0] in (3, 7) and target[1] > 9:
|
| 102 |
+
target[1] = 0
|
| 103 |
+
if 0 <= target[0] < 10 and 0 <= target[1] < 10 and ROADS[*target]:
|
| 104 |
+
self.player = target
|
| 105 |
+
|
| 106 |
+
def _rob_bank(self) -> float:
|
| 107 |
+
position = tuple(self.player)
|
| 108 |
+
if position not in self.banks:
|
| 109 |
+
return 0.0
|
| 110 |
+
self.banks.remove(position)
|
| 111 |
+
self.robbed += 1
|
| 112 |
+
if len(self.cops) < 3:
|
| 113 |
+
self.cops.append({"position": [*position], "previous": [*position]})
|
| 114 |
+
return float(self.robbed * 10)
|
| 115 |
+
|
| 116 |
+
def _move_cops(self) -> None:
|
| 117 |
+
for cop in self.cops:
|
| 118 |
+
position = list(cop["position"])
|
| 119 |
+
previous = list(cop["previous"])
|
| 120 |
+
candidates = []
|
| 121 |
+
for action in DELTAS:
|
| 122 |
+
row_delta, column_delta = DELTAS[action]
|
| 123 |
+
target = [position[0] + row_delta, position[1] + column_delta]
|
| 124 |
+
if position[0] in (3, 7) and target[1] < 0:
|
| 125 |
+
target[1] = 9
|
| 126 |
+
elif position[0] in (3, 7) and target[1] > 9:
|
| 127 |
+
target[1] = 0
|
| 128 |
+
if 0 <= target[0] < 10 and 0 <= target[1] < 10 and ROADS[*target] and target != previous:
|
| 129 |
+
distance = abs(target[0] - self.player[0]) + abs(target[1] - self.player[1])
|
| 130 |
+
candidates.append((distance, action, target))
|
| 131 |
+
if candidates:
|
| 132 |
+
cop["previous"] = position
|
| 133 |
+
cop["position"] = min(candidates)[2]
|
| 134 |
+
|
| 135 |
+
def _detonate_dynamite(self) -> float:
|
| 136 |
+
reward = 0.0
|
| 137 |
+
remaining = []
|
| 138 |
+
for stick in self.dynamite:
|
| 139 |
+
stick["timer"] -= 1
|
| 140 |
+
if stick["timer"] > 0:
|
| 141 |
+
remaining.append(stick)
|
| 142 |
+
continue
|
| 143 |
+
blast = [stick["row"], stick["column"]]
|
| 144 |
+
if blast == self.player:
|
| 145 |
+
self._lose_car()
|
| 146 |
+
pursuit_count = len(self.cops)
|
| 147 |
+
value = {1: 10.0, 2: 30.0, 3: 50.0}.get(pursuit_count, 0.0)
|
| 148 |
+
destroyed = [cop for cop in self.cops if list(cop["position"]) == blast]
|
| 149 |
+
for cop in destroyed:
|
| 150 |
+
self.cops.remove(cop)
|
| 151 |
+
reward += value
|
| 152 |
+
if self.spawned_banks < 9:
|
| 153 |
+
self.banks.add(BANK_LOCATIONS[self.spawned_banks])
|
| 154 |
+
self.spawned_banks += 1
|
| 155 |
+
self.dynamite = remaining
|
| 156 |
+
return reward
|
| 157 |
+
|
| 158 |
+
def _lose_car(self) -> None:
|
| 159 |
+
self.cars -= 1
|
| 160 |
+
self.player = [9, 0]
|
| 161 |
+
self.previous_player = self.player.copy()
|
| 162 |
+
self.fuel = 100
|
| 163 |
+
self.cops.clear()
|
| 164 |
+
self.dynamite.clear()
|
| 165 |
+
if self.cars <= 0:
|
| 166 |
+
self.terminal = True
|
| 167 |
+
|
| 168 |
+
def _leave_town(self) -> float:
|
| 169 |
+
reward = TOWN_BONUSES[self.town] if self.robbed >= 9 else 0.0
|
| 170 |
+
full_fuel = self.robbed >= 9
|
| 171 |
+
if full_fuel:
|
| 172 |
+
self.cars += 1
|
| 173 |
+
self.town += 1
|
| 174 |
+
if self.town == 4:
|
| 175 |
+
self.terminal = True
|
| 176 |
+
else:
|
| 177 |
+
self._start_town(full_fuel=full_fuel)
|
| 178 |
+
return reward
|
| 179 |
+
|
| 180 |
+
def state(self) -> np.ndarray:
|
| 181 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 182 |
+
state[*self.player, 0] = True
|
| 183 |
+
for cop in self.cops:
|
| 184 |
+
state[*list(cop["position"]), 1] = True
|
| 185 |
+
for bank in self.banks:
|
| 186 |
+
state[*bank, 2] = True
|
| 187 |
+
state[0, 9, 2] = True
|
| 188 |
+
for stick in self.dynamite:
|
| 189 |
+
state[stick["row"], stick["column"], 3] = True
|
| 190 |
+
state[*self.previous_player, 4] = True
|
| 191 |
+
for position in self.previous_cops:
|
| 192 |
+
state[*position, 4] = True
|
| 193 |
+
state[9, : min(10, (self.fuel + 9) // 10), 5] = True
|
| 194 |
+
state[8, : min(10, self.cars), 5] = True
|
| 195 |
+
state[7, : self.town + 1, 5] = True
|
| 196 |
+
state[6, : min(9, self.robbed), 5] = True
|
| 197 |
+
return state
|
runtime/minatar_synth/games/barge_hop.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from itertools import combinations
|
| 3 |
+
import math
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
HORIZON = 400
|
| 9 |
+
TIME_LIMIT = 1000
|
| 10 |
+
FRAMES_PER_STEP = 4
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@dataclass
|
| 14 |
+
class Barge:
|
| 15 |
+
x: float
|
| 16 |
+
width: float
|
| 17 |
+
velocity: float
|
| 18 |
+
phase: float
|
| 19 |
+
frequency: float
|
| 20 |
+
amplitude: float
|
| 21 |
+
top: float = 65
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class BargeHopEnvironment:
|
| 25 |
+
score_cap = 1000
|
| 26 |
+
|
| 27 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 28 |
+
self.sticky_action_prob = sticky_action_prob
|
| 29 |
+
self.horizon = min(horizon, HORIZON)
|
| 30 |
+
self.seed(0)
|
| 31 |
+
self.reset()
|
| 32 |
+
|
| 33 |
+
def seed(self, seed: int) -> None:
|
| 34 |
+
self.random = np.random.default_rng(seed)
|
| 35 |
+
|
| 36 |
+
def reset(self) -> None:
|
| 37 |
+
self.steps = self.frame = self.level = 0
|
| 38 |
+
self.pressed = self.terminal = False
|
| 39 |
+
self.remaining = float(TIME_LIMIT)
|
| 40 |
+
self._setup_round()
|
| 41 |
+
|
| 42 |
+
def _setup_round(self) -> None:
|
| 43 |
+
speed = 0.175 * (1 + math.sqrt(self.level * 0.3))
|
| 44 |
+
self.barges = [
|
| 45 |
+
Barge(
|
| 46 |
+
center + self.random.uniform(-4, 4),
|
| 47 |
+
float(self.random.integers(20, 29)),
|
| 48 |
+
float(self.random.choice((-1, 1))) * speed * self.random.uniform(0.9, 1.5),
|
| 49 |
+
self.random.uniform(0, math.tau),
|
| 50 |
+
self.random.uniform(0.02, 0.05),
|
| 51 |
+
self.random.uniform(1, 2),
|
| 52 |
+
)
|
| 53 |
+
for center in (32, 75, 118)
|
| 54 |
+
]
|
| 55 |
+
self._return_to_dock()
|
| 56 |
+
|
| 57 |
+
def _return_to_dock(self) -> None:
|
| 58 |
+
self.x, self.feet = 8.0, 63.0
|
| 59 |
+
self.vx = self.vy = 0.0
|
| 60 |
+
self.grounded = True
|
| 61 |
+
self.support: Barge | None = None
|
| 62 |
+
|
| 63 |
+
@classmethod
|
| 64 |
+
def normalize_score(cls, score: float) -> float:
|
| 65 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 66 |
+
|
| 67 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 68 |
+
if self.terminal:
|
| 69 |
+
return 0.0, True
|
| 70 |
+
pressed = self.pressed if self.random.random() < self.sticky_action_prob else action == 5
|
| 71 |
+
jump = pressed and not self.pressed
|
| 72 |
+
self.pressed = pressed
|
| 73 |
+
self.steps += 1
|
| 74 |
+
reward = 0
|
| 75 |
+
for frame in range(FRAMES_PER_STEP):
|
| 76 |
+
reward += self._advance_frame(jump and frame == 0)
|
| 77 |
+
if self.terminal:
|
| 78 |
+
break
|
| 79 |
+
self.terminal |= self.steps >= self.horizon
|
| 80 |
+
return float(reward), self.terminal
|
| 81 |
+
|
| 82 |
+
def _move_barges(self) -> None:
|
| 83 |
+
for barge in self.barges:
|
| 84 |
+
barge.x += barge.velocity
|
| 85 |
+
barge.top = 65 - barge.amplitude * (
|
| 86 |
+
1 - math.sin(self.frame * barge.frequency + barge.phase)
|
| 87 |
+
)
|
| 88 |
+
low, high = 15 + barge.width / 2, 135 - barge.width / 2
|
| 89 |
+
if barge.x <= low:
|
| 90 |
+
barge.x, barge.velocity = low, abs(barge.velocity)
|
| 91 |
+
elif barge.x >= high:
|
| 92 |
+
barge.x, barge.velocity = high, -abs(barge.velocity)
|
| 93 |
+
for first, second in combinations(self.barges, 2):
|
| 94 |
+
gap = (first.width + second.width) / 2 - (second.x - first.x)
|
| 95 |
+
if gap <= 0:
|
| 96 |
+
continue
|
| 97 |
+
first.x -= gap / 2
|
| 98 |
+
second.x += gap / 2
|
| 99 |
+
if first.velocity > 0:
|
| 100 |
+
first.velocity *= -1
|
| 101 |
+
if second.velocity < 0:
|
| 102 |
+
second.velocity *= -1
|
| 103 |
+
|
| 104 |
+
def _advance_frame(self, jump: bool) -> int:
|
| 105 |
+
difficulty = 1 + self.level / 5
|
| 106 |
+
self.frame += 1
|
| 107 |
+
self.remaining -= math.sqrt(difficulty)
|
| 108 |
+
if self.remaining <= 0:
|
| 109 |
+
self.terminal = True
|
| 110 |
+
return 0
|
| 111 |
+
old_support_x = self.support.x if self.support is not None else 0
|
| 112 |
+
self._move_barges()
|
| 113 |
+
if self.grounded:
|
| 114 |
+
if self.support is not None:
|
| 115 |
+
self.x += self.support.x - old_support_x
|
| 116 |
+
self.feet = self.support.top
|
| 117 |
+
if not jump:
|
| 118 |
+
return 0
|
| 119 |
+
self.grounded = False
|
| 120 |
+
self.support = None
|
| 121 |
+
self.vx = 0.7 * math.sqrt(difficulty)
|
| 122 |
+
self.vy = -2.1 * math.sqrt(difficulty)
|
| 123 |
+
previous_feet = self.feet
|
| 124 |
+
self.vy += 0.15 * (0.5 if self.pressed else 1.5) * difficulty
|
| 125 |
+
self.x += self.vx
|
| 126 |
+
self.feet += self.vy
|
| 127 |
+
if self.vy < 0:
|
| 128 |
+
return 0
|
| 129 |
+
if self.x >= 133 and previous_feet <= 63 <= self.feet:
|
| 130 |
+
reward = int(120 * self.remaining / TIME_LIMIT)
|
| 131 |
+
self.level += 1
|
| 132 |
+
self.remaining = min(TIME_LIMIT, self.remaining + TIME_LIMIT / 2)
|
| 133 |
+
self._setup_round()
|
| 134 |
+
return reward
|
| 135 |
+
for barge in self.barges:
|
| 136 |
+
if abs(self.x - barge.x) <= barge.width / 2 + 3 and previous_feet <= barge.top <= self.feet:
|
| 137 |
+
self.support = barge
|
| 138 |
+
self.grounded = True
|
| 139 |
+
self.feet = barge.top
|
| 140 |
+
self.vx = self.vy = 0
|
| 141 |
+
return 1
|
| 142 |
+
if self.feet >= 71:
|
| 143 |
+
self._return_to_dock()
|
| 144 |
+
return 0
|
| 145 |
+
|
| 146 |
+
def state(self) -> np.ndarray:
|
| 147 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 148 |
+
column = min(9, int(self.x / 15))
|
| 149 |
+
row = int((self.feet - 4) / 10)
|
| 150 |
+
state[row, column, 0] = True
|
| 151 |
+
state[row, column, 4] = self.grounded
|
| 152 |
+
state[6, (0, 9), 1] = True
|
| 153 |
+
state[7:9, :, 2] = True
|
| 154 |
+
for barge in self.barges:
|
| 155 |
+
left = max(0, int((barge.x - barge.width / 2) / 15))
|
| 156 |
+
right = min(9, int((barge.x + barge.width / 2) / 15))
|
| 157 |
+
state[int(barge.top / 10), left:right + 1, 3] = True
|
| 158 |
+
state[int(barge.top / 10), left:right + 1, 5] |= barge.velocity < 0
|
| 159 |
+
state[9, :math.ceil(max(0, self.remaining) / 100), 5] = True
|
| 160 |
+
state[8, 0, 5] = self.pressed
|
| 161 |
+
return state
|
runtime/minatar_synth/games/baroll.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
HORIZON = 400
|
| 7 |
+
FRAMES_PER_STEP = 4
|
| 8 |
+
GROUND = 86
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@dataclass
|
| 12 |
+
class Barrel:
|
| 13 |
+
x: float
|
| 14 |
+
y: float = -5
|
| 15 |
+
velocity: float = 0
|
| 16 |
+
rolling: bool = False
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class BarollEnvironment:
|
| 20 |
+
score_cap = HORIZON * FRAMES_PER_STEP
|
| 21 |
+
|
| 22 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 23 |
+
self.sticky_action_prob = sticky_action_prob
|
| 24 |
+
self.horizon = min(horizon, HORIZON)
|
| 25 |
+
self.seed(0)
|
| 26 |
+
self.reset()
|
| 27 |
+
|
| 28 |
+
def seed(self, seed: int) -> None:
|
| 29 |
+
self.random = np.random.default_rng(seed)
|
| 30 |
+
|
| 31 |
+
def reset(self) -> None:
|
| 32 |
+
self.steps = self.spawn_timer = 0
|
| 33 |
+
self.pressed = self.terminal = self.jumping = False
|
| 34 |
+
self.y = float(GROUND)
|
| 35 |
+
self.velocity = 0.0
|
| 36 |
+
self.barrels: list[Barrel] = []
|
| 37 |
+
|
| 38 |
+
@classmethod
|
| 39 |
+
def normalize_score(cls, score: float) -> float:
|
| 40 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 41 |
+
|
| 42 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 43 |
+
if self.terminal:
|
| 44 |
+
return 0.0, True
|
| 45 |
+
pressed = self.pressed if self.random.random() < self.sticky_action_prob else action == 5
|
| 46 |
+
tap = pressed and not self.pressed
|
| 47 |
+
self.pressed = pressed
|
| 48 |
+
self.steps += 1
|
| 49 |
+
reward = 0
|
| 50 |
+
for frame in range(FRAMES_PER_STEP):
|
| 51 |
+
self._advance_frame(tap and frame == 0)
|
| 52 |
+
reward += int(not pressed)
|
| 53 |
+
if self.terminal:
|
| 54 |
+
break
|
| 55 |
+
self.terminal |= self.steps >= self.horizon
|
| 56 |
+
return float(reward), self.terminal
|
| 57 |
+
|
| 58 |
+
def _advance_frame(self, tap: bool) -> None:
|
| 59 |
+
if self.spawn_timer == 0:
|
| 60 |
+
start = 100 if self.jumping else 10
|
| 61 |
+
self.barrels.append(Barrel(self.random.uniform(start, 200)))
|
| 62 |
+
self.spawn_timer = int(self.random.integers(30, 90))
|
| 63 |
+
self.spawn_timer -= 1
|
| 64 |
+
for barrel in self.barrels:
|
| 65 |
+
if barrel.rolling:
|
| 66 |
+
barrel.x -= 1
|
| 67 |
+
else:
|
| 68 |
+
barrel.velocity = (barrel.velocity + 0.2) * 0.92
|
| 69 |
+
barrel.y += barrel.velocity
|
| 70 |
+
if barrel.y > 85:
|
| 71 |
+
barrel.y = GROUND
|
| 72 |
+
barrel.rolling = True
|
| 73 |
+
barrel.x -= 1 if self.pressed else 2
|
| 74 |
+
self.barrels = [barrel for barrel in self.barrels if barrel.x > -5]
|
| 75 |
+
|
| 76 |
+
if self.jumping:
|
| 77 |
+
self.y += self.velocity
|
| 78 |
+
self.velocity += 0.1 if self.pressed else 0.2
|
| 79 |
+
if self.y > 85:
|
| 80 |
+
self.y = GROUND
|
| 81 |
+
self.jumping = self.pressed
|
| 82 |
+
self.velocity = -3 if self.pressed else 0
|
| 83 |
+
elif tap:
|
| 84 |
+
self.jumping = True
|
| 85 |
+
self.velocity = -3.6
|
| 86 |
+
self.terminal = any(
|
| 87 |
+
abs(barrel.x - 9) < 5 and abs(barrel.y - self.y) < 6
|
| 88 |
+
for barrel in self.barrels
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
def state(self) -> np.ndarray:
|
| 92 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 93 |
+
row = int(self.y) // 10
|
| 94 |
+
state[row, 0, 0] = True
|
| 95 |
+
state[row, 0, 4] = self.jumping and self.velocity < 0
|
| 96 |
+
for barrel in self.barrels:
|
| 97 |
+
if barrel.x >= 0 and barrel.y >= 0:
|
| 98 |
+
state[int(barrel.y) // 10, int(barrel.x) // 20, 2 if barrel.rolling else 1] = True
|
| 99 |
+
state[9, :, 3] = True
|
| 100 |
+
state[9, 0, 5] = self.pressed
|
| 101 |
+
for bit in range(9):
|
| 102 |
+
state[9, bit + 1, 5] = bool(self.steps & (1 << bit))
|
| 103 |
+
return state
|
runtime/minatar_synth/games/baserunner_dash.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
HORIZON = 400
|
| 7 |
+
FRAMES_PER_STEP = 8
|
| 8 |
+
BASES = ((50, 90), (90, 50), (50, 10), (10, 50))
|
| 9 |
+
LEG_LENGTH = math.dist(BASES[0], BASES[1])
|
| 10 |
+
RUNNER_SPEED = 0.6
|
| 11 |
+
BALL_SPEED = 0.3
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class BaserunnerDashEnvironment:
|
| 15 |
+
score_cap = HORIZON * FRAMES_PER_STEP // math.ceil(LEG_LENGTH / RUNNER_SPEED)
|
| 16 |
+
|
| 17 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 18 |
+
self.sticky_action_prob = sticky_action_prob
|
| 19 |
+
self.horizon = min(horizon, HORIZON)
|
| 20 |
+
self.seed(0)
|
| 21 |
+
self.reset()
|
| 22 |
+
|
| 23 |
+
def seed(self, seed: int) -> None:
|
| 24 |
+
self.random = np.random.default_rng(seed)
|
| 25 |
+
|
| 26 |
+
def reset(self) -> None:
|
| 27 |
+
self.steps = 0
|
| 28 |
+
self.pressed = self.terminal = False
|
| 29 |
+
self.runner_base = 1
|
| 30 |
+
self.progress = 0.0
|
| 31 |
+
self.ball = BASES[0]
|
| 32 |
+
self.ball_base = 0
|
| 33 |
+
self.ball_target = 2
|
| 34 |
+
self.thrown = True
|
| 35 |
+
|
| 36 |
+
@property
|
| 37 |
+
def runner_target(self) -> int:
|
| 38 |
+
return (self.runner_base + 1) % len(BASES)
|
| 39 |
+
|
| 40 |
+
@property
|
| 41 |
+
def runner_position(self) -> tuple[float, float]:
|
| 42 |
+
x, y = BASES[self.runner_base]
|
| 43 |
+
target_x, target_y = BASES[self.runner_target]
|
| 44 |
+
fraction = self.progress / LEG_LENGTH
|
| 45 |
+
return x + (target_x - x) * fraction, y + (target_y - y) * fraction
|
| 46 |
+
|
| 47 |
+
@classmethod
|
| 48 |
+
def normalize_score(cls, score: float) -> float:
|
| 49 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 50 |
+
|
| 51 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 52 |
+
if self.terminal:
|
| 53 |
+
return 0.0, True
|
| 54 |
+
if self.random.random() >= self.sticky_action_prob:
|
| 55 |
+
self.pressed = action == 5
|
| 56 |
+
self.steps += 1
|
| 57 |
+
reward = 0
|
| 58 |
+
for _ in range(FRAMES_PER_STEP):
|
| 59 |
+
reward += self._advance_runner()
|
| 60 |
+
self._advance_ball()
|
| 61 |
+
runner_x, runner_y = self.runner_position
|
| 62 |
+
ball_x, ball_y = self.ball
|
| 63 |
+
if self.progress > 0 and not self.thrown and abs(runner_x - ball_x) < 6 and abs(runner_y - ball_y) < 6:
|
| 64 |
+
self.terminal = True
|
| 65 |
+
break
|
| 66 |
+
self.terminal |= self.steps >= self.horizon
|
| 67 |
+
return float(reward), self.terminal
|
| 68 |
+
|
| 69 |
+
def _advance_runner(self) -> int:
|
| 70 |
+
if not self.pressed:
|
| 71 |
+
self.progress = max(0, self.progress - RUNNER_SPEED)
|
| 72 |
+
return 0
|
| 73 |
+
self.progress += RUNNER_SPEED
|
| 74 |
+
if self.progress < LEG_LENGTH:
|
| 75 |
+
return 0
|
| 76 |
+
self.runner_base = self.runner_target
|
| 77 |
+
self.progress = 0.0
|
| 78 |
+
return 1
|
| 79 |
+
|
| 80 |
+
def _advance_ball(self) -> None:
|
| 81 |
+
x, y = self.ball
|
| 82 |
+
target = BASES[self.ball_target]
|
| 83 |
+
dx, dy = target[0] - x, target[1] - y
|
| 84 |
+
distance = math.hypot(dx, dy)
|
| 85 |
+
speed = BALL_SPEED * (3 if self.thrown else 1)
|
| 86 |
+
if distance <= speed:
|
| 87 |
+
self.ball = target
|
| 88 |
+
self.ball_base = self.ball_target
|
| 89 |
+
self.thrown = False
|
| 90 |
+
self.ball_target = self.runner_target if self.ball_base == self.runner_base else self.runner_base
|
| 91 |
+
return
|
| 92 |
+
self.ball = x + dx * speed / distance, y + dy * speed / distance
|
| 93 |
+
if not self.thrown and self.random.random() < 0.05:
|
| 94 |
+
self.thrown = True
|
| 95 |
+
if self.ball_base not in (self.runner_base, self.runner_target):
|
| 96 |
+
self.ball_target = self.runner_target
|
| 97 |
+
|
| 98 |
+
def state(self) -> np.ndarray:
|
| 99 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 100 |
+
for x, y in BASES:
|
| 101 |
+
state[y // 10, x // 10, 0] = True
|
| 102 |
+
runner_x, runner_y = self.runner_position
|
| 103 |
+
state[int(runner_y) // 10, int(runner_x) // 10, 1] = True
|
| 104 |
+
ball_x, ball_y = self.ball
|
| 105 |
+
state[int(ball_y) // 10, int(ball_x) // 10, 2 if self.thrown else 3] = True
|
| 106 |
+
target_x, target_y = BASES[self.ball_target]
|
| 107 |
+
state[target_y // 10, target_x // 10, 4] = True
|
| 108 |
+
for index, channel in ((self.runner_base, 4), (self.runner_target, 5)):
|
| 109 |
+
state[0, index, channel] = True
|
| 110 |
+
for bit in range(9):
|
| 111 |
+
state[9, bit, 5] = bool(self.steps & (1 << bit))
|
| 112 |
+
state[9, 9, 5] = self.pressed
|
| 113 |
+
return state
|
runtime/minatar_synth/games/battle_zone.py
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
DIRECTIONS = (
|
| 5 |
+
(-1, 0),
|
| 6 |
+
(-1, 1),
|
| 7 |
+
(0, 1),
|
| 8 |
+
(1, 1),
|
| 9 |
+
(1, 0),
|
| 10 |
+
(1, -1),
|
| 11 |
+
(0, -1),
|
| 12 |
+
(-1, -1),
|
| 13 |
+
)
|
| 14 |
+
POINTS = {"tank": 1_000.0, "fighter": 2_000.0, "supertank": 3_000.0, "saucer": 5_000.0}
|
| 15 |
+
WAVES = (
|
| 16 |
+
("tank", "tank", "tank", "fighter", "saucer"),
|
| 17 |
+
("tank", "tank", "fighter", "fighter", "saucer"),
|
| 18 |
+
("tank", "tank", "fighter", "supertank", "saucer"),
|
| 19 |
+
("tank", "fighter", "fighter", "supertank", "saucer"),
|
| 20 |
+
("tank", "fighter", "supertank", "supertank", "saucer"),
|
| 21 |
+
)
|
| 22 |
+
OBSTACLES = {(2, 2), (2, 7), (5, 1), (5, 8), (7, 3), (7, 6)}
|
| 23 |
+
SPAWNS = ((0, 1), (0, 5), (0, 9), (4, 0), (4, 9))
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class BattleZoneEnvironment:
|
| 27 |
+
"""Radar combat with oriented movement, cover, shell dodging, and four target classes."""
|
| 28 |
+
|
| 29 |
+
score_cap = 60_000.0
|
| 30 |
+
|
| 31 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = 1_500):
|
| 32 |
+
self.sticky_action_prob = sticky_action_prob
|
| 33 |
+
self.horizon = horizon
|
| 34 |
+
self.random = np.random.RandomState()
|
| 35 |
+
self.seed(0)
|
| 36 |
+
self.reset()
|
| 37 |
+
|
| 38 |
+
@classmethod
|
| 39 |
+
def normalize_score(cls, raw_score: float) -> float:
|
| 40 |
+
return min(max(raw_score, 0.0), cls.score_cap) / cls.score_cap
|
| 41 |
+
|
| 42 |
+
def seed(self, seed: int) -> None:
|
| 43 |
+
self.random.seed(seed)
|
| 44 |
+
|
| 45 |
+
def reset(self) -> None:
|
| 46 |
+
self.player = [9, 5]
|
| 47 |
+
self.previous_player = self.player.copy()
|
| 48 |
+
self.direction = 0
|
| 49 |
+
self.lives = 5
|
| 50 |
+
self.wave = 0
|
| 51 |
+
self.steps = 0
|
| 52 |
+
self.total_score = 0.0
|
| 53 |
+
self.bonus_awarded = False
|
| 54 |
+
self.player_shell: list[int] | None = None
|
| 55 |
+
self.enemy_shells: list[list[int]] = []
|
| 56 |
+
self.last_action = 0
|
| 57 |
+
self.terminal = False
|
| 58 |
+
self._start_wave()
|
| 59 |
+
|
| 60 |
+
def _start_wave(self) -> None:
|
| 61 |
+
self.enemies = [
|
| 62 |
+
{"kind": kind, "position": [*SPAWNS[index]], "previous": [*SPAWNS[index]]}
|
| 63 |
+
for index, kind in enumerate(WAVES[self.wave])
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 67 |
+
if self.terminal:
|
| 68 |
+
return 0.0, True
|
| 69 |
+
if self.random.rand() < self.sticky_action_prob:
|
| 70 |
+
action = self.last_action
|
| 71 |
+
self.last_action = action
|
| 72 |
+
self.previous_player = self.player.copy()
|
| 73 |
+
if action == 1:
|
| 74 |
+
self.direction = (self.direction - 1) % 8
|
| 75 |
+
elif action == 3:
|
| 76 |
+
self.direction = (self.direction + 1) % 8
|
| 77 |
+
elif action in (2, 4):
|
| 78 |
+
multiplier = 1 if action == 2 else -1
|
| 79 |
+
row_delta, column_delta = DIRECTIONS[self.direction]
|
| 80 |
+
target = [self.player[0] + multiplier * row_delta, self.player[1] + multiplier * column_delta]
|
| 81 |
+
if self._open(target):
|
| 82 |
+
self.player = target
|
| 83 |
+
elif action == 5 and self.player_shell is None:
|
| 84 |
+
row_delta, column_delta = DIRECTIONS[self.direction]
|
| 85 |
+
self.player_shell = [self.player[0], self.player[1], row_delta, column_delta, 9]
|
| 86 |
+
|
| 87 |
+
self._move_enemies()
|
| 88 |
+
self._move_shells()
|
| 89 |
+
reward = self._resolve_player_shell()
|
| 90 |
+
self._resolve_enemy_shells()
|
| 91 |
+
self._enemy_fire()
|
| 92 |
+
self.steps += 1
|
| 93 |
+
if self.total_score >= 50_000 and not self.bonus_awarded:
|
| 94 |
+
self.lives += 1
|
| 95 |
+
self.bonus_awarded = True
|
| 96 |
+
if not self.enemies:
|
| 97 |
+
self.wave += 1
|
| 98 |
+
if self.wave == len(WAVES):
|
| 99 |
+
self.terminal = True
|
| 100 |
+
else:
|
| 101 |
+
self._start_wave()
|
| 102 |
+
if self.steps >= self.horizon:
|
| 103 |
+
self.terminal = True
|
| 104 |
+
return reward, self.terminal
|
| 105 |
+
|
| 106 |
+
@staticmethod
|
| 107 |
+
def _open(position: list[int]) -> bool:
|
| 108 |
+
return 0 <= position[0] < 10 and 0 <= position[1] < 10 and tuple(position) not in OBSTACLES
|
| 109 |
+
|
| 110 |
+
def _move_enemies(self) -> None:
|
| 111 |
+
for enemy in self.enemies:
|
| 112 |
+
enemy["previous"] = list(enemy["position"])
|
| 113 |
+
kind = str(enemy["kind"])
|
| 114 |
+
if kind == "saucer":
|
| 115 |
+
enemy["position"][1] = (enemy["position"][1] + 1) % 10
|
| 116 |
+
continue
|
| 117 |
+
interval = {"tank": 3, "fighter": 2, "supertank": 1}[kind]
|
| 118 |
+
if self.steps % interval != 0:
|
| 119 |
+
continue
|
| 120 |
+
position = list(enemy["position"])
|
| 121 |
+
row_delta = int(np.sign(self.player[0] - position[0]))
|
| 122 |
+
column_delta = int(np.sign(self.player[1] - position[1]))
|
| 123 |
+
if kind == "fighter" and self.steps % 4 < 2:
|
| 124 |
+
column_delta = -column_delta or 1
|
| 125 |
+
candidates = ([position[0] + row_delta, position[1] + column_delta], [position[0] + row_delta, position[1]])
|
| 126 |
+
for target in candidates:
|
| 127 |
+
if self._open(target) and target != self.player:
|
| 128 |
+
enemy["position"] = target
|
| 129 |
+
break
|
| 130 |
+
|
| 131 |
+
def _move_shells(self) -> None:
|
| 132 |
+
if self.player_shell is not None:
|
| 133 |
+
self.player_shell[0] += self.player_shell[2]
|
| 134 |
+
self.player_shell[1] += self.player_shell[3]
|
| 135 |
+
self.player_shell[4] -= 1
|
| 136 |
+
if not self._open(self.player_shell[:2]) or self.player_shell[4] <= 0:
|
| 137 |
+
self.player_shell = None
|
| 138 |
+
remaining = []
|
| 139 |
+
for shell in self.enemy_shells:
|
| 140 |
+
shell[0] += shell[2]
|
| 141 |
+
shell[1] += shell[3]
|
| 142 |
+
shell[4] -= 1
|
| 143 |
+
if self._open(shell[:2]) and shell[4] > 0:
|
| 144 |
+
remaining.append(shell)
|
| 145 |
+
self.enemy_shells = remaining
|
| 146 |
+
|
| 147 |
+
def _resolve_player_shell(self) -> float:
|
| 148 |
+
if self.player_shell is None:
|
| 149 |
+
return 0.0
|
| 150 |
+
hit = next((enemy for enemy in self.enemies if enemy["position"] == self.player_shell[:2]), None)
|
| 151 |
+
if hit is None:
|
| 152 |
+
return 0.0
|
| 153 |
+
self.enemies.remove(hit)
|
| 154 |
+
self.player_shell = None
|
| 155 |
+
reward = POINTS[str(hit["kind"])]
|
| 156 |
+
self.total_score += reward
|
| 157 |
+
return reward
|
| 158 |
+
|
| 159 |
+
def _resolve_enemy_shells(self) -> None:
|
| 160 |
+
remaining = []
|
| 161 |
+
for shell in self.enemy_shells:
|
| 162 |
+
friendly_hit = next((enemy for enemy in self.enemies if enemy["position"] == shell[:2]), None)
|
| 163 |
+
if friendly_hit is not None:
|
| 164 |
+
self.enemies.remove(friendly_hit)
|
| 165 |
+
elif shell[:2] == self.player:
|
| 166 |
+
self._lose_tank()
|
| 167 |
+
else:
|
| 168 |
+
remaining.append(shell)
|
| 169 |
+
self.enemy_shells = remaining
|
| 170 |
+
|
| 171 |
+
def _enemy_fire(self) -> None:
|
| 172 |
+
for enemy in self.enemies:
|
| 173 |
+
kind = str(enemy["kind"])
|
| 174 |
+
if kind == "saucer":
|
| 175 |
+
continue
|
| 176 |
+
interval = {"tank": 12, "fighter": 9, "supertank": 7}[kind]
|
| 177 |
+
if self.steps % interval != 0:
|
| 178 |
+
continue
|
| 179 |
+
position = list(enemy["position"])
|
| 180 |
+
row_delta = int(np.sign(self.player[0] - position[0]))
|
| 181 |
+
column_delta = int(np.sign(self.player[1] - position[1]))
|
| 182 |
+
self.enemy_shells.append([position[0], position[1], row_delta, column_delta, 10])
|
| 183 |
+
|
| 184 |
+
def _lose_tank(self) -> None:
|
| 185 |
+
self.lives -= 1
|
| 186 |
+
self.player = [9, 5]
|
| 187 |
+
self.previous_player = self.player.copy()
|
| 188 |
+
self.direction = 0
|
| 189 |
+
self.player_shell = None
|
| 190 |
+
self.enemy_shells.clear()
|
| 191 |
+
if self.lives <= 0:
|
| 192 |
+
self.terminal = True
|
| 193 |
+
|
| 194 |
+
def _in_view(self, position: list[int]) -> bool:
|
| 195 |
+
row_delta = int(np.sign(position[0] - self.player[0]))
|
| 196 |
+
column_delta = int(np.sign(position[1] - self.player[1]))
|
| 197 |
+
if (row_delta, column_delta) == (0, 0):
|
| 198 |
+
return True
|
| 199 |
+
target_direction = DIRECTIONS.index((row_delta, column_delta))
|
| 200 |
+
return min((target_direction - self.direction) % 8, (self.direction - target_direction) % 8) <= 1
|
| 201 |
+
|
| 202 |
+
def state(self) -> np.ndarray:
|
| 203 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 204 |
+
state[*self.player, 0] = True
|
| 205 |
+
for enemy in self.enemies:
|
| 206 |
+
position = list(enemy["position"])
|
| 207 |
+
state[*position, 1] = True
|
| 208 |
+
if self._in_view(position):
|
| 209 |
+
state[*position, 2] = True
|
| 210 |
+
type_marker = {"tank": 0, "fighter": 1, "supertank": 2, "saucer": 3}[str(enemy["kind"])]
|
| 211 |
+
state[position[0], type_marker, 5] = True
|
| 212 |
+
state[*list(enemy["previous"]), 4] = True
|
| 213 |
+
for shell in self.enemy_shells:
|
| 214 |
+
state[shell[0], shell[1], 1] = True
|
| 215 |
+
if self.player_shell is not None:
|
| 216 |
+
state[self.player_shell[0], self.player_shell[1], 3] = True
|
| 217 |
+
state[*self.previous_player, 4] = True
|
| 218 |
+
for obstacle in OBSTACLES:
|
| 219 |
+
state[*obstacle, 5] = True
|
| 220 |
+
state[9, : min(10, self.lives), 5] = True
|
| 221 |
+
state[8, : self.wave + 1, 5] = True
|
| 222 |
+
state[7, self.direction, 5] = True
|
| 223 |
+
return state
|
runtime/minatar_synth/games/beam_rider.py
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
BEAM_COLUMNS = (1, 3, 5, 7, 9)
|
| 5 |
+
SECTORS = (1, 2, 4, 6)
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class BeamRiderEnvironment:
|
| 9 |
+
"""Five-beam sector defense with short lasers, scarce torpedoes, and rejuvenators."""
|
| 10 |
+
|
| 11 |
+
score_cap = 34_200.0
|
| 12 |
+
|
| 13 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = 2_000):
|
| 14 |
+
self.sticky_action_prob = sticky_action_prob
|
| 15 |
+
self.horizon = horizon
|
| 16 |
+
self.random = np.random.RandomState()
|
| 17 |
+
self.seed(0)
|
| 18 |
+
self.reset()
|
| 19 |
+
|
| 20 |
+
@classmethod
|
| 21 |
+
def normalize_score(cls, raw_score: float) -> float:
|
| 22 |
+
return min(max(raw_score, 0.0), cls.score_cap) / cls.score_cap
|
| 23 |
+
|
| 24 |
+
def seed(self, seed: int) -> None:
|
| 25 |
+
self.random.seed(seed)
|
| 26 |
+
|
| 27 |
+
def reset(self) -> None:
|
| 28 |
+
self.lane = 2
|
| 29 |
+
self.lives = 3
|
| 30 |
+
self.sector_index = 0
|
| 31 |
+
self.steps = 0
|
| 32 |
+
self.last_action = 0
|
| 33 |
+
self.terminal = False
|
| 34 |
+
self._start_sector()
|
| 35 |
+
|
| 36 |
+
@property
|
| 37 |
+
def sector(self) -> int:
|
| 38 |
+
return SECTORS[self.sector_index]
|
| 39 |
+
|
| 40 |
+
def _start_sector(self) -> None:
|
| 41 |
+
self.torpedoes = 3
|
| 42 |
+
self.saucers_destroyed = 0
|
| 43 |
+
self.saucers_spawned = 0
|
| 44 |
+
self.objects: list[dict[str, int | str | bool]] = []
|
| 45 |
+
self.shots: list[dict[str, int | str]] = []
|
| 46 |
+
self.previous_objects: list[list[int]] = []
|
| 47 |
+
self.sector_step = 0
|
| 48 |
+
self.sentinel: dict[str, int | str | bool] | None = None
|
| 49 |
+
self.rejuvenator_spawned = False
|
| 50 |
+
self.chirpers_spawned = 0
|
| 51 |
+
self.hazard_spawned = False
|
| 52 |
+
|
| 53 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 54 |
+
if self.terminal:
|
| 55 |
+
return 0.0, True
|
| 56 |
+
if self.random.rand() < self.sticky_action_prob:
|
| 57 |
+
action = self.last_action
|
| 58 |
+
self.last_action = action
|
| 59 |
+
if action == 1:
|
| 60 |
+
self.lane = max(0, self.lane - 1)
|
| 61 |
+
elif action == 3:
|
| 62 |
+
self.lane = min(4, self.lane + 1)
|
| 63 |
+
elif action == 5:
|
| 64 |
+
self.shots.append({"row": 8, "lane": self.lane, "kind": "laser", "ttl": 4})
|
| 65 |
+
elif action == 2 and self.torpedoes > 0:
|
| 66 |
+
self.torpedoes -= 1
|
| 67 |
+
self.shots.append({"row": 8, "lane": self.lane, "kind": "torpedo", "ttl": 10})
|
| 68 |
+
|
| 69 |
+
self._spawn_objects()
|
| 70 |
+
self.previous_objects = [[int(obj["row"]), int(obj["lane"])] for obj in self.objects]
|
| 71 |
+
self._move_objects()
|
| 72 |
+
self._move_shots()
|
| 73 |
+
reward = self._resolve_shots()
|
| 74 |
+
self._resolve_player_contacts()
|
| 75 |
+
reward += self._move_sentinel()
|
| 76 |
+
self.sector_step += 1
|
| 77 |
+
self.steps += 1
|
| 78 |
+
if self.saucers_destroyed == 15 and self.sentinel is None:
|
| 79 |
+
self.sentinel = {"row": 0, "column": 0, "kind": "sentinel", "active": True}
|
| 80 |
+
if self.sentinel is not None and not bool(self.sentinel["active"]):
|
| 81 |
+
self.sector_index += 1
|
| 82 |
+
if self.sector_index == len(SECTORS):
|
| 83 |
+
self.terminal = True
|
| 84 |
+
else:
|
| 85 |
+
self._start_sector()
|
| 86 |
+
if self.steps >= self.horizon:
|
| 87 |
+
self.terminal = True
|
| 88 |
+
return reward, self.terminal
|
| 89 |
+
|
| 90 |
+
def _spawn_objects(self) -> None:
|
| 91 |
+
if self.saucers_spawned < 15 and self.sector_step % 5 == 0:
|
| 92 |
+
self.objects.append({"row": 0, "lane": int(self.random.randint(5)), "kind": "saucer", "red": False})
|
| 93 |
+
self.saucers_spawned += 1
|
| 94 |
+
if not self.rejuvenator_spawned and self.sector_step == 20:
|
| 95 |
+
self.objects.append({"row": 0, "lane": int(self.random.randint(5)), "kind": "rejuvenator", "red": False})
|
| 96 |
+
self.rejuvenator_spawned = True
|
| 97 |
+
if self.sector >= 4 and self.chirpers_spawned < 3 and self.sector_step in (12, 30, 48):
|
| 98 |
+
self.objects.append({"row": 0, "lane": int(self.random.randint(5)), "kind": "chirper", "red": False})
|
| 99 |
+
self.chirpers_spawned += 1
|
| 100 |
+
if self.sector >= 2 and not self.hazard_spawned and self.sector_step == 10:
|
| 101 |
+
kind = "debris" if self.sector < 6 else "blocker"
|
| 102 |
+
self.objects.append({"row": 0, "lane": int(self.random.randint(5)), "kind": kind, "red": False})
|
| 103 |
+
self.hazard_spawned = True
|
| 104 |
+
|
| 105 |
+
def _move_objects(self) -> None:
|
| 106 |
+
for obj in self.objects:
|
| 107 |
+
obj["row"] = int(obj["row"]) + 1
|
| 108 |
+
if obj["kind"] == "chirper" and int(obj["row"]) % 2 == 0:
|
| 109 |
+
obj["lane"] = int(np.clip(int(obj["lane"]) + self.random.choice((-1, 1)), 0, 4))
|
| 110 |
+
for obj in self.objects:
|
| 111 |
+
if obj["kind"] == "saucer" and int(obj["row"]) > 9:
|
| 112 |
+
obj["row"] = 0
|
| 113 |
+
obj["lane"] = int(self.random.randint(5))
|
| 114 |
+
self.objects = [obj for obj in self.objects if int(obj["row"]) <= 9 or obj["kind"] == "saucer"]
|
| 115 |
+
|
| 116 |
+
def _move_shots(self) -> None:
|
| 117 |
+
for shot in self.shots:
|
| 118 |
+
shot["row"] = int(shot["row"]) - 1
|
| 119 |
+
shot["ttl"] = int(shot["ttl"]) - 1
|
| 120 |
+
self.shots = [shot for shot in self.shots if int(shot["row"]) >= 0 and int(shot["ttl"]) > 0]
|
| 121 |
+
|
| 122 |
+
def _resolve_shots(self) -> float:
|
| 123 |
+
reward = 0.0
|
| 124 |
+
remaining = []
|
| 125 |
+
for shot in self.shots:
|
| 126 |
+
hit = next(
|
| 127 |
+
(obj for obj in self.objects if int(obj["row"]) == int(shot["row"]) and int(obj["lane"]) == int(shot["lane"])),
|
| 128 |
+
None,
|
| 129 |
+
)
|
| 130 |
+
if hit is None:
|
| 131 |
+
remaining.append(shot)
|
| 132 |
+
continue
|
| 133 |
+
kind = str(hit["kind"])
|
| 134 |
+
torpedo = shot["kind"] == "torpedo"
|
| 135 |
+
laser_vulnerable = kind in ("saucer", "chirper")
|
| 136 |
+
if kind == "rejuvenator" and not bool(hit["red"]):
|
| 137 |
+
hit["red"] = True
|
| 138 |
+
continue
|
| 139 |
+
if torpedo or laser_vulnerable:
|
| 140 |
+
self.objects.remove(hit)
|
| 141 |
+
if kind == "saucer":
|
| 142 |
+
self.saucers_destroyed += 1
|
| 143 |
+
reward += 100.0 * self.sector
|
| 144 |
+
elif kind == "chirper":
|
| 145 |
+
reward += 200.0 * self.sector
|
| 146 |
+
else:
|
| 147 |
+
remaining.append(shot)
|
| 148 |
+
self.shots = remaining
|
| 149 |
+
return reward
|
| 150 |
+
|
| 151 |
+
def _resolve_player_contacts(self) -> None:
|
| 152 |
+
contacts = [obj for obj in self.objects if int(obj["row"]) == 9 and int(obj["lane"]) == self.lane]
|
| 153 |
+
for obj in contacts:
|
| 154 |
+
self.objects.remove(obj)
|
| 155 |
+
if obj["kind"] == "rejuvenator" and not bool(obj["red"]):
|
| 156 |
+
self.lives += 1
|
| 157 |
+
else:
|
| 158 |
+
self._lose_ship()
|
| 159 |
+
|
| 160 |
+
def _move_sentinel(self) -> float:
|
| 161 |
+
if self.sentinel is None or not bool(self.sentinel["active"]):
|
| 162 |
+
return 0.0
|
| 163 |
+
self.sentinel["column"] = int(self.sentinel["column"]) + 1
|
| 164 |
+
hit = next(
|
| 165 |
+
(
|
| 166 |
+
shot
|
| 167 |
+
for shot in self.shots
|
| 168 |
+
if shot["kind"] == "torpedo"
|
| 169 |
+
and int(shot["row"]) == 0
|
| 170 |
+
and BEAM_COLUMNS[int(shot["lane"])] == int(self.sentinel["column"])
|
| 171 |
+
),
|
| 172 |
+
None,
|
| 173 |
+
)
|
| 174 |
+
if hit is not None:
|
| 175 |
+
self.shots.remove(hit)
|
| 176 |
+
self.sentinel["active"] = False
|
| 177 |
+
return 500.0 * self.sector + 100.0 * self.lives
|
| 178 |
+
elif int(self.sentinel["column"]) > 9:
|
| 179 |
+
self.sentinel["active"] = False
|
| 180 |
+
return 0.0
|
| 181 |
+
|
| 182 |
+
def _lose_ship(self) -> None:
|
| 183 |
+
self.lives -= 1
|
| 184 |
+
self.lane = 2
|
| 185 |
+
self.shots.clear()
|
| 186 |
+
if self.lives <= 0:
|
| 187 |
+
self.terminal = True
|
| 188 |
+
|
| 189 |
+
def state(self) -> np.ndarray:
|
| 190 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 191 |
+
state[9, BEAM_COLUMNS[self.lane], 0] = True
|
| 192 |
+
for obj in self.objects:
|
| 193 |
+
row, column = int(obj["row"]), BEAM_COLUMNS[int(obj["lane"])]
|
| 194 |
+
dangerous = obj["kind"] != "rejuvenator" or bool(obj["red"])
|
| 195 |
+
if dangerous:
|
| 196 |
+
state[row, column, 1] = True
|
| 197 |
+
if obj["kind"] in ("saucer", "chirper") or (obj["kind"] == "rejuvenator" and not bool(obj["red"])):
|
| 198 |
+
state[row, column, 2] = True
|
| 199 |
+
marker = {"saucer": 0, "chirper": 1, "debris": 2, "blocker": 3, "rejuvenator": 4}[str(obj["kind"])]
|
| 200 |
+
state[row, marker, 5] = True
|
| 201 |
+
if self.sentinel is not None and bool(self.sentinel["active"]):
|
| 202 |
+
state[0, min(9, int(self.sentinel["column"])), 2] = True
|
| 203 |
+
for shot in self.shots:
|
| 204 |
+
row, column = int(shot["row"]), BEAM_COLUMNS[int(shot["lane"])]
|
| 205 |
+
state[row, column, 3] = True
|
| 206 |
+
if shot["kind"] == "torpedo":
|
| 207 |
+
state[row, column, 5] = True
|
| 208 |
+
for row, lane in self.previous_objects:
|
| 209 |
+
if 0 <= row < 10:
|
| 210 |
+
state[row, BEAM_COLUMNS[lane], 4] = True
|
| 211 |
+
state[9, : min(10, self.lives), 5] = True
|
| 212 |
+
state[8, : self.torpedoes, 5] = True
|
| 213 |
+
state[7, : self.sector_index + 1, 5] = True
|
| 214 |
+
state[6, : min(10, (15 - self.saucers_destroyed + 1) // 2), 5] = True
|
| 215 |
+
return state
|
runtime/minatar_synth/games/berzerk.py
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
DELTAS = {1: (0, -1), 2: (-1, 0), 3: (0, 1), 4: (1, 0)}
|
| 5 |
+
OPENINGS = {(0, 5), (9, 5), (5, 0), (5, 9)}
|
| 6 |
+
BASE_INTERIOR = {(2, 2), (2, 3), (2, 4), (4, 7), (5, 7), (6, 7), (7, 2), (7, 3)}
|
| 7 |
+
SPAWN_CANDIDATES = ((1, 1), (1, 8), (4, 4), (7, 7), (8, 1), (3, 8), (6, 2))
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def room_walls(room: int) -> set[tuple[int, int]]:
|
| 11 |
+
boundary = {
|
| 12 |
+
(row, column)
|
| 13 |
+
for row in range(10)
|
| 14 |
+
for column in range(10)
|
| 15 |
+
if row in (0, 9) or column in (0, 9)
|
| 16 |
+
} - OPENINGS
|
| 17 |
+
interior = BASE_INTERIOR
|
| 18 |
+
for _ in range(room):
|
| 19 |
+
interior = {(column, 9 - row) for row, column in interior}
|
| 20 |
+
return boundary | interior
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class BerzerkEnvironment:
|
| 24 |
+
"""Four electrified mazes with robot crossfire and invincible Evil Otto pressure."""
|
| 25 |
+
|
| 26 |
+
score_cap = 1_200.0
|
| 27 |
+
|
| 28 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = 1_000):
|
| 29 |
+
self.sticky_action_prob = sticky_action_prob
|
| 30 |
+
self.horizon = horizon
|
| 31 |
+
self.random = np.random.RandomState()
|
| 32 |
+
self.seed(0)
|
| 33 |
+
self.reset()
|
| 34 |
+
|
| 35 |
+
@classmethod
|
| 36 |
+
def normalize_score(cls, raw_score: float) -> float:
|
| 37 |
+
return min(max(raw_score, 0.0), cls.score_cap) / cls.score_cap
|
| 38 |
+
|
| 39 |
+
def seed(self, seed: int) -> None:
|
| 40 |
+
self.random.seed(seed)
|
| 41 |
+
|
| 42 |
+
def reset(self) -> None:
|
| 43 |
+
self.room = 0
|
| 44 |
+
self.lives = 3
|
| 45 |
+
self.steps = 0
|
| 46 |
+
self.last_action = 0
|
| 47 |
+
self.death_this_step = False
|
| 48 |
+
self.terminal = False
|
| 49 |
+
self._start_room([9, 5])
|
| 50 |
+
|
| 51 |
+
def _start_room(self, entry: list[int]) -> None:
|
| 52 |
+
self.walls = room_walls(self.room)
|
| 53 |
+
self.player = entry.copy()
|
| 54 |
+
self.previous_player = self.player.copy()
|
| 55 |
+
self.aim = 2
|
| 56 |
+
available = [position for position in SPAWN_CANDIDATES if position not in self.walls and list(position) != self.player]
|
| 57 |
+
self.robots = [list(position) for position in available[:5]]
|
| 58 |
+
self.previous_robots = [robot.copy() for robot in self.robots]
|
| 59 |
+
self.player_shot: list[int] | None = None
|
| 60 |
+
self.robot_shots: list[list[int]] = []
|
| 61 |
+
self.otto: list[int] | None = None
|
| 62 |
+
self.room_steps = 0
|
| 63 |
+
self.clear_bonus_awarded = False
|
| 64 |
+
|
| 65 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 66 |
+
if self.terminal:
|
| 67 |
+
return 0.0, True
|
| 68 |
+
if self.random.rand() < self.sticky_action_prob:
|
| 69 |
+
action = self.last_action
|
| 70 |
+
self.last_action = action
|
| 71 |
+
self.death_this_step = False
|
| 72 |
+
self.previous_player = self.player.copy()
|
| 73 |
+
if action in DELTAS:
|
| 74 |
+
self.aim = action
|
| 75 |
+
if self._move_player(action):
|
| 76 |
+
self.steps += 1
|
| 77 |
+
return 0.0, self.terminal
|
| 78 |
+
elif action == 5 and self.player_shot is None:
|
| 79 |
+
row_delta, column_delta = DELTAS[self.aim]
|
| 80 |
+
self.player_shot = [self.player[0], self.player[1], row_delta, column_delta, 8]
|
| 81 |
+
|
| 82 |
+
self.previous_robots = [robot.copy() for robot in self.robots]
|
| 83 |
+
reward = self._move_robots()
|
| 84 |
+
self._move_shots()
|
| 85 |
+
reward += self._resolve_player_shot()
|
| 86 |
+
reward += self._resolve_robot_shots()
|
| 87 |
+
self._spawn_and_move_otto()
|
| 88 |
+
reward += self._resolve_otto()
|
| 89 |
+
if not self.robots and not self.clear_bonus_awarded:
|
| 90 |
+
reward += 50.0
|
| 91 |
+
self.clear_bonus_awarded = True
|
| 92 |
+
self.room_steps += 1
|
| 93 |
+
self.steps += 1
|
| 94 |
+
if self.steps >= self.horizon:
|
| 95 |
+
self.terminal = True
|
| 96 |
+
return reward, self.terminal
|
| 97 |
+
|
| 98 |
+
def _move_player(self, action: int) -> bool:
|
| 99 |
+
row_delta, column_delta = DELTAS[action]
|
| 100 |
+
target = [self.player[0] + row_delta, self.player[1] + column_delta]
|
| 101 |
+
if not (0 <= target[0] < 10 and 0 <= target[1] < 10):
|
| 102 |
+
if tuple(self.player) in OPENINGS:
|
| 103 |
+
self.room += 1
|
| 104 |
+
if self.room == 4:
|
| 105 |
+
self.terminal = True
|
| 106 |
+
else:
|
| 107 |
+
opposite = {1: [5, 9], 2: [9, 5], 3: [5, 0], 4: [0, 5]}[action]
|
| 108 |
+
self._start_room(opposite)
|
| 109 |
+
return True
|
| 110 |
+
return False
|
| 111 |
+
if tuple(target) in self.walls:
|
| 112 |
+
self._lose_life()
|
| 113 |
+
elif target in self.robots or target == self.otto:
|
| 114 |
+
self._lose_life()
|
| 115 |
+
else:
|
| 116 |
+
self.player = target
|
| 117 |
+
return False
|
| 118 |
+
|
| 119 |
+
def _move_robots(self) -> float:
|
| 120 |
+
if self.room_steps % 2 != 0:
|
| 121 |
+
return 0.0
|
| 122 |
+
reward = 0.0
|
| 123 |
+
for robot in self.robots.copy():
|
| 124 |
+
row_delta = int(np.sign(self.player[0] - robot[0]))
|
| 125 |
+
column_delta = int(np.sign(self.player[1] - robot[1]))
|
| 126 |
+
target = [robot[0] + row_delta, robot[1] + column_delta]
|
| 127 |
+
if tuple(target) in self.walls:
|
| 128 |
+
if robot in self.robots:
|
| 129 |
+
self.robots.remove(robot)
|
| 130 |
+
reward += 50.0
|
| 131 |
+
elif target in self.robots:
|
| 132 |
+
if robot in self.robots:
|
| 133 |
+
self.robots.remove(robot)
|
| 134 |
+
reward += 50.0
|
| 135 |
+
if target in self.robots:
|
| 136 |
+
self.robots.remove(target)
|
| 137 |
+
reward += 50.0
|
| 138 |
+
elif target == self.player:
|
| 139 |
+
self._lose_life()
|
| 140 |
+
elif robot in self.robots:
|
| 141 |
+
robot[:] = target
|
| 142 |
+
if self.room_steps % 8 == 0:
|
| 143 |
+
for robot in self.robots:
|
| 144 |
+
row_delta = int(np.sign(self.player[0] - robot[0]))
|
| 145 |
+
column_delta = int(np.sign(self.player[1] - robot[1]))
|
| 146 |
+
self.robot_shots.append([robot[0], robot[1], row_delta, column_delta, 8])
|
| 147 |
+
return reward
|
| 148 |
+
|
| 149 |
+
def _move_shots(self) -> None:
|
| 150 |
+
if self.player_shot is not None:
|
| 151 |
+
self.player_shot[0] += self.player_shot[2]
|
| 152 |
+
self.player_shot[1] += self.player_shot[3]
|
| 153 |
+
self.player_shot[4] -= 1
|
| 154 |
+
if (
|
| 155 |
+
self.player_shot[4] <= 0
|
| 156 |
+
or not (0 <= self.player_shot[0] < 10 and 0 <= self.player_shot[1] < 10)
|
| 157 |
+
or tuple(self.player_shot[:2]) in self.walls
|
| 158 |
+
):
|
| 159 |
+
self.player_shot = None
|
| 160 |
+
remaining = []
|
| 161 |
+
for shot in self.robot_shots:
|
| 162 |
+
shot[0] += shot[2]
|
| 163 |
+
shot[1] += shot[3]
|
| 164 |
+
shot[4] -= 1
|
| 165 |
+
if shot[4] > 0 and 0 <= shot[0] < 10 and 0 <= shot[1] < 10 and tuple(shot[:2]) not in self.walls:
|
| 166 |
+
remaining.append(shot)
|
| 167 |
+
self.robot_shots = remaining
|
| 168 |
+
|
| 169 |
+
def _resolve_player_shot(self) -> float:
|
| 170 |
+
if self.player_shot is None:
|
| 171 |
+
return 0.0
|
| 172 |
+
hit = next((robot for robot in self.robots if robot == self.player_shot[:2]), None)
|
| 173 |
+
if hit is None:
|
| 174 |
+
return 0.0
|
| 175 |
+
self.robots.remove(hit)
|
| 176 |
+
self.player_shot = None
|
| 177 |
+
return 50.0
|
| 178 |
+
|
| 179 |
+
def _resolve_robot_shots(self) -> float:
|
| 180 |
+
reward = 0.0
|
| 181 |
+
remaining = []
|
| 182 |
+
for shot in self.robot_shots:
|
| 183 |
+
hit = next((robot for robot in self.robots if robot == shot[:2]), None)
|
| 184 |
+
if hit is not None:
|
| 185 |
+
self.robots.remove(hit)
|
| 186 |
+
reward += 50.0
|
| 187 |
+
elif shot[:2] == self.player:
|
| 188 |
+
self._lose_life()
|
| 189 |
+
else:
|
| 190 |
+
remaining.append(shot)
|
| 191 |
+
self.robot_shots = remaining
|
| 192 |
+
return reward
|
| 193 |
+
|
| 194 |
+
def _spawn_and_move_otto(self) -> None:
|
| 195 |
+
if self.otto is None and self.room_steps >= 40:
|
| 196 |
+
self.otto = [9, 5]
|
| 197 |
+
if self.otto is None or self.room_steps % 2 != 0:
|
| 198 |
+
return
|
| 199 |
+
self.otto[0] += int(np.sign(self.player[0] - self.otto[0]))
|
| 200 |
+
self.otto[1] += int(np.sign(self.player[1] - self.otto[1]))
|
| 201 |
+
|
| 202 |
+
def _resolve_otto(self) -> float:
|
| 203 |
+
if self.otto is None:
|
| 204 |
+
return 0.0
|
| 205 |
+
reward = 0.0
|
| 206 |
+
hit = next((robot for robot in self.robots if robot == self.otto), None)
|
| 207 |
+
if hit is not None:
|
| 208 |
+
self.robots.remove(hit)
|
| 209 |
+
reward += 50.0
|
| 210 |
+
if self.otto == self.player:
|
| 211 |
+
self._lose_life()
|
| 212 |
+
return reward
|
| 213 |
+
|
| 214 |
+
def _lose_life(self) -> None:
|
| 215 |
+
if self.death_this_step:
|
| 216 |
+
return
|
| 217 |
+
self.death_this_step = True
|
| 218 |
+
self.lives -= 1
|
| 219 |
+
self.player = [9, 5]
|
| 220 |
+
self.previous_player = self.player.copy()
|
| 221 |
+
self.player_shot = None
|
| 222 |
+
self.robot_shots.clear()
|
| 223 |
+
if self.lives <= 0:
|
| 224 |
+
self.terminal = True
|
| 225 |
+
|
| 226 |
+
def state(self) -> np.ndarray:
|
| 227 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 228 |
+
state[*self.player, 0] = True
|
| 229 |
+
for robot in self.robots:
|
| 230 |
+
state[*robot, 1] = True
|
| 231 |
+
state[*robot, 2] = True
|
| 232 |
+
for shot in self.robot_shots:
|
| 233 |
+
state[shot[0], shot[1], 1] = True
|
| 234 |
+
if self.otto is not None:
|
| 235 |
+
state[*self.otto, 1] = True
|
| 236 |
+
if self.player_shot is not None:
|
| 237 |
+
state[self.player_shot[0], self.player_shot[1], 3] = True
|
| 238 |
+
state[*self.previous_player, 4] = True
|
| 239 |
+
for robot in self.previous_robots:
|
| 240 |
+
state[*robot, 4] = True
|
| 241 |
+
for wall in self.walls:
|
| 242 |
+
state[*wall, 5] = True
|
| 243 |
+
state[9, : self.lives, 5] = True
|
| 244 |
+
state[8, : self.room + 1, 5] = True
|
| 245 |
+
state[7, self.aim, 5] = True
|
| 246 |
+
return state
|
runtime/minatar_synth/games/blade_dancer.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
FRAMES_PER_STEP = 4
|
| 9 |
+
PLAYER_X = 35
|
| 10 |
+
GROUND = 80
|
| 11 |
+
JUMP_DURATION = 30
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@dataclass
|
| 15 |
+
class Enemy:
|
| 16 |
+
x: float
|
| 17 |
+
y: float
|
| 18 |
+
size: float
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class BladeDancerEnvironment:
|
| 22 |
+
score_cap = 100
|
| 23 |
+
|
| 24 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 25 |
+
self.sticky_action_prob = sticky_action_prob
|
| 26 |
+
self.horizon = min(horizon, HORIZON)
|
| 27 |
+
self.seed(0)
|
| 28 |
+
self.reset()
|
| 29 |
+
|
| 30 |
+
def seed(self, seed: int) -> None:
|
| 31 |
+
self.random = np.random.default_rng(seed)
|
| 32 |
+
|
| 33 |
+
def reset(self) -> None:
|
| 34 |
+
self.steps = 0
|
| 35 |
+
self.pressed = self.terminal = False
|
| 36 |
+
self.jump_ticks = 0.0
|
| 37 |
+
self.y = float(GROUND)
|
| 38 |
+
self.angle = math.pi / 2
|
| 39 |
+
self.length = 10.0
|
| 40 |
+
self.multiplier = 1
|
| 41 |
+
self.enemies: list[Enemy] = []
|
| 42 |
+
self.platforms = [(0.0, 200.0)]
|
| 43 |
+
|
| 44 |
+
@classmethod
|
| 45 |
+
def normalize_score(cls, score: float) -> float:
|
| 46 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 47 |
+
|
| 48 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 49 |
+
if self.terminal:
|
| 50 |
+
return 0.0, True
|
| 51 |
+
pressed = self.pressed if self.random.random() < self.sticky_action_prob else action == 5
|
| 52 |
+
if pressed and not self.pressed and not self.jump_ticks:
|
| 53 |
+
self.jump_ticks = JUMP_DURATION
|
| 54 |
+
self.multiplier = 1
|
| 55 |
+
self.pressed = pressed
|
| 56 |
+
self.steps += 1
|
| 57 |
+
reward = 0
|
| 58 |
+
for _ in range(FRAMES_PER_STEP):
|
| 59 |
+
reward += self._advance_frame()
|
| 60 |
+
if self.terminal:
|
| 61 |
+
break
|
| 62 |
+
self.terminal |= self.steps >= self.horizon
|
| 63 |
+
return float(reward), self.terminal
|
| 64 |
+
|
| 65 |
+
def _sword_vector(self) -> tuple[float, float]:
|
| 66 |
+
return self.length * math.cos(self.angle), self.length * math.sin(self.angle)
|
| 67 |
+
|
| 68 |
+
def _advance_frame(self) -> int:
|
| 69 |
+
if self.jump_ticks:
|
| 70 |
+
self.jump_ticks = max(0, self.jump_ticks - (0.5 if self.pressed else 1))
|
| 71 |
+
progress = self.jump_ticks / JUMP_DURATION
|
| 72 |
+
self.y = GROUND - math.sin(progress * math.pi) * 30
|
| 73 |
+
self.angle = self.angle - 0.3 if self.jump_ticks else math.pi / 2
|
| 74 |
+
self.length = 8 + (1 - progress) * 22 if self.jump_ticks else 10
|
| 75 |
+
|
| 76 |
+
if not self.enemies or self.random.random() < 0.04:
|
| 77 |
+
if len(self.enemies) == 32:
|
| 78 |
+
self.enemies.pop(0)
|
| 79 |
+
self.enemies.append(Enemy(205, self.random.uniform(30, 75), self.random.uniform(3, 6)))
|
| 80 |
+
reward = 0
|
| 81 |
+
survivors = []
|
| 82 |
+
sword_x, sword_y = self._sword_vector()
|
| 83 |
+
sword_radius = 2.5 if self.jump_ticks else 1
|
| 84 |
+
for enemy in self.enemies:
|
| 85 |
+
enemy.x -= 1
|
| 86 |
+
dx, dy = enemy.x - PLAYER_X, enemy.y - (self.y - 4)
|
| 87 |
+
fraction = min(1, max(0, (dx * sword_x + dy * sword_y) / self.length**2))
|
| 88 |
+
distance = math.hypot(dx - fraction * sword_x, dy - fraction * sword_y)
|
| 89 |
+
radius = enemy.size / 2
|
| 90 |
+
if distance <= radius + sword_radius:
|
| 91 |
+
reward += self.multiplier
|
| 92 |
+
self.multiplier += 1
|
| 93 |
+
continue
|
| 94 |
+
if abs(dx) < 2 + radius and abs(enemy.y - self.y) < 4 + radius:
|
| 95 |
+
self.terminal = True
|
| 96 |
+
if enemy.x >= -10:
|
| 97 |
+
survivors.append(enemy)
|
| 98 |
+
self.enemies = survivors
|
| 99 |
+
|
| 100 |
+
self.platforms = [(left - 1, right - 1) for left, right in self.platforms if right >= 1]
|
| 101 |
+
end = self.platforms[-1][1]
|
| 102 |
+
if end < 200:
|
| 103 |
+
gap = self.random.uniform(15, 30) if self.random.random() < 0.32 else 0
|
| 104 |
+
length = self.random.uniform(30, 100) if gap else self.random.uniform(50, 150)
|
| 105 |
+
self.platforms.append((end + gap, end + gap + length))
|
| 106 |
+
if not self.jump_ticks and not any(left <= PLAYER_X <= right for left, right in self.platforms):
|
| 107 |
+
self.terminal = True
|
| 108 |
+
return reward
|
| 109 |
+
|
| 110 |
+
def state(self) -> np.ndarray:
|
| 111 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 112 |
+
row, column = int(self.y) // 10, PLAYER_X // 20
|
| 113 |
+
state[row, column, 0] = True
|
| 114 |
+
state[row, column, 4] = self.jump_ticks > 0
|
| 115 |
+
sword_x, sword_y = self._sword_vector()
|
| 116 |
+
for fraction in np.linspace(0, 1, 12):
|
| 117 |
+
y = int(self.y - 4 + fraction * sword_y) // 10
|
| 118 |
+
x = int(PLAYER_X + fraction * sword_x) // 20
|
| 119 |
+
state[y, x, 1] = True
|
| 120 |
+
for enemy in self.enemies:
|
| 121 |
+
if 0 <= enemy.x < 200:
|
| 122 |
+
state[int(enemy.y) // 10, int(enemy.x) // 20, 2] = True
|
| 123 |
+
for column in range(10):
|
| 124 |
+
x = 20 * column + PLAYER_X % 20
|
| 125 |
+
state[8, column, 3] = any(left <= x <= right for left, right in self.platforms)
|
| 126 |
+
state[9, 0, 5] = self.pressed
|
| 127 |
+
for row, value in enumerate((int(self.jump_ticks * 2), self.multiplier, self.steps)):
|
| 128 |
+
for bit in range(9):
|
| 129 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 130 |
+
return state
|
runtime/minatar_synth/games/blast_buoy.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
FRAMES_PER_STEP = 4
|
| 9 |
+
BLAST_RADIUS = 25
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@dataclass
|
| 13 |
+
class Submarine:
|
| 14 |
+
x: float
|
| 15 |
+
y: float
|
| 16 |
+
vx: float
|
| 17 |
+
fire_timer: int
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@dataclass
|
| 21 |
+
class Bullet:
|
| 22 |
+
x: float
|
| 23 |
+
y: float
|
| 24 |
+
vx: float
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class BlastBuoyEnvironment:
|
| 28 |
+
score_cap = 500
|
| 29 |
+
|
| 30 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 31 |
+
self.sticky_action_prob = sticky_action_prob
|
| 32 |
+
self.horizon = min(horizon, HORIZON)
|
| 33 |
+
self.seed(0)
|
| 34 |
+
self.reset()
|
| 35 |
+
|
| 36 |
+
def seed(self, seed: int) -> None:
|
| 37 |
+
self.random = np.random.default_rng(seed)
|
| 38 |
+
|
| 39 |
+
def reset(self) -> None:
|
| 40 |
+
self.steps = self.spawn_timer = self.combo_timer = self.shield = self.freeze = 0
|
| 41 |
+
self.combo = 1
|
| 42 |
+
self.pressed = self.terminal = False
|
| 43 |
+
self.x, self.y = 30.0, 40.0
|
| 44 |
+
self.vx = self.vy = 0.0
|
| 45 |
+
self.submarines: list[Submarine] = []
|
| 46 |
+
self.bullets: list[Bullet] = []
|
| 47 |
+
|
| 48 |
+
@classmethod
|
| 49 |
+
def normalize_score(cls, score: float) -> float:
|
| 50 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 51 |
+
|
| 52 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 53 |
+
if self.terminal:
|
| 54 |
+
return 0.0, True
|
| 55 |
+
if self.random.random() >= self.sticky_action_prob:
|
| 56 |
+
self.pressed = action == 5
|
| 57 |
+
self.steps += 1
|
| 58 |
+
reward = 0
|
| 59 |
+
for _ in range(FRAMES_PER_STEP):
|
| 60 |
+
reward += self._advance_frame()
|
| 61 |
+
if self.terminal:
|
| 62 |
+
break
|
| 63 |
+
self.terminal |= self.steps >= self.horizon
|
| 64 |
+
return float(reward), self.terminal
|
| 65 |
+
|
| 66 |
+
def _advance_frame(self) -> int:
|
| 67 |
+
if self.freeze:
|
| 68 |
+
self.freeze -= 1
|
| 69 |
+
return 0
|
| 70 |
+
self.vy = min(1.1, max(-1.1, self.vy + (0.06 if self.pressed else -0.05)))
|
| 71 |
+
self.vx *= 0.95
|
| 72 |
+
self.x = min(95, max(5, self.x + self.vx))
|
| 73 |
+
self.y = min(87, max(13, self.y + self.vy))
|
| 74 |
+
if self.y in (13, 87):
|
| 75 |
+
self.vy = 0
|
| 76 |
+
|
| 77 |
+
self.shield = max(0, self.shield - 1)
|
| 78 |
+
if self.combo_timer:
|
| 79 |
+
self.combo_timer -= 1
|
| 80 |
+
else:
|
| 81 |
+
self.combo = 1
|
| 82 |
+
|
| 83 |
+
if not self.spawn_timer:
|
| 84 |
+
direction = int(self.random.choice((-1, 1)))
|
| 85 |
+
self.submarines.append(Submarine(
|
| 86 |
+
-8 if direction > 0 else 108,
|
| 87 |
+
self.random.uniform(13, 87),
|
| 88 |
+
direction * self.random.uniform(0.2, 0.45),
|
| 89 |
+
int(self.random.integers(60, 121)),
|
| 90 |
+
))
|
| 91 |
+
self.spawn_timer = int(self.random.integers(50, 91))
|
| 92 |
+
self.spawn_timer -= 1
|
| 93 |
+
|
| 94 |
+
contact = None
|
| 95 |
+
for sub in self.submarines:
|
| 96 |
+
sub.x += sub.vx
|
| 97 |
+
sub.fire_timer -= 1
|
| 98 |
+
if sub.fire_timer < 0 and 0 < sub.x < 100 and abs(sub.y - self.y) < 10:
|
| 99 |
+
if math.hypot(sub.x - self.x, sub.y - self.y) > 36:
|
| 100 |
+
direction = 1 if self.x > sub.x else -1
|
| 101 |
+
self.bullets.append(Bullet(sub.x + direction * 6, sub.y, direction * 0.95))
|
| 102 |
+
sub.fire_timer = int(self.random.integers(90, 151))
|
| 103 |
+
if contact is None and not self.shield and abs(sub.x - self.x) < 7.5 and abs(sub.y - self.y) < 5:
|
| 104 |
+
contact = sub
|
| 105 |
+
|
| 106 |
+
self.submarines = [sub for sub in self.submarines if -12 <= sub.x <= 112]
|
| 107 |
+
reward = self._blast(contact) if contact is not None else 0
|
| 108 |
+
|
| 109 |
+
survivors = []
|
| 110 |
+
for bullet in self.bullets:
|
| 111 |
+
bullet.x += bullet.vx
|
| 112 |
+
if abs(bullet.x - self.x) < 5.5 and abs(bullet.y - self.y) < 4:
|
| 113 |
+
self.terminal |= not self.shield
|
| 114 |
+
elif -5 <= bullet.x <= 105:
|
| 115 |
+
survivors.append(bullet)
|
| 116 |
+
self.bullets = survivors
|
| 117 |
+
return reward
|
| 118 |
+
|
| 119 |
+
def _blast(self, center: Submarine) -> int:
|
| 120 |
+
self.submarines.remove(center)
|
| 121 |
+
remaining = [
|
| 122 |
+
sub for sub in self.submarines
|
| 123 |
+
if math.hypot(sub.x - center.x, sub.y - center.y) >= BLAST_RADIUS
|
| 124 |
+
]
|
| 125 |
+
extras = len(self.submarines) - len(remaining)
|
| 126 |
+
self.submarines = remaining
|
| 127 |
+
self.bullets = [
|
| 128 |
+
bullet for bullet in self.bullets
|
| 129 |
+
if math.hypot(bullet.x - center.x, bullet.y - center.y) >= BLAST_RADIUS
|
| 130 |
+
]
|
| 131 |
+
reward = (5 + 8 * extras) * self.combo
|
| 132 |
+
self.combo += 1
|
| 133 |
+
self.combo_timer = 120
|
| 134 |
+
angle = math.atan2(self.y - center.y, self.x - center.x)
|
| 135 |
+
self.vx, self.vy = 2.2 * math.cos(angle), 2.2 * math.sin(angle)
|
| 136 |
+
self.shield = 40
|
| 137 |
+
self.freeze = min(8, 3 + extras)
|
| 138 |
+
return reward
|
| 139 |
+
|
| 140 |
+
def state(self) -> np.ndarray:
|
| 141 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 142 |
+
row, column = int(self.y) // 10, int(self.x) // 10
|
| 143 |
+
state[row, column, 0] = True
|
| 144 |
+
state[row, column, 4] = self.shield > 0
|
| 145 |
+
for sub in self.submarines:
|
| 146 |
+
if 0 <= sub.x < 100:
|
| 147 |
+
state[int(sub.y) // 10, int(sub.x) // 10, 1 if sub.vx > 0 else 2] = True
|
| 148 |
+
for bullet in self.bullets:
|
| 149 |
+
if 0 <= bullet.x < 100:
|
| 150 |
+
state[int(bullet.y) // 10, int(bullet.x) // 10, 3] = True
|
| 151 |
+
for row, value in enumerate((self.steps, self.combo, self.combo_timer, self.shield, self.freeze)):
|
| 152 |
+
for bit in range(9):
|
| 153 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 154 |
+
state[9, :2, 5] = self.pressed, self.vy > 0
|
| 155 |
+
return state
|
runtime/minatar_synth/games/blaze_thrust.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
FRAMES_PER_STEP = 4
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@dataclass
|
| 12 |
+
class Bubble:
|
| 13 |
+
x: float
|
| 14 |
+
y: float
|
| 15 |
+
size: float
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class BlazeThrustEnvironment:
|
| 19 |
+
score_cap = 250
|
| 20 |
+
|
| 21 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 22 |
+
self.sticky_action_prob = sticky_action_prob
|
| 23 |
+
self.horizon = min(horizon, HORIZON)
|
| 24 |
+
self.seed(0)
|
| 25 |
+
self.reset()
|
| 26 |
+
|
| 27 |
+
def seed(self, seed: int) -> None:
|
| 28 |
+
self.random = np.random.default_rng(seed)
|
| 29 |
+
|
| 30 |
+
def reset(self) -> None:
|
| 31 |
+
self.steps = self.hold_time = 0
|
| 32 |
+
self.pressed = self.terminal = False
|
| 33 |
+
self.x, self.y = 50.0, 90.0
|
| 34 |
+
self.vx = self.vy = 0.0
|
| 35 |
+
self.size = 9.0
|
| 36 |
+
self.multiplier = 1
|
| 37 |
+
self.next_bubble = self.random.uniform(10, 40)
|
| 38 |
+
self.winds = [(0.0, self.random.uniform(20, 40))]
|
| 39 |
+
self.bubbles: list[Bubble] = []
|
| 40 |
+
|
| 41 |
+
@classmethod
|
| 42 |
+
def normalize_score(cls, score: float) -> float:
|
| 43 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 44 |
+
|
| 45 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 46 |
+
if self.terminal:
|
| 47 |
+
return 0.0, True
|
| 48 |
+
if self.random.random() >= self.sticky_action_prob:
|
| 49 |
+
self.pressed = action == 5
|
| 50 |
+
self.steps += 1
|
| 51 |
+
reward = 0
|
| 52 |
+
for _ in range(FRAMES_PER_STEP):
|
| 53 |
+
reward += self._advance_frame()
|
| 54 |
+
if self.terminal:
|
| 55 |
+
break
|
| 56 |
+
self.terminal |= self.steps >= self.horizon
|
| 57 |
+
return float(reward), self.terminal
|
| 58 |
+
|
| 59 |
+
def _wind_left(self, y: float) -> bool:
|
| 60 |
+
return any(top <= y <= bottom for top, bottom in self.winds)
|
| 61 |
+
|
| 62 |
+
def _advance_frame(self) -> int:
|
| 63 |
+
self.vy += -0.1 if self.pressed else 0.1
|
| 64 |
+
self.hold_time = self.hold_time + 1 if self.pressed else max(0, self.hold_time - 3)
|
| 65 |
+
scroll = max(0, (60 - self.y) * 0.1)
|
| 66 |
+
self.x += self.vx
|
| 67 |
+
self.y += self.vy + scroll
|
| 68 |
+
self.vx *= 0.95
|
| 69 |
+
self.vy *= 0.95
|
| 70 |
+
if self.x < 5:
|
| 71 |
+
self.x, self.vx = 10, abs(self.vx) * 0.5
|
| 72 |
+
elif self.x > 95:
|
| 73 |
+
self.x, self.vx = 90, -abs(self.vx) * 0.5
|
| 74 |
+
if self.y > 95:
|
| 75 |
+
self.y, self.vy = 90, -abs(self.vy) * 0.3
|
| 76 |
+
|
| 77 |
+
if scroll:
|
| 78 |
+
self.winds = [(top + scroll, bottom + scroll) for top, bottom in self.winds if top + scroll < 120]
|
| 79 |
+
top = self.winds[0][0]
|
| 80 |
+
if top >= 0:
|
| 81 |
+
bottom = top - self.random.uniform(20, 40)
|
| 82 |
+
self.winds.insert(0, (bottom - self.random.uniform(20, 40), bottom))
|
| 83 |
+
self.vx += self.size * (-0.02 if self._wind_left(self.y) else 0.02)
|
| 84 |
+
self.size *= 1 - 0.0016 * (1 + self.hold_time * 0.01)
|
| 85 |
+
if self.size <= 2:
|
| 86 |
+
self.terminal = True
|
| 87 |
+
return 0
|
| 88 |
+
|
| 89 |
+
reward = self._advance_bubbles(scroll)
|
| 90 |
+
if scroll and len(self.bubbles) < 15:
|
| 91 |
+
self.next_bubble -= scroll
|
| 92 |
+
if self.next_bubble <= 0:
|
| 93 |
+
size = self.random.uniform(3, 9)
|
| 94 |
+
self.bubbles.append(Bubble(self.random.uniform(10, 90), -size, size))
|
| 95 |
+
self.next_bubble = self.random.uniform(5, 55)
|
| 96 |
+
return reward
|
| 97 |
+
|
| 98 |
+
def _advance_bubbles(self, scroll: float) -> int:
|
| 99 |
+
reward = 0
|
| 100 |
+
remaining = []
|
| 101 |
+
for bubble in self.bubbles:
|
| 102 |
+
bubble.y += scroll + 0.3 - (bubble.size - 3) * 0.015
|
| 103 |
+
bubble.x += bubble.size * (-0.02 if self._wind_left(bubble.y) else 0.02)
|
| 104 |
+
bubble.x = min(95, max(5, bubble.x))
|
| 105 |
+
if math.hypot(bubble.x - self.x, bubble.y - self.y) <= bubble.size + self.size:
|
| 106 |
+
reward += self.multiplier
|
| 107 |
+
self.multiplier = min(16, self.multiplier + 1)
|
| 108 |
+
self.size = min(25, self.size + bubble.size * 0.2)
|
| 109 |
+
elif bubble.y > 120:
|
| 110 |
+
self.multiplier = 1
|
| 111 |
+
else:
|
| 112 |
+
remaining.append(bubble)
|
| 113 |
+
self.bubbles = remaining
|
| 114 |
+
return reward
|
| 115 |
+
|
| 116 |
+
def state(self) -> np.ndarray:
|
| 117 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 118 |
+
row, column = int(self.y) // 10, int(self.x) // 10
|
| 119 |
+
state[row, column, 0] = True
|
| 120 |
+
state[row, column, 4] = self.vy < 0
|
| 121 |
+
for row in range(10):
|
| 122 |
+
state[row, :, 1 if self._wind_left(10 * row + 5) else 2] = True
|
| 123 |
+
for bubble in self.bubbles:
|
| 124 |
+
if 0 <= bubble.y < 100:
|
| 125 |
+
state[int(bubble.y) // 10, int(bubble.x) // 10, 3] = True
|
| 126 |
+
for row, value in enumerate((round(self.size * 10), self.multiplier, self.hold_time, self.steps)):
|
| 127 |
+
for bit in range(10):
|
| 128 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 129 |
+
state[row + 4, 0, 5] = bool(value & 1024)
|
| 130 |
+
state[9, 0, 5] = self.pressed
|
| 131 |
+
return state
|
runtime/minatar_synth/games/block_stacker.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
FRAMES_PER_STEP = 4
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@dataclass
|
| 12 |
+
class Block:
|
| 13 |
+
x: float
|
| 14 |
+
y: float
|
| 15 |
+
width: float
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class BlockStackerEnvironment:
|
| 19 |
+
score_cap = 50
|
| 20 |
+
|
| 21 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 22 |
+
self.sticky_action_prob = sticky_action_prob
|
| 23 |
+
self.horizon = min(horizon, HORIZON)
|
| 24 |
+
self.seed(0)
|
| 25 |
+
self.reset()
|
| 26 |
+
|
| 27 |
+
def seed(self, seed: int) -> None:
|
| 28 |
+
self.random = np.random.default_rng(seed)
|
| 29 |
+
|
| 30 |
+
def reset(self) -> None:
|
| 31 |
+
self.steps = self.stacked = 0
|
| 32 |
+
self.pressed = self.falling = self.terminal = False
|
| 33 |
+
self.angle = self.vx = self.vy = self.drift = 0.0
|
| 34 |
+
self.current = Block(50, 25, self.random.uniform(10, 30))
|
| 35 |
+
self.tower = [Block(50, 99, 99)]
|
| 36 |
+
|
| 37 |
+
@classmethod
|
| 38 |
+
def normalize_score(cls, score: float) -> float:
|
| 39 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 40 |
+
|
| 41 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 42 |
+
if self.terminal:
|
| 43 |
+
return 0.0, True
|
| 44 |
+
pressed = self.pressed if self.random.random() < self.sticky_action_prob else action == 5
|
| 45 |
+
tap = pressed and not self.pressed
|
| 46 |
+
self.pressed = pressed
|
| 47 |
+
self.steps += 1
|
| 48 |
+
reward = 0
|
| 49 |
+
for frame in range(FRAMES_PER_STEP):
|
| 50 |
+
reward += self._advance_frame(tap and frame == 0)
|
| 51 |
+
if self.terminal:
|
| 52 |
+
break
|
| 53 |
+
self.terminal |= self.steps >= self.horizon
|
| 54 |
+
return float(reward), self.terminal
|
| 55 |
+
|
| 56 |
+
def _advance_frame(self, tap: bool) -> int:
|
| 57 |
+
difficulty = 1 + self.stacked / 50
|
| 58 |
+
self.angle += 0.05 * difficulty
|
| 59 |
+
previous_y = self.current.y
|
| 60 |
+
if self.falling:
|
| 61 |
+
self.current.x += self.vx * difficulty
|
| 62 |
+
self.current.y += self.vy * difficulty
|
| 63 |
+
self.vy += 0.1
|
| 64 |
+
else:
|
| 65 |
+
swing = math.sin(self.angle)
|
| 66 |
+
self.current.x = 50 - 20 * math.sin(swing)
|
| 67 |
+
self.current.y = 5 + 20 * math.cos(swing)
|
| 68 |
+
if tap:
|
| 69 |
+
self.falling = True
|
| 70 |
+
self.vx, self.vy = -math.cos(self.angle), 0.0
|
| 71 |
+
|
| 72 |
+
for block in self.tower:
|
| 73 |
+
block.x = (block.x + self.drift) % 100
|
| 74 |
+
top = min(block.y for block in self.tower)
|
| 75 |
+
reward = 0
|
| 76 |
+
if self.falling:
|
| 77 |
+
for block in sorted(self.tower, key=lambda block: block.y):
|
| 78 |
+
distance = abs((self.current.x - block.x + 50) % 100 - 50)
|
| 79 |
+
if previous_y <= block.y - 10 <= self.current.y and distance < (self.current.width + block.width) / 2:
|
| 80 |
+
self.current.y = block.y - 10
|
| 81 |
+
self.tower.append(self.current)
|
| 82 |
+
self.current = Block(50, 25, self.random.uniform(10, 30))
|
| 83 |
+
self.falling = False
|
| 84 |
+
self.stacked += 1
|
| 85 |
+
self.drift = self.random.uniform(-1, 1) * self.stacked / 50
|
| 86 |
+
reward = 1
|
| 87 |
+
break
|
| 88 |
+
else:
|
| 89 |
+
self.terminal = self.current.y > 105
|
| 90 |
+
scroll = difficulty * 0.01 + max(0, 70 - top) * 0.1
|
| 91 |
+
for block in self.tower:
|
| 92 |
+
block.y += scroll
|
| 93 |
+
self.tower = [block for block in self.tower if block.y <= 106]
|
| 94 |
+
self.terminal |= not self.tower
|
| 95 |
+
return reward
|
| 96 |
+
|
| 97 |
+
def state(self) -> np.ndarray:
|
| 98 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 99 |
+
for block in self.tower:
|
| 100 |
+
self._draw_block(state, block, 0, wraps=True)
|
| 101 |
+
self._draw_block(state, self.current, 2 if self.falling else 1, wraps=False)
|
| 102 |
+
if not self.falling:
|
| 103 |
+
for fraction in np.linspace(0, 1, 8):
|
| 104 |
+
x = 50 + (self.current.x - 50) * fraction
|
| 105 |
+
y = (self.current.y - 5) * fraction
|
| 106 |
+
state[int(y) // 10, int(x) // 10, 3] = True
|
| 107 |
+
if self.drift < 0:
|
| 108 |
+
state[:, :, 4] = state[:, :, 0]
|
| 109 |
+
state[9, 0, 5] = self.pressed
|
| 110 |
+
for row, value in enumerate((self.steps, self.stacked)):
|
| 111 |
+
for bit in range(9):
|
| 112 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 113 |
+
state[9, 1, 5] = (self.vx < 0 if self.falling else math.cos(self.angle) > 0)
|
| 114 |
+
return state
|
| 115 |
+
|
| 116 |
+
@staticmethod
|
| 117 |
+
def _draw_block(state: np.ndarray, block: Block, channel: int, wraps: bool) -> None:
|
| 118 |
+
for row in range(10):
|
| 119 |
+
if row * 10 < block.y + 5 and (row + 1) * 10 > block.y - 5:
|
| 120 |
+
for column in range(10):
|
| 121 |
+
dx = column * 10 + 5 - block.x
|
| 122 |
+
distance = abs((dx + 50) % 100 - 50) if wraps else abs(dx)
|
| 123 |
+
state[row, column, channel] |= distance < block.width / 2 + 5
|
runtime/minatar_synth/games/boarding.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
FRAMES_PER_STEP = 4
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@dataclass
|
| 12 |
+
class Board:
|
| 13 |
+
x: float
|
| 14 |
+
y: float
|
| 15 |
+
slope: int
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@dataclass
|
| 19 |
+
class Passenger:
|
| 20 |
+
x: float
|
| 21 |
+
y: float
|
| 22 |
+
kind: int
|
| 23 |
+
vx: float = 0
|
| 24 |
+
vy: float = 0
|
| 25 |
+
bounces: int = 0
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class BoardingEnvironment:
|
| 29 |
+
score_cap = 250
|
| 30 |
+
|
| 31 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 32 |
+
self.sticky_action_prob = sticky_action_prob
|
| 33 |
+
self.horizon = min(horizon, HORIZON)
|
| 34 |
+
self.seed(0)
|
| 35 |
+
self.reset()
|
| 36 |
+
|
| 37 |
+
def seed(self, seed: int) -> None:
|
| 38 |
+
self.random = np.random.default_rng(seed)
|
| 39 |
+
|
| 40 |
+
def reset(self) -> None:
|
| 41 |
+
self.steps = self.cooldown = 0
|
| 42 |
+
self.pressed = self.ready = self.terminal = False
|
| 43 |
+
self.x, self.direction, self.kind = 50.0, -1, -1
|
| 44 |
+
self.gate = self.gate_target = 91.0
|
| 45 |
+
self.board_credit = 0
|
| 46 |
+
self.boards: list[Board] = []
|
| 47 |
+
self.passengers: list[Passenger] = []
|
| 48 |
+
for y in range(47, 88, 2):
|
| 49 |
+
self._add_board(y)
|
| 50 |
+
|
| 51 |
+
def _add_board(self, y: float) -> None:
|
| 52 |
+
x = self.random.uniform(9, 90)
|
| 53 |
+
if all(math.hypot(board.x - x, board.y - y) >= 16 for board in self.boards):
|
| 54 |
+
self.boards.append(Board(x, y, int(self.random.choice((-1, 1)))))
|
| 55 |
+
|
| 56 |
+
@classmethod
|
| 57 |
+
def normalize_score(cls, score: float) -> float:
|
| 58 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 59 |
+
|
| 60 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 61 |
+
if self.terminal:
|
| 62 |
+
return 0.0, True
|
| 63 |
+
pressed = self.pressed if self.random.random() < self.sticky_action_prob else action == 5
|
| 64 |
+
released = self.pressed and not pressed
|
| 65 |
+
self.pressed = pressed
|
| 66 |
+
self.steps += 1
|
| 67 |
+
reward = 0
|
| 68 |
+
for frame in range(FRAMES_PER_STEP):
|
| 69 |
+
reward += self._advance_frame(released and frame == 0)
|
| 70 |
+
if self.terminal:
|
| 71 |
+
break
|
| 72 |
+
self.terminal |= self.steps >= self.horizon
|
| 73 |
+
return float(reward), self.terminal
|
| 74 |
+
|
| 75 |
+
def _advance_frame(self, released: bool) -> int:
|
| 76 |
+
for board in self.boards:
|
| 77 |
+
board.y -= 0.1
|
| 78 |
+
self.boards = [board for board in self.boards if 15 < board.y < self.gate]
|
| 79 |
+
self.gate_target = max(11, self.gate_target - 0.1)
|
| 80 |
+
self.gate += (self.gate_target - self.gate) * 0.1
|
| 81 |
+
if self.gate <= 12:
|
| 82 |
+
self.terminal = True
|
| 83 |
+
return 0
|
| 84 |
+
if self.board_credit >= 2:
|
| 85 |
+
self._add_board(self.gate - 3)
|
| 86 |
+
self.board_credit -= 2
|
| 87 |
+
|
| 88 |
+
self.cooldown = max(0, self.cooldown - 1)
|
| 89 |
+
boarding = self.pressed and self.ready
|
| 90 |
+
if boarding and not self.cooldown:
|
| 91 |
+
self.passengers.append(Passenger(self.x, 9, self.kind))
|
| 92 |
+
self.cooldown = 10
|
| 93 |
+
self.x += self.direction * (0.1 if boarding else 1)
|
| 94 |
+
if self.x < 10 or self.x > 90:
|
| 95 |
+
self.direction = 1 if self.x < 10 else -1
|
| 96 |
+
if released:
|
| 97 |
+
if self.ready:
|
| 98 |
+
self.kind *= -1
|
| 99 |
+
self.ready = True
|
| 100 |
+
|
| 101 |
+
reward = 0
|
| 102 |
+
survivors = []
|
| 103 |
+
for passenger in self.passengers:
|
| 104 |
+
self._move_passenger(passenger)
|
| 105 |
+
if passenger.y <= self.gate:
|
| 106 |
+
survivors.append(passenger)
|
| 107 |
+
elif (passenger.x - 50) * passenger.kind > 0:
|
| 108 |
+
reward += passenger.bounces
|
| 109 |
+
self.gate_target = min(91, self.gate_target + 2 * passenger.bounces)
|
| 110 |
+
self.board_credit += passenger.bounces
|
| 111 |
+
else:
|
| 112 |
+
reward -= 1 + passenger.bounces
|
| 113 |
+
self.gate_target -= min(20, math.sqrt(1 + passenger.bounces))
|
| 114 |
+
self.passengers = survivors
|
| 115 |
+
return reward
|
| 116 |
+
|
| 117 |
+
def _move_passenger(self, passenger: Passenger) -> None:
|
| 118 |
+
old_x, old_y = passenger.x, passenger.y
|
| 119 |
+
passenger.vx *= 0.9
|
| 120 |
+
passenger.vy = (passenger.vy + 0.2) * 0.9
|
| 121 |
+
passenger.x += passenger.vx
|
| 122 |
+
passenger.y += passenger.vy
|
| 123 |
+
for board in self.boards:
|
| 124 |
+
previous = old_y - board.y - board.slope * (old_x - board.x)
|
| 125 |
+
current = passenger.y - board.y - board.slope * (passenger.x - board.x)
|
| 126 |
+
if previous > 0 or current < 0 or current == previous:
|
| 127 |
+
continue
|
| 128 |
+
crossing_x = old_x + (passenger.x - old_x) * (-previous / (current - previous))
|
| 129 |
+
if abs(crossing_x - board.x) <= 4:
|
| 130 |
+
impulse = math.hypot(passenger.vx, passenger.vy) * math.sqrt(2)
|
| 131 |
+
passenger.vx += board.slope * impulse
|
| 132 |
+
passenger.vy += impulse
|
| 133 |
+
passenger.x += 2 * passenger.vx
|
| 134 |
+
passenger.y += 2 * passenger.vy
|
| 135 |
+
passenger.bounces += 1
|
| 136 |
+
break
|
| 137 |
+
if passenger.x < 0 or passenger.x > 99:
|
| 138 |
+
passenger.x = min(99, max(0, passenger.x))
|
| 139 |
+
passenger.vx *= -1
|
| 140 |
+
|
| 141 |
+
def state(self) -> np.ndarray:
|
| 142 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 143 |
+
state[0, int(self.x) // 10, 0] = True
|
| 144 |
+
state[int(self.gate) // 10, :, 1] = True
|
| 145 |
+
for board in self.boards:
|
| 146 |
+
state[int(board.y) // 10, int(board.x) // 10, 2 if board.slope < 0 else 3] = True
|
| 147 |
+
for passenger in self.passengers:
|
| 148 |
+
state[int(passenger.y) // 10, int(passenger.x) // 10, 4 if passenger.kind < 0 else 5] = True
|
| 149 |
+
state[0, :4, 1] = self.kind > 0, self.direction > 0, self.ready, self.pressed
|
| 150 |
+
return state
|
runtime/minatar_synth/games/bola_thread.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
FRAMES_PER_STEP = 4
|
| 9 |
+
SCROLL = 0.55
|
| 10 |
+
ROTATION = 0.022
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@dataclass
|
| 14 |
+
class Wall:
|
| 15 |
+
x: float
|
| 16 |
+
gap_y: float
|
| 17 |
+
height: float
|
| 18 |
+
nub: bool
|
| 19 |
+
passed: bool = False
|
| 20 |
+
|
| 21 |
+
def blocked(self, y: float, margin: float = 0) -> bool:
|
| 22 |
+
return (
|
| 23 |
+
abs(y - self.gap_y) + margin > self.height / 2
|
| 24 |
+
or self.nub and abs(y - 50) < 4 + margin
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@dataclass
|
| 29 |
+
class Beacon:
|
| 30 |
+
x: float
|
| 31 |
+
y: float
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class BolaThreadEnvironment:
|
| 35 |
+
score_cap = 150
|
| 36 |
+
|
| 37 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 38 |
+
self.sticky_action_prob = sticky_action_prob
|
| 39 |
+
self.horizon = min(horizon, HORIZON)
|
| 40 |
+
self.seed(0)
|
| 41 |
+
self.reset()
|
| 42 |
+
|
| 43 |
+
def seed(self, seed: int) -> None:
|
| 44 |
+
self.random = np.random.default_rng(seed)
|
| 45 |
+
|
| 46 |
+
def reset(self) -> None:
|
| 47 |
+
self.steps = self.freeze = 0
|
| 48 |
+
self.pressed = self.terminal = False
|
| 49 |
+
self.angle, self.radius = 0.0, 20.0
|
| 50 |
+
self.next_wall, self.next_beacon = 60.0, 100
|
| 51 |
+
self.walls: list[Wall] = []
|
| 52 |
+
self.beacons: list[Beacon] = []
|
| 53 |
+
|
| 54 |
+
@property
|
| 55 |
+
def balls(self) -> tuple[tuple[float, float], tuple[float, float]]:
|
| 56 |
+
dx = math.cos(self.angle) * self.radius
|
| 57 |
+
dy = math.sin(self.angle) * self.radius
|
| 58 |
+
return (30 + dx, 50 + dy), (30 - dx, 50 - dy)
|
| 59 |
+
|
| 60 |
+
@classmethod
|
| 61 |
+
def normalize_score(cls, score: float) -> float:
|
| 62 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 63 |
+
|
| 64 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 65 |
+
if self.terminal:
|
| 66 |
+
return 0.0, True
|
| 67 |
+
if self.random.random() >= self.sticky_action_prob:
|
| 68 |
+
self.pressed = action == 5
|
| 69 |
+
self.steps += 1
|
| 70 |
+
reward = 0
|
| 71 |
+
for _ in range(FRAMES_PER_STEP):
|
| 72 |
+
reward += self._advance_frame()
|
| 73 |
+
if self.terminal:
|
| 74 |
+
break
|
| 75 |
+
self.terminal |= self.steps >= self.horizon
|
| 76 |
+
return float(reward), self.terminal
|
| 77 |
+
|
| 78 |
+
def _advance_frame(self) -> int:
|
| 79 |
+
if self.freeze:
|
| 80 |
+
self.freeze -= 1
|
| 81 |
+
return 0
|
| 82 |
+
self.radius = max(6, self.radius - 0.6) if self.pressed else min(26, self.radius + 0.45)
|
| 83 |
+
self.angle = (self.angle + ROTATION) % math.tau
|
| 84 |
+
self.next_wall -= SCROLL
|
| 85 |
+
self.next_beacon -= 1
|
| 86 |
+
if self.next_wall < 0:
|
| 87 |
+
arrival_angle = self.angle + ROTATION * (108 - 30) / SCROLL
|
| 88 |
+
nub = self.random.random() < 0.36 and abs(math.sin(arrival_angle)) > 0.8
|
| 89 |
+
gap_y = self.random.uniform(42, 58)
|
| 90 |
+
height = self.random.uniform(42, 54) if nub else self.random.uniform(25, 40)
|
| 91 |
+
self.walls.append(Wall(108, gap_y, height, nub))
|
| 92 |
+
self.next_wall = self.random.uniform(65, 90)
|
| 93 |
+
if self.next_beacon < 0:
|
| 94 |
+
self.beacons.append(Beacon(108, self.random.uniform(20, 80)))
|
| 95 |
+
self.next_beacon = int(self.random.integers(90, 151))
|
| 96 |
+
|
| 97 |
+
reward = 0
|
| 98 |
+
balls = self.balls
|
| 99 |
+
for wall in self.walls:
|
| 100 |
+
wall.x -= SCROLL
|
| 101 |
+
if any(abs(x - wall.x) < 4 and wall.blocked(y, margin=2) for x, y in balls):
|
| 102 |
+
self.terminal = True
|
| 103 |
+
return reward
|
| 104 |
+
if not wall.passed and wall.x < 30:
|
| 105 |
+
wall.passed = True
|
| 106 |
+
bonus = max(1, int((self.radius - 6) // 3))
|
| 107 |
+
reward += bonus
|
| 108 |
+
self.freeze = 2 + 2 * (bonus >= 3) + 2 * (bonus >= 5)
|
| 109 |
+
self.walls = [wall for wall in self.walls if wall.x >= -5]
|
| 110 |
+
survivors = []
|
| 111 |
+
for beacon in self.beacons:
|
| 112 |
+
beacon.x -= SCROLL
|
| 113 |
+
if any(abs(x - beacon.x) < 4.5 and abs(y - beacon.y) < 4.5 for x, y in balls):
|
| 114 |
+
reward += 8
|
| 115 |
+
elif beacon.x >= -5:
|
| 116 |
+
survivors.append(beacon)
|
| 117 |
+
self.beacons = survivors
|
| 118 |
+
return reward
|
| 119 |
+
|
| 120 |
+
def state(self) -> np.ndarray:
|
| 121 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 122 |
+
state[5, 3, 0] = True
|
| 123 |
+
for x, y in self.balls:
|
| 124 |
+
state[int(y) // 10, int(x) // 10, 1] = True
|
| 125 |
+
for wall in self.walls:
|
| 126 |
+
if 0 <= wall.x < 100:
|
| 127 |
+
column = int(wall.x) // 10
|
| 128 |
+
for row in range(10):
|
| 129 |
+
state[row, column, 2] |= abs(10 * row + 5 - wall.gap_y) > wall.height / 2
|
| 130 |
+
state[4:6, column, 3] |= wall.nub
|
| 131 |
+
for beacon in self.beacons:
|
| 132 |
+
if 0 <= beacon.x < 100:
|
| 133 |
+
state[int(beacon.y) // 10, int(beacon.x) // 10, 4] = True
|
| 134 |
+
for row, value in enumerate((self.steps, round(self.radius * 10), round(self.angle * 80), self.freeze)):
|
| 135 |
+
for bit in range(9):
|
| 136 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 137 |
+
state[9, 0, 5] = self.pressed
|
| 138 |
+
return state
|
runtime/minatar_synth/games/bomb_up.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
FRAMES_PER_STEP = 4
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@dataclass
|
| 12 |
+
class Body:
|
| 13 |
+
x: float
|
| 14 |
+
y: float
|
| 15 |
+
vx: float = 0
|
| 16 |
+
vy: float = 0
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@dataclass
|
| 20 |
+
class Blast:
|
| 21 |
+
x: float
|
| 22 |
+
y: float
|
| 23 |
+
age: int = 0
|
| 24 |
+
|
| 25 |
+
@property
|
| 26 |
+
def radius(self) -> float:
|
| 27 |
+
return 25 * math.sin(self.age * 0.15)
|
| 28 |
+
|
| 29 |
+
def hits(self, x: float, y: float, tolerance: float) -> bool:
|
| 30 |
+
return abs(math.hypot(x - self.x, y - self.y) - self.radius) <= tolerance
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class BombUpEnvironment:
|
| 34 |
+
score_cap = 250
|
| 35 |
+
|
| 36 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 37 |
+
self.sticky_action_prob = sticky_action_prob
|
| 38 |
+
self.horizon = min(horizon, HORIZON)
|
| 39 |
+
self.seed(0)
|
| 40 |
+
self.reset()
|
| 41 |
+
|
| 42 |
+
def seed(self, seed: int) -> None:
|
| 43 |
+
self.random = np.random.default_rng(seed)
|
| 44 |
+
|
| 45 |
+
def reset(self) -> None:
|
| 46 |
+
self.steps = self.spawn_timer = 0
|
| 47 |
+
self.multiplier = 1
|
| 48 |
+
self.pressed = self.terminal = False
|
| 49 |
+
self.player = Body(50, 50)
|
| 50 |
+
self.spawner = Body(80, 0)
|
| 51 |
+
self.bomb: Body | None = None
|
| 52 |
+
self.blast: Blast | None = None
|
| 53 |
+
self.rocks: list[Body] = []
|
| 54 |
+
|
| 55 |
+
@classmethod
|
| 56 |
+
def normalize_score(cls, score: float) -> float:
|
| 57 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 58 |
+
|
| 59 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 60 |
+
if self.terminal:
|
| 61 |
+
return 0.0, True
|
| 62 |
+
pressed = self.pressed if self.random.random() < self.sticky_action_prob else action == 5
|
| 63 |
+
tap = pressed and not self.pressed
|
| 64 |
+
self.pressed = pressed
|
| 65 |
+
self.steps += 1
|
| 66 |
+
reward = 0
|
| 67 |
+
for frame in range(FRAMES_PER_STEP):
|
| 68 |
+
reward += self._advance_frame(tap and frame == 0)
|
| 69 |
+
if self.terminal:
|
| 70 |
+
break
|
| 71 |
+
self.terminal |= self.steps >= self.horizon
|
| 72 |
+
return float(reward), self.terminal
|
| 73 |
+
|
| 74 |
+
@staticmethod
|
| 75 |
+
def _bounce(body: Body) -> None:
|
| 76 |
+
if (body.x < 7 and body.vx < 0) or (body.x > 93 and body.vx > 0):
|
| 77 |
+
body.x = min(93, max(7, body.x))
|
| 78 |
+
body.vx *= -0.7
|
| 79 |
+
|
| 80 |
+
def _tap(self) -> None:
|
| 81 |
+
player = self.player
|
| 82 |
+
if self.bomb is None:
|
| 83 |
+
direction = -1 if player.vx < 0 else 1
|
| 84 |
+
self.bomb = Body(player.x + 2 * direction, player.y, player.vx + 0.5 * direction, player.vy)
|
| 85 |
+
self.multiplier = 1
|
| 86 |
+
return
|
| 87 |
+
self.blast = Blast(self.bomb.x, self.bomb.y)
|
| 88 |
+
dx, dy = player.x - self.bomb.x, player.y - self.bomb.y
|
| 89 |
+
distance_squared = max(1, dx * dx + dy * dy)
|
| 90 |
+
player.vx += 20 * dx / distance_squared
|
| 91 |
+
player.vy += 20 * dy / distance_squared
|
| 92 |
+
self.bomb = None
|
| 93 |
+
|
| 94 |
+
def _advance_frame(self, tap: bool) -> int:
|
| 95 |
+
player = self.player
|
| 96 |
+
scroll = (40 - player.y) * 0.3 if player.y < 40 else min(0, (60 - player.y) * 0.2)
|
| 97 |
+
self._bounce(player)
|
| 98 |
+
player.vx *= 0.99
|
| 99 |
+
player.vy = (player.vy + 0.02) * 0.99
|
| 100 |
+
player.x += player.vx
|
| 101 |
+
player.y += player.vy + scroll
|
| 102 |
+
if tap:
|
| 103 |
+
self._tap()
|
| 104 |
+
if self.bomb is not None:
|
| 105 |
+
bomb = self.bomb
|
| 106 |
+
self._bounce(bomb)
|
| 107 |
+
bomb.vx *= 0.98
|
| 108 |
+
bomb.vy = (bomb.vy + 0.15) * 0.98
|
| 109 |
+
bomb.x += bomb.vx
|
| 110 |
+
bomb.y += bomb.vy + scroll
|
| 111 |
+
if bomb.y > 103:
|
| 112 |
+
self.bomb = None
|
| 113 |
+
if self.blast is not None:
|
| 114 |
+
self.blast.age += 1
|
| 115 |
+
if self.blast.radius < 0:
|
| 116 |
+
self.blast = None
|
| 117 |
+
|
| 118 |
+
self.spawner.vx = (self.spawner.vx + self.random.uniform(-0.1, 0.1)) * 0.99
|
| 119 |
+
self.spawner.x += self.spawner.vx
|
| 120 |
+
self._bounce(self.spawner)
|
| 121 |
+
if self.spawn_timer == 0:
|
| 122 |
+
bottom = self.random.random() > (0.05 if player.y > 30 else 0.9)
|
| 123 |
+
y = 103 if bottom else -3
|
| 124 |
+
vy = -self.random.uniform(0.5, 1) if bottom else player.vy / 2
|
| 125 |
+
self.rocks.append(Body(self.spawner.x, y, vy=vy))
|
| 126 |
+
self.spawn_timer = int(self.random.integers(8, 11))
|
| 127 |
+
self.spawn_timer -= 1
|
| 128 |
+
|
| 129 |
+
reward = 0
|
| 130 |
+
remaining = []
|
| 131 |
+
for rock in self.rocks:
|
| 132 |
+
rock.y += rock.vy
|
| 133 |
+
rock.vy = (rock.vy + 0.01) * 0.99
|
| 134 |
+
rock.y += rock.vy + scroll
|
| 135 |
+
if self.blast is not None and self.blast.hits(rock.x, rock.y, 3.5):
|
| 136 |
+
reward += self.multiplier
|
| 137 |
+
self.multiplier += 1
|
| 138 |
+
continue
|
| 139 |
+
if abs(rock.x - player.x) < 5.5 and abs(rock.y - player.y) < 5.5:
|
| 140 |
+
self.terminal = True
|
| 141 |
+
if -50 <= rock.y <= 150:
|
| 142 |
+
remaining.append(rock)
|
| 143 |
+
self.rocks = remaining
|
| 144 |
+
return reward
|
| 145 |
+
|
| 146 |
+
def state(self) -> np.ndarray:
|
| 147 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 148 |
+
row = min(9, max(0, int(self.player.y) // 10))
|
| 149 |
+
column = min(9, max(0, int(self.player.x) // 10))
|
| 150 |
+
state[row, column, 0] = True
|
| 151 |
+
state[row, column, 4] = self.player.vy < 0
|
| 152 |
+
if self.bomb is not None and 0 <= self.bomb.x < 100 and 0 <= self.bomb.y < 100:
|
| 153 |
+
state[int(self.bomb.y) // 10, int(self.bomb.x) // 10, 1] = True
|
| 154 |
+
for rock in self.rocks:
|
| 155 |
+
if 0 <= rock.y < 100:
|
| 156 |
+
state[int(rock.y) // 10, int(rock.x) // 10, 2] = True
|
| 157 |
+
if self.blast is not None:
|
| 158 |
+
for y in range(10):
|
| 159 |
+
for x in range(10):
|
| 160 |
+
state[y, x, 3] = self.blast.hits(x * 10 + 5, y * 10 + 5, 7)
|
| 161 |
+
for row, value in enumerate((self.steps, self.multiplier)):
|
| 162 |
+
for bit in range(9):
|
| 163 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 164 |
+
state[9, :3, 5] = self.pressed, self.bomb is not None, self.player.vx < 0
|
| 165 |
+
return state
|
runtime/minatar_synth/games/bore_comet.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
FRAMES_PER_STEP = 4
|
| 9 |
+
PLAYER_X = 35
|
| 10 |
+
SCROLL = 0.6
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@dataclass
|
| 14 |
+
class Rock:
|
| 15 |
+
x: float
|
| 16 |
+
y: float
|
| 17 |
+
width: float
|
| 18 |
+
height: float
|
| 19 |
+
bore: int = 0
|
| 20 |
+
|
| 21 |
+
@property
|
| 22 |
+
def threshold(self) -> int:
|
| 23 |
+
return round(math.sqrt(self.width * self.height) / (SCROLL / 0.5))
|
| 24 |
+
|
| 25 |
+
def contains(self, y: float) -> bool:
|
| 26 |
+
return abs(self.x - PLAYER_X) < self.width / 2 + 2 and abs(self.y - y) < self.height / 2 + 2
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@dataclass
|
| 30 |
+
class Enemy:
|
| 31 |
+
x: float
|
| 32 |
+
y: float
|
| 33 |
+
vx: float
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class BoreCometEnvironment:
|
| 37 |
+
score_cap = 500
|
| 38 |
+
|
| 39 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 40 |
+
self.sticky_action_prob = sticky_action_prob
|
| 41 |
+
self.horizon = min(horizon, HORIZON)
|
| 42 |
+
self.seed(0)
|
| 43 |
+
self.reset()
|
| 44 |
+
|
| 45 |
+
def seed(self, seed: int) -> None:
|
| 46 |
+
self.random = np.random.default_rng(seed)
|
| 47 |
+
|
| 48 |
+
def reset(self) -> None:
|
| 49 |
+
self.steps = self.freeze = 0
|
| 50 |
+
self.pressed = self.terminal = self.was_in_rock = False
|
| 51 |
+
self.y, self.vy = 30.0, 0.0
|
| 52 |
+
self.next_rock, self.next_enemy = 20.0, 60
|
| 53 |
+
self.rocks: list[Rock] = []
|
| 54 |
+
self.enemies: list[Enemy] = []
|
| 55 |
+
|
| 56 |
+
@classmethod
|
| 57 |
+
def normalize_score(cls, score: float) -> float:
|
| 58 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 59 |
+
|
| 60 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 61 |
+
if self.terminal:
|
| 62 |
+
return 0.0, True
|
| 63 |
+
if self.random.random() >= self.sticky_action_prob:
|
| 64 |
+
self.pressed = action == 5
|
| 65 |
+
self.steps += 1
|
| 66 |
+
reward = 0
|
| 67 |
+
for _ in range(FRAMES_PER_STEP):
|
| 68 |
+
reward += self._advance_frame()
|
| 69 |
+
if self.terminal:
|
| 70 |
+
break
|
| 71 |
+
self.terminal |= self.steps >= self.horizon
|
| 72 |
+
return float(reward), self.terminal
|
| 73 |
+
|
| 74 |
+
def _advance_frame(self) -> int:
|
| 75 |
+
if self.freeze:
|
| 76 |
+
self.freeze -= 1
|
| 77 |
+
return 0
|
| 78 |
+
rock = next((rock for rock in reversed(self.rocks) if rock.contains(self.y)), None)
|
| 79 |
+
if rock is not None and not self.pressed:
|
| 80 |
+
self.terminal = True
|
| 81 |
+
return 0
|
| 82 |
+
|
| 83 |
+
reward = 0
|
| 84 |
+
if self.pressed:
|
| 85 |
+
self.vy -= 0.06 if rock is not None else 0.11
|
| 86 |
+
if rock is not None:
|
| 87 |
+
rock.bore += 1
|
| 88 |
+
reward += 1
|
| 89 |
+
if rock.bore >= rock.threshold:
|
| 90 |
+
reward += rock.threshold + self._blast(15 + (rock.width + rock.height) / 4, 3)
|
| 91 |
+
self.rocks.remove(rock)
|
| 92 |
+
rock = None
|
| 93 |
+
self.vy = min(1.8, max(-1.5, self.vy + 0.05))
|
| 94 |
+
self.y += self.vy * (0.45 if rock is not None else 1)
|
| 95 |
+
if self.y < 10:
|
| 96 |
+
self.y, self.vy = 10, 0
|
| 97 |
+
if self.was_in_rock and rock is None and self.pressed:
|
| 98 |
+
reward += self._blast(22, 2)
|
| 99 |
+
self.was_in_rock = rock is not None
|
| 100 |
+
|
| 101 |
+
self._spawn()
|
| 102 |
+
for obstacle in self.rocks:
|
| 103 |
+
obstacle.x -= SCROLL
|
| 104 |
+
self.rocks = [obstacle for obstacle in self.rocks if obstacle.x >= -20]
|
| 105 |
+
for enemy in self.enemies:
|
| 106 |
+
enemy.x += enemy.vx
|
| 107 |
+
if rock is None and math.hypot(enemy.x - PLAYER_X, enemy.y - self.y) < 5:
|
| 108 |
+
self.terminal = True
|
| 109 |
+
self.enemies = [enemy for enemy in self.enemies if enemy.x >= -8]
|
| 110 |
+
self.terminal |= self.y > 91
|
| 111 |
+
return reward
|
| 112 |
+
|
| 113 |
+
def _blast(self, radius: float, freeze: int) -> int:
|
| 114 |
+
survivors = [enemy for enemy in self.enemies if math.hypot(enemy.x - PLAYER_X, enemy.y - self.y) >= radius]
|
| 115 |
+
kills = len(self.enemies) - len(survivors)
|
| 116 |
+
self.enemies = survivors
|
| 117 |
+
if kills or freeze == 3:
|
| 118 |
+
self.freeze = max(self.freeze, min(8, freeze + kills))
|
| 119 |
+
return 3 * kills**2
|
| 120 |
+
|
| 121 |
+
def _spawn(self) -> None:
|
| 122 |
+
self.next_rock -= SCROLL
|
| 123 |
+
if self.next_rock < 0:
|
| 124 |
+
self.rocks.append(Rock(112, self.random.uniform(25, 75), self.random.uniform(14, 30), self.random.uniform(10, 22)))
|
| 125 |
+
self.next_rock = self.random.uniform(30, 55)
|
| 126 |
+
self.next_enemy -= 1
|
| 127 |
+
if self.next_enemy < 0:
|
| 128 |
+
self.enemies.append(Enemy(105, self.random.uniform(8, 84), -SCROLL - self.random.uniform(0.2, 0.5)))
|
| 129 |
+
self.next_enemy = int(self.random.integers(50, 91))
|
| 130 |
+
|
| 131 |
+
def state(self) -> np.ndarray:
|
| 132 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 133 |
+
row = int(self.y) // 10
|
| 134 |
+
state[row, 3, 0] = True
|
| 135 |
+
state[row, 3, 4] = self.pressed
|
| 136 |
+
state[9, :, 3] = True
|
| 137 |
+
for rock in self.rocks:
|
| 138 |
+
for row in range(10):
|
| 139 |
+
if abs(row * 10 + 5 - rock.y) < rock.height / 2 + 5:
|
| 140 |
+
for column in range(10):
|
| 141 |
+
state[row, column, 1] |= abs(column * 10 + 5 - rock.x) < rock.width / 2 + 5
|
| 142 |
+
for enemy in self.enemies:
|
| 143 |
+
if 0 <= enemy.x < 100:
|
| 144 |
+
state[int(enemy.y) // 10, int(enemy.x) // 10, 2] = True
|
| 145 |
+
active_rock = next((rock for rock in reversed(self.rocks) if rock.contains(self.y)), None)
|
| 146 |
+
for row, value in enumerate((self.steps, self.freeze, active_rock.bore if active_rock else 0)):
|
| 147 |
+
for bit in range(9):
|
| 148 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 149 |
+
state[9, :2, 5] = self.vy < 0, self.was_in_rock
|
| 150 |
+
return state
|
runtime/minatar_synth/games/bottop.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
HORIZON = 400
|
| 7 |
+
FRAMES_PER_STEP = 4
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@dataclass
|
| 11 |
+
class Spike:
|
| 12 |
+
x: float
|
| 13 |
+
y: float
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class BottopEnvironment:
|
| 17 |
+
score_cap = HORIZON * FRAMES_PER_STEP / 10
|
| 18 |
+
|
| 19 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 20 |
+
self.sticky_action_prob = sticky_action_prob
|
| 21 |
+
self.horizon = min(horizon, HORIZON)
|
| 22 |
+
self.seed(0)
|
| 23 |
+
self.reset()
|
| 24 |
+
|
| 25 |
+
def seed(self, seed: int) -> None:
|
| 26 |
+
self.random = np.random.default_rng(seed)
|
| 27 |
+
|
| 28 |
+
def reset(self) -> None:
|
| 29 |
+
self.steps = 0
|
| 30 |
+
self.spawn_distance = self.height = self.velocity = 0.0
|
| 31 |
+
self.jumping = self.pressed = self.terminal = False
|
| 32 |
+
self.spikes: list[Spike] = []
|
| 33 |
+
|
| 34 |
+
@classmethod
|
| 35 |
+
def normalize_score(cls, score: float) -> float:
|
| 36 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 37 |
+
|
| 38 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 39 |
+
if self.terminal:
|
| 40 |
+
return 0.0, True
|
| 41 |
+
if self.random.random() >= self.sticky_action_prob:
|
| 42 |
+
self.pressed = action == 5
|
| 43 |
+
self.steps += 1
|
| 44 |
+
frames = 0
|
| 45 |
+
for _ in range(FRAMES_PER_STEP):
|
| 46 |
+
self._advance_frame()
|
| 47 |
+
frames += 1
|
| 48 |
+
if self.terminal:
|
| 49 |
+
break
|
| 50 |
+
self.terminal |= self.steps >= self.horizon
|
| 51 |
+
return frames / 10, self.terminal
|
| 52 |
+
|
| 53 |
+
def _advance_frame(self) -> None:
|
| 54 |
+
self.spawn_distance -= 1
|
| 55 |
+
if self.spawn_distance < 0:
|
| 56 |
+
if self.random.random() < 0.33:
|
| 57 |
+
y = float(self.random.choice((8, 92)))
|
| 58 |
+
else:
|
| 59 |
+
y = self.random.uniform(8, 92)
|
| 60 |
+
self.spikes.append(Spike(103, y))
|
| 61 |
+
self.spawn_distance += self.random.uniform(30, 60)
|
| 62 |
+
for spike in self.spikes:
|
| 63 |
+
spike.x -= 1
|
| 64 |
+
self.spikes = [spike for spike in self.spikes if spike.x > -3]
|
| 65 |
+
|
| 66 |
+
if not self.jumping and self.pressed:
|
| 67 |
+
self.jumping = True
|
| 68 |
+
self.velocity = 3
|
| 69 |
+
if self.jumping:
|
| 70 |
+
self.velocity -= 0.1 if self.pressed else 0.3
|
| 71 |
+
self.height += self.velocity
|
| 72 |
+
if self.height < 0:
|
| 73 |
+
self.height = self.velocity = 0
|
| 74 |
+
self.jumping = False
|
| 75 |
+
top, bottom = 8 + self.height, 92 - self.height
|
| 76 |
+
self.terminal = any(
|
| 77 |
+
abs(spike.x - 9) < 6 and min(abs(spike.y - top), abs(spike.y - bottom)) < 6
|
| 78 |
+
for spike in self.spikes
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
def state(self) -> np.ndarray:
|
| 82 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 83 |
+
state[int(8 + self.height) // 10, 0, 0] = True
|
| 84 |
+
state[int(92 - self.height) // 10, 0, 1] = True
|
| 85 |
+
for spike in self.spikes:
|
| 86 |
+
if 0 <= spike.x < 100:
|
| 87 |
+
state[int(spike.y) // 10, int(spike.x) // 10, 2] = True
|
| 88 |
+
state[(0, 9), :, 3] = True
|
| 89 |
+
state[0, :3, 4] = self.pressed, self.jumping, self.velocity > 0
|
| 90 |
+
for bit in range(9):
|
| 91 |
+
state[9, bit, 5] = bool(self.steps & (1 << bit))
|
| 92 |
+
return state
|
runtime/minatar_synth/games/bowling.py
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
PIN_POSITIONS = ((4, 6), (3, 7), (5, 7), (2, 8), (4, 8), (6, 8), (1, 9), (3, 9), (5, 9), (7, 9))
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class BowlingEnvironment:
|
| 8 |
+
"""Ten-frame steerable bowling with standard strike, spare, and tenth-frame scoring."""
|
| 9 |
+
|
| 10 |
+
score_cap = 300.0
|
| 11 |
+
|
| 12 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = 500):
|
| 13 |
+
self.sticky_action_prob = sticky_action_prob
|
| 14 |
+
self.horizon = horizon
|
| 15 |
+
self.random = np.random.RandomState()
|
| 16 |
+
self.seed(0)
|
| 17 |
+
self.reset()
|
| 18 |
+
|
| 19 |
+
@classmethod
|
| 20 |
+
def normalize_score(cls, raw_score: float) -> float:
|
| 21 |
+
return min(max(raw_score, 0.0), cls.score_cap) / cls.score_cap
|
| 22 |
+
|
| 23 |
+
def seed(self, seed: int) -> None:
|
| 24 |
+
self.random.seed(seed)
|
| 25 |
+
|
| 26 |
+
def reset(self) -> None:
|
| 27 |
+
self.bowler_row = 4
|
| 28 |
+
self.ball: list[int] | None = None
|
| 29 |
+
self.previous_ball: list[int] | None = None
|
| 30 |
+
self.hook = 0
|
| 31 |
+
self.standing = set(range(10))
|
| 32 |
+
self.frames: list[list[int]] = [[] for _ in range(10)]
|
| 33 |
+
self.frame = 0
|
| 34 |
+
self.confirmed_score = 0
|
| 35 |
+
self.steps = 0
|
| 36 |
+
self.last_action = 0
|
| 37 |
+
self.terminal = False
|
| 38 |
+
|
| 39 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 40 |
+
if self.terminal:
|
| 41 |
+
return 0.0, True
|
| 42 |
+
if self.random.rand() < self.sticky_action_prob:
|
| 43 |
+
action = self.last_action
|
| 44 |
+
self.last_action = action
|
| 45 |
+
if self.ball is None:
|
| 46 |
+
if action == 2:
|
| 47 |
+
self.bowler_row = max(1, self.bowler_row - 1)
|
| 48 |
+
elif action == 4:
|
| 49 |
+
self.bowler_row = min(7, self.bowler_row + 1)
|
| 50 |
+
elif action == 5:
|
| 51 |
+
self.ball = [self.bowler_row, 0]
|
| 52 |
+
self.previous_ball = self.ball.copy()
|
| 53 |
+
self.hook = 0
|
| 54 |
+
else:
|
| 55 |
+
self.previous_ball = self.ball.copy()
|
| 56 |
+
if action == 2:
|
| 57 |
+
self.ball[0] = max(1, self.ball[0] - 1)
|
| 58 |
+
self.hook = max(-1, self.hook - 1)
|
| 59 |
+
elif action == 4:
|
| 60 |
+
self.ball[0] = min(7, self.ball[0] + 1)
|
| 61 |
+
self.hook = min(1, self.hook + 1)
|
| 62 |
+
self.ball[1] += 1
|
| 63 |
+
if self.ball[1] >= 9:
|
| 64 |
+
previous_score = self.confirmed_score
|
| 65 |
+
self._finish_roll()
|
| 66 |
+
self.confirmed_score = self._score_available()
|
| 67 |
+
reward = float(self.confirmed_score - previous_score)
|
| 68 |
+
self.steps += 1
|
| 69 |
+
return reward, self.terminal
|
| 70 |
+
self.steps += 1
|
| 71 |
+
if self.steps >= self.horizon:
|
| 72 |
+
self.terminal = True
|
| 73 |
+
return 0.0, self.terminal
|
| 74 |
+
|
| 75 |
+
def _finish_roll(self) -> None:
|
| 76 |
+
knocked = self._knocked_pins()
|
| 77 |
+
self.standing -= knocked
|
| 78 |
+
pinfall = len(knocked)
|
| 79 |
+
frame_rolls = self.frames[self.frame]
|
| 80 |
+
frame_rolls.append(pinfall)
|
| 81 |
+
self.ball = None
|
| 82 |
+
self.previous_ball = None
|
| 83 |
+
self.hook = 0
|
| 84 |
+
if self.frame < 9:
|
| 85 |
+
if pinfall == 10 or len(frame_rolls) == 2:
|
| 86 |
+
self.frame += 1
|
| 87 |
+
self.standing = set(range(10))
|
| 88 |
+
return
|
| 89 |
+
if len(frame_rolls) == 1:
|
| 90 |
+
if pinfall == 10:
|
| 91 |
+
self.standing = set(range(10))
|
| 92 |
+
elif len(frame_rolls) == 2:
|
| 93 |
+
if frame_rolls[0] == 10:
|
| 94 |
+
if pinfall == 10:
|
| 95 |
+
self.standing = set(range(10))
|
| 96 |
+
elif sum(frame_rolls) == 10:
|
| 97 |
+
self.standing = set(range(10))
|
| 98 |
+
else:
|
| 99 |
+
self.terminal = True
|
| 100 |
+
else:
|
| 101 |
+
self.terminal = True
|
| 102 |
+
|
| 103 |
+
def _knocked_pins(self) -> set[int]:
|
| 104 |
+
if not self.standing or self.ball is None:
|
| 105 |
+
return set()
|
| 106 |
+
entry_row = self.ball[0]
|
| 107 |
+
if self.standing == set(range(10)) and entry_row == 4 and self.hook != 0:
|
| 108 |
+
return self.standing.copy()
|
| 109 |
+
knocked = {
|
| 110 |
+
index
|
| 111 |
+
for index in self.standing
|
| 112 |
+
if abs(PIN_POSITIONS[index][0] - entry_row) <= 1
|
| 113 |
+
}
|
| 114 |
+
if self.hook < 0:
|
| 115 |
+
knocked |= {index for index in self.standing if PIN_POSITIONS[index][0] < entry_row and abs(PIN_POSITIONS[index][0] - entry_row) <= 2}
|
| 116 |
+
elif self.hook > 0:
|
| 117 |
+
knocked |= {index for index in self.standing if PIN_POSITIONS[index][0] > entry_row and abs(PIN_POSITIONS[index][0] - entry_row) <= 2}
|
| 118 |
+
return knocked
|
| 119 |
+
|
| 120 |
+
def _score_available(self) -> int:
|
| 121 |
+
flattened = [roll for frame in self.frames for roll in frame]
|
| 122 |
+
score = 0
|
| 123 |
+
cursor = 0
|
| 124 |
+
for frame_index in range(9):
|
| 125 |
+
frame = self.frames[frame_index]
|
| 126 |
+
if not frame:
|
| 127 |
+
break
|
| 128 |
+
if frame[0] == 10:
|
| 129 |
+
if cursor + 2 >= len(flattened):
|
| 130 |
+
break
|
| 131 |
+
score += 10 + flattened[cursor + 1] + flattened[cursor + 2]
|
| 132 |
+
cursor += 1
|
| 133 |
+
elif len(frame) == 2 and sum(frame) == 10:
|
| 134 |
+
if cursor + 2 >= len(flattened):
|
| 135 |
+
break
|
| 136 |
+
score += 10 + flattened[cursor + 2]
|
| 137 |
+
cursor += 2
|
| 138 |
+
elif len(frame) == 2:
|
| 139 |
+
score += sum(frame)
|
| 140 |
+
cursor += 2
|
| 141 |
+
else:
|
| 142 |
+
break
|
| 143 |
+
tenth = self.frames[9]
|
| 144 |
+
if self.frame == 9 or self.terminal:
|
| 145 |
+
score += sum(tenth)
|
| 146 |
+
return score
|
| 147 |
+
|
| 148 |
+
def state(self) -> np.ndarray:
|
| 149 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 150 |
+
state[self.bowler_row, 0, 0] = True
|
| 151 |
+
state[0, :, 1] = True
|
| 152 |
+
state[9, :, 1] = True
|
| 153 |
+
for index in self.standing:
|
| 154 |
+
state[*PIN_POSITIONS[index], 2] = True
|
| 155 |
+
if self.ball is not None:
|
| 156 |
+
state[*self.ball, 3] = True
|
| 157 |
+
if self.previous_ball is not None:
|
| 158 |
+
state[*self.previous_ball, 4] = True
|
| 159 |
+
state[9, : self.frame + 1, 5] = True
|
| 160 |
+
state[8, : min(10, len(self.frames[self.frame]) + 1), 5] = True
|
| 161 |
+
score_bucket = min(9, self.confirmed_score // 30)
|
| 162 |
+
state[7, : score_bucket + 1, 5] = True
|
| 163 |
+
state[6, self.hook + 1, 5] = True
|
| 164 |
+
return state
|
runtime/minatar_synth/games/boxing.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
DELTAS = {1: (0, -1), 2: (-1, 0), 3: (0, 1), 4: (1, 0)}
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class BoxingEnvironment:
|
| 8 |
+
"""Two-minute top-down bout with guarded jabs, power punches, and counter windows."""
|
| 9 |
+
|
| 10 |
+
score_cap = 100.0
|
| 11 |
+
|
| 12 |
+
def __init__(self, sticky_action_prob: float = 0.1, round_steps: int = 120):
|
| 13 |
+
self.sticky_action_prob = sticky_action_prob
|
| 14 |
+
self.round_steps = round_steps
|
| 15 |
+
self.random = np.random.RandomState()
|
| 16 |
+
self.seed(0)
|
| 17 |
+
self.reset()
|
| 18 |
+
|
| 19 |
+
@classmethod
|
| 20 |
+
def normalize_score(cls, raw_score: float) -> float:
|
| 21 |
+
return min(max(raw_score, 0.0), cls.score_cap) / cls.score_cap
|
| 22 |
+
|
| 23 |
+
def seed(self, seed: int) -> None:
|
| 24 |
+
self.random.seed(seed)
|
| 25 |
+
|
| 26 |
+
def reset(self) -> None:
|
| 27 |
+
self.player = [5, 2]
|
| 28 |
+
self.opponent = [5, 7]
|
| 29 |
+
self.previous_player = self.player.copy()
|
| 30 |
+
self.previous_opponent = self.opponent.copy()
|
| 31 |
+
self.player_score = 0
|
| 32 |
+
self.opponent_score = 0
|
| 33 |
+
self.player_recovery = 0
|
| 34 |
+
self.opponent_recovery = 0
|
| 35 |
+
self.player_punch: list[int] | None = None
|
| 36 |
+
self.opponent_punch: list[int] | None = None
|
| 37 |
+
self.steps = 0
|
| 38 |
+
self.last_action = 0
|
| 39 |
+
self.terminal = False
|
| 40 |
+
|
| 41 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 42 |
+
if self.terminal:
|
| 43 |
+
return 0.0, True
|
| 44 |
+
if self.random.rand() < self.sticky_action_prob:
|
| 45 |
+
action = self.last_action
|
| 46 |
+
self.last_action = action
|
| 47 |
+
self.previous_player = self.player.copy()
|
| 48 |
+
self.previous_opponent = self.opponent.copy()
|
| 49 |
+
self.player_punch = None
|
| 50 |
+
self.opponent_punch = None
|
| 51 |
+
reward = 0.0
|
| 52 |
+
if action in DELTAS:
|
| 53 |
+
self._move(self.player, self.opponent, action)
|
| 54 |
+
elif action == 5 and self.player_recovery == 0:
|
| 55 |
+
scored = self._punch(self.player, self.opponent, defender_is_player=False)
|
| 56 |
+
reward = float(min(scored, 100 - self.player_score))
|
| 57 |
+
self.player_score += int(reward)
|
| 58 |
+
self.player_recovery = 2
|
| 59 |
+
if self.player_score >= 100:
|
| 60 |
+
self.player_score = 100
|
| 61 |
+
self.terminal = True
|
| 62 |
+
return reward, True
|
| 63 |
+
|
| 64 |
+
if self.steps < 60 or self.steps % 2 == 0:
|
| 65 |
+
self._opponent_action()
|
| 66 |
+
self.player_recovery = max(0, self.player_recovery - 1)
|
| 67 |
+
self.opponent_recovery = max(0, self.opponent_recovery - 1)
|
| 68 |
+
self.steps += 1
|
| 69 |
+
if self.opponent_score >= 100 or self.steps >= self.round_steps:
|
| 70 |
+
self.opponent_score = min(100, self.opponent_score)
|
| 71 |
+
self.terminal = True
|
| 72 |
+
return reward, self.terminal
|
| 73 |
+
|
| 74 |
+
@staticmethod
|
| 75 |
+
def _move(boxer: list[int], other: list[int], action: int) -> None:
|
| 76 |
+
row_delta, column_delta = DELTAS[action]
|
| 77 |
+
target = [boxer[0] + row_delta, boxer[1] + column_delta]
|
| 78 |
+
if 1 <= target[0] <= 8 and 1 <= target[1] <= 8 and target != other:
|
| 79 |
+
boxer[:] = target
|
| 80 |
+
|
| 81 |
+
def _punch(self, attacker: list[int], defender: list[int], defender_is_player: bool) -> int:
|
| 82 |
+
row_distance = abs(attacker[0] - defender[0])
|
| 83 |
+
column_distance = abs(attacker[1] - defender[1])
|
| 84 |
+
distance = max(row_distance, column_distance)
|
| 85 |
+
if distance > 2:
|
| 86 |
+
return 0
|
| 87 |
+
recovery = self.player_recovery if defender_is_player else self.opponent_recovery
|
| 88 |
+
angled = row_distance > 0 and column_distance > 0
|
| 89 |
+
if not angled and recovery == 0:
|
| 90 |
+
return 0
|
| 91 |
+
points = 2 if distance == 1 else 1
|
| 92 |
+
punch_cell = [
|
| 93 |
+
attacker[0] + int(np.sign(defender[0] - attacker[0])),
|
| 94 |
+
attacker[1] + int(np.sign(defender[1] - attacker[1])),
|
| 95 |
+
]
|
| 96 |
+
if defender_is_player:
|
| 97 |
+
self.opponent_punch = punch_cell
|
| 98 |
+
else:
|
| 99 |
+
self.player_punch = punch_cell
|
| 100 |
+
self._push(attacker, defender)
|
| 101 |
+
return points
|
| 102 |
+
|
| 103 |
+
@staticmethod
|
| 104 |
+
def _push(attacker: list[int], defender: list[int]) -> None:
|
| 105 |
+
row_delta = int(np.sign(defender[0] - attacker[0]))
|
| 106 |
+
column_delta = int(np.sign(defender[1] - attacker[1]))
|
| 107 |
+
target = [
|
| 108 |
+
int(np.clip(defender[0] + row_delta, 1, 8)),
|
| 109 |
+
int(np.clip(defender[1] + column_delta, 1, 8)),
|
| 110 |
+
]
|
| 111 |
+
if target != attacker:
|
| 112 |
+
defender[:] = target
|
| 113 |
+
|
| 114 |
+
def _opponent_action(self) -> None:
|
| 115 |
+
row_distance = abs(self.opponent[0] - self.player[0])
|
| 116 |
+
column_distance = abs(self.opponent[1] - self.player[1])
|
| 117 |
+
if max(row_distance, column_distance) <= 2 and self.opponent_recovery == 0:
|
| 118 |
+
self.opponent_score += self._punch(self.opponent, self.player, defender_is_player=True)
|
| 119 |
+
self.opponent_recovery = 2
|
| 120 |
+
return
|
| 121 |
+
candidates = []
|
| 122 |
+
for action in DELTAS:
|
| 123 |
+
row_delta, column_delta = DELTAS[action]
|
| 124 |
+
target = [self.opponent[0] + row_delta, self.opponent[1] + column_delta]
|
| 125 |
+
if not (1 <= target[0] <= 8 and 1 <= target[1] <= 8) or target == self.player:
|
| 126 |
+
continue
|
| 127 |
+
distance = max(abs(target[0] - self.player[0]), abs(target[1] - self.player[1]))
|
| 128 |
+
angled_bonus = -1 if target[0] != self.player[0] and target[1] != self.player[1] else 0
|
| 129 |
+
candidates.append((distance + angled_bonus, self.random.rand(), action))
|
| 130 |
+
if candidates:
|
| 131 |
+
self._move(self.opponent, self.player, min(candidates)[2])
|
| 132 |
+
|
| 133 |
+
def state(self) -> np.ndarray:
|
| 134 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 135 |
+
state[*self.player, 0] = True
|
| 136 |
+
state[*self.opponent, 1] = True
|
| 137 |
+
state[*self.opponent, 2] = True
|
| 138 |
+
if self.player_punch is not None:
|
| 139 |
+
state[*self.player_punch, 3] = True
|
| 140 |
+
state[*self.previous_player, 4] = True
|
| 141 |
+
state[*self.previous_opponent, 4] = True
|
| 142 |
+
state[9, : min(10, (self.player_score + 9) // 10), 5] = True
|
| 143 |
+
state[8, : min(10, (self.opponent_score + 9) // 10), 5] = True
|
| 144 |
+
remaining = max(0, self.round_steps - self.steps)
|
| 145 |
+
state[7, : min(10, (remaining + 11) // 12), 5] = True
|
| 146 |
+
state[6, : self.player_recovery, 5] = True
|
| 147 |
+
return state
|
runtime/minatar_synth/games/breed_c.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
SIZE = 9
|
| 5 |
+
CENTER = SIZE // 2
|
| 6 |
+
EMPTY = -1
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
GROWTH_INTERVAL = 30
|
| 9 |
+
SETTLE_DELAY = 15
|
| 10 |
+
DIRECTIONS = ((0, -1), (-1, 0), (1, 0), (0, 1))
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class BreedCEnvironment:
|
| 14 |
+
score_cap = 250
|
| 15 |
+
|
| 16 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 17 |
+
self.sticky_action_prob = sticky_action_prob
|
| 18 |
+
self.horizon = min(horizon, HORIZON)
|
| 19 |
+
self.seed(0)
|
| 20 |
+
self.reset()
|
| 21 |
+
|
| 22 |
+
def seed(self, seed: int) -> None:
|
| 23 |
+
self.random = np.random.default_rng(seed)
|
| 24 |
+
|
| 25 |
+
def reset(self) -> None:
|
| 26 |
+
self.steps = self.last_action = self.growth_count = self.settle = self.clears = 0
|
| 27 |
+
self.multiplier = 1
|
| 28 |
+
self.timer = GROWTH_INTERVAL
|
| 29 |
+
self.terminal = False
|
| 30 |
+
self.cursor = CENTER, CENTER
|
| 31 |
+
self.grid = np.full((SIZE, SIZE), EMPTY, dtype=np.int8)
|
| 32 |
+
self.grid[CENTER, CENTER] = self.random.integers(4)
|
| 33 |
+
self._grow()
|
| 34 |
+
|
| 35 |
+
@classmethod
|
| 36 |
+
def normalize_score(cls, score: float) -> float:
|
| 37 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 38 |
+
|
| 39 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 40 |
+
if self.terminal:
|
| 41 |
+
return 0.0, True
|
| 42 |
+
if self.random.random() < self.sticky_action_prob:
|
| 43 |
+
action = self.last_action
|
| 44 |
+
select = action == 5 and self.last_action != 5
|
| 45 |
+
self.last_action = action
|
| 46 |
+
self.steps += 1
|
| 47 |
+
reward = 0
|
| 48 |
+
if 1 <= action <= 4:
|
| 49 |
+
dy, dx = DIRECTIONS[action - 1]
|
| 50 |
+
y, x = self.cursor
|
| 51 |
+
self.cursor = min(SIZE - 1, max(0, y + dy)), min(SIZE - 1, max(0, x + dx))
|
| 52 |
+
if self.settle:
|
| 53 |
+
self.settle -= 1
|
| 54 |
+
if not self.settle:
|
| 55 |
+
self._compact()
|
| 56 |
+
self.clears += 1
|
| 57 |
+
self.timer = GROWTH_INTERVAL
|
| 58 |
+
else:
|
| 59 |
+
if self.grid[CENTER, CENTER] == EMPTY:
|
| 60 |
+
self.grid[CENTER, CENTER] = self.random.integers(4)
|
| 61 |
+
if select:
|
| 62 |
+
reward = self._erase()
|
| 63 |
+
if not self.settle:
|
| 64 |
+
self.timer -= 1
|
| 65 |
+
if self.timer <= 0:
|
| 66 |
+
self._grow()
|
| 67 |
+
self.terminal = self.steps >= self.horizon or bool(np.all(self.grid != EMPTY))
|
| 68 |
+
return float(reward), self.terminal
|
| 69 |
+
|
| 70 |
+
def _erase(self) -> int:
|
| 71 |
+
color = self.grid[self.cursor]
|
| 72 |
+
group = set()
|
| 73 |
+
if color != EMPTY:
|
| 74 |
+
group.add(self.cursor)
|
| 75 |
+
pending = [self.cursor]
|
| 76 |
+
while pending:
|
| 77 |
+
y, x = pending.pop()
|
| 78 |
+
for dy, dx in DIRECTIONS:
|
| 79 |
+
ny, nx = y + dy, x + dx
|
| 80 |
+
neighbor = ny, nx
|
| 81 |
+
if 0 <= ny < SIZE and 0 <= nx < SIZE and neighbor not in group and self.grid[neighbor] == color:
|
| 82 |
+
group.add(neighbor)
|
| 83 |
+
pending.append(neighbor)
|
| 84 |
+
size = len(group)
|
| 85 |
+
if size < 4:
|
| 86 |
+
self._grow()
|
| 87 |
+
self._grow()
|
| 88 |
+
return -size
|
| 89 |
+
for cell in group:
|
| 90 |
+
self.grid[cell] = EMPTY
|
| 91 |
+
reward = size * self.multiplier
|
| 92 |
+
self.multiplier += 1
|
| 93 |
+
self.settle = SETTLE_DELAY
|
| 94 |
+
return reward
|
| 95 |
+
|
| 96 |
+
def _grow(self) -> None:
|
| 97 |
+
lines = self.grid if self.growth_count % 2 == 0 else self.grid.T
|
| 98 |
+
old = lines.copy()
|
| 99 |
+
lines[:, :CENTER - 1] = old[:, 1:CENTER]
|
| 100 |
+
lines[:, CENTER + 2:] = old[:, CENTER + 1:-1]
|
| 101 |
+
for line in lines:
|
| 102 |
+
color = int(line[CENTER])
|
| 103 |
+
if color != EMPTY:
|
| 104 |
+
direction = int(self.random.choice((-1, 1)))
|
| 105 |
+
new_color = (color + int(self.random.integers(3))) % 4
|
| 106 |
+
line[CENTER - direction] = color
|
| 107 |
+
line[CENTER] = line[CENTER + direction] = new_color
|
| 108 |
+
self.growth_count += 1
|
| 109 |
+
self.multiplier = 1
|
| 110 |
+
self.timer = GROWTH_INTERVAL
|
| 111 |
+
|
| 112 |
+
def _compact(self) -> None:
|
| 113 |
+
axes = (self.grid, self.grid.T)
|
| 114 |
+
if self.clears % 2:
|
| 115 |
+
axes = axes[::-1]
|
| 116 |
+
moved = True
|
| 117 |
+
while moved:
|
| 118 |
+
moved = False
|
| 119 |
+
for lines in axes:
|
| 120 |
+
for line in lines:
|
| 121 |
+
for start, stop, step in ((CENTER, 0, -1), (CENTER, SIZE - 1, 1)):
|
| 122 |
+
for index in range(start, stop, step):
|
| 123 |
+
source = index + step
|
| 124 |
+
if line[index] == EMPTY and line[source] != EMPTY:
|
| 125 |
+
line[index], line[source] = line[source], EMPTY
|
| 126 |
+
moved = True
|
| 127 |
+
|
| 128 |
+
def state(self) -> np.ndarray:
|
| 129 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 130 |
+
for color in range(4):
|
| 131 |
+
state[:SIZE, :SIZE, color] = self.grid == color
|
| 132 |
+
state[*self.cursor, 4] = True
|
| 133 |
+
for row, value in enumerate((self.steps, self.timer, self.settle, self.multiplier)):
|
| 134 |
+
for bit in range(9):
|
| 135 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 136 |
+
state[9, 0, 5] = self.last_action == 5
|
| 137 |
+
state[9, 1, 5] = self.growth_count % 2 == 1
|
| 138 |
+
return state
|
runtime/minatar_synth/games/bridge_cross.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
FRAMES_PER_STEP = 4
|
| 9 |
+
BRIGADE_LEFT = 10
|
| 10 |
+
BRIGADE_RIGHT = 40
|
| 11 |
+
BRIDGE_TOP = 32
|
| 12 |
+
GROUND = 37
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@dataclass
|
| 16 |
+
class Bridge:
|
| 17 |
+
x: float
|
| 18 |
+
length: float
|
| 19 |
+
|
| 20 |
+
def overlaps_brigade(self) -> bool:
|
| 21 |
+
return self.x <= BRIGADE_RIGHT and self.x + self.length >= BRIGADE_LEFT
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class BridgeCrossEnvironment:
|
| 25 |
+
score_cap = 500
|
| 26 |
+
|
| 27 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 28 |
+
self.sticky_action_prob = sticky_action_prob
|
| 29 |
+
self.horizon = min(horizon, HORIZON)
|
| 30 |
+
self.seed(0)
|
| 31 |
+
self.reset()
|
| 32 |
+
|
| 33 |
+
def seed(self, seed: int) -> None:
|
| 34 |
+
self.random = np.random.default_rng(seed)
|
| 35 |
+
|
| 36 |
+
def reset(self) -> None:
|
| 37 |
+
self.steps = self.score_timer = 0
|
| 38 |
+
self.spawn_timer = 0.0
|
| 39 |
+
self.multiplier = 1
|
| 40 |
+
self.y, self.vy = float(GROUND), 0.0
|
| 41 |
+
self.pressed = self.jumping = self.falling = self.on_bridge = self.terminal = False
|
| 42 |
+
self.bridges: list[Bridge] = []
|
| 43 |
+
|
| 44 |
+
@classmethod
|
| 45 |
+
def normalize_score(cls, score: float) -> float:
|
| 46 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 47 |
+
|
| 48 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 49 |
+
if self.terminal:
|
| 50 |
+
return 0.0, True
|
| 51 |
+
pressed = self.pressed if self.random.random() < self.sticky_action_prob else action == 5
|
| 52 |
+
tap = pressed and not self.pressed
|
| 53 |
+
self.pressed = pressed
|
| 54 |
+
self.steps += 1
|
| 55 |
+
reward = 0
|
| 56 |
+
for frame in range(FRAMES_PER_STEP):
|
| 57 |
+
reward += self._advance_frame(tap and frame == 0)
|
| 58 |
+
if self.terminal:
|
| 59 |
+
break
|
| 60 |
+
self.terminal |= self.steps >= self.horizon
|
| 61 |
+
return float(reward), self.terminal
|
| 62 |
+
|
| 63 |
+
def _advance_frame(self, tap: bool) -> int:
|
| 64 |
+
if self.spawn_timer <= 0:
|
| 65 |
+
self.bridges.append(Bridge(153, self.random.uniform(20, 40)))
|
| 66 |
+
self.spawn_timer = self.random.uniform(50, 100)
|
| 67 |
+
self.spawn_timer -= 1
|
| 68 |
+
for bridge in self.bridges:
|
| 69 |
+
bridge.x -= 1
|
| 70 |
+
self.bridges = [bridge for bridge in self.bridges if bridge.x + bridge.length > 0]
|
| 71 |
+
above_bridge = any(bridge.overlaps_brigade() for bridge in self.bridges)
|
| 72 |
+
|
| 73 |
+
if self.y < 35 and not above_bridge:
|
| 74 |
+
self.falling, self.on_bridge = True, False
|
| 75 |
+
if self.jumping or self.falling:
|
| 76 |
+
self.y += self.vy
|
| 77 |
+
self.vy += 0.1 if self.pressed else 0.2
|
| 78 |
+
surface = BRIDGE_TOP if above_bridge else GROUND
|
| 79 |
+
if self.y > surface and self.vy > 0:
|
| 80 |
+
self.y, self.vy = float(surface), 0.0
|
| 81 |
+
self.jumping = self.falling = False
|
| 82 |
+
self.on_bridge = above_bridge
|
| 83 |
+
|
| 84 |
+
if tap and not self.jumping:
|
| 85 |
+
self.jumping, self.on_bridge = True, False
|
| 86 |
+
self.vy = -2
|
| 87 |
+
self.multiplier = math.ceil(self.multiplier / 2)
|
| 88 |
+
if not self.jumping and self.y > BRIDGE_TOP and above_bridge:
|
| 89 |
+
self.terminal = True
|
| 90 |
+
|
| 91 |
+
if self.on_bridge:
|
| 92 |
+
self.score_timer -= 1
|
| 93 |
+
if self.score_timer < 0:
|
| 94 |
+
reward = self.multiplier
|
| 95 |
+
self.multiplier += 1
|
| 96 |
+
self.score_timer = 10
|
| 97 |
+
return reward
|
| 98 |
+
return 0
|
| 99 |
+
|
| 100 |
+
def state(self) -> np.ndarray:
|
| 101 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 102 |
+
row = int(self.y) // 5
|
| 103 |
+
state[row, :3, 0] = True
|
| 104 |
+
state[row, :3, 3] = self.on_bridge
|
| 105 |
+
state[8:, :, 2] = True
|
| 106 |
+
for bridge in self.bridges:
|
| 107 |
+
for column in range(10):
|
| 108 |
+
state[7, column, 1] |= column * 15 < bridge.x + bridge.length and (column + 1) * 15 > bridge.x
|
| 109 |
+
state[0, :3, 4] = self.pressed, self.jumping, self.vy < 0
|
| 110 |
+
for row, value in enumerate((self.steps, self.multiplier, self.score_timer)):
|
| 111 |
+
for bit in range(9):
|
| 112 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 113 |
+
return state
|
runtime/minatar_synth/games/bs_fish.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
HORIZON = 400
|
| 7 |
+
SPAWN_INTERVAL = 8
|
| 8 |
+
MAX_FISH = (HORIZON // SPAWN_INTERVAL) * 13
|
| 9 |
+
BIRD_ROW = 2
|
| 10 |
+
NORMAL, BIG, FAKE, EYE = range(4)
|
| 11 |
+
FLIGHT = (8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
| 12 |
+
FRAGMENTS = ((0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3),
|
| 13 |
+
(1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 5))
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@dataclass
|
| 17 |
+
class Fish:
|
| 18 |
+
column: int
|
| 19 |
+
kind: int = NORMAL
|
| 20 |
+
age: int = 0
|
| 21 |
+
row_offset: int = 0
|
| 22 |
+
|
| 23 |
+
@property
|
| 24 |
+
def row(self) -> int:
|
| 25 |
+
return max(0, min(9, FLIGHT[self.age] + self.row_offset))
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class BSFishEnvironment:
|
| 29 |
+
score_cap = MAX_FISH * (1 + 5 * (MAX_FISH - 1))
|
| 30 |
+
|
| 31 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 32 |
+
self.sticky_action_prob = sticky_action_prob
|
| 33 |
+
self.horizon = min(horizon, HORIZON)
|
| 34 |
+
self.seed(0)
|
| 35 |
+
self.reset()
|
| 36 |
+
|
| 37 |
+
def seed(self, seed: int) -> None:
|
| 38 |
+
self.random = np.random.default_rng(seed)
|
| 39 |
+
|
| 40 |
+
def reset(self) -> None:
|
| 41 |
+
self.steps = self.last_action = self.speed = 0
|
| 42 |
+
self.multiplier = 1
|
| 43 |
+
self.special_in = 3
|
| 44 |
+
self.fish: list[Fish] = []
|
| 45 |
+
self.terminal = False
|
| 46 |
+
|
| 47 |
+
@property
|
| 48 |
+
def bird_column(self) -> int:
|
| 49 |
+
return 1 + self.speed // 2
|
| 50 |
+
|
| 51 |
+
@classmethod
|
| 52 |
+
def normalize_score(cls, score: float) -> float:
|
| 53 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 54 |
+
|
| 55 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 56 |
+
if self.terminal:
|
| 57 |
+
return 0.0, True
|
| 58 |
+
if self.random.random() < self.sticky_action_prob:
|
| 59 |
+
action = self.last_action
|
| 60 |
+
self.last_action = action
|
| 61 |
+
self.steps += 1
|
| 62 |
+
self.speed = min(6, self.speed + 1) if action == 5 else max(0, self.speed - 1)
|
| 63 |
+
if self.steps % SPAWN_INTERVAL == 1:
|
| 64 |
+
if self.special_in == 0:
|
| 65 |
+
kind = int(self.random.choice((BIG, FAKE)))
|
| 66 |
+
self.special_in = int(self.random.integers(3, 9))
|
| 67 |
+
else:
|
| 68 |
+
kind = NORMAL
|
| 69 |
+
self.special_in -= 1
|
| 70 |
+
self.fish.append(Fish(int(self.random.integers(7, 10)), kind))
|
| 71 |
+
reward = 0
|
| 72 |
+
remaining = []
|
| 73 |
+
for fish in self.fish:
|
| 74 |
+
fish.age += 1
|
| 75 |
+
if self.speed >= 4 or self.steps % 3 == 0:
|
| 76 |
+
fish.column -= 1
|
| 77 |
+
if fish.age == len(FLIGHT) or fish.column < -1:
|
| 78 |
+
if fish.kind != BIG:
|
| 79 |
+
self.multiplier = max(1, self.multiplier - 1)
|
| 80 |
+
continue
|
| 81 |
+
if fish.kind == FAKE and fish.row <= 5:
|
| 82 |
+
remaining.extend(Fish(fish.column + column - 2, EYE if (row, column) == (1, 2) else NORMAL,
|
| 83 |
+
fish.age, row - 1) for row, column in FRAGMENTS)
|
| 84 |
+
elif fish.kind == BIG:
|
| 85 |
+
if abs(fish.row - BIRD_ROW) <= 1 and abs(fish.column - self.bird_column) <= 1:
|
| 86 |
+
self.terminal = True
|
| 87 |
+
remaining.append(fish)
|
| 88 |
+
elif fish.row == BIRD_ROW and fish.column == self.bird_column:
|
| 89 |
+
reward += self.multiplier
|
| 90 |
+
self.multiplier += 10 if fish.kind == EYE else 1
|
| 91 |
+
else:
|
| 92 |
+
remaining.append(fish)
|
| 93 |
+
self.fish = remaining
|
| 94 |
+
self.terminal |= self.steps >= self.horizon
|
| 95 |
+
return float(reward), self.terminal
|
| 96 |
+
|
| 97 |
+
def state(self) -> np.ndarray:
|
| 98 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 99 |
+
state[BIRD_ROW, self.bird_column, 0] = True
|
| 100 |
+
for fish in self.fish:
|
| 101 |
+
if 0 <= fish.column < 10:
|
| 102 |
+
channel = (2, 1, 1, 3)[fish.kind]
|
| 103 |
+
state[fish.row, fish.column, channel] = True
|
| 104 |
+
next_row = FLIGHT[min(fish.age + 1, len(FLIGHT) - 1)] + fish.row_offset
|
| 105 |
+
if 0 <= next_row < 10:
|
| 106 |
+
state[next_row, fish.column, 4] = True
|
| 107 |
+
for row, value in enumerate((self.steps, self.speed, self.multiplier, self.special_in, self.last_action)):
|
| 108 |
+
for bit in range(13):
|
| 109 |
+
index = row * 13 + bit
|
| 110 |
+
state[index // 10, index % 10, 5] = bool(value & (1 << bit))
|
| 111 |
+
return state
|
runtime/minatar_synth/games/c_nodes.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
HORIZON = 400
|
| 8 |
+
FRAMES_PER_STEP = 4
|
| 9 |
+
DIRECTIONS = ((0, -1), (-1, 0), (1, 0), (0, 1))
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@dataclass(eq=False)
|
| 13 |
+
class Node:
|
| 14 |
+
x: float
|
| 15 |
+
y: float
|
| 16 |
+
vx: float = 0
|
| 17 |
+
vy: float = 0
|
| 18 |
+
anchored: bool = True
|
| 19 |
+
tethered: bool = False
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@dataclass
|
| 23 |
+
class Link:
|
| 24 |
+
a: Node
|
| 25 |
+
b: Node
|
| 26 |
+
length: float
|
| 27 |
+
|
| 28 |
+
def near(self, x: float, y: float) -> bool:
|
| 29 |
+
dx, dy = self.b.x - self.a.x, self.b.y - self.a.y
|
| 30 |
+
px, py = x - self.a.x, y - self.a.y
|
| 31 |
+
squared = dx * dx + dy * dy
|
| 32 |
+
fraction = min(1, max(0, (px * dx + py * dy) / squared)) if squared else 0
|
| 33 |
+
return math.hypot(px - fraction * dx, py - fraction * dy) <= 5
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class CNodesEnvironment:
|
| 37 |
+
score_cap = 500
|
| 38 |
+
|
| 39 |
+
def __init__(self, sticky_action_prob: float = 0.1, horizon: int = HORIZON):
|
| 40 |
+
self.sticky_action_prob = sticky_action_prob
|
| 41 |
+
self.horizon = min(horizon, HORIZON)
|
| 42 |
+
self.seed(0)
|
| 43 |
+
self.reset()
|
| 44 |
+
|
| 45 |
+
def seed(self, seed: int) -> None:
|
| 46 |
+
self.random = np.random.default_rng(seed)
|
| 47 |
+
|
| 48 |
+
def reset(self) -> None:
|
| 49 |
+
self.steps = self.last_action = 0
|
| 50 |
+
self.multiplier = 1
|
| 51 |
+
self.next_node = self.boost = 0.0
|
| 52 |
+
self.cursor = (3, 5)
|
| 53 |
+
self.terminal = False
|
| 54 |
+
self.nodes: list[Node] = []
|
| 55 |
+
self.links: list[Link] = []
|
| 56 |
+
|
| 57 |
+
@classmethod
|
| 58 |
+
def normalize_score(cls, score: float) -> float:
|
| 59 |
+
return min(max(score, 0), cls.score_cap) / cls.score_cap
|
| 60 |
+
|
| 61 |
+
def act(self, action: int) -> tuple[float, bool]:
|
| 62 |
+
if self.terminal:
|
| 63 |
+
return 0.0, True
|
| 64 |
+
if self.random.random() < self.sticky_action_prob:
|
| 65 |
+
action = self.last_action
|
| 66 |
+
if 1 <= action <= 4:
|
| 67 |
+
dy, dx = DIRECTIONS[action - 1]
|
| 68 |
+
row, col = self.cursor
|
| 69 |
+
self.cursor = min(9, max(2, row + dy)), min(9, max(0, col + dx))
|
| 70 |
+
if action == 5 and self.last_action != 5:
|
| 71 |
+
row, col = self.cursor
|
| 72 |
+
self.links = [link for link in self.links if not link.near(col * 10 + 5, row * 10 + 5)]
|
| 73 |
+
self.multiplier = 1
|
| 74 |
+
self.boost = 1
|
| 75 |
+
self.last_action = action
|
| 76 |
+
self.steps += 1
|
| 77 |
+
reward = 0
|
| 78 |
+
for _ in range(FRAMES_PER_STEP):
|
| 79 |
+
reward += self._advance_frame()
|
| 80 |
+
if self.terminal:
|
| 81 |
+
break
|
| 82 |
+
self.terminal |= self.steps >= self.horizon
|
| 83 |
+
return float(reward), self.terminal
|
| 84 |
+
|
| 85 |
+
def _advance_frame(self) -> int:
|
| 86 |
+
lowest = max((node.y for node in self.nodes), default=0)
|
| 87 |
+
scroll = 0.03 + self.boost + max(0, 29 - lowest) * 0.1
|
| 88 |
+
self.boost *= 0.7
|
| 89 |
+
self.next_node -= scroll
|
| 90 |
+
if self.next_node < 0:
|
| 91 |
+
new = Node(self.random.uniform(10, 90), 0)
|
| 92 |
+
for node in self.nodes:
|
| 93 |
+
if node.anchored and abs(new.x - node.x) / 2 + abs(node.y) < 30 and self.random.random() < 0.7:
|
| 94 |
+
self.links.append(Link(new, node, math.hypot(new.x - node.x, node.y)))
|
| 95 |
+
self.nodes.append(new)
|
| 96 |
+
self.next_node = 4
|
| 97 |
+
for link in self.links:
|
| 98 |
+
dx, dy = link.b.x - link.a.x, link.b.y - link.a.y
|
| 99 |
+
distance = math.hypot(dx, dy)
|
| 100 |
+
if distance:
|
| 101 |
+
force = (distance - link.length) * 0.02 / distance
|
| 102 |
+
link.a.vx += force * dx
|
| 103 |
+
link.a.vy += force * dy
|
| 104 |
+
link.b.vx -= force * dx
|
| 105 |
+
link.b.vy -= force * dy
|
| 106 |
+
for node in self.nodes:
|
| 107 |
+
node.y += scroll
|
| 108 |
+
node.anchored &= node.y <= 20
|
| 109 |
+
if node.anchored:
|
| 110 |
+
node.vx = node.vy = 0
|
| 111 |
+
else:
|
| 112 |
+
node.x += node.vx
|
| 113 |
+
node.y += node.vy
|
| 114 |
+
node.vx *= 0.95
|
| 115 |
+
node.vy = (node.vy + 0.03) * 0.95
|
| 116 |
+
self.links = [link for link in self.links if min(link.a.y, link.b.y) <= 100]
|
| 117 |
+
self._update_tethers()
|
| 118 |
+
reward = 0
|
| 119 |
+
survivors = []
|
| 120 |
+
for node in self.nodes:
|
| 121 |
+
if node.tethered and node.y > 100:
|
| 122 |
+
self.terminal = True
|
| 123 |
+
if node.y > 140:
|
| 124 |
+
reward += self.multiplier
|
| 125 |
+
self.multiplier = min(64, self.multiplier * 2)
|
| 126 |
+
else:
|
| 127 |
+
survivors.append(node)
|
| 128 |
+
self.nodes = survivors
|
| 129 |
+
alive = set(survivors)
|
| 130 |
+
self.links = [link for link in self.links if link.a in alive and link.b in alive]
|
| 131 |
+
return reward
|
| 132 |
+
|
| 133 |
+
def _update_tethers(self) -> None:
|
| 134 |
+
connected = {node: [] for node in self.nodes}
|
| 135 |
+
for link in self.links:
|
| 136 |
+
connected[link.a].append(link.b)
|
| 137 |
+
connected[link.b].append(link.a)
|
| 138 |
+
pending = [node for node in self.nodes if node.anchored]
|
| 139 |
+
reached = set(pending)
|
| 140 |
+
while pending:
|
| 141 |
+
for neighbor in connected[pending.pop()]:
|
| 142 |
+
if neighbor not in reached:
|
| 143 |
+
reached.add(neighbor)
|
| 144 |
+
pending.append(neighbor)
|
| 145 |
+
for node in self.nodes:
|
| 146 |
+
node.tethered = node in reached and not node.anchored
|
| 147 |
+
|
| 148 |
+
def state(self) -> np.ndarray:
|
| 149 |
+
state = np.zeros((10, 10, 6), dtype=bool)
|
| 150 |
+
for node in self.nodes:
|
| 151 |
+
if 0 <= node.x < 100 and 0 <= node.y < 100:
|
| 152 |
+
channel = 0 if node.anchored else 1 if node.tethered else 2
|
| 153 |
+
state[int(node.y) // 10, int(node.x) // 10, channel] = True
|
| 154 |
+
for link in self.links:
|
| 155 |
+
for fraction in np.linspace(0, 1, 12):
|
| 156 |
+
x = link.a.x + (link.b.x - link.a.x) * fraction
|
| 157 |
+
y = link.a.y + (link.b.y - link.a.y) * fraction
|
| 158 |
+
if 0 <= x < 100 and 0 <= y < 100:
|
| 159 |
+
state[int(y) // 10, int(x) // 10, 3] = True
|
| 160 |
+
row, col = self.cursor
|
| 161 |
+
state[row, col, 4] = True
|
| 162 |
+
for row, value in enumerate((self.steps, self.multiplier, self.last_action)):
|
| 163 |
+
for bit in range(9):
|
| 164 |
+
state[row, bit, 5] = bool(value & (1 << bit))
|
| 165 |
+
return state
|