# meshwho/powergridreconfig architecture

> Architecture diagram of the GitHub repository meshwho/powergridreconfig, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/meshwho/powergridreconfig
- Repository: https://github.com/meshwho/powergridreconfig

## Overview

#### Purpose and entry points
PowerGridReconfig is a Python 3.11 research framework for emergency power-grid topology control, centered on case118 simulations. Its integrated workflow is pool-guided, AlphaZero-like self-play: sample fixed scenarios, generate neural-MCTS trajectories, train a graph policy/value model, evaluate on a fixed set, and accept or reject the candidate. Stable CLIs are `scripts/self_play/loop.py`, `scripts/self_play/train_graph_baseline.py`, and `scripts/evaluation/evaluate_checkpoint.py`.

#### Configuration, planning, and run orchestration
Typed YAML configuration defines pool, generation, replay, training, evaluation, and acceptance contracts. The loop validates bootstrap artifacts, creates an iteration plan, supports fail-closed resume, and writes atomic completion markers. Core paths: `grid_topology_ai/config/self_play.py`, `grid_topology_ai/self_play/plan.py`, `grid_topology_ai/self_play/pipeline.py`, `grid_topology_ai/self_play/iteration.py`.

#### Scenario/runtime and physical-state boundary
Raw GridFM-style scenario data is adapted into NumPy/PYPOWER-ready state and stored by scenario ID. State construction produces versioned graph features and validates topology, identifiers, finite values, and physical limits. Environment reset executes no-op AC power flow before classification. Core paths: `grid_topology_ai/data_adapter.py`, `grid_topology_ai/runtime/scenario_store.py`, `grid_topology_ai/state_builder.py`, `grid_topology_ai/environment.py`.

#### Power-flow simulation and physical objective
PYPOWER is the authoritative AC power-flow backend, with reusable network/Newton workspaces and warm starts. Physical success is evaluated separately from episode termination: convergence, connectivity, thermal/voltage/generator/angle constraints must all pass. Redispatch is represented as a handoff boundary, not a production optimizer. Core paths: `grid_topology_ai/pypower_backend.py`, `grid_topology_ai/power_flow_problem.py`, `grid_topology_ai/physical_objective.py`, `grid_topology_ai/termination.py`.

#### Topology action and search control
The policy uses one stable action slot per branch plus stop/handoff. A branch action opens active branches or closes explicitly allowed inactive branches; legality is state-dependent and masked. MCTS uses neural priors/values, progressive widening, DC screening, and optional diagnostic continuation gating; the gate never rewrites executed actions or policy targets. Alternative beam planners support teacher/debug workflows. Core paths: `grid_topology_ai/topology_actions.py`, `grid_topology_ai/action_space.py`, `grid_topology_ai/search/mcts.py`, `grid_topology_ai/search/dc_action_screener.py`.

#### Neural training and replay
Generation records graph-state examples with MCTS visit distributions and required outcome value targets. Replay accumulates across iterations, then scenario-level—not row-level—train/validation splitting feeds checkpoint fine-tuning. Graph policy/value networks and normalization metadata are checkpoint contracts; fine-tuning retains parent normalization. Core paths: `grid_topology_ai/self_play/generation.py`, `grid_topology_ai/self_play/replay.py`, `grid_topology_ai/models/graph_policy_value_net_v2.py`, `grid_topology_ai/training/graph_policy_value.py`.

#### Evaluation, acceptance, and artifact integrity
Candidates are evaluated against fixed scenarios in primary `ungated` mode; constrained continuation results are diagnostic only. Metrics drive checkpoint selection and acceptance, with paired checks and fail-closed validation. State, replay, checkpoint, physics, and topology-action provenance prevent mixing incompatible artifacts. Caches reduce repeated exact/DC power-flow and structural-topology work. Core paths: `grid_topology_ai/evaluation/checkpoint.py`, `grid_topology_ai/self_play/acceptance.py`, `grid_topology_ai/state_artifact_schema.py`, `grid_topology_ai/cache/exact_power_flow.py`.

Main entry points: [`grid_topology_ai/search/mcts.py`](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/search/mcts.py) (Neural MCTS), [`grid_topology_ai/self_play/pipeline.py`](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/self_play/pipeline.py) (Pipeline runner), [`grid_topology_ai/environment.py`](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/environment.py) (Grid environment), [`grid_topology_ai/training/graph_policy_value.py`](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/training/graph_policy_value.py) (Checkpoint fine-tuning)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_orchestration["Self-Play Orchestration"]
  node_loop_cli["Self-play CLI<br/>Python CLI<br/>[loop.py]"]
  node_config["Typed configuration<br/>YAML contract<br/>[self_play.py]"]
  node_plan["Iteration plan<br/>planner<br/>[plan.py]"]
  node_pipeline["Pipeline runner<br/>orchestrator<br/>[pipeline.py]"]
end

subgraph group_runtime["Grid Runtime"]
  node_scenario_store[("Scenario store<br/>scenario runtime<br/>[scenario_store.py]")]
  node_data_adapter["Scenario adapter<br/>data adapter<br/>[data_adapter.py]"]
  node_state_builder["State builder<br/>graph state<br/>[state_builder.py]"]
  node_environment["Grid environment<br/>simulation environment<br/>[environment.py]"]
  node_power_flow{{"AC power flow<br/>PYPOWER backend<br/>[pypower_backend.py]"}}
  node_physical_objective["Physical objective<br/>constraint evaluator"]
end

subgraph group_search["Search &amp; Actions"]
  node_action_space["Topology action space<br/>masked action contract<br/>[action_space.py]"]
  node_mcts["Neural MCTS<br/>search planner<br/>[mcts.py]"]
  node_dc_screener["DC action screener<br/>search filter"]
end

subgraph group_learning["Learning"]
  node_generation["Trajectory generation<br/>self-play generator<br/>[generation.py]"]
  node_replay[("Replay store<br/>training dataset<br/>[replay.py]")]
  node_graph_model["Graph policy/value model<br/>neural network"]
  node_training["Checkpoint fine-tuning<br/>trainer"]
end

subgraph group_evaluation["Evaluation &amp; Integrity"]
  node_checkpoint_eval["Checkpoint evaluation<br/>fixed-scenario evaluator<br/>[checkpoint.py]"]
  node_acceptance["Candidate acceptance<br/>promotion gate<br/>[acceptance.py]"]
  node_artifact_schema["Artifact provenance<br/>compatibility schema"]
  node_pf_cache[("Power-flow cache<br/>simulation cache")]
end

node_loop_cli -->|"loads"| node_config
node_loop_cli -->|"creates"| node_plan
node_plan -->|"drives iteration"| node_pipeline
node_config -->|"configures"| node_pipeline
node_data_adapter -->|"adapts and stores"| node_scenario_store
node_scenario_store -->|"supplies scenarios"| node_environment
node_scenario_store -->|"supplies raw state"| node_state_builder
node_state_builder -->|"provides graph state"| node_environment
node_environment -->|"runs AC flow"| node_power_flow
node_power_flow -->|"provides solution"| node_physical_objective
node_physical_objective -->|"reports physical success"| node_environment
node_action_space -->|"applies legal topology actions"| node_environment
node_state_builder -->|"determines legality"| node_action_space
node_mcts -->|"selects masked actions"| node_action_space
node_mcts -->|"screens expansions"| node_dc_screener
node_mcts -->|"queries priors and values"| node_graph_model
node_mcts -->|"simulates trajectories"| node_environment
node_pipeline -->|"runs"| node_generation
node_generation -->|"uses"| node_mcts
node_generation -->|"writes examples"| node_replay
node_pipeline -->|"runs"| node_training
node_replay -->|"provides scenario split"| node_training
node_training -->|"fine-tunes"| node_graph_model
node_pipeline -->|"evaluates candidate"| node_checkpoint_eval
node_checkpoint_eval -->|"provides paired metrics"| node_acceptance
node_acceptance -->|"promotes or rejects"| node_pipeline
node_artifact_schema -.->|"validates provenance"| node_replay
node_artifact_schema -.->|"guards checkpoint compatibility"| node_training
node_pf_cache -.->|"reuses results"| node_power_flow
node_pf_cache -.->|"avoids repeated screening"| node_mcts

click node_loop_cli "https://github.com/meshwho/powergridreconfig/blob/main/scripts/self_play/loop.py"
click node_config "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/config/self_play.py"
click node_plan "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/self_play/plan.py"
click node_pipeline "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/self_play/pipeline.py"
click node_scenario_store "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/runtime/scenario_store.py"
click node_data_adapter "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/data_adapter.py"
click node_state_builder "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/state_builder.py"
click node_environment "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/environment.py"
click node_power_flow "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/pypower_backend.py"
click node_physical_objective "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/physical_objective.py"
click node_action_space "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/action_space.py"
click node_mcts "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/search/mcts.py"
click node_dc_screener "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/search/dc_action_screener.py"
click node_generation "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/self_play/generation.py"
click node_replay "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/self_play/replay.py"
click node_graph_model "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/models/graph_policy_value_net_v2.py"
click node_training "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/training/graph_policy_value.py"
click node_checkpoint_eval "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/evaluation/checkpoint.py"
click node_acceptance "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/self_play/acceptance.py"
click node_artifact_schema "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/state_artifact_schema.py"
click node_pf_cache "https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/cache/exact_power_flow.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_loop_cli,node_config,node_plan,node_pipeline toneBlue
class node_scenario_store,node_data_adapter,node_state_builder,node_environment,node_power_flow,node_physical_objective toneAmber
class node_action_space,node_mcts,node_dc_screener toneMint
class node_generation,node_replay,node_graph_model,node_training toneRose
class node_checkpoint_eval,node_acceptance,node_artifact_schema,node_pf_cache toneIndigo
```

## Components

### Self-Play Orchestration

- [Self-play CLI](https://github.com/meshwho/powergridreconfig/blob/main/scripts/self_play/loop.py): Python CLI · Runs the iterative generate-train-evaluate loop. · `scripts/self_play/loop.py`
- [Typed configuration](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/config/self_play.py): YAML contract · Defines pool, generation, replay, training, evaluation, and acceptance settings. · `grid_topology_ai/config/self_play.py`
- [Iteration plan](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/self_play/plan.py): planner · Creates resumable, fail-closed iteration work plans. · `grid_topology_ai/self_play/plan.py`
- [Pipeline runner](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/self_play/pipeline.py): orchestrator · Coordinates generation, training, evaluation, and completion markers. · `grid_topology_ai/self_play/pipeline.py`

### Grid Runtime

- [Scenario store](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/runtime/scenario_store.py): scenario runtime · Stores adapted fixed scenarios by scenario ID. · `grid_topology_ai/runtime/scenario_store.py`
- [Scenario adapter](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/data_adapter.py): data adapter · Converts GridFM-style inputs to NumPy/PYPOWER-ready data. · `grid_topology_ai/data_adapter.py`
- [State builder](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/state_builder.py): graph state · Builds versioned graph features and validates physical state inputs. · `grid_topology_ai/state_builder.py`
- [Grid environment](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/environment.py): simulation environment · Resets with no-op AC flow and executes topology episodes. · `grid_topology_ai/environment.py`
- [AC power flow](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/pypower_backend.py): PYPOWER backend · Authoritative warm-started AC solver with reusable workspaces. · `grid_topology_ai/pypower_backend.py`
- [Physical objective](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/physical_objective.py): constraint evaluator · Separates physical success from episode termination. · `grid_topology_ai/physical_objective.py`

### Search & Actions

- [Topology action space](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/action_space.py): masked action contract · Maps stable branch slots plus stop/handoff to legal actions. · `grid_topology_ai/action_space.py`
- [Neural MCTS](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/search/mcts.py): search planner · Uses policy/value priors, progressive widening, and DC screening. · `grid_topology_ai/search/mcts.py`
- [DC action screener](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/search/dc_action_screener.py): search filter · Screens candidate actions before expensive search expansion. · `grid_topology_ai/search/dc_action_screener.py`

### Learning

- [Trajectory generation](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/self_play/generation.py): self-play generator · Records graph states, MCTS visits, and outcome targets. · `grid_topology_ai/self_play/generation.py`
- [Replay store](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/self_play/replay.py): training dataset · Accumulates examples and splits train/validation by scenario. · `grid_topology_ai/self_play/replay.py`
- [Graph policy/value model](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/models/graph_policy_value_net_v2.py): neural network · Produces action priors and state values under a checkpoint contract. · `grid_topology_ai/models/graph_policy_value_net_v2.py`
- [Checkpoint fine-tuning](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/training/graph_policy_value.py): trainer · Trains candidates while retaining parent normalization metadata. · `grid_topology_ai/training/graph_policy_value.py`

### Evaluation & Integrity

- [Checkpoint evaluation](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/evaluation/checkpoint.py): fixed-scenario evaluator · Measures candidates primarily in ungated mode. · `grid_topology_ai/evaluation/checkpoint.py`
- [Candidate acceptance](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/self_play/acceptance.py): promotion gate · Uses paired metrics and fail-closed validation to promote checkpoints. · `grid_topology_ai/self_play/acceptance.py`
- [Artifact provenance](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/state_artifact_schema.py): compatibility schema · Prevents incompatible state, replay, checkpoint, physics, and action artifacts from mixing. · `grid_topology_ai/state_artifact_schema.py`
- [Power-flow cache](https://github.com/meshwho/powergridreconfig/blob/main/grid_topology_ai/cache/exact_power_flow.py): simulation cache · Caches exact/DC power-flow and structural topology work. · `grid_topology_ai/cache/exact_power_flow.py`

## Connections

- Self-play CLI → Typed configuration: loads
- Self-play CLI → Iteration plan: creates
- Iteration plan → Pipeline runner: drives iteration
- Typed configuration → Pipeline runner: configures
- Scenario adapter → Scenario store: adapts and stores
- Scenario store → Grid environment: supplies scenarios
- Scenario store → State builder: supplies raw state
- State builder → Grid environment: provides graph state
- Grid environment → AC power flow: runs AC flow
- AC power flow → Physical objective: provides solution
- Physical objective → Grid environment: reports physical success
- Topology action space → Grid environment: applies legal topology actions
- State builder → Topology action space: determines legality
- Neural MCTS → Topology action space: selects masked actions
- Neural MCTS → DC action screener: screens expansions
- Neural MCTS → Graph policy/value model: queries priors and values
- Neural MCTS → Grid environment: simulates trajectories
- Pipeline runner → Trajectory generation: runs
- Trajectory generation → Neural MCTS: uses
- Trajectory generation → Replay store: writes examples
- Pipeline runner → Checkpoint fine-tuning: runs
- Replay store → Checkpoint fine-tuning: provides scenario split
- Checkpoint fine-tuning → Graph policy/value model: fine-tunes
- Pipeline runner → Checkpoint evaluation: evaluates candidate
- Checkpoint evaluation → Candidate acceptance: provides paired metrics
- Candidate acceptance → Pipeline runner: promotes or rejects
- Artifact provenance → Replay store: validates provenance
- Artifact provenance → Checkpoint fine-tuning: guards checkpoint compatibility
- Power-flow cache → AC power flow: reuses results
- Power-flow cache → Neural MCTS: avoids repeated screening
