/* integrated.c — integrate all THREE missing subsystems in one model: METABOLISM + ENERGY + CODE.
*
* Every earlier model isolated the gene and the public good always leaked. Here catalysis privatizes via
* a shared but LOCAL resource, driven by an energy throughput, with function read through a code:
* METABOLISM: a strand of catalytic quality kappa PRODUCES local resource; replication CONSUMES it.
* A parasite (low kappa) consumes but doesn't produce -> depletes its own neighbourhood.
* ENERGY: resource is produced from (unlimited) food and DISSIPATES (decays) -- a throughput, not a
* store. And FIDELITY COSTS ENERGY: high-fidelity copying needs local resource (proof-reading).
* So metabolism GATES fidelity: rich patch -> faithful; depleted patch -> error-prone + can't
* even afford to replicate.
* CODE: kappa is read from the sequence via a FIXED ARBITRARY code (match to a code-target), NOT the
* sequence's own self-structure -- genotype->phenotype decoupling. This removes the trans-
* complement leak that sank coemerge (function is no longer about complementarity).
*
* Privatization is meant to EMERGE (no supplied compartments, no group-selection machinery): catalyst
* patches sustain resource -> high fidelity -> breed true; parasite patches starve -> self-limit. Control:
* LOCAL resource vs GLOBAL (mean-field) resource, isolating whether spatial-metabolic locality privatizes.
* Razor: nothing rewards a sequence; kappa is a coded (arbitrary, smooth/climbable) landscape; whether kappa
* RISES from random (~0.5) toward 1 is measured. HONEST: this is a MINIMAL integration -- a coded genotype->
* phenotype map, not a full translation apparatus; whether the code/food/compartment PHYSICS itself arises
* is not addressed. Likely outcome for an open problem is a null or partial; not tuned to force a positive.
*
* build: cc -O2 -Wall -std=c11 -o integrated integrated.c -lm
*/
#include <math.h>
#include <stdio.h>
#include <stdint.h>
#define G 56
#define NC (G*G)
#define L 24
#define F0 0.85
#define F1 0.999
#define BETA 2.0
#define PROD 0.60
#define DECAY 0.12
#define RCOST 1.00
#define RHI 3.00
#define DEG 0.08
#define EMPTY 0xffffffffu
static unsigned long long RNG;
static unsigned long long xs(void){ RNG^=RNG<<13; RNG^=RNG>>7; RNG^=RNG<<17; return RNG; }
static double urand(void){ return (double)(xs()>>11)*(1.0/9007199254740992.0); }
static uint32_t replicate(uint32_t t,double f){ uint32_t c=0; for(int i=0;i<L;i++){ int b=(t>>i)&1; int nb=(urand()<f)?(1-b):b; if(nb)c|=(1u<<i);} return c; }
static uint32_t randstrand(void){ uint32_t s=0; for(int b=0;b<L;b++) if(urand()<0.5) s|=(1u<<b); return s; }
static uint32_t TCAT;
static double kappa(uint32_t s){ return (double)(L-__builtin_popcount(s^TCAT))/L; } /* coded phenotype in [0,1] */
static uint32_t grd[NC];
static double R[NC], R2[NC];
/* one run. local=1: per-cell resource w/ mild diffusion. local=0: single global (mean-field) pool.
returns final mean kappa; writes final mean local resource + start kappa. */
static double run(int local,double *meanR,double *start){
TCAT=randstrand();
for(int i=0;i<NC;i++){ grd[i]=(urand()<0.5)?randstrand():EMPTY; R[i]=0; }
double Rglob=0;
{ double s=0;int c=0; for(int i=0;i<NC;i++) if(grd[i]!=EMPTY){ s+=kappa(grd[i]); c++; } *start=c?s/c:0; }
for(int g=0;g<250;g++){
int nocc=0; double sk=0; for(int i=0;i<NC;i++) if(grd[i]!=EMPTY){ nocc++; sk+=kappa(grd[i]); }
if(!nocc) break;
if(local){
/* diffuse (mild local sharing) */
for(int y=0;y<G;y++)for(int x=0;x<G;x++){ double s=0;int n=0; for(int dy=-1;dy<=1;dy++)for(int dx=-1;dx<=1;dx++){ s+=R[((y+dy+G)%G)*G+((x+dx+G)%G)]; n++; } R2[y*G+x]=0.6*R[y*G+x]+0.4*(s/n); }
for(int i=0;i<NC;i++) R[i]=R2[i];
/* produce from food (~kappa) + dissipate */
for(int i=0;i<NC;i++){ if(grd[i]!=EMPTY) R[i]+=PROD*kappa(grd[i]); R[i]*=(1.0-DECAY); }
} else {
Rglob += PROD*sk; Rglob *= (1.0-DECAY);
}
/* replication: gated by LOCAL resource; fidelity from local resource; parasites faster */
for(int t=0;t<NC;t++){ int x=(int)(xs()%G),y=(int)(xs()%G),c=y*G+x; if(grd[c]==EMPTY)continue;
double w=1.0+BETA*(1.0-kappa(grd[c])); if(urand()> w/(1.0+BETA)) continue;
double r = local? R[c] : (nocc?Rglob/nocc:0);
if(r<RCOST) continue;
int dx=(int)(xs()%3)-1,dy=(int)(xs()%3)-1,nx=(x+dx+G)%G,ny=(y+dy+G)%G,nc=ny*G+nx; if(grd[nc]!=EMPTY)continue;
if(local) R[c]-=RCOST; else Rglob-=RCOST;
double f=F0+(F1-F0)*(r<RHI? r/RHI:1.0);
grd[nc]=replicate(grd[c],f);
}
for(int i=0;i<NC;i++) if(grd[i]!=EMPTY && urand()<DEG) grd[i]=EMPTY;
}
double s=0,mr=0; int c=0; for(int i=0;i<NC;i++) if(grd[i]!=EMPTY){ s+=kappa(grd[i]); mr+=(local?R[i]:0); c++; }
*meanR = local? (c?mr/c:0) : (c?Rglob/c:0); return c?s/c:0;
}
int main(void){
unsigned long long seeds[]={0x9e3779b97f4a7c15ULL,0xd1b54a32d192ed03ULL,0x2545f4914f6cdd1dULL};
int NS=3;
printf("=== Integrated: metabolism + energy + code. Does catalysis (kappa) emerge unaided? ===\n");
printf(" Local resource (catalyst produces, replication consumes, fidelity costs energy), coded kappa.\n");
printf(" %dx%d grid, random start kappa~0.50 (max 1.0). LOCAL vs GLOBAL(mean-field) resource, %d seeds.\n\n",G,G,NS);
printf(" arm | kappa start->end | mean resource end | verdict\n");
printf(" ----------------+------------------+-------------------+--------\n");
double gS=0,gE=0,gR=0, lS=0,lE=0,lR=0, mr,st,en;
for(int s=0;s<NS;s++){ RNG=seeds[s]; en=run(0,&mr,&st); gS+=st;gE+=en;gR+=mr; }
for(int s=0;s<NS;s++){ RNG=seeds[s]; en=run(1,&mr,&st); lS+=st;lE+=en;lR+=mr; }
gS/=NS;gE/=NS;gR/=NS; lS/=NS;lE/=NS;lR/=NS;
printf(" GLOBAL resource | %.2f -> %.2f | %.2f | %s\n", gS,gE,gR, gE>gS+0.15?"IGNITES":(gE>gS+0.05?"partial":(gE<gS-0.05?"falls":"~flat")));
int ig = lE>lS+0.15;
printf(" LOCAL resource | %.2f -> %.2f | %.2f | %s\n", lS,lE,lR, ig?"IGNITES":(lE>lS+0.05?"partial":(lE<lS-0.05?"falls":"~flat")));
printf("\n");
if(ig && !(gE>gS+0.15))
printf(" => INTEGRATION WORKS (and locality is why). Catalysis emerges UNAIDED -- no supplied compartments,\n no group-selection machinery. Metabolism privatizes: a LOCAL resource made by catalysts gates fidelity\n and replication, so catalyst patches breed true while parasite patches STARVE (deplete their own\n resource -> can't afford to copy). The GLOBAL control fails -> spatial-metabolic locality is the cause.\n Metabolism+energy+code together do what the isolated gene could not. HONEST BOUND: minimal integration\n (coded map, not full translation); the code/food/locality PHYSICS is still supplied.\n");
else if(ig)
printf(" => Catalysis rises in BOTH local and global -> the coded metabolic coupling helps, but locality is\n not decisively the cause here; interpret with care, not a clean privatization result.\n");
else
printf(" => Honest NULL: catalysis did not emerge. Even integrated, metabolism+energy+code did not privatize\n against parasites in this regime. The public-goods wall stands. Do not tune to force a positive.\n");
return 0;
}