File size: 5,497 Bytes
6fbb45f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""Generator configuration.



One frozen dataclass holds every knob: which levels of the experimental design to generate, how

many replicates per cell, the physical model constants and the potential-field parameters.

``scripts/run_local_sweep.py`` turns each field into a command-line flag, and the resolved

configuration is written next to the data as ``data/config.json`` so that any episode can be

re-created bit for bit from ``(config, episode_id)``.

"""
from __future__ import annotations

import json
from dataclasses import asdict, dataclass, field, fields
from pathlib import Path
from typing import List

from . import design

DATASET_VERSION = "2.0"
ROUTERS = ("potential", "potential_split", "potential_static",
           "shortest_path", "ecmp", "adaptive_shortest_path")


def _f(default, help_text):
    return field(default=default, metadata={"help": help_text})


@dataclass(frozen=True)
class SimConfig:
    # Experimental design (see src/design.py for the level definitions)
    replicates: int = _f(10, "episodes per design cell; episodes = replicates x cells")
    topologies: tuple = _f(design.TOPOLOGIES, "topology families to include")
    sizes: tuple = _f(design.SIZES, "nominal network sizes to include")
    traffic_profiles: tuple = _f(tuple(design.TRAFFIC_PROFILES), "traffic profiles to include")
    load_levels: tuple = _f(tuple(design.LOAD_LEVELS), "load levels to include")
    dynamics_levels: tuple = _f(tuple(design.DYNAMICS_LEVELS), "topology-dynamics levels to include")
    routers: tuple = _f(ROUTERS, "routers replayed on identical traffic and events")

    # Sweep
    seed: int = _f(2026, "base seed; episode e draws from numpy default_rng([seed, e])")
    shard_episodes: int = _f(40, "episodes per Parquet shard (one shard = one resumable unit)")
    field_budget_bytes: int = _f(1_000_000, "potential field of the tracked flows is logged every k steps to stay within this budget")

    # Physical model
    steps: int = _f(1000, "simulated steps per episode (1 step = 1 ms)")
    buffer_size: int = _f(256, "per-node drop-tail buffer in packets (< 32768)")
    mean_degree: int = _f(6, "target mean degree of the random-graph families")
    capacity_range: tuple = _f((8, 80), "link capacity in packets/step for random graphs (1 pkt/step ~ 12 Mbps)")
    latency_range: tuple = _f((1, 10), "link propagation delay in steps for random graphs")
    fat_tree_capacity: int = _f(40, "fat-tree fabric link capacity, packets/step (host links carry twice this)")
    flows_per_endpoint: int = _f(2, "flows per endpoint node; sources and sinks are distinct ordered pairs")
    tracked_flows: int = _f(8, "flows per episode with step-level telemetry and stored potential field")

    # Potential field  L_g phi = b  with  b = source + background/(N-1) + gain * queue/buffer
    source_injection: float = _f(1.0, "unit injection at a flow's source")
    background_injection: float = _f(0.5, "total background injection spread over all non-sink nodes")
    congestion_gain: float = _f(2.0, "repulsive injection of a full buffer (alpha)")

    def __post_init__(self):
        for f in fields(self):  # JSON round-trips turn tuples into lists
            if isinstance(f.default, tuple):
                object.__setattr__(self, f.name, tuple(getattr(self, f.name)))
        for name, allowed in (("topologies", design.TOPOLOGIES), ("sizes", design.SIZES),
                              ("traffic_profiles", design.TRAFFIC_PROFILES), ("load_levels", design.LOAD_LEVELS),
                              ("dynamics_levels", design.DYNAMICS_LEVELS), ("routers", ROUTERS)):
            chosen = getattr(self, name)
            if not chosen or set(chosen) - set(allowed):
                raise ValueError(f"{name} must be a non-empty subset of {tuple(allowed)}, got {chosen}")
        if self.replicates < 1:
            raise ValueError("replicates must be at least 1")
        if not 0 < self.buffer_size < 32768:
            raise ValueError("buffer_size must fit in an int16 telemetry column (< 32768)")
        if self.flows_per_endpoint < 1 or self.tracked_flows < 1:
            raise ValueError("flows_per_endpoint and tracked_flows must be positive")
        if self.mean_degree < 2:
            raise ValueError("mean_degree must be at least 2")
        for name in ("capacity_range", "latency_range"):
            lo, hi = getattr(self, name)
            if not 0 < lo <= hi:
                raise ValueError(f"{name} must satisfy 0 < low <= high, got {(lo, hi)}")
        if self.background_injection <= 0:
            raise ValueError("background_injection must be positive: it guarantees loop-free descent")

    @property
    def cells(self) -> List[design.Cell]:
        return design.cells(self.topologies, self.sizes, self.traffic_profiles, self.load_levels, self.dynamics_levels)

    @property
    def episodes(self) -> int:
        return self.replicates * len(self.cells)

    def to_json(self) -> str:
        return json.dumps(asdict(self), indent=2)

    def save(self, path: Path) -> None:
        path.write_text(self.to_json(), encoding="utf-8")

    @classmethod
    def load(cls, path: Path) -> "SimConfig":
        return cls(**json.loads(path.read_text(encoding="utf-8")))

    def __eq__(self, other) -> bool:  # tuples vs. JSON lists compare equal
        return isinstance(other, SimConfig) and self.to_json() == other.to_json()