"""ST-GFN and baseline GFlowNet models. Reimplemented from the paper text (Sec. 3, Def. 5, Alg. 1, App. C) since no author code accompanies the submission. IMPORTANT INTERPRETATION NOTE (documented in the logbook): The paper's Unified Spectral Loss (Definition 5) contains only ||P_F_hat(s,t) - P_B_hat(s,t)||^2 + lambda * E_a[ ||V_hat(s,a)||_H^2 ] i.e. it has NO reward/terminal term. Taken literally that objective is reward-independent and cannot train a GFlowNet to sample proportional to R (its global minimum is P_F == P_B with zero spectral energy). Definition 2 does carry the reward terms (r_term, r_int), so we read Definition 5 as the *regularizer pair* that rides on top of a standard reward-matching GFlowNet objective. We therefore implement L_STGFN = L_TB(with intrinsic AC reward) + w_c * L_spectral_consistency + lambda * L_spectral_reg with lambda adaptive (lambda = exp(theta_lambda)) per Eq. 14-15. All baselines share the identical backbone/optimizer so the comparison isolates the spectral machinery. """ from __future__ import annotations import math import numpy as np import torch import torch.nn as nn import torch.nn.functional as F class RFF(nn.Module): """Random Fourier Feature map z(s) = sqrt(2/D)[cos(w_i^T s + b_i)]_{i