“””

THE KEY EMERGES — composition.py

 

Composition is the second major use of the survival kernel.

 

SAME KERNEL. NEW SUBSTRATE.

 

At the signal level, the kernel searched for transforms that revealed

invariant structure in noisy point clouds.

 

At the composition level, the kernel searches for relationships between

two independently promoted structures that satisfy the world constraint.

 

The substrate has changed. The kernel has not.

 

WHAT COMPOSITION DISCOVERS:

 

Two independently promoted structures:

S1: a radial/boundary structure from arm 1

S2: a periodic/groove structure from arm 2

 

Neither contains the key. Neither was evolved to fit the other.

Their compatibility — if it exists — must be discovered.

 

Composition search finds a relationship (translation, rotation,

phase alignment, scale matching) that combines S1 and S2 into

a single object satisfying world admissibility.

 

Most compositions fail. The world enforces this. Failure is visible.

Some compositions satisfy A but not B. Some satisfy A and B but

not C. One — if the search succeeds — satisfies all three.

 

That object is not “the key.” It is a survived composed structure.

The world decides whether it works.

 

CODE COMMENT CONTRACT:

At this layer, comments must explicitly state that the kernel is

now operating on promoted structures and their relationships,

not on atoms or raw geometry.

 

COMPOSITION CANDIDATE REPRESENTATION:

 

A composition candidate encodes a relationship between S1 and S2:

radial_scale:      scale S1’s mean radius toward R_lock

groove_scale:      scale S2’s amplitude toward A_lock

phase_offset:      additional phase rotation for groove alignment

rotation:          global rotation of the composed object

center_x, center_y: translation of composed object

 

The realized geometry is:

r(theta) = radial_scale * mean_radius_S1

+ groove_scale * amplitude_S2 * sin(omega * theta

+ phase_S2 + phase_offset)

“””

 

from **future** import annotations

import numpy as np

from typing import Any, Callable, List, Optional, Tuple

from core_types import (

ArmID, CompositionCandidate, CompositionResult,

EvaluationResult, PromotedStructure, RealizedComposition,

StageLabel, SurvivalConfig, SurvivalResult

)

from world_constraint import (

WorldParameters, DEFAULT_WORLD,

evaluate_world, check_admissibility, candidate_to_polar

)

 

# —————————————————————————

 

# Composition parameter bounds

 

# —————————————————————————

 

# These bounds are defined by what relationships between two promoted

 

# structures are physically meaningful — not by knowledge of the answer.

 

#

 

# The correct composition (radial_scale -> 1/mean_radius_S1 * R_lock,

 

# groove_scale -> A_lock/amplitude_S2, phase_offset -> phi_lock - phase_S2)

 

# must be discovered by the kernel, not pre-specified.

 

COMPOSITION_PARAM_BOUNDS = np.array([

[0.5,   2.0],    # radial_scale: scale S1 radius toward target

[0.1,  50.0],    # groove_scale: scale S2 amplitude toward A_lock

# Wide range — arm 2 promoted amplitude can be very small

# if the transform didn’t fully normalize. The composition

# search must still find the correct scaling.

# Correct value = A_lock / amplitude_S2, which varies widely.

[-np.pi, np.pi],  # phase_offset: alignment of groove with world phase

[-np.pi, np.pi],  # rotation: global rotation

[-0.1,  0.1],   # center_x

[-0.1,  0.1],   # center_y

])

 

# —————————————————————————

 

# Geometry realization

 

# —————————————————————————

 

def realize_composition(s1: PromotedStructure,

s2: PromotedStructure,

params: np.ndarray,

world: WorldParameters = DEFAULT_WORLD,

M: int = 360) -> np.ndarray:

“””

Realize a composition candidate as a 2D polar profile geometry.

 

```

The kernel is now operating on promoted structures and their

relationships, not on atoms or raw geometry.

 

r(theta) = radial_component + groove_component

 

where:

    radial_component = radial_scale * mean_radius_S1

    groove_component = groove_scale * amplitude_S2

                       * sin(omega * theta + phase_S2 + phase_offset)

 

Parameters

----------

s1     : promoted structure from arm 1 (radial invariant)

s2     : promoted structure from arm 2 (periodic invariant)

params : composition parameter vector

world  : world parameters

M      : number of angular samples

 

Returns

-------

geometry : np.ndarray (M, 2) — realized 2D point cloud

"""

radial_scale  = params[0]

groove_scale  = params[1]

phase_offset  = params[2]

rotation      = params[3]

cx, cy        = params[4], params[5]

 

thetas = np.linspace(0.0, 2.0 * np.pi, M, endpoint=False)

 

# Radial component from S1

mean_r = s1.structural_params["mean_radius"]

r_radial = radial_scale * mean_r

 

# Groove component from S2

amp_s2    = s2.structural_params["amplitude"]

phase_s2  = s2.structural_params["phase"]

omega     = s2.structural_params["omega"]

 

groove = groove_scale * amp_s2 * np.sin(

    omega * thetas + phase_s2 + phase_offset

)

 

# Composed polar profile

r = r_radial + groove

 

# Convert to Cartesian with global rotation and translation

x = r * np.cos(thetas + rotation) + cx

y = r * np.sin(thetas + rotation) + cy

 

return np.stack([x, y], axis=1)

```

 

# —————————————————————————

 

# Composition candidate factory and operators

 

# —————————————————————————

 

def random_composition_candidate(s1: PromotedStructure,

s2: PromotedStructure,

candidate_id: str,

generation: int = 0,

rng: Optional[np.random.Generator] = None

) -> CompositionCandidate:

“””

Generate a random composition candidate within parameter bounds.

 

```

Initialization is within admissible bounds — restricting search

to lawful compositions, not known answers.

 

The kernel is now operating on promoted structures and their

relationships, not on atoms or raw geometry.

"""

if rng is None:

    rng = np.random.default_rng()

 

params = np.array([

    rng.uniform(lo, hi)

    for lo, hi in COMPOSITION_PARAM_BOUNDS

])

 

return CompositionCandidate(

    candidate_id=candidate_id,

    structure_a=s1,

    structure_b=s2,

    relation_params=params,

    param_bounds=COMPOSITION_PARAM_BOUNDS.copy(),

    generation_born=generation

)

```

 

def mutate_composition(candidate: CompositionCandidate,

mutation_rate: float = 0.35,

mutation_scale: float = 0.15,

rng: Optional[np.random.Generator] = None

) -> CompositionCandidate:

“””

Produce a mutated copy of a composition candidate.

 

```

No outcome-directed mutation. Operators defined by parameter

family bounds, not by knowledge of target structure.

"""

if rng is None:

    rng = np.random.default_rng()

 

new_params = candidate.relation_params.copy()

bounds = candidate.param_bounds

 

for i in range(len(new_params)):

    if rng.random() < mutation_rate:

        lo, hi = bounds[i]

        scale = (hi - lo) * mutation_scale

        new_params[i] += rng.normal(0.0, scale)

        new_params[i] = np.clip(new_params[i], lo, hi)

 

return CompositionCandidate(

    candidate_id=candidate.candidate_id + "_m",

    structure_a=candidate.structure_a,

    structure_b=candidate.structure_b,

    relation_params=new_params,

    param_bounds=bounds.copy(),

    generation_born=candidate.generation_born

)

```

 

def crossover_compositions(parent_a: CompositionCandidate,

parent_b: CompositionCandidate,

child_id: str,

rng: Optional[np.random.Generator] = None

) -> CompositionCandidate:

“”“Uniform crossover between two composition candidates.”””

if rng is None:

rng = np.random.default_rng()

 

```

mask = rng.random(len(parent_a.relation_params)) < 0.5

new_params = np.where(mask,

                       parent_a.relation_params,

                       parent_b.relation_params)

 

return CompositionCandidate(

    candidate_id=child_id,

    structure_a=parent_a.structure_a,

    structure_b=parent_a.structure_b,

    relation_params=new_params,

    param_bounds=parent_a.param_bounds.copy(),

    generation_born=max(parent_a.generation_born, parent_b.generation_born)

)

```

 

# —————————————————————————

 

# Composition evaluator

 

# —————————————————————————

 

def evaluate_composition(candidate: CompositionCandidate,

world: WorldParameters = DEFAULT_WORLD

) -> EvaluationResult:

“””

Evaluate a composition candidate against the world constraint.

 

```

The kernel is now operating on promoted structures and their

relationships, not on atoms or raw geometry.

 

Evaluation proceeds in two steps (same dual-pressure as signal level):

 

Step A: world admissibility pre-screen (hard veto)

    If the realized geometry cannot satisfy world constraints,

    eliminate immediately. This is inadmissibility, not non-survival.

 

Step B: compatibility score (for admissible candidates)

    How closely does the composition satisfy all world constraints?

    Scored as combined constraint satisfaction.

 

Returns EvaluationResult compatible with the survival kernel interface.

"""

s1 = candidate.structure_a

s2 = candidate.structure_b

params = candidate.relation_params

 

# Realize geometry

try:

    geometry = realize_composition(s1, s2, params, world)

except Exception as e:

    return EvaluationResult(

        candidate_id=candidate.candidate_id,

        is_admissible=False,

        invariance_score=0.0,

        failure_reason=f"geometry realization failed: {e}",

        diagnostics={"step": "realization_error"}

    )

 

# Step A: admissibility pre-screen

admissible = check_admissibility(geometry, world)

 

if not admissible:

    # INADMISSIBLE — hard veto

    # This is constraint violation, not non-survival.

    # Tracked separately per code comment contract.

    return EvaluationResult(

        candidate_id=candidate.candidate_id,

        is_admissible=False,

        invariance_score=0.0,

        failure_reason="inadmissible: world boundary constraint violated",

        diagnostics={"step": "admissibility_veto"}

    )

 

# Step B: full world evaluation as compatibility score

world_result = evaluate_world(geometry, candidate.candidate_id, world)

 

# Convert world result to invariance score for kernel interface

# Score encodes how far along the constraint chain the candidate got

score = _world_result_to_score(world_result)

 

failure_reason = None

if not world_result.success:

    failure_reason = f"world: {world_result.failure_mode.name}"

 

return EvaluationResult(

    candidate_id=candidate.candidate_id,

    is_admissible=True,

    invariance_score=score,

    failure_reason=failure_reason,

    diagnostics={

        "can_insert": world_result.can_insert,

        "can_rotate": world_result.can_rotate,

        "can_activate": world_result.can_activate,

        "success": world_result.success,

        "failure_mode": world_result.failure_mode.name,

        "world_diagnostics": world_result.constraint_diagnostics

    }

)

```

 

def _world_result_to_score(world_result) -> float:

“””

Convert a WorldResult into a scalar fitness score for the kernel.

 

```

Scoring encodes progress through the constraint chain:

    Cannot insert:           0.0 – 0.3  (constraint A severity)

    Inserts, jams:           0.3 – 0.6  (constraint B severity)

    Rotates, no trigger:     0.6 – 0.9  (constraint C severity)

    Full success:            1.0

 

Within each failure band, partial credit is based on how close

the candidate came to passing. This gives the GA a gradient

to climb even before the first full success.

 

This is NOT a smoothed objective — it is a staged gate with

partial credit within each stage. The distinction matters:

the world still enforces hard thresholds; the score just

provides signal for the GA to work with.

"""

diag = world_result.constraint_diagnostics

 

if world_result.success:

    return 1.0

 

if not world_result.can_insert:

    # Failed constraint A: score based on boundary closeness

    if "constraint_A" in diag:

        E_A = diag["constraint_A"].get("E_A", 1.0)

        epsilon_A = diag["constraint_A"].get("epsilon_A", 0.035)

        # Score 0 to 0.3 based on how close E_A is to epsilon_A

        closeness = np.exp(-5.0 * max(E_A - epsilon_A, 0) / epsilon_A)

        return float(0.3 * closeness)

    return 0.1

 

if not world_result.can_rotate:

    # Passed A, failed B: score in [0.3, 0.6]

    if "constraint_B" in diag:

        E_B = diag["constraint_B"].get("E_B", 1.0)

        epsilon_B = diag["constraint_B"].get("epsilon_B", 0.004)

        closeness = np.exp(-3.0 * max(E_B - epsilon_B, 0) / epsilon_B)

        return float(0.3 + 0.3 * closeness)

    return 0.35

 

if not world_result.can_activate:

    # Passed A and B, failed C: score in [0.6, 0.9]

    if "constraint_C" in diag:

        E_C = diag["constraint_C"].get("E_C", 1.0)

        T   = diag["constraint_C"].get("T", 0.0)

        tau = diag["constraint_C"].get("tau_trigger", 0.006)

        epsilon_C = diag["constraint_C"].get("epsilon_C", 0.004)

        c_residual = np.exp(-3.0 * max(E_C - epsilon_C, 0) / epsilon_C)

        c_trigger  = np.clip(T / tau, 0.0, 1.0) if tau > 0 else 0.0

        return float(0.6 + 0.3 * 0.5 * (c_residual + c_trigger))

    return 0.65

 

return 0.95  # Should not reach here if world_result.success is False

```

 

# —————————————————————————

 

# Composition promotion gate

 

# —————————————————————————

 

def make_composition_gate(success_required: bool = True) -> Callable:

“””

Promotion gate for composition search.

 

```

If success_required=True: promote only when a candidate achieves

score=1.0 (full world success) for stability_gens generations.

 

If success_required=False: promote when score exceeds threshold

(useful for debugging — allows promotion on partial success).

 

The gate is stateful — it tracks consecutive high-scoring generations.

"""

consecutive_success = [0]

consecutive_high    = [0]

 

def gate(eval_history: List[EvaluationResult]) -> bool:

    admissible = [e for e in eval_history if e.is_admissible]

    if not admissible:

        consecutive_success[0] = 0

        consecutive_high[0]    = 0

        return False

 

    best_score = max(e.invariance_score for e in admissible)

 

    # Track full success

    if best_score >= 1.0:

        consecutive_success[0] += 1

    else:

        consecutive_success[0] = 0

 

    # Track high score (partial success threshold)

    if best_score >= 0.9:

        consecutive_high[0] += 1

    else:

        consecutive_high[0] = 0

 

    if success_required:

        return consecutive_success[0] >= 3

    else:

        return consecutive_high[0] >= 3

 

return gate

```

 

# —————————————————————————

 

# Realize final composed object

 

# —————————————————————————

 

def realize_final_composition(best_candidate: CompositionCandidate,

world: WorldParameters = DEFAULT_WORLD

) -> RealizedComposition:

“””

Realize the final surviving composition as a concrete object.

 

```

This is the candidate key. It is not called a key internally

because the system does not know it is a key. It is a survived

composed structure being applied to the world.

 

The shape is the visible artifact of the survived composition.

The structure is what survived.

"""

geometry = realize_composition(

    best_candidate.structure_a,

    best_candidate.structure_b,

    best_candidate.relation_params,

    world

)

 

structural_params = {

    "radial_scale":  float(best_candidate.relation_params[0]),

    "groove_scale":  float(best_candidate.relation_params[1]),

    "phase_offset":  float(best_candidate.relation_params[2]),

    "rotation":      float(best_candidate.relation_params[3]),

    "center_x":      float(best_candidate.relation_params[4]),

    "center_y":      float(best_candidate.relation_params[5]),

    "s1_id":         best_candidate.structure_a.structure_id,

    "s2_id":         best_candidate.structure_b.structure_id,

}

 

return RealizedComposition(

    composition_id=best_candidate.candidate_id,

    source_candidate=best_candidate,

    geometry=geometry,

    structural_params=structural_params

)

```

 

# —————————————————————————

 

# Sanity check

 

# —————————————————————————

 

if **name** == “**main**”:

import gc

from signal_model import build_signal_arms

from transform_families import (

random_candidate, mutate_candidate, crossover_candidates

)

from invariance_metrics import evaluate_candidate as eval_signal

from world_constraint import DEFAULT_WORLD, evaluate_world

from core_types import ArmID, SurvivalConfig, StageLabel

from survival_kernel import (

run_survival_kernel, make_threshold_gate, initialize_population

)

from promotion import promote_structure

 

```

stream_1, stream_2, c1, c2 = build_signal_arms(seed=42)

world = DEFAULT_WORLD

 

print("THE KEY EMERGES — composition.py sanity check")

print("=" * 60)

print()

 

# --- Run both arms and promote ---

print("Step 1: Run survival on both arms and promote")

 

rng_1 = np.random.default_rng(0)

def f1(n): return [random_candidate(ArmID.ARM_1, f"a1_{i}", rng=rng_1) for i in range(n)]

def e1(c): return eval_signal(c, stream_1, world)

def m1(c): return mutate_candidate(c, rng=rng_1)

def x1(a,b,cid): return crossover_candidates(a,b,cid,rng=rng_1)

 

cfg1 = SurvivalConfig(population_size=25, max_generations=30,

                       promotion_threshold=0.80, promotion_stability_gens=3)

g1   = make_threshold_gate(0.80, 3)

r1   = run_survival_kernel(initialize_population(f1,25), e1, m1, x1,

                            g1, cfg1, StageLabel.LENS_SEARCH,

                            ArmID.ARM_1, verbose=False)

 

# Get best candidate from arm 1

rng_1b = np.random.default_rng(0)

cands_1 = [random_candidate(ArmID.ARM_1, f"a1_{i}", rng=rng_1b) for i in range(25)]

evals_1 = [e1(c) for c in cands_1]

bi1 = max(range(25), key=lambda i: evals_1[i].invariance_score if evals_1[i].is_admissible else -1)

s1_obj = promote_structure(r1, cands_1[bi1], evals_1[bi1], stream_1.exposures, "S1", world)

 

rng_2 = np.random.default_rng(1)

def f2(n): return [random_candidate(ArmID.ARM_2, f"a2_{i}", rng=rng_2) for i in range(n)]

def e2(c): return eval_signal(c, stream_2, world)

def m2(c): return mutate_candidate(c, mutation_rate=0.4, rng=rng_2)

def x2(a,b,cid): return crossover_candidates(a,b,cid,rng=rng_2)

 

cfg2 = SurvivalConfig(population_size=25, max_generations=35,

                       promotion_threshold=0.70, promotion_stability_gens=4)

g2   = make_threshold_gate(0.70, 4)

r2   = run_survival_kernel(initialize_population(f2,25), e2, m2, x2,

                            g2, cfg2, StageLabel.LENS_SEARCH,

                            ArmID.ARM_2, verbose=False)

 

rng_2b = np.random.default_rng(1)

cands_2 = [random_candidate(ArmID.ARM_2, f"a2_{i}", rng=rng_2b) for i in range(25)]

evals_2 = [e2(c) for c in cands_2]

bi2 = max(range(25), key=lambda i: evals_2[i].invariance_score if evals_2[i].is_admissible else -1)

s2_obj = promote_structure(r2, cands_2[bi2], evals_2[bi2], stream_2.exposures, "S2", world)

 

print(f"  S1 promoted: {s1_obj is not None}, "

      f"mean_r={s1_obj.structural_params['mean_radius']:.4f}")

print(f"  S2 promoted: {s2_obj is not None}, "

      f"amp={s2_obj.structural_params['amplitude']:.4f}, "

      f"phase={s2_obj.structural_params['phase']:.4f}")

gc.collect()

 

print()

print("Step 2: Composition search")

print("Same kernel, new substrate. No modifications for this level.")

print("Kernel now operates on promoted structures, not raw geometry.")

print()

 

rng_c = np.random.default_rng(7)

 

def fc(n): return [

    random_composition_candidate(s1_obj, s2_obj, f"comp_{i}", rng=rng_c)

    for i in range(n)

]

def ec(c): return evaluate_composition(c, world)

def mc(c): return mutate_composition(c, rng=rng_c)

def xc(a,b,cid): return crossover_compositions(a, b, cid, rng=rng_c)

 

cfg_c = SurvivalConfig(

    population_size=30,

    max_generations=80,

    elite_fraction=0.2,

    mutation_rate=0.35,

    crossover_rate=0.5,

    promotion_threshold=0.95,

    promotion_stability_gens=3,

    rng_seed=7

)

gate_c = make_composition_gate(success_required=False)

pop_c  = initialize_population(fc, cfg_c.population_size)

 

result_c = run_survival_kernel(

    pop_c, ec, mc, xc, gate_c, cfg_c,

    StageLabel.COMPOSITION_SEARCH,

    arm_id=None,

    verbose=True

)

 

print(f"\nComposition result: promoted={result_c.promoted}, "

      f"gen={result_c.promotion_generation}")

print(f"Best trajectory (every 10): "

      f"{[round(result_c.best_per_generation[g],3) for g in range(0,len(result_c.best_per_generation),10)]}")

 

# Realize best composition and test against world

print()

print("Step 3: Realize best composition and test world")

 

rng_cb = np.random.default_rng(7)

test_cands = [random_composition_candidate(s1_obj, s2_obj, f"tc_{i}", rng=rng_cb)

              for i in range(30)]

test_evals = [ec(c) for c in test_cands]

best_ci = max(range(len(test_evals)),

              key=lambda i: test_evals[i].invariance_score

              if test_evals[i].is_admissible else -1)

 

best_comp = test_cands[best_ci]

realized  = realize_final_composition(best_comp, world)

wr        = evaluate_world(realized.geometry, "final_test", world)

 

print(f"  Best initial composition score: {test_evals[best_ci].invariance_score:.4f}")

print(f"  World result: can_insert={wr.can_insert}, "

      f"can_rotate={wr.can_rotate}, "

      f"can_activate={wr.can_activate}, "

      f"success={wr.success}")

print(f"  Failure mode: {wr.failure_mode.name}")

 

print()

print("=" * 60)

print("COMPOSITION ARCHITECTURE VERIFIED:")

print("  Same survival kernel ran composition search")

print("  Operated on promoted structures, not raw signals")

print("  World constraint enforced at every evaluation")

print("  Failure modes are real constraint violations")

print("  Same kernel. New substrate. No modifications.")

print("=" * 60)

```


Back to site index