I stumbled upon a nice discovery in July - that Sanskrit was long since describing much of what I was trying to build from scratch. And so why re-invent the dharmachakra?
| Golden | Complex |
| ------------------------ | --------------------------------- |
| (x=1+\frac1x) | (x=1-\frac1x) |
| (x^2-x-1=0) | (x^2-x+1=0) |
| (\Delta=5) | (\Delta=-3) |
| Hyperbolic / real growth | Rotational / unit-circle symmetry |
This is a mathematically meaningful duality. In the context of your HDGL exploration, it suggests a natural pairing between the “growth” substrate (the golden ratio) and the “phase” substrate (the sixth roots of unity), with the sign change in the reciprocal term separating the two behaviors.
import time
import numpy as np
class CyberneticEngine:
def __init__(self, epsilon: float = 1e-5):
# 1. The Algebraic Engine (The Core Resonators)
self.Omega = (1.0 + 5.0 ** 0.5) / 2.0 # Golden Ratio φ
self.psi = -1.0 / self.Omega # Conjugate root ψ
self.epsilon = epsilon # Perturbation scaling factor
self.one_eff = 1.0 # Initialized 1_eff (δ -> 0)
def lcg_tsc_entropy(self, seed: int) -> float:
"""Delta <- GetTSC ⊕ LCG"""
tsc = int(time.perf_counter() * 1e9) & 0xFFFFFFFF
# Minimal LCG implementation (Numerical Recipes constants)
lcg_state = (1103515245 * seed + 12345) & 0xFFFFFFFF
mixed_entropy = (tsc ^ lcg_state) / 0xFFFFFFFF
return float(mixed_entropy)
def system_pull(self, omega_val: float, n: int = 1) -> float:
"""C: pull -> (√Ω, ψ★) gain Ω⁻ⁿ"""
pull = (np.sqrt(abs(omega_val)) + self.psi)
gain = self.Omega ** (-n)
return float(pull * gain)
def forward_transmit(self, current_state: float, entropy_delta: float) -> float:
"""Ωₙ₊₁ = 1 + 1/Ωₙ + ε·Δ + C(Ω)"""
c_omega = self.system_pull(current_state)
next_state = 1.0 + (1.0 / current_state) + (self.epsilon * entropy_delta) + c_omega
return float(next_state)
def lossless_reconstruct(self, current_state: float, next_state: float) -> float:
"""reconstruct: Δ ≡ (Ω′ - 1 - 1/Ω - C(Ω)) / ε"""
c_omega = self.system_pull(current_state)
recovered_delta = (next_state - 1.0 - (1.0 / current_state) - c_omega) / self.epsilon
return float(recovered_delta)
def oracle_collapse(self, p: float) -> float:
"""ORACLE := |e^(iπΛ_φ(p)) + 1_eff|"""
# Log-periodic scaling function Λ_φ(x)
ln_omega = np.log(self.Omega)
ln2 = np.log(2.0)
lambda_phi = (np.log(p * ln2 / ln_omega) / ln_omega) - (1.0 / (2.0 * self.Omega))
# Complex wave phase matching
wave = np.exp(1j * np.pi * lambda_phi)
collapse_value = np.abs(wave + self.one_eff)
return float(collapse_value)
# Execution Sandbox
if __name__ == "__main__":
engine = CyberneticEngine()
initial_seed = 42
# Run a transmission step
delta_in = engine.lcg_tsc_entropy(initial_seed)
state_0 = engine.Omega
state_1 = engine.forward_transmit(state_0, delta_in)
# Lossless reconstruction check
delta_out = engine.lossless_reconstruct(state_0, state_1)
numerical_residue = abs(delta_in - delta_out)
print(f"[-] Shared State Law Executed.")
print(f"[-] Entropy Injected (Δ): {delta_in:.8f}")
print(f"[-] Entropy Recovered(Δ): {delta_out:.8f}")
print(f"[-] Numerical Residue : {numerical_residue:.16e}")
print(f"[-] Lock Status : {'wu-wei achieved' if numerical_residue < 1e-12 else 'unstable'}")
inverse(forward(x)) == x
rather than a physical conservation law.
This is perfectly fine if your goal is to prove invertibility of your transform.
It is not evidence that entropy has been physically preserved—it shows that you’ve written an invertible encoding.
3. The entropy source
tsc ^ lcg_state
is reasonable as a lightweight entropy mixer.
Its quality depends almost entirely on
time.perf_counter()
because the LCG is completely deterministic.
If you want stronger unpredictability, Python’s standard library provides secrets.randbits() or os.urandom(), though for experiments a deterministic LCG is often useful for reproducibility.
Oracle collapse
The oracle computes
That means the expensive complex exponential can be replaced by a cosine:
def oracle_collapse(self, p):
ln_phi = np.log(self.Omega)
Lambda = (
np.log(p * np.log(2.0) / ln_phi)
/ ln_phi
- 1.0 / (2.0 * self.Omega)
)
return 2.0 * abs(np.cos(np.pi * Lambda / 2.0))
This produces the same result to floating-point precision while avoiding complex arithmetic.
Overall assessment
As a numerical experiment, I’d describe the script this way:
- Strengths
- Clean separation of the forward map, inverse map, entropy source, and oracle.
- The forward and inverse maps are algebraically consistent.
- The oracle is mathematically well-defined and periodic on a logarithmic scale.
- Limitations
- The “lossless reconstruction” is guaranteed by construction because it inverts the forward equation.
- The entropy source is not cryptographically secure.
- The function
system_pull()is currently heuristic rather than derived from an underlying mathematical model, so any interpretation of its behavior is speculative unless you define the theory that motivates it.
# शून्यत्वम्
shunyatvam = lambda p, Ω, Ω_next, C, V_φ, V_E, δ=0, base=10: (abs(np.exp((Ω**(-base**0) - Ω) * ((np.log(p * np.log(base) / np.log(Ω)) / np.log(Ω)) - (base**0 / (base * Ω)))) + (base**0 + δ)) * (V_φ & V_E)) + (Ω_next - ((Ω + base**0) / Ω) - C) == (base**0 - base**0)
![]()
That is no longer oscillatory.
It is purely exponential decay.
This is a surprisingly elegant simplification.
3. The bitwise operator
This is probably the largest practical issue.
(V_φ & V_E)
is a bitwise AND.
Unless
V_φ
and
V_E
are integer masks,
this is almost certainly not what you intend.
If these are continuous fields you probably meant one of
V_φ * V_E
or
min(V_φ, V_E)
or
V_φ @ V_E
depending on the algebra.
4. Equality inside a lambda
The lambda returns
expression == 0
which evaluates to
True
or
False
because
(base**0 - base**0)
is simply
0
for every non-zero base.
Usually numerical solvers return the residual
return expression
and let the caller check
abs(residual) < tolerance
rather than testing exact equality.
That means the only appearance of ϕ inside the exponent is through the logarithmic scaling function Λ, not through the coefficient.
# बीजसूत्रम्
def निष्पत्ति(p, Ω, Ω_next, C, V_φ, V_E, δ=0, base=10):
"""
शून्यत्वम्: The Expanded Parameterless One-Liner Unified Engine.
Parameters:
p : अङ्कः (The candidate prime number to process through the Oracle collapse)
Ω : निश्चलबिन्दुः (The golden state value / core field attractor)
Ω_next : अग्रिमस्थितिः (The telemetry trajectory state observed at n+1)
C : आकर्षणम् (The systematic pull value derived from the structural gain matrix)
V_φ : सौवर्णलक्षणम् (Boolean logic value representing golden vantage alignment)
V_E : ऐशन्स्टाईनलक्षणम् (Boolean logic value representing Eisenstein vantage alignment)
δ : विचलनम् (The structural residue value drifting toward zero state)
base : आधारः (The numerical coordinate framework base scale parameter)
Returns:
bool : कैवल्यम् (True if the entire topological cybernetic engine collapses cleanly to zero)
"""
return (abs(np.exp((Ω**(-(base**(base**0-base**0))) - Ω) * ((np.log(p * np.log(base) / np.log(Ω)) / np.log(Ω)) - ((base**(base**0-base**0)) / (base * Ω)))) + ((base**(base**0-base**0)) + δ)) * (V_φ & V_E)) + (Ω_next - ((Ω + (base**(base**0-base**0))) / Ω) - C) == (base**0 - base**0)
The remaining issue
The one part that still stands out is
(V_φ & V_E)
This is a bitwise AND, not a logical conjunction over arbitrary numeric values.
If V_φ and V_E are Boolean flags (True/False), then & behaves similarly to logical AND, although and is the more idiomatic Python operator.
If they are intended to represent field amplitudes, projections, or weights, then & has no natural mathematical interpretation. In that case, a product (*), inner product, or another explicitly defined binary operator would better reflect the underlying algebra.
Interpretation
Ignoring implementation details, your function defines a single residual whose value is tested against zero. It combines three ingredients:
- A log-periodic oracle based on logϕ.
- A state-transition residual measuring deviation from your update law.
- A vantage gate requiring both Vϕ and VE to participate.
That structure is internally coherent as a numerical experiment. Whether it corresponds to a deeper mathematical invariant would require proving properties of the oracle term independently—for example, showing that it satisfies some conservation law or exhibits a demonstrable correlation with the phenomenon (such as prime distributions) that you’re investigating. At present, the code defines the quantity; it does not by itself establish such properties.
// शून्यत्वम् (The Deeply Expanded Parameterless Hierarchical One-Liner Engine)
// This architecture unrolls the core invariant equation into an unbroken pipeline.
// Every mathematical subsystem is decoupled to trace the analytical collapse.
const शून्यत्वम् = (
p, // अङ्कः (The candidate prime number tracking field wave transformation)
Ω, // निश्चलबिन्दुः (The core golden ratio field structural anchor)
Ω_next, // अग्रिमस्थितिः (The forward observable state parameter boundary)
V_φ, // सौवर्णलक्षणम् (The golden vantage orientation logic value)
V_E, // ऐशन्स्टाईनलक्षणम् (The Eisenstein lattice orientation logic value)
n, // कालः (The structural dimension level index pointer)
δ, // विचलनम् (The decaying numerical noise component tracking error)
base // आधारः (The scaling baseline coordinate framework system)
) => {
// १. शून्य-एक-मान-निश्चयः (Dynamic Zero and Identity Extraction Phase)
const शून्यम् = base ** 0 - base ** 0;
const एकम् = Math.pow(base, शून्यम्);
// २. तरङ्गसाम्य-स्थिरीकरणम् (Lightspeed Phase Constant Processing)
const ऋणात्मक_एकम् = -एकम्;
const घात_निश्चयः = Math.pow(Ω, ऋणात्मक_एकम्);
const प्रकाशवेगः = घात_निश्चयः - Ω;
// ३. लघुगणक-काल-नियमः (Log-Periodic Scaling Component Calculation)
const आधार_लघुगणकः = Math.log(base);
const क्षेत्र_लघुगणकः = Math.log(Ω);
const सङ्ख्या_गुणनम् = p * आधार_लघुगणकः;
const अनुपात_मूलम् = सङ्ख्या_गुणनम् / क्षेत्र_लघुगणकः;
const काल_अनुपातः = Math.log(अनुपात_मूलम्);
const प्रगाढता = काल_अनुपातः / क्षेत्र_लघुगणकः;
// ४. क्षेत्र-अंश-वियोजनम् (Field Density Invariant Resolution)
const अधोभाग_गुणनम् = base * Ω;
const अंश_अनुपातः = एकम् / अधोभाग_गुणनम्;
const लैम्ब्डा_मूल्यम् = प्रगाढता - अंश_अनुपातः;
// ५. कला-परिवर्तन-तरङ्गः (Complex Phase Wave Interaction Mapping)
const कला_कोणः = प्रकाशवेगः * लैम्ब्डा_मूल्यम्;
const घाताङ्क_तरङ्गः = Math.exp(कला_कोणः);
// ६. प्रभाव-स्थिति-संयोजनम् (Effective Unit Dynamic Adjustment)
const प्रभाव_एकम् = एकम् + δ;
const तरङ्ग_योगः = घाताङ्क_तरङ्गः + प्रभाव_एकम्;
const आकाशवाणी_मूल्यम् = Math.abs(तरङ्ग_योगः);
// ७. लक्षण-द्वय-सम्बन्धः (Dual-Vantage Logic Gate Conjunction)
const दृष्टि_सम्बन्धः = V_φ & V_E;
const तरङ्ग_लयः = आकाशवाणी_मूल्यम् * दृष्टि_सम्बन्धः;
// ८. अग्रिम-स्थिति-सन्तुलनम् (Trajectory Progress Analysis Segment)
const ऊर्ध्व_योगः = Ω + एकम्;
const मुख्य_अनुपातः = ऊर्ध्व_योगः / Ω;
const गति_भेदः = Ω_next - मुख्य_अनुपातः;
// ९. आकर्षण-प्रतिकर्ष-नियमः (Systemic Attractor Force Derivation)
const परम_मूल्यम् = Math.abs(Ω);
const वर्गमूल_मूल्यम् = Math.sqrt(परम_मूल्यम्);
const ऋणात्मक_अंशः = -एकम्;
const विपरीत_अनुपातः = ऋणात्मक_अंशः / Ω;
const आकर्षण_योगः = वर्गमूल_मूल्यम् + विपरीत_अनुपातः;
// १०. काल-क्षय-गुणनम् (Dimension Decay Coefficient Application)
const ऋणात्मक_कालः = -n;
const काल_क्षयः = Math.pow(Ω, ऋणात्मक_कालः);
const आकर्षण_बलम् = आकर्षण_योगः * काल_क्षयः;
// ११. अन्तिम-सन्तुलन-अन्वेषणम् (Final Invariant Verification Evaluation)
const सन्तुलन_मूल्यम् = गति_भेदः - आकर्षण_बलम्;
const एकीकृत_परिणामः = तरङ्ग_लयः + सन्तुलन_मूल्यम्;
// १२. शून्यत्व-प्राप्ति-परीक्षा (Clean State Collapse Validation Output)
const कैवल्य_सिद्धिः = एकीकृत_परिणाम === शून्यम्;
return कैवल्य_सिद्धिः;
};
This version is much easier to analyze than the previous one-liner because you’ve decomposed the computation into named stages. From a software perspective, the flow is coherent. From a mathematical perspective, several simplifications appear immediately.
1. The “dynamic” constants collapse
These definitions
const शून्यम् = base ** 0 - base ** 0;
const एकम् = Math.pow(base, शून्यम्);
always evaluate (for any nonzero base) to
शून्यम्=0,एकम्=1.
So they are symbolic rather than dynamic.
2. The golden-ratio coefficient disappears
Later you compute
const प्रकाशवेगः = Ω^(-1) - Ω;
3. The trajectory residual simplifies
You compute
const मुख्य_अनुपातः = (Ω + 1)/Ω;
Again,
const गति_भेदः = Ω_next - Ω;
whenever Ω is φ.
4. The attractor simplifies
You compute
That changes the character completely.
The earlier version was oscillatory:
- bounded
- periodic in log-space
- always on the unit circle
The current version is
- monotone
- exponentially decaying
- unbounded near small arguments
- not periodic.
So although the surrounding code resembles the earlier oracle, mathematically it is a different function.
6. Exact equality
This is probably the only significant engineering concern.
const कैवल्य_सिद्धिः =
एकीकृत_परिणामः === 0;
With floating-point arithmetic,
0.00000000000000022
is not uncommon.
Therefore
=== 0
will almost never be true except for specially constructed inputs.
A numerical residual is usually tested as
Math.abs(एकीकृत_परिणामः) < tolerance
for some tolerance such as 1e-12.
7. The bitwise gate
V_φ & V_E
is still a JavaScript bitwise operator.
If
V_φ = true
V_E = true
then JavaScript converts them to
1 & 1 = 1
which works.
If either value is numeric rather than Boolean, the semantics become bit-level integer operations after implicit conversion, which is unlikely to match a mathematical “vantage conjunction.” If the intent is purely logical, V_φ && V_E is clearer.
Overall assessment
I think this version is substantially better than the previous one-liner for readability and analysis. The staged structure makes the computational graph explicit and allows algebraic simplification at each step.
From a mathematical standpoint, however, much of the machinery becomes constant once you substitute Ω=ϕ:
- 1/ϕ−ϕ=−1
- (ϕ+1)/ϕ=ϕ
- 1/ϕ=ϕ−1
As a result, several sections that appear dynamic are actually evaluating fixed identities of the golden ratio. That’s not necessarily a flaw—it can be viewed as encoding those identities procedurally—but it does mean the implementation can be reduced considerably without changing its behavior when Ω is fixed to ϕ.
The remaining open question isn’t whether the implementation is internally consistent—it largely is—but whether the chosen oracle function and attractor have demonstrable mathematical properties (for example, provable invariants or measurable correlation with the phenomena you’re studying) beyond being well-defined computational constructions.
// शून्यत्वम् (The Full 100-Line Parametric Time-Series Trajectory Engine)
// This code generates a sequence of states over time to track numerical residue stability.
const शून्यत्वम् = (p, Ω, Ω_next, V_φ, V_E, n, δ, base) => {
const शून्यम् = base ** 0 - base ** 0;
const एकम् = Math.pow(base, शून्यम्);
const ऋणात्मक_एकम् = -एकम्;
const घात_निश्चयः = Math.pow(Ω, ऋणात्मक_एकम्);
const प्रकाशवेगः = घात_निश्चयः - Ω;
const आधार_लघुगणकः = Math.log(base);
const क्षेत्र_लघुगणकः = Math.log(Ω);
const सङ्ख्या_गुणनम् = p * आधार_लघुगणकः;
const अनुपात_मूलम् = सङ्ख्या_गुणनम् / क्षेत्र_लघुगणकः;
const काल_अनुपातः = Math.log(अनुपात_मूलम्);
const प्रगाढता = काल_अनुपातः / क्षेत्र_लघुगणकः;
const अधोभाग_गुणनम् = base * Ω;
const अंश_अनुपातः = एकम् / अधोभाग_गुणनम्;
const लैम्ब्डा_मूल्यम् = प्रगाढता - अंश_अनुपातः;
const कला_कोणः = प्रकाशवेगः * लैम्ब्डा_मूल्यम्;
const घाताङ्क_तरङ्गः = Math.exp(कला_कोणः);
const प्रभाव_एकम् = एकम् + δ;
const तरङ्ग_योगः = घाताङ्क_तरङ्गः + प्रभाव_एकम्;
const आकाशवाणी_मूल्यम् = Math.abs(तरङ्ग_योगः);
const दृष्टि_सम्बन्धः = V_φ & V_E;
const तरङ्ग_लयः = आकाशवाणी_मूल्यम् * दृष्टि_सम्बन्धः;
const ऊर्ध्व_योगः = Ω + एकम्;
const मुख्य_अनुपातः = ऊर्ध्व_योगः / Ω;
const गति_भेदः = Ω_next - मुख्य_अनुपातः;
const परम_मूल्यम् = Math.abs(Ω);
const वर्गमूल_मूल्यम् = Math.sqrt(परम_मूल्यम्);
const ऋणात्मक_अंशः = -एकम्;
const विपरीत_अनुपातः = ऋणात्मक_अंशः / Ω;
const आकर्षण_योगः = वर्गमूल_मूल्यम् + विपरीत_अनुपातः;
const ऋणात्मक_कालः = -n;
const काल_क्षयः = Math.pow(Ω, ऋणात्मक_कालः);
const आकर्षण_बलम् = आकर्षण_योगः * काल_क्षयः;
const सन्तुलन_मूल्यम् = गति_भेदः - आकर्षण_बलम्;
const एकीकृत_परिणामः = तरङ्ग_लयः + सन्तुलन_मूल्यम्;
return {
सिद्धः: Math.abs(एकीकृत_परिणामः - शून्यम्) < 1e-12,
अवशेषः: एकीकृत_परिणामः
};
};
const कालानुक्रम_श्रेणी = (समय_अवधि) => {
const आधारः = 10;
const सुवर्ण_अनुपातः = (1.0 + Math.sqrt(5.0)) / 2.0;
let वर्तमान_स्थितिः = सुवर्ण_अनुपातः;
let विचलनम् = 1e-5;
const प्रधान_सङ्ख्या = 7;
console.log("शून्यत्वम् कालानुक्रम श्रेणी आरम्भः\n" + "=".repeat(50));
for (let क्षणः = 0; क्षणः < समय_अवधि; क्षणः++) {
const यादृच्छिक_तत्वम् = Math.random() * 1e-6;
const अग्रिम_स्थितिः = (वर्तमान_स्थितिः + 1.0) / वर्तमान_स्थितिः + यादृच्छिक_तत्वम्;
const परिणामः = शून्यत्वम्(
प्रधान_सङ्ख्या,
वर्तमान_स्थितिः,
अग्रिम_स्थितिः,
1,
1,
क्षणः,
विचलनम्,
आधारः
);
console.log(
`क्षणः: ${क्षणः.toString().padStart(2, '0')} | ` +
`स्थितिः: ${वर्तमान_स्थितिः.toFixed(6)} | ` +
`अवशेषः: ${परिणामः.अवशेषः.toExponential(4)} | ` +
`सन्तुलनम्: ${परिणामः.सिद्धः ? "स्थिरम् (wu-wei)" : "विचलन्"}`
);
वर्तमान_स्थितिः = अग्रिम_स्थितिः;
विचलनम् *= 0.9;
}
};
// Execute a 10-step sequence analysis
कालानुक्रम_श्रेणी(10);
This version is closer to a genuine dynamical system than the earlier ones because you’re iterating the state over time rather than evaluating a single residual. That said, the dynamics are still largely determined by your update rule rather than emerging from the oracle itself.
Here’s what the system is actually doing.
State evolution
Each iteration computes
The oracle no longer drives the dynamics
Notice that
const अग्रिम_स्थितिः =
(वर्तमान_स्थितिः + 1) / वर्तमान_स्थितिः
+ यादृच्छिक_तत्वम्;
is computed before
शून्यत्वम्(…)
is called.
Therefore
- the oracle measures the trajectory,
- it does not influence the trajectory.
If your goal is a cybernetic feedback engine, you’d normally expect something like
So your printed residual is essentially
- oracle contribution
- minus attractor
- plus tiny random noise.
That means the residual is not expected to approach zero automatically.
One interesting observation
Earlier we found
ϕ−1−ϕ=−1.
At time zero,
Ω=ϕ,
so
प्रकाशवेगः
is exactly
−1.
But after the first iteration,
Ω does not equal ϕ
because of the injected noise.
Now
Ω^−1−Ω
is no longer exactly −1.
So unlike the previous static versions, this coefficient actually becomes dynamic after the first step.
That’s a nice consequence of making Ω evolve.
Stability
Because
the iteration is a contraction.
Therefore the state trajectory is stable even with small perturbations.
Engineering suggestion
If your stated objective is a cybernetic engine, I’d consider making the oracle part of the feedback law instead of just an observer. Conceptually,

where f is some bounded function of the oracle output and the current residual. That creates a closed-loop system whose stability, equilibria, and response to perturbations can be analyzed directly.
As written, your code implements a stable φ-attracting iteration together with an independent diagnostic residual. That’s a coherent numerical experiment, but the oracle isn’t yet participating in the state evolution; it’s reporting on it. If your goal is to study feedback or control, connecting the oracle back into the update equation would make the model substantially richer and open the door to analyzing its closed-loop behavior.
What you’ve built is closer to a cybernetic engine than a conventional algorithm, provided you define “cybernetic” in the sense introduced by Norbert Wiener: a system with state, feedback, sensing, and correction.
A classical cybernetic engine has these components:
+---------------------------+
| |
| OBSERVER |
| (oracle / residual) |
| |
+-------------▲-------------+
│
│
Residual / Error
│
│
+-----------+ +-----┴------+ +--------------+
| STATE |---->| DYNAMICS |---->| NEXT STATE |
| Ω(n) | | T(Ω) | | Ω(n+1) |
+-----------+ +------------+ +--------------+
▲ │
│ │
└──────────── Feedback ─────────────┘
is the error signal.
That is exactly what many cybernetic systems monitor.
What is missing?
One thing.
The residual never influences the future.
A true cybernetic loop is
measure
↓
compute error
↓
adjust controller
↓
new state
↓
measure again
Your current flow is
state
↓
compute next state
↓
measure
↓
print
There is no control action.
Closing the loop
Suppose instead
or whatever oracle you ultimately settle on.
Now the controller strength varies according to the oracle.
The resulting engine
Prime p
│
▼
Λφ(p) Oracle
│
▼
Collapse Measure
│
▼
Controller Gain
│
▼
Residual ──► Feedback Law ──► Ω(n+1)
▲ │
└──────── Measure ◄───────┘
This is recognizably a cybernetic architecture: it has a state, an observer, a controller, and a feedback path.
A note on terminology
From a control-theory perspective, your current implementation is best described as an observer with diagnostics rather than a complete cybernetic controller, because the observer does not yet influence the system. Once you feed the residual or oracle output back into the state update, it becomes a true closed-loop cybernetic engine whose stability and behavior can be analyzed using the tools of dynamical systems and control theory.
If your objective is a complete cybernetic controller, then the architecture needs four fundamental elements:
- Plant (system dynamics) – the state evolution.
- Observer (sensor) – measures the state and computes derived quantities.
- Controller – computes a corrective action from the observation.
- Feedback – applies that corrective action back into the plant.
A generic discrete-time formulation looks like this:
Disturbance η
│
▼
+---------------------+
| Plant |
| Ω(n+1)=f(Ω,u,η) |
+----------+----------+
│
▼
Measured Ω
│
▼
+---------------------+
| Observer |
| Oracle + Residual |
+----------+----------+
│
O(n), R(n)
│
▼
+---------------------+
| Controller |
| u = -k·O·R |
+----------+----------+
│
└──────────────► back to Plant
What this provides
With this architecture you can study questions such as:
- Does the state converge to a fixed point?
- Is the equilibrium stable under perturbations?
- How sensitive is the system to changes in the gain k?
- Does the oracle improve or degrade convergence compared with a baseline controller?
Those are questions that can be investigated mathematically and numerically.
Practical note
Calling this a “complete cybernetic controller” is appropriate as an architectural description because it contains the essential elements of a closed-loop control system: a plant, an observer, a controller, and feedback. Whether a particular oracle function is advantageous or has special mathematical properties remains a separate question that would need to be established through analysis or empirical testing.
cybernetic0.py
#!/usr/bin/env python3
"""
==========================================================================
GENERIC CLOSED-LOOP CYBERNETIC CONTROLLER
==========================================================================
Architecture
Disturbance
│
▼
+------------------+
| Plant |
+------------------+
│
▼
+------------------+
| Observer |
+------------------+
│
▼
+------------------+
| Controller |
+------------------+
│
└──────────────┐
│
▼
Control Input
==========================================================================
This demonstrates:
• Plant
• Observer
• Controller
• Feedback
• Disturbance
• Closed-loop simulation
"""
import math
import random
from dataclasses import dataclass
# ============================================================
# Controller Parameters
# ============================================================
@dataclass
class ControllerParameters:
kp: float = 0.35
ki: float = 0.02
kd: float = 0.10
reference: float = (1.0 + math.sqrt(5.0)) / 2.0
disturbance_amplitude: float = 1e-5
max_control: float = 1.0
# ============================================================
# Plant
# ============================================================
class Plant:
def __init__(self, initial_state):
self.state = initial_state
def update(self, control, disturbance):
x = self.state
# Example nonlinear dynamics
next_state = (
1.0
+ 1.0 / x
+ control
+ disturbance
)
self.state = next_state
return next_state
# ============================================================
# Observer
# ============================================================
class Observer:
def __init__(self):
self.last_measurement = None
def measure(self, state):
self.last_measurement = state
return state
def residual(self, reference):
return reference - self.last_measurement
def oracle(self, p, omega):
ln_phi = math.log((1.0 + math.sqrt(5.0)) / 2.0)
Lambda = (
math.log(
p * math.log(2.0) / ln_phi
)
/ ln_phi
-
1.0 / (2.0 * omega)
)
# bounded diagnostic quantity
return 2.0 * abs(
math.cos(math.pi * Lambda / 2.0)
)
# ============================================================
# PID Controller
# ============================================================
class PIDController:
def __init__(self, params):
self.params = params
self.integral = 0.0
self.previous_error = 0.0
def update(self, error, oracle_gain):
self.integral += error
derivative = error - self.previous_error
self.previous_error = error
u = (
self.params.kp * error
+
self.params.ki * self.integral
+
self.params.kd * derivative
)
# Oracle scales controller strength
u *= oracle_gain
limit = self.params.max_control
u = max(-limit, min(limit, u))
return u
# ============================================================
# Cybernetic Engine
# ============================================================
class CyberneticEngine:
def __init__(self, initial_state):
self.params = ControllerParameters()
self.plant = Plant(initial_state)
self.observer = Observer()
self.controller = PIDController(self.params)
self.time = 0
def step(self, prime):
disturbance = (
random.uniform(
-self.params.disturbance_amplitude,
self.params.disturbance_amplitude,
)
)
measured = self.observer.measure(
self.plant.state
)
error = self.observer.residual(
self.params.reference
)
oracle = self.observer.oracle(
prime,
measured,
)
control = self.controller.update(
error,
oracle,
)
next_state = self.plant.update(
control,
disturbance,
)
residue = abs(error)
self.time += 1
return {
"time": self.time,
"state": next_state,
"error": error,
"control": control,
"oracle": oracle,
"disturbance": disturbance,
"residue": residue,
}
# ============================================================
# Demonstration
# ============================================================
if __name__ == "__main__":
phi = (1.0 + math.sqrt(5.0)) / 2.0
engine = CyberneticEngine(
initial_state=phi
)
print("=" * 78)
print(" CLOSED-LOOP CYBERNETIC CONTROLLER")
print("=" * 78)
for _ in range(20):
result = engine.step(7)
print(
f"t={result['time']:02d} | "
f"Ω={result['state']:.12f} | "
f"error={result['error']:+.6e} | "
f"control={result['control']:+.6e} | "
f"oracle={result['oracle']:.6f} | "
f"residue={result['residue']:.6e}"
)
This code implements a standard closed-loop architecture:
- Plant: nonlinear state update.
- Observer: measures the current state and computes an error relative to a reference.
- Controller: a PID controller that computes a bounded control signal.
- Feedback: the controller output is fed back into the plant.
- Disturbance: small random perturbations test robustness.
- Oracle: a bounded diagnostic quantity that scales the controller gain. It functions as an additional modulation signal rather than being assumed to confer any special predictive properties.
This is a conventional cybernetic feedback system whose stability and tuning can be analyzed using established control theory.
YIELDS
py cybernetic.py
==============================================================================
CLOSED-LOOP CYBERNETIC CONTROLLER
==============================================================================
t=01 | Ω=1.618041113070 | error=+0.000000e+00 | control=+0.000000e+00 | oracle=1.429415 | residue=0.000000e+00
t=02 | Ω=1.618019445478 | error=-7.124320e-06 | control=-4.786286e-06 | oracle=1.429412 | residue=7.124320e-06
t=03 | Ω=1.618053747977 | error=+1.454327e-05 | control=+1.058527e-05 | oracle=1.429421 | residue=1.454327e-05
t=04 | Ω=1.618001920478 | error=-1.975923e-05 | control=-1.514140e-05 | oracle=1.429407 | residue=1.975923e-05
t=05 | Ω=1.618065488362 | error=+3.206827e-05 | control=+2.401612e-05 | oracle=1.429428 | residue=3.206827e-05
t=06 | Ω=1.617997219481 | error=-3.149961e-05 | control=-2.518189e-05 | oracle=1.429402 | residue=3.149961e-05
t=07 | Ω=1.618071664955 | error=+3.676927e-05 | control=+2.886890e-05 | oracle=1.429430 | residue=3.676927e-05
t=08 | Ω=1.617987509710 | error=-3.767621e-05 | control=-2.985270e-05 | oracle=1.429399 | residue=3.767621e-05
t=09 | Ω=1.618085183304 | error=+4.647904e-05 | control=+3.624931e-05 | oracle=1.429434 | residue=4.647904e-05
t=10 | Ω=1.617977900269 | error=-5.119455e-05 | control=-4.007066e-05 | oracle=1.429393 | residue=5.119455e-05
t=11 | Ω=1.618092462361 | error=+5.608848e-05 | control=+4.450294e-05 | oracle=1.429438 | residue=5.608848e-05
t=12 | Ω=1.617975186409 | error=-5.847361e-05 | control=-4.619441e-05 | oracle=1.429390 | residue=5.847361e-05
t=13 | Ω=1.618095535171 | error=+5.880234e-05 | control=+4.729855e-05 | oracle=1.429440 | residue=5.880234e-05
t=14 | Ω=1.617956209000 | error=-6.154642e-05 | control=-4.863724e-05 | oracle=1.429389 | residue=6.154642e-05
t=15 | Ω=1.618128032386 | error=+7.777975e-05 | control=+6.040940e-05 | oracle=1.429448 | residue=7.777975e-05
t=16 | Ω=1.617923224182 | error=-9.404364e-05 | control=-7.271712e-05 | oracle=1.429375 | residue=9.404364e-05
t=17 | Ω=1.618155837404 | error=+1.107646e-04 | control=+8.675111e-05 | oracle=1.429461 | residue=1.107646e-04
t=18 | Ω=1.617892737483 | error=-1.218487e-04 | control=-9.563269e-05 | oracle=1.429364 | residue=1.218487e-04
t=19 | Ω=1.618204803759 | error=+1.412513e-04 | control=+1.108922e-04 | oracle=1.429474 | residue=1.412513e-04
t=20 | Ω=1.617834721732 | error=-1.708150e-04 | control=-1.323294e-04 | oracle=1.429343 | residue=1.708150e-04
You’re no longer missing the architecture. You now have a complete closed-loop system:
Plant
Observer
Controller
Feedback
Disturbance
State memory
Reference state (φ)
What’s missing now is engineering, not structure.
Looking at your output, one thing stands out immediately:
t=15 error = +7.78e-05
t=16 error = -9.40e-05
t=17 error = +1.11e-04
t=18 error = -1.22e-04
t=19 error = +1.41e-04
t=20 error = -1.71e-04
The error is growing, not shrinking.
That means your controller is stable in the bounded sense (it doesn’t explode immediately), but it is not regulating the plant toward the reference.
1. The controller sign
Your plant is approximately
t
Ω
Ω_next
reference
error
P
I
D
control
oracle
disturbance
Then plot
error(t)
control(t)
state(t)
oracle(t)
If the controller is correct:
error
│\
│ \
│ \
│ \
│ \______
└────────────── t
If incorrect:
error
│ /\
│ / \
│/ \
│ \
│ \____
└──────────────
or
/\/\/\/\/\/\/
with increasing amplitude.
The biggest thing still missing
Your observer measures only state.
A complete cybernetic controller usually includes a state estimator.
Instead of
measure Ω
estimate
Ω
Ω̇
noise
confidence
using something like:
- α–β filter
- Kalman filter
- Extended Kalman filter (for nonlinear dynamics)
- Unscented Kalman filter
That separates measurement noise from the underlying state before the controller acts.
The same in codebox:
You're no longer missing the ARCHITECTURE.
You now have a complete closed-loop system:
✅ Plant
✅ Observer
✅ Controller
✅ Feedback
✅ Disturbance
✅ State memory
✅ Reference state (φ)
What is missing now is ENGINEERING, not structure.
============================================================
CURRENT OUTPUT ANALYSIS
============================================================
Your output shows:
t=15 error = +7.78e-05
t=16 error = -9.40e-05
t=17 error = +1.11e-04
t=18 error = -1.22e-04
t=19 error = +1.41e-04
t=20 error = -1.71e-04
The error is growing, not shrinking.
The controller is bounded, but it is not regulating the plant toward
the reference state.
============================================================
1. CONTROLLER SIGN
============================================================
Your plant:
Ω(n+1) = 1 + 1/Ω(n) + u(n) + η(n)
Suppose:
Ω > φ
Then:
error = φ - Ω < 0
A correcting controller should move the state downward:
u = k * error
If instead:
u = -k * error
the controller pushes the state farther away.
Verify the feedback sign first.
============================================================
2. LINEARIZE THE PLANT
============================================================
The nonlinear map:
T(x) = 1 + 1/x
Derivative:
T'(x) = -1/x²
At the golden fixed point:
T'(φ) = -0.381966...
The closed loop becomes approximately:
x(n+1) = (-0.381966 - k)x(n)
For convergence:
|-0.381966 - k| < 1
Therefore:
-0.618 < k < 1.382
The gain must remain inside this region.
============================================================
3. INTEGRAL WINDUP
============================================================
PID contains:
integral += error
Without limits, the integral term accumulates forever.
Add:
integral = clamp(
integral,
-maximum,
maximum
)
Otherwise the integral component eventually dominates the controller.
============================================================
4. ORACLE SCALING
============================================================
Current oracle output:
1.429412
1.429421
1.429430
1.429434
The variation is approximately:
1e-5
Therefore the oracle is effectively a constant gain multiplier:
control ≈ 1.429 * PID
Currently it contributes almost no dynamic behavior.
============================================================
5. DISTURBANCE
============================================================
Injected disturbance:
random() * 1e-6
This is reasonable.
A functioning controller should still reduce error despite this noise.
Currently:
disturbance + dynamics > correction
so the error grows.
============================================================
NEXT ENGINEERING STEP
============================================================
Record every timestep:
time
Ω
Ω_next
reference
error
proportional term
integral term
derivative term
control output
oracle value
disturbance
Example state record:
{
t,
omega,
reference,
error,
P,
I,
D,
control,
oracle,
disturbance
}
Then plot:
state(t)
error(t)
control(t)
oracle(t)
Expected stable behavior:
error
|
|\
| \
| \
| \________
|
+---------------- time
Unstable behavior:
error
|
| /\
| / \
| / \
|/ \
|
+---------------- time
Oscillation:
error
|
| /\/\/\/\/
|
+---------------- time
============================================================
THE BIGGEST MISSING COMPONENT
============================================================
The observer currently only measures:
Ω
A stronger cybernetic controller estimates:
Ω
velocity
noise
confidence
Possible estimators:
- Alpha-Beta Filter
- Kalman Filter
- Extended Kalman Filter
- Unscented Kalman Filter
The purpose:
Separate the true system state from measurement noise before
the controller reacts.
============================================================
CURRENT STATUS
============================================================
Architecture:
COMPLETE
Closed loop:
IMPLEMENTED
Mathematical stability:
READY FOR ANALYSIS
Controller tuning:
REQUIRED
Observer:
BASIC MEASUREMENT ONLY
============================================================
NEXT MAJOR STEP
============================================================
Do not add more equations yet.
Analyze the existing closed loop:
1. Verify feedback direction.
2. Tune gains.
3. Separate P/I/D contributions.
4. Compare against a baseline PID.
5. Measure whether the oracle improves regulation.
The goal is to determine whether each subsystem contributes
to stability and control.
#!/usr/bin/env python3
"""
===============================================================================
CYBERNETIC A/B CONTROL EXPERIMENT
===============================================================================
Three controllers receive the IDENTICAL disturbance sequence:
1. BASELINE PID
2. PID + PHI ORACLE GAIN
3. ORACLE-ONLY FEEDBACK
The plant is the nonlinear golden-ratio map:
Ω[n+1] = 1 + 1/Ω[n] + u[n] + η[n]
with equilibrium:
φ = (1 + √5) / 2
The experiment deliberately starts away from φ and uses a deterministic
disturbance sequence so the controllers can be compared fairly.
Measured:
MAE
RMSE
MAX ERROR
SETTLING TIME
TOTAL CONTROL EFFORT
FINAL ERROR
The oracle does NOT get assumed to be useful. Its value is measured against
a conventional baseline.
===============================================================================
"""
from __future__ import annotations
import math
import random
from dataclasses import dataclass
from typing import Dict, List
# =============================================================================
# CONSTANTS
# =============================================================================
PHI = (1.0 + math.sqrt(5.0)) / 2.0
# =============================================================================
# PARAMETERS
# =============================================================================
@dataclass
class ExperimentConfig:
steps: int = 5000
# Deliberately begin away from equilibrium.
initial_offset: float = 1.0e-2
# Reproducible disturbance.
disturbance_amplitude: float = 1.0e-6
random_seed: int = 22177
# Reference.
reference: float = PHI
# PID gains.
kp: float = 0.35
ki: float = 0.005
kd: float = 0.08
# Integral anti-windup.
integral_limit: float = 1.0e-2
# Controller saturation.
max_control: float = 5.0e-2
# Oracle configuration.
prime: int = 7
base: float = 10.0
# Oracle feedback strength.
oracle_feedback_gain: float = 0.75
# Settling criterion.
settling_tolerance: float = 1.0e-7
settling_window: int = 100
# =============================================================================
# DISTURBANCE GENERATOR
# =============================================================================
def generate_disturbances(
config: ExperimentConfig,
) -> List[float]:
"""
Generate one deterministic disturbance sequence.
Every controller receives exactly the same sequence.
"""
rng = random.Random(config.random_seed)
return [
rng.uniform(
-config.disturbance_amplitude,
config.disturbance_amplitude,
)
for _ in range(config.steps)
]
# =============================================================================
# PLANT
# =============================================================================
class GoldenPlant:
"""
Nonlinear plant:
Ω[n+1] = 1 + 1/Ω[n] + u[n] + η[n]
"""
def __init__(self, initial_state: float):
if initial_state <= 0.0:
raise ValueError("Initial Ω must be positive.")
self.state = initial_state
def update(
self,
control: float,
disturbance: float,
) -> float:
omega = self.state
if omega == 0.0:
raise ZeroDivisionError("Plant state Ω reached zero.")
next_state = (
1.0
+ 1.0 / omega
+ control
+ disturbance
)
if not math.isfinite(next_state):
raise FloatingPointError(
f"Non-finite plant state generated: {next_state}"
)
self.state = next_state
return next_state
# =============================================================================
# ORACLE
# =============================================================================
class PhiOracle:
"""
Log-periodic φ-based observer.
Λφ(p) =
ln(p ln(base) / ln(Ω)) / ln(Ω)
- 1 / (base Ω)
Bounded collapse observable:
O = 2 |cos(π Λφ / 2)|
This is treated strictly as an experimental signal.
"""
def __init__(
self,
base: float = 10.0,
):
if base <= 0.0 or base == 1.0:
raise ValueError("base must be > 0 and != 1.")
self.base = base
def evaluate(
self,
p: int,
omega: float,
) -> float:
if p <= 0:
raise ValueError("p must be positive.")
if omega <= 0.0:
return 0.0
ln_base = math.log(self.base)
ln_omega = math.log(omega)
if ln_omega == 0.0:
return 0.0
argument = (
p
* ln_base
/ ln_omega
)
if argument <= 0.0:
return 0.0
Lambda = (
math.log(argument)
/ ln_omega
- 1.0 / (self.base * omega)
)
oracle = 2.0 * abs(
math.cos(
math.pi * Lambda / 2.0
)
)
if not math.isfinite(oracle):
return 0.0
return oracle
# =============================================================================
# PID CORE
# =============================================================================
class PIDCore:
"""
Standard PID controller core.
u = kp e + ki integral + kd derivative
"""
def __init__(
self,
kp: float,
ki: float,
kd: float,
integral_limit: float,
output_limit: float,
):
self.kp = kp
self.ki = ki
self.kd = kd
self.integral_limit = integral_limit
self.output_limit = output_limit
self.integral = 0.0
self.previous_error = 0.0
@staticmethod
def clamp(
value: float,
low: float,
high: float,
) -> float:
return max(low, min(high, value))
def reset(self) -> None:
self.integral = 0.0
self.previous_error = 0.0
def update(
self,
error: float,
) -> Dict[str, float]:
self.integral += error
self.integral = self.clamp(
self.integral,
-self.integral_limit,
self.integral_limit,
)
derivative = (
error
- self.previous_error
)
self.previous_error = error
proportional_term = (
self.kp * error
)
integral_term = (
self.ki * self.integral
)
derivative_term = (
self.kd * derivative
)
control = (
proportional_term
+ integral_term
+ derivative_term
)
control = self.clamp(
control,
-self.output_limit,
self.output_limit,
)
return {
"p": proportional_term,
"i": integral_term,
"d": derivative_term,
"control": control,
}
# =============================================================================
# METRICS
# =============================================================================
@dataclass
class Metrics:
mae: float
rmse: float
max_error: float
settling_time: int | None
control_effort: float
final_error: float
def calculate_metrics(
errors: List[float],
controls: List[float],
config: ExperimentConfig,
) -> Metrics:
if not errors:
raise ValueError("No errors supplied.")
absolute_errors = [
abs(e) for e in errors
]
mae = sum(
absolute_errors
) / len(absolute_errors)
rmse = math.sqrt(
sum(
e * e
for e in errors
)
/ len(errors)
)
max_error = max(
absolute_errors
)
control_effort = sum(
abs(u)
for u in controls
)
settling_time = None
window = config.settling_window
tolerance = config.settling_tolerance
if len(errors) >= window:
for index in range(
len(errors) - window + 1
):
window_errors = errors[
index:index + window
]
if all(
abs(e) <= tolerance
for e in window_errors
):
settling_time = index
break
final_error = errors[-1]
return Metrics(
mae=mae,
rmse=rmse,
max_error=max_error,
settling_time=settling_time,
control_effort=control_effort,
final_error=final_error,
)
# =============================================================================
# EXPERIMENT RESULT
# =============================================================================
@dataclass
class ExperimentResult:
name: str
states: List[float]
errors: List[float]
controls: List[float]
oracle_values: List[float]
disturbances: List[float]
metrics: Metrics
# =============================================================================
# BASELINE PID
# =============================================================================
def run_baseline_pid(
config: ExperimentConfig,
disturbances: List[float],
) -> ExperimentResult:
plant = GoldenPlant(
config.reference
+ config.initial_offset
)
controller = PIDCore(
kp=config.kp,
ki=config.ki,
kd=config.kd,
integral_limit=config.integral_limit,
output_limit=config.max_control,
)
states = []
errors = []
controls = []
oracle_values = []
oracle = PhiOracle(
base=config.base
)
for disturbance in disturbances:
state = plant.state
error = (
config.reference
- state
)
pid = controller.update(
error
)
control = pid["control"]
next_state = plant.update(
control,
disturbance,
)
states.append(next_state)
errors.append(error)
controls.append(control)
oracle_values.append(
oracle.evaluate(
config.prime,
state,
)
)
metrics = calculate_metrics(
errors,
controls,
config,
)
return ExperimentResult(
name="BASELINE PID",
states=states,
errors=errors,
controls=controls,
oracle_values=oracle_values,
disturbances=disturbances,
metrics=metrics,
)
# =============================================================================
# PID + ORACLE
# =============================================================================
def run_pid_oracle(
config: ExperimentConfig,
disturbances: List[float],
) -> ExperimentResult:
plant = GoldenPlant(
config.reference
+ config.initial_offset
)
controller = PIDCore(
kp=config.kp,
ki=config.ki,
kd=config.kd,
integral_limit=config.integral_limit,
output_limit=config.max_control,
)
oracle = PhiOracle(
base=config.base
)
states = []
errors = []
controls = []
oracle_values = []
for disturbance in disturbances:
state = plant.state
error = (
config.reference
- state
)
oracle_value = oracle.evaluate(
config.prime,
state,
)
pid = controller.update(
error
)
# Normalize the oracle so it acts as a gain
# without arbitrarily multiplying the PID by 2.
normalized_oracle = (
oracle_value / 2.0
)
control = (
pid["control"]
* (
1.0
+ config.oracle_feedback_gain
* normalized_oracle
)
)
control = PIDCore.clamp(
control,
-config.max_control,
config.max_control,
)
next_state = plant.update(
control,
disturbance,
)
states.append(next_state)
errors.append(error)
controls.append(control)
oracle_values.append(oracle_value)
metrics = calculate_metrics(
errors,
controls,
config,
)
return ExperimentResult(
name="PID + ORACLE GAIN",
states=states,
errors=errors,
controls=controls,
oracle_values=oracle_values,
disturbances=disturbances,
metrics=metrics,
)
# =============================================================================
# ORACLE-ONLY CONTROLLER
# =============================================================================
def run_oracle_only(
config: ExperimentConfig,
disturbances: List[float],
) -> ExperimentResult:
plant = GoldenPlant(
config.reference
+ config.initial_offset
)
oracle = PhiOracle(
base=config.base
)
states = []
errors = []
controls = []
oracle_values = []
for disturbance in disturbances:
state = plant.state
error = (
config.reference
- state
)
oracle_value = oracle.evaluate(
config.prime,
state,
)
# Direct proportional feedback modulated by oracle.
control = (
config.kp
* error
* (
0.5
+ oracle_value / 2.0
)
)
control = PIDCore.clamp(
control,
-config.max_control,
config.max_control,
)
next_state = plant.update(
control,
disturbance,
)
states.append(next_state)
errors.append(error)
controls.append(control)
oracle_values.append(oracle_value)
metrics = calculate_metrics(
errors,
controls,
config,
)
return ExperimentResult(
name="ORACLE-ONLY",
states=states,
errors=errors,
controls=controls,
oracle_values=oracle_values,
disturbances=disturbances,
metrics=metrics,
)
# =============================================================================
# REPORTING
# =============================================================================
def format_settling_time(
value: int | None,
) -> str:
if value is None:
return "NOT SETTLED"
return str(value)
def print_result(
result: ExperimentResult,
) -> None:
m = result.metrics
print()
print("=" * 78)
print(result.name)
print("=" * 78)
print(
f"MAE : {m.mae:.12e}"
)
print(
f"RMSE : {m.rmse:.12e}"
)
print(
f"MAX ERROR : {m.max_error:.12e}"
)
print(
f"SETTLING TIME : "
f"{format_settling_time(m.settling_time)}"
)
print(
f"CONTROL EFFORT : {m.control_effort:.12e}"
)
print(
f"FINAL ERROR : {m.final_error:.12e}"
)
print(
f"FINAL STATE : {result.states[-1]:.12f}"
)
print(
f"FINAL ORACLE : "
f"{result.oracle_values[-1]:.12f}"
)
# =============================================================================
# COMPARISON
# =============================================================================
def print_comparison(
results: List[ExperimentResult],
) -> None:
print()
print("=" * 100)
print("A/B COMPARISON")
print("=" * 100)
header = (
f"{'CONTROLLER':<22}"
f"{'MAE':>16}"
f"{'RMSE':>16}"
f"{'MAX':>16}"
f"{'EFFORT':>16}"
f"{'FINAL':>16}"
)
print(header)
print("-" * 100)
for result in results:
m = result.metrics
print(
f"{result.name:<22}"
f"{m.mae:>16.6e}"
f"{m.rmse:>16.6e}"
f"{m.max_error:>16.6e}"
f"{m.control_effort:>16.6e}"
f"{m.final_error:>16.6e}"
)
# =============================================================================
# WINNER ANALYSIS
# =============================================================================
def determine_best(
results: List[ExperimentResult],
) -> None:
best_mae = min(
results,
key=lambda r: r.metrics.mae,
)
best_rmse = min(
results,
key=lambda r: r.metrics.rmse,
)
least_effort = min(
results,
key=lambda r: r.metrics.control_effort,
)
print()
print("=" * 78)
print("EXPERIMENTAL OUTCOME")
print("=" * 78)
print(
f"LOWEST MAE : {best_mae.name}"
)
print(
f"LOWEST RMSE : {best_rmse.name}"
)
print(
f"LOWEST EFFORT : {least_effort.name}"
)
print()
print(
"Interpretation:"
)
print(
"The oracle is considered beneficial only if its addition "
"measurably improves regulation against the identical disturbance "
"sequence rather than merely producing an interesting signal."
)
# =============================================================================
# OPTIONAL TEXT TRACE
# =============================================================================
def print_trace(
result: ExperimentResult,
count: int = 20,
) -> None:
print()
print("=" * 100)
print(f"TRACE: {result.name}")
print("=" * 100)
print(
f"{'t':>5}"
f"{'Ω':>18}"
f"{'error':>18}"
f"{'control':>18}"
f"{'oracle':>14}"
)
print("-" * 100)
limit = min(
count,
len(result.states),
)
for index in range(limit):
print(
f"{index + 1:>5}"
f"{result.states[index]:>18.12f}"
f"{result.errors[index]:>+18.8e}"
f"{result.controls[index]:>+18.8e}"
f"{result.oracle_values[index]:>14.8f}"
)
# =============================================================================
# MAIN EXPERIMENT
# =============================================================================
def main() -> None:
config = ExperimentConfig()
print("=" * 78)
print(" CLOSED-LOOP CYBERNETIC A/B EXPERIMENT")
print("=" * 78)
print(
f"Reference φ : {config.reference:.15f}"
)
print(
f"Initial Ω : "
f"{config.reference + config.initial_offset:.15f}"
)
print(
f"Steps : {config.steps}"
)
print(
f"Disturbance : ±{config.disturbance_amplitude:.3e}"
)
print(
f"Random seed : {config.random_seed}"
)
print(
f"Prime / p : {config.prime}"
)
print(
f"Base : {config.base}"
)
print()
# One disturbance stream for EVERY controller.
disturbances = generate_disturbances(
config
)
baseline = run_baseline_pid(
config,
disturbances,
)
pid_oracle = run_pid_oracle(
config,
disturbances,
)
oracle_only = run_oracle_only(
config,
disturbances,
)
results = [
baseline,
pid_oracle,
oracle_only,
]
# Detailed traces.
for result in results:
print_trace(
result,
count=20,
)
# Individual reports.
for result in results:
print_result(
result
)
# Side-by-side comparison.
print_comparison(
results
)
# Basic outcome.
determine_best(
results
)
print()
print("=" * 78)
print(" EXPERIMENT COMPLETE")
print("=" * 78)
# =============================================================================
# ENTRY POINT
# =============================================================================
if __name__ == "__main__":
main()
YIELDS:
==============================================================================
CLOSED-LOOP CYBERNETIC A/B EXPERIMENT
==============================================================================
Reference φ : 1.618033988749895
Initial Ω : 1.628033988749895
Steps : 5000
Disturbance : ±1.000e-06
Random seed : 22177
Prime / p : 7
Base : 10.0
909 |
837 |
====================================================================================================
TRACE: BASELINE PID
====================================================================================================
t Ω error control oracle
----------------------------------------------------------------------------------------------------
1 1.609887533466 -1.00000000e-02 -4.35000000e-03 0.36611448
2 1.625454441251 +8.14645528e-03 +4.29370805e-03 1.00277381
3 1.611322771934 -7.42045250e-03 -3.88888098e-03 0.45846208
4 1.624075355709 +6.71121682e-03 +3.46664553e-03 0.95433538
5 1.612556493471 -6.04136696e-03 -3.17770587e-03 0.50776024
6 1.622957247575 +5.47749528e-03 +2.82299907e-03 0.91230737
7 1.613563938965 -4.92325882e-03 -2.59545047e-03 0.54766091
8 1.622043726032 +4.47004979e-03 +2.29808281e-03 0.87774151
9 1.614386011103 -4.00973728e-03 -2.11973901e-03 0.58020115
10 1.621301127956 +3.64797765e-03 +1.86970127e-03 0.84938378
11 1.615055106634 -3.26713921e-03 -1.73275187e-03 0.60660573
12 1.620694314450 +2.97888212e-03 +1.52114106e-03 0.82620852
13 1.615603611377 -2.66032570e-03 -1.41670164e-03 0.62814650
14 1.620198960494 +2.43037737e-03 +1.23558920e-03 0.80715030
15 1.616049618616 -2.16497174e-03 -1.15849203e-03 0.64570450
16 1.619795353625 +1.98437013e-03 +1.00327476e-03 0.79161555
17 1.616414429953 -1.76136487e-03 -9.48145470e-04 0.65999182
18 1.619466284903 +1.61955880e-03 +8.13408303e-04 0.77888466
19 1.616711116723 -1.43229615e-03 -7.76524700e-04 0.67162746
20 1.619198666662 +1.32287203e-03 +6.58960374e-04 0.76851556
837 |
====================================================================================================
TRACE: PID + ORACLE GAIN
====================================================================================================
t Ω error control oracle
----------------------------------------------------------------------------------------------------
1 1.609290309218 -1.00000000e-02 -4.94722425e-03 0.36611448
2 1.627691206125 +8.74367953e-03 +6.29995351e-03 1.02277526
3 1.608768543152 -9.65721738e-03 -5.59768782e-03 0.37839038
4 1.628201084211 +9.26544560e-03 +6.60703954e-03 1.04017128
5 1.608313621366 -1.01670955e-02 -5.86035586e-03 0.36013039
6 1.628737312981 +9.72036738e-03 +6.96709852e-03 1.05527720
7 1.607848482572 -1.07033242e-02 -6.12428026e-03 0.34092765
8 1.629288260092 +1.01855062e-02 +7.33959090e-03 1.07066152
9 1.607373244699 -1.12542713e-02 -6.39124703e-03 0.32120080
10 1.629857229109 +1.06607441e-02 +7.72330522e-03 1.08631459
11 1.606887718058 -1.18232404e-02 -6.66224618e-03 0.30083399
12 1.630439177901 +1.11462707e-02 +8.11890597e-03 1.10223663
13 1.606397559657 -1.24051892e-02 -6.93493054e-03 0.28001045
14 1.631032652426 +1.16364291e-02 +8.52208208e-03 1.11823696
15 1.605900518326 -1.29986637e-02 -7.20795463e-03 0.25878517
16 1.631637079605 +1.21334704e-02 +8.93430332e-03 1.13438447
17 1.605400978851 -1.36030909e-02 -7.48104694e-03 0.23718181
18 1.632249521028 +1.26330099e-02 +9.35252901e-03 1.15053247
19 1.604899186310 -1.42155323e-02 -7.75249901e-03 0.21530871
20 1.632868384576 +1.31348024e-02 +9.77627532e-03 1.16666978
837 |
====================================================================================================
TRACE: ORACLE-ONLY
====================================================================================================
t Ω error control oracle
----------------------------------------------------------------------------------------------------
1 1.611846833124 -1.00000000e-02 -2.39070034e-03 0.36611448
2 1.622502449710 +6.18715563e-03 +2.09677667e-03 0.93652490
3 1.615108059701 -4.46846096e-03 -1.22291498e-03 0.56386855
4 1.620088353158 +2.92592905e-03 +9.34146483e-04 0.82437093
5 1.616656448174 -2.05436441e-03 -5.93061688e-04 0.64962161
6 1.618988342905 +1.37754058e-03 +4.26796187e-04 0.77042724
7 1.617387885861 -9.54354155e-04 -2.82000572e-04 0.68850508
8 1.618477676534 +6.46102888e-04 +1.97282491e-04 0.74481269
9 1.617731617736 -4.43687784e-04 -1.32502418e-04 0.70650783
10 1.618242079884 +3.02371014e-04 +9.16882592e-05 0.73274846
11 1.617891360205 -2.08091134e-04 -6.24461518e-05 0.71480229
12 1.618130836376 +1.42628545e-04 +4.31093124e-05 0.72713624
13 1.617968679358 -9.68476258e-05 -2.91293776e-05 0.71871623
14 1.618078308861 +6.53093923e-05 +1.97086271e-05 0.72441853
15 1.618003678232 -4.43201114e-05 -1.33447261e-05 0.72056376
16 1.618053904391 +3.03105182e-05 +9.14037666e-06 0.72318809
17 1.618021018788 -1.99156410e-05 -5.99956397e-06 0.72142201
18 1.618042504301 +1.29699618e-05 +3.90981076e-06 0.72257839
19 1.618028427481 -8.51555095e-06 -2.56589735e-06 0.72182289
20 1.618037801102 +5.56126917e-06 +1.67619783e-06 0.72231788
697 |
==============================================================================
BASELINE PID
==============================================================================
MAE : 2.062482504957e-05
RMSE : 3.049037123565e-04
MAX ERROR : 1.000000000000e-02
SETTLING TIME : NOT SETTLED
CONTROL EFFORT : 5.697915914391e-02
FINAL ERROR : -3.791005285247e-07
FINAL STATE : 1.618033570703
FINAL ORACLE : 0.722109002014
697 |
==============================================================================
PID + ORACLE GAIN
==============================================================================
MAE : 8.019911306266e-02
RMSE : 8.050083385390e-02
MAX ERROR : 8.207944843281e-02
SETTLING TIME : NOT SETTLED
CONTROL EFFORT : 2.477299118377e+02
FINAL ERROR : 7.983670163250e-02
FINAL STATE : 1.700111560332
FINAL ORACLE : 1.709479116689
697 |
==============================================================================
ORACLE-ONLY
==============================================================================
MAE : 6.643794036608e-06
RMSE : 1.868837189567e-04
MAX ERROR : 1.000000000000e-02
SETTLING TIME : NOT SETTLED
CONTROL EFFORT : 9.530682034651e-03
FINAL ERROR : -5.573059509434e-07
FINAL STATE : 1.618033502883
FINAL ORACLE : 0.722102735727
745 |
====================================================================================================
A/B COMPARISON
====================================================================================================
CONTROLLER MAE RMSE MAX EFFORT FINAL
----------------------------------------------------------------------------------------------------
BASELINE PID 2.062483e-05 3.049037e-04 1.000000e-02 5.697916e-02 -3.791005e-07
PID + ORACLE GAIN 8.019911e-02 8.050083e-02 8.207945e-02 2.477299e+02 7.983670e-02
ORACLE-ONLY 6.643794e-06 1.868837e-04 1.000000e-02 9.530682e-03 -5.573060e-07
799 |
==============================================================================
EXPERIMENTAL OUTCOME
==============================================================================
LOWEST MAE : ORACLE-ONLY
LOWEST RMSE : ORACLE-ONLY
LOWEST EFFORT : ORACLE-ONLY
816 |
Interpretation:
The oracle is considered beneficial only if its addition measurably improves regulation against the identical disturbance sequence rather than merely producing an interesting signal.
960 |
==============================================================================
EXPERIMENT COMPLETE
==============================================================================
#!/usr/bin/env python3
# =============================================================================
# शून्यत्वम् — FULL CYBERNETIC ENGINE
# =============================================================================
#
# CLOSED-LOOP GOLDEN-RATIO CYBERNETIC CONTROLLER
#
# Architecture
#
# ┌──────────────────────────────────────────────────────────────┐
# │ CYBERNETIC LOOP │
# │ │
# │ disturbance │
# │ │ │
# │ ▼ │
# │ ┌───────────┐ measured state ┌──────────────┐ │
# │ │ PLANT │ ─────────────────────────► │ OBSERVER │ │
# │ │ Ω[n] │ │ Ω, ΔΩ, R │ │
# │ └─────┬─────┘ └──────┬───────┘ │
# │ ▲ │ │
# │ │ │ │
# │ │ control ▼ │
# │ │ ┌─────────────┐ │
# │ └──────────────────────────────────│ CONTROLLER │ │
# │ │ oracle + │ │
# │ │ feedback │ │
# │ └─────────────┘ │
# │ │
# └──────────────────────────────────────────────────────────────┘
#
# Core plant:
#
# Ω[n+1] = 1 + 1/Ω[n] + u[n] + η[n]
#
# Golden fixed point:
#
# φ = (1 + √5) / 2
#
# φ = 1 + 1/φ
#
# Oracle:
#
# Λφ(p, Ω)
# = ln(p ln(base) / ln(Ω)) / ln(Ω)
# - 1 / (base Ω)
#
# O = 2 |cos(π Λφ / 2)|
#
# Controller:
#
# e[n] = φ - Ω[n]
#
# u[n] = feedback(e, Δe, integral, O, Ω)
#
# The oracle is part of the closed loop.
# It is NOT merely an observer output.
#
# Features:
#
# • nonlinear golden-ratio plant
# • oracle
# • state observer
# • filtered state velocity
# • proportional / integral / derivative control
# • oracle-modulated feedback
# • integral anti-windup
# • actuator saturation
# • slew-rate limiting
# • deadband
# • safety bounds
# • disturbance handling
# • confidence estimation
# • telemetry
# • rolling statistics
# • fault detection
# • automatic recovery
# • deterministic or stochastic disturbance source
# • JSON telemetry export
# • CSV telemetry export
# • live console telemetry
# • clean shutdown
#
# Standard library only.
# =============================================================================
from __future__ import annotations
import csv
import json
import math
import random
import signal
import sys
import time
from collections import deque
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Deque, Dict, List, Optional
# =============================================================================
# MATHEMATICAL CONSTANTS
# =============================================================================
PHI = (1.0 + math.sqrt(5.0)) / 2.0
PHI_CONJUGATE = (1.0 - math.sqrt(5.0)) / 2.0
PHI_RECIPROCAL = 1.0 / PHI
PHI_MAP_DERIVATIVE = -1.0 / (PHI * PHI)
# =============================================================================
# CONFIGURATION
# =============================================================================
@dataclass
class CyberneticConfig:
# -------------------------------------------------------------------------
# Runtime
# -------------------------------------------------------------------------
steps: int = 10000
print_every: int = 10
telemetry_window: int = 256
# -------------------------------------------------------------------------
# Reference
# -------------------------------------------------------------------------
reference: float = PHI
# Start away from the equilibrium to engage the controller immediately.
initial_offset: float = 1.0e-2
# -------------------------------------------------------------------------
# Plant
# -------------------------------------------------------------------------
disturbance_amplitude: float = 1.0e-6
deterministic_disturbance: bool = True
random_seed: int = 22177
plant_minimum: float = 1.0e-9
plant_maximum: float = 10.0
# -------------------------------------------------------------------------
# Oracle
# -------------------------------------------------------------------------
prime: int = 7
base: float = 10.0
oracle_enabled: bool = True
# The oracle contributes to control strength.
#
# gain = 0.5 + oracle / 2
#
# O ∈ [0, 2]
# gain ∈ [0.5, 1.5]
oracle_floor: float = 0.5
oracle_ceiling: float = 1.5
# -------------------------------------------------------------------------
# Controller
# -------------------------------------------------------------------------
kp: float = 0.35
ki: float = 0.005
kd: float = 0.08
# Total integral accumulation limit.
integral_limit: float = 1.0e-2
# Absolute actuator limit.
max_control: float = 5.0e-2
# Maximum controller change per cycle.
max_control_delta: float = 1.0e-2
# Ignore microscopic errors.
deadband: float = 1.0e-12
# -------------------------------------------------------------------------
# State filtering
# -------------------------------------------------------------------------
velocity_alpha: float = 0.25
error_alpha: float = 0.20
# -------------------------------------------------------------------------
# Stabilization
# -------------------------------------------------------------------------
# Maximum allowed error before recovery is engaged.
recovery_error_limit: float = 0.1
# Recovery gain.
recovery_gain: float = 0.5
# Maximum integral growth while recovering.
recovery_integral_scale: float = 0.25
# -------------------------------------------------------------------------
# Settling
# -------------------------------------------------------------------------
settling_tolerance: float = 1.0e-8
settling_window: int = 100
# -------------------------------------------------------------------------
# Fault detection
# -------------------------------------------------------------------------
divergence_limit: float = 1.0
maximum_residual: float = 1.0
# -------------------------------------------------------------------------
# Output
# -------------------------------------------------------------------------
csv_path: str = "cybernetic_telemetry.csv"
json_path: str = "cybernetic_summary.json"
write_files: bool = True
# =============================================================================
# DISTURBANCE SOURCE
# =============================================================================
class DisturbanceSource:
"""
Generates plant disturbances.
Deterministic mode:
a reproducible sequence from the configured seed.
Stochastic mode:
uses the process-global random source.
"""
def __init__(
self,
config: CyberneticConfig,
) -> None:
self.config = config
self.rng = random.Random(
config.random_seed
)
def next(self) -> float:
amplitude = self.config.disturbance_amplitude
if amplitude <= 0.0:
return 0.0
return self.rng.uniform(
-amplitude,
amplitude,
)
# =============================================================================
# PLANT
# =============================================================================
class GoldenPlant:
"""
Nonlinear plant.
Ω[n+1]
= 1
+ 1/Ω[n]
+ u[n]
+ η[n]
The uncontrolled fixed point is φ.
"""
def __init__(
self,
initial_state: float,
config: CyberneticConfig,
) -> None:
if not math.isfinite(initial_state):
raise ValueError(
"Initial plant state must be finite."
)
if initial_state <= config.plant_minimum:
raise ValueError(
"Initial plant state must be positive."
)
if initial_state >= config.plant_maximum:
raise ValueError(
"Initial plant state exceeds maximum."
)
self.config = config
self.state = initial_state
def nominal_map(
self,
omega: float,
) -> float:
if omega == 0.0:
raise ZeroDivisionError(
"Nominal map received Ω = 0."
)
return 1.0 + 1.0 / omega
def derivative(
self,
omega: float,
) -> float:
if omega == 0.0:
return float("-inf")
return -1.0 / (omega * omega)
def update(
self,
control: float,
disturbance: float,
) -> float:
omega = self.state
nominal = self.nominal_map(
omega
)
next_state = (
nominal
+ control
+ disturbance
)
if not math.isfinite(next_state):
raise FloatingPointError(
f"Non-finite state generated: {next_state}"
)
if next_state <= self.config.plant_minimum:
raise FloatingPointError(
f"Plant crossed lower safety bound: {next_state}"
)
if next_state >= self.config.plant_maximum:
raise FloatingPointError(
f"Plant crossed upper safety bound: {next_state}"
)
self.state = next_state
return next_state
# =============================================================================
# ORACLE
# =============================================================================
class PhiOracle:
"""
φ-scaled logarithmic observer.
Λφ(p, Ω)
= ln(
p ln(base) / ln(Ω)
) / ln(Ω)
- 1 / (base Ω)
O
= 2 |cos(π Λφ / 2)|
O is bounded in [0, 2].
"""
def __init__(
self,
config: CyberneticConfig,
) -> None:
self.config = config
if config.base <= 0.0:
raise ValueError(
"Oracle base must be positive."
)
if config.base == 1.0:
raise ValueError(
"Oracle base must not equal 1."
)
if config.prime <= 0:
raise ValueError(
"Oracle p must be positive."
)
def lambda_value(
self,
omega: float,
) -> float:
if omega <= 0.0:
return float("nan")
ln_base = math.log(
self.config.base
)
ln_omega = math.log(
omega
)
if ln_omega == 0.0:
return float("nan")
argument = (
self.config.prime
* ln_base
/ ln_omega
)
if argument <= 0.0:
return float("nan")
value = (
math.log(argument)
/ ln_omega
-
1.0
/
(
self.config.base
* omega
)
)
return value
def evaluate(
self,
omega: float,
) -> float:
if not self.config.oracle_enabled:
return 1.0
lam = self.lambda_value(
omega
)
if not math.isfinite(lam):
return 1.0
oracle = (
2.0
*
abs(
math.cos(
math.pi
*
lam
/
2.0
)
)
)
if not math.isfinite(oracle):
return 1.0
return max(
0.0,
min(
2.0,
oracle
)
)
def gain(
self,
omega: float,
) -> float:
if not self.config.oracle_enabled:
return 1.0
oracle = self.evaluate(
omega
)
value = (
self.config.oracle_floor
+
(
self.config.oracle_ceiling
-
self.config.oracle_floor
)
*
(
oracle / 2.0
)
)
return value
# =============================================================================
# STATE OBSERVER
# =============================================================================
class StateObserver:
"""
Estimates:
Ω
Ω velocity
filtered error
residual
confidence
"""
def __init__(
self,
reference: float,
config: CyberneticConfig,
) -> None:
self.reference = reference
self.config = config
self.initialized = False
self.previous_state = reference
self.velocity = 0.0
self.filtered_error = 0.0
self.residual = 0.0
self.confidence = 1.0
def reset(
self,
state: float,
) -> None:
self.initialized = True
self.previous_state = state
self.velocity = 0.0
self.filtered_error = (
self.reference - state
)
self.residual = 0.0
self.confidence = 1.0
def update(
self,
state: float,
expected_nominal: float,
control: float,
disturbance: float,
) -> Dict[str, float]:
if not self.initialized:
self.reset(
state
)
raw_velocity = (
state
-
self.previous_state
)
self.velocity = (
self.config.velocity_alpha
*
raw_velocity
+
(
1.0
-
self.config.velocity_alpha
)
*
self.velocity
)
error = (
self.reference
-
state
)
self.filtered_error = (
self.config.error_alpha
*
error
+
(
1.0
-
self.config.error_alpha
)
*
self.filtered_error
)
predicted_state = (
expected_nominal
+
control
+
disturbance
)
self.residual = (
state
-
predicted_state
)
residual_penalty = min(
1.0,
abs(
self.residual
)
/
max(
self.config.maximum_residual,
1.0e-30,
)
)
velocity_penalty = min(
1.0,
abs(
self.velocity
)
/
max(
self.config.recovery_error_limit,
1.0e-30,
)
)
self.confidence = max(
0.0,
min(
1.0,
1.0
-
0.5
*
residual_penalty
-
0.5
*
velocity_penalty,
)
)
self.previous_state = state
return {
"state": state,
"velocity": self.velocity,
"error": error,
"filtered_error": self.filtered_error,
"residual": self.residual,
"confidence": self.confidence,
}
# =============================================================================
# CONTROLLER TELEMETRY
# =============================================================================
@dataclass
class ControlTerms:
proportional: float = 0.0
integral: float = 0.0
derivative: float = 0.0
oracle_gain: float = 1.0
feedback: float = 0.0
recovery: float = 0.0
pre_saturation: float = 0.0
post_saturation: float = 0.0
slew_limited: bool = False
# =============================================================================
# CYBERNETIC CONTROLLER
# =============================================================================
class CyberneticController:
"""
Closed-loop controller.
The control law combines:
P = kp * filtered_error
I = ki * integral(error)
D = kd * (-estimated_velocity)
then applies the oracle as a gain field:
oracle_gain = G(O)
followed by recovery, saturation, and slew limiting.
The feedback path is:
measured state
↓
observer
↓
error + velocity + confidence
↓
controller
↓
control
↓
plant
↓
measured state ...
"""
def __init__(
self,
config: CyberneticConfig,
oracle: PhiOracle,
) -> None:
self.config = config
self.oracle = oracle
self.integral = 0.0
self.previous_control = 0.0
self.last_terms = ControlTerms()
def reset(self) -> None:
self.integral = 0.0
self.previous_control = 0.0
self.last_terms = ControlTerms()
def clamp(
self,
value: float,
low: float,
high: float,
) -> float:
return max(
low,
min(
high,
value
)
)
def update(
self,
state: float,
error: float,
filtered_error: float,
velocity: float,
confidence: float,
omega: float,
) -> float:
cfg = self.config
# ---------------------------------------------------------------------
# Deadband
# ---------------------------------------------------------------------
effective_error = error
if abs(
effective_error
) <= cfg.deadband:
effective_error = 0.0
# ---------------------------------------------------------------------
# Integral
# ---------------------------------------------------------------------
self.integral += (
effective_error
)
self.integral = self.clamp(
self.integral,
-cfg.integral_limit,
cfg.integral_limit,
)
# ---------------------------------------------------------------------
# PID terms
# ---------------------------------------------------------------------
proportional = (
cfg.kp
*
filtered_error
)
integral = (
cfg.ki
*
self.integral
)
# Negative velocity is a useful direct damping signal:
#
# velocity > 0 => state moving upward
# derivative term becomes negative
#
derivative = (
-cfg.kd
*
velocity
)
base_feedback = (
proportional
+
integral
+
derivative
)
# ---------------------------------------------------------------------
# Oracle gain
# ---------------------------------------------------------------------
oracle_gain = self.oracle.gain(
omega
)
# Lower confidence reduces the authority of the controller.
confidence_gain = (
0.25
+
0.75
*
confidence
)
feedback = (
base_feedback
*
oracle_gain
*
confidence_gain
)
# ---------------------------------------------------------------------
# Recovery controller
# ---------------------------------------------------------------------
recovery = 0.0
if abs(
error
) >= cfg.recovery_error_limit:
recovery = (
cfg.recovery_gain
*
error
)
self.integral *= (
cfg.recovery_integral_scale
)
pre_saturation = (
feedback
+
recovery
)
# ---------------------------------------------------------------------
# Actuator saturation
# ---------------------------------------------------------------------
saturated = self.clamp(
pre_saturation,
-cfg.max_control,
cfg.max_control,
)
# ---------------------------------------------------------------------
# Slew-rate limiting
# ---------------------------------------------------------------------
delta = (
saturated
-
self.previous_control
)
slew_limited = False
if delta > cfg.max_control_delta:
control = (
self.previous_control
+
cfg.max_control_delta
)
slew_limited = True
elif delta < -cfg.max_control_delta:
control = (
self.previous_control
-
cfg.max_control_delta
)
slew_limited = True
else:
control = saturated
self.previous_control = control
self.last_terms = ControlTerms(
proportional=proportional,
integral=integral,
derivative=derivative,
oracle_gain=oracle_gain,
feedback=feedback,
recovery=recovery,
pre_saturation=pre_saturation,
post_saturation=control,
slew_limited=slew_limited,
)
return control
# =============================================================================
# ROLLING STATISTICS
# =============================================================================
class RollingStatistics:
def __init__(
self,
window: int,
) -> None:
self.window = max(
1,
window
)
self.errors: Deque[float] = deque(
maxlen=self.window
)
self.controls: Deque[float] = deque(
maxlen=self.window
)
self.oracle: Deque[float] = deque(
maxlen=self.window
)
self.residuals: Deque[float] = deque(
maxlen=self.window
)
def add(
self,
error: float,
control: float,
oracle: float,
residual: float,
) -> None:
self.errors.append(
error
)
self.controls.append(
control
)
self.oracle.append(
oracle
)
self.residuals.append(
residual
)
@staticmethod
def mean(
values: Deque[float],
) -> float:
if not values:
return 0.0
return sum(values) / len(
values
)
@staticmethod
def rms(
values: Deque[float],
) -> float:
if not values:
return 0.0
return math.sqrt(
sum(
value * value
for value in values
)
/
len(values)
)
def summary(self) -> Dict[str, float]:
return {
"mean_abs_error":
self.mean(
deque(
abs(x)
for x in self.errors
)
),
"rms_error":
self.rms(
self.errors
),
"mean_control":
self.mean(
self.controls
),
"mean_oracle":
self.mean(
self.oracle
),
"mean_abs_residual":
self.mean(
deque(
abs(x)
for x in self.residuals
)
),
}
# =============================================================================
# TELEMETRY RECORD
# =============================================================================
@dataclass
class TelemetryRecord:
step: int
state: float
reference: float
error: float
filtered_error: float
velocity: float
residual: float
confidence: float
oracle: float
oracle_gain: float
proportional: float
integral: float
derivative: float
feedback: float
recovery: float
control: float
disturbance: float
nominal_state: float
closed_loop_state: float
stable: bool
recovered: bool
# =============================================================================
# FAULT STATE
# =============================================================================
@dataclass
class FaultState:
faulted: bool = False
fault_count: int = 0
last_fault: str = ""
# =============================================================================
# CYBERNETIC ENGINE
# =============================================================================
class CyberneticEngine:
"""
Complete stateful cybernetic machine.
"""
def __init__(
self,
config: Optional[CyberneticConfig] = None,
) -> None:
self.config = (
config
if config is not None
else CyberneticConfig()
)
self.disturbance_source = (
DisturbanceSource(
self.config
)
)
initial_state = (
self.config.reference
+
self.config.initial_offset
)
self.plant = GoldenPlant(
initial_state,
self.config,
)
self.oracle = PhiOracle(
self.config
)
self.observer = StateObserver(
self.config.reference,
self.config,
)
self.controller = CyberneticController(
self.config,
self.oracle,
)
self.statistics = RollingStatistics(
self.config.telemetry_window
)
self.telemetry: List[
TelemetryRecord
] = []
self.fault = FaultState()
self.running = True
self.settled_count = 0
self.first_settled_step: Optional[
int
] = None
self.total_abs_error = 0.0
self.total_squared_error = 0.0
self.maximum_abs_error = 0.0
self.total_control_effort = 0.0
self.total_disturbance_energy = 0.0
self.controller.reset()
# -------------------------------------------------------------------------
# Stop
# -------------------------------------------------------------------------
def stop(self) -> None:
self.running = False
# -------------------------------------------------------------------------
# Fault handling
# -------------------------------------------------------------------------
def register_fault(
self,
message: str,
) -> None:
self.fault.faulted = True
self.fault.fault_count += 1
self.fault.last_fault = message
def clear_fault(self) -> None:
self.fault.faulted = False
self.fault.last_fault = ""
def recover(
self,
) -> None:
"""
Controlled recovery.
Reset controller memory while preserving the plant.
"""
self.controller.reset()
self.observer.reset(
self.plant.state
)
self.clear_fault()
# -------------------------------------------------------------------------
# Stability
# -------------------------------------------------------------------------
def is_stable(
self,
error: float,
residual: float,
) -> bool:
if not math.isfinite(error):
return False
if not math.isfinite(residual):
return False
if abs(error) > self.config.divergence_limit:
return False
if abs(residual) > self.config.maximum_residual:
return False
if self.plant.state <= self.config.plant_minimum:
return False
if self.plant.state >= self.config.plant_maximum:
return False
return True
# -------------------------------------------------------------------------
# Settling
# -------------------------------------------------------------------------
def update_settling(
self,
step: int,
error: float,
) -> None:
if (
abs(error)
<= self.config.settling_tolerance
):
self.settled_count += 1
if (
self.first_settled_step
is None
):
if (
self.settled_count
>= self.config.settling_window
):
self.first_settled_step = (
step
-
self.config.settling_window
+
1
)
else:
self.settled_count = 0
# -------------------------------------------------------------------------
# Single cybernetic cycle
# -------------------------------------------------------------------------
def step(
self,
step: int,
) -> TelemetryRecord:
previous_state = (
self.plant.state
)
# ---------------------------------------------------------------------
# Disturbance
# ---------------------------------------------------------------------
disturbance = (
self.disturbance_source.next()
)
# ---------------------------------------------------------------------
# Oracle
# ---------------------------------------------------------------------
oracle_value = (
self.oracle.evaluate(
previous_state
)
)
oracle_gain = (
self.oracle.gain(
previous_state
)
)
# ---------------------------------------------------------------------
# Nominal plant prediction
# ---------------------------------------------------------------------
nominal_state = (
self.plant.nominal_map(
previous_state
)
)
# ---------------------------------------------------------------------
# Preliminary observation
# ---------------------------------------------------------------------
predicted_without_control = (
nominal_state
+
disturbance
)
estimated_velocity = (
self.observer.velocity
)
error = (
self.config.reference
-
previous_state
)
observer_data = (
self.observer.update(
state=previous_state,
expected_nominal=nominal_state,
control=self.controller.previous_control,
disturbance=disturbance,
)
)
# Replace raw values with filtered observer values.
filtered_error = (
observer_data[
"filtered_error"
]
)
velocity = (
observer_data[
"velocity"
]
)
confidence = (
observer_data[
"confidence"
]
)
# ---------------------------------------------------------------------
# Controller
# ---------------------------------------------------------------------
control = (
self.controller.update(
state=previous_state,
error=error,
filtered_error=filtered_error,
velocity=velocity,
confidence=confidence,
omega=previous_state,
)
)
# ---------------------------------------------------------------------
# Plant update
# ---------------------------------------------------------------------
try:
closed_loop_state = (
self.plant.update(
control,
disturbance,
)
)
except (
FloatingPointError,
ZeroDivisionError,
) as exc:
self.register_fault(
str(exc)
)
# Hold the current state if possible.
closed_loop_state = (
previous_state
)
self.plant.state = (
previous_state
)
# ---------------------------------------------------------------------
# Actual residual
# ---------------------------------------------------------------------
actual_residual = (
closed_loop_state
-
(
nominal_state
+
control
+
disturbance
)
)
# ---------------------------------------------------------------------
# Error after plant transition
# ---------------------------------------------------------------------
next_error = (
self.config.reference
-
closed_loop_state
)
recovered = False
# ---------------------------------------------------------------------
# Stability
# ---------------------------------------------------------------------
stable = self.is_stable(
next_error,
actual_residual,
)
if not stable:
self.register_fault(
"closed-loop stability boundary exceeded"
)
self.recover()
recovered = True
stable = True
# ---------------------------------------------------------------------
# Aggregate statistics
# ---------------------------------------------------------------------
self.total_abs_error += abs(
error
)
self.total_squared_error += (
error * error
)
self.maximum_abs_error = max(
self.maximum_abs_error,
abs(error)
)
self.total_control_effort += (
abs(control)
)
self.total_disturbance_energy += (
disturbance * disturbance
)
self.statistics.add(
error=error,
control=control,
oracle=oracle_value,
residual=actual_residual,
)
self.update_settling(
step,
next_error,
)
terms = (
self.controller.last_terms
)
record = TelemetryRecord(
step=step,
state=previous_state,
reference=self.config.reference,
error=error,
filtered_error=filtered_error,
velocity=(
velocity
if math.isfinite(
velocity
)
else estimated_velocity
),
residual=actual_residual,
confidence=confidence,
oracle=oracle_value,
oracle_gain=oracle_gain,
proportional=terms.proportional,
integral=terms.integral,
derivative=terms.derivative,
feedback=terms.feedback,
recovery=terms.recovery,
control=control,
disturbance=disturbance,
nominal_state=nominal_state,
closed_loop_state=closed_loop_state,
stable=stable,
recovered=recovered,
)
self.telemetry.append(
record
)
return record
# -------------------------------------------------------------------------
# Console
# -------------------------------------------------------------------------
def print_header(self) -> None:
print(
"=" * 124
)
print(
" शून्यत्वम् — FULL CYBERNETIC ENGINE"
)
print(
"=" * 124
)
print(
f"φ = {PHI:.15f}"
)
print(
f"ψ = {PHI_CONJUGATE:.15f}"
)
print(
f"1/φ = {PHI_RECIPROCAL:.15f}"
)
print(
f"plant derivative= {PHI_MAP_DERIVATIVE:.15f}"
)
print(
f"initial Ω = "
f"{self.plant.state:.15f}"
)
print(
f"reference = "
f"{self.config.reference:.15f}"
)
print(
f"oracle p = "
f"{self.config.prime}"
)
print(
f"oracle base = "
f"{self.config.base}"
)
print(
f"steps = "
f"{self.config.steps}"
)
print(
f"disturbance = "
f"±{self.config.disturbance_amplitude:.3e}"
)
print(
f"oracle = "
f"{'ON' if self.config.oracle_enabled else 'OFF'}"
)
print()
print(
f"{'STEP':>8}"
f"{'Ω':>18}"
f"{'ERROR':>16}"
f"{'VELO':>14}"
f"{'ORACLE':>12}"
f"{'GAIN':>10}"
f"{'CONTROL':>14}"
f"{'CONF':>10}"
f"{'RESID':>14}"
f"{'STATE':>12}"
)
print(
"-" * 124
)
def print_record(
self,
record: TelemetryRecord,
) -> None:
state = (
"OK"
if record.stable
else "FAULT"
)
if record.recovered:
state = "RECOVER"
print(
f"{record.step:>8}"
f"{record.state:>18.12f}"
f"{record.error:>+16.6e}"
f"{record.velocity:>+14.6e}"
f"{record.oracle:>12.8f}"
f"{record.oracle_gain:>10.6f}"
f"{record.control:>+14.6e}"
f"{record.confidence:>10.6f}"
f"{record.residual:>+14.6e}"
f"{state:>12}"
)
# -------------------------------------------------------------------------
# Run
# -------------------------------------------------------------------------
def run(self) -> None:
self.print_header()
for step in range(
self.config.steps
):
if not self.running:
break
record = self.step(
step
)
if (
step % self.config.print_every
== 0
):
self.print_record(
record
)
print()
self.print_summary()
# -------------------------------------------------------------------------
# Summary
# -------------------------------------------------------------------------
def summary(self) -> Dict[str, object]:
count = len(
self.telemetry
)
if count == 0:
return {
"steps": 0,
"error": None,
}
rms = math.sqrt(
self.total_squared_error
/
count
)
mae = (
self.total_abs_error
/
count
)
final_state = (
self.plant.state
)
final_error = (
self.config.reference
-
final_state
)
recent = (
self.statistics.summary()
)
return {
"engine": "शून्यत्वम्",
"architecture": "closed-loop cybernetic controller",
"steps": count,
"reference_phi": self.config.reference,
"initial_state": (
self.config.reference
+
self.config.initial_offset
),
"final_state": final_state,
"final_error": final_error,
"mean_absolute_error": mae,
"root_mean_square_error": rms,
"maximum_absolute_error": (
self.maximum_abs_error
),
"total_control_effort": (
self.total_control_effort
),
"disturbance_energy": (
self.total_disturbance_energy
),
"settling_step": (
self.first_settled_step
),
"fault_count": (
self.fault.fault_count
),
"last_fault": (
self.fault.last_fault
),
"rolling": recent,
}
def print_summary(self) -> None:
summary = self.summary()
print(
"=" * 78
)
print(
" CYBERNETIC STATE SUMMARY"
)
print(
"=" * 78
)
print(
f"steps : "
f"{summary.get('steps')}"
)
print(
f"reference φ : "
f"{summary.get('reference_phi'):.15f}"
)
print(
f"final Ω : "
f"{summary.get('final_state'):.15f}"
)
print(
f"final error : "
f"{summary.get('final_error'):+.12e}"
)
print(
f"MAE : "
f"{summary.get('mean_absolute_error'):.12e}"
)
print(
f"RMSE : "
f"{summary.get('root_mean_square_error'):.12e}"
)
print(
f"maximum error : "
f"{summary.get('maximum_absolute_error'):.12e}"
)
print(
f"control effort : "
f"{summary.get('total_control_effort'):.12e}"
)
print(
f"disturbance energy : "
f"{summary.get('disturbance_energy'):.12e}"
)
settling = (
summary.get(
"settling_step"
)
)
if settling is None:
print(
"settling : NOT ESTABLISHED"
)
else:
print(
f"settling step : "
f"{settling}"
)
print(
f"fault count : "
f"{summary.get('fault_count')}"
)
if summary.get(
"last_fault"
):
print(
f"last fault : "
f"{summary.get('last_fault')}"
)
rolling = summary.get(
"rolling",
{}
)
print()
print(
"ROLLING WINDOW"
)
print(
f"mean |error| : "
f"{rolling.get('mean_abs_error', 0.0):.12e}"
)
print(
f"RMS error : "
f"{rolling.get('rms_error', 0.0):.12e}"
)
print(
f"mean control : "
f"{rolling.get('mean_control', 0.0):+.12e}"
)
print(
f"mean oracle : "
f"{rolling.get('mean_oracle', 0.0):.12e}"
)
print(
f"mean |residual| : "
f"{rolling.get('mean_abs_residual', 0.0):.12e}"
)
print()
# -------------------------------------------------------------------------
# CSV
# -------------------------------------------------------------------------
def write_csv(
self,
path: str,
) -> None:
if not self.telemetry:
return
output = Path(
path
)
fields = list(
asdict(
self.telemetry[0]
).keys()
)
with output.open(
"w",
newline="",
encoding="utf-8",
) as handle:
writer = csv.DictWriter(
handle,
fieldnames=fields,
)
writer.writeheader()
for record in self.telemetry:
writer.writerow(
asdict(record)
)
# -------------------------------------------------------------------------
# JSON
# -------------------------------------------------------------------------
def write_json(
self,
path: str,
) -> None:
output = Path(
path
)
payload = {
"configuration": asdict(
self.config
),
"summary": self.summary(),
}
with output.open(
"w",
encoding="utf-8",
) as handle:
json.dump(
payload,
handle,
indent=2,
)
handle.write("\n")
# -------------------------------------------------------------------------
# Files
# -------------------------------------------------------------------------
def export(
self,
) -> None:
if not self.config.write_files:
return
self.write_csv(
self.config.csv_path
)
self.write_json(
self.config.json_path
)
# =============================================================================
# SIGNAL HANDLING
# =============================================================================
_ENGINE: Optional[
CyberneticEngine
] = None
def handle_signal(
signum: int,
frame,
) -> None:
del frame
if _ENGINE is not None:
print()
print(
f"[signal {signum}] shutdown requested"
)
_ENGINE.stop()
# =============================================================================
# MAIN
# =============================================================================
def main() -> int:
global _ENGINE
config = CyberneticConfig()
_ENGINE = CyberneticEngine(
config
)
signal.signal(
signal.SIGINT,
handle_signal
)
if hasattr(
signal,
"SIGTERM"
):
signal.signal(
signal.SIGTERM,
handle_signal
)
start = time.perf_counter()
try:
_ENGINE.run()
except KeyboardInterrupt:
print()
print(
"Keyboard interrupt."
)
_ENGINE.stop()
except Exception as exc:
print()
print(
"=" * 78
)
print(
"CYBERNETIC ENGINE FAULT"
)
print(
"=" * 78
)
print(
f"{type(exc).__name__}: {exc}"
)
_ENGINE.register_fault(
str(exc)
)
_ENGINE.stop()
finally:
_ENGINE.export()
elapsed = (
time.perf_counter()
-
start
)
print()
print(
f"runtime : "
f"{elapsed:.6f} s"
)
print(
f"rate : "
f"{len(_ENGINE.telemetry) / elapsed:.3f} cycles/s"
if elapsed > 0.0
else "rate : n/a"
)
print()
print(
"CYBERNETIC ENGINE CLOSED."
)
return 0
# =============================================================================
# ENTRY
# =============================================================================
if __name__ == "__main__":
raise SystemExit(
main()
)
































