// readSamples.c // K. J. Rock // December 19, 2024 - January 18th, 2025 // g++ -o rs readSamples.c // Version 1.07.6 to keep in sync with ecoDraw.c and ecology.html #include #include typedef unsigned int chromosome; struct vt { int x, y; }; float fRan( void ) { return (float) rand() / (float) RAND_MAX; } void displayBits( chromosome seq ); // Display chromosome as bits void loadGMSamples( void ); // Read random sample file void loadSpot( void ); // Read spot sample file void seeHBDR( void ); // Display direction genes void seeCNDR( void ); // Display direction genes void seeEN( void ); // Display energy genes void seePL( void ); // Display plant genes void seeHB( void ); // Display herbivore genes void seeCN( void ); // Display carnivore genes void seeSN( void ); // Display sensor genes void seeGM( void ); // Display genetic marker chromosome void seeSpeed( void ); // Display average herbivore, carnivore speed void poisonHisto( void ); // Create histogram of PL::G2 Poison strength void resistHisto( void ); // Create histogram of HB::G0 Resist poisons void sightHisto( void ); // Create histogram of SN::G5 Sight ability void camoHisto( void ); // Create histogram of HB::G2 Herbivore camouflage void eatHisto( void ); // Create histogram of HB::G1 Eat spines void spineHisto( void ); // Create histogram of PL::G1 Spine probability void seeEnergy( void ); // Show average organism energy level void compare( void ); // Display the gene wars A,B,C void loadSamples( void ); void loadSampTwo( void ); // Fill second sample buffers void convert( void ); void storeGMSamples( void ); // Store 1000 samples of P, H, C population struct pnode // Plant node { struct vt X; // X is an i,j pair based on an SZxSZ grid float energy; // Life force bool alive; // Deva int age; // Metabolize chromosome EN, PL; // EN for energy genes, PL for plant genes chromosome GM; // GM is a genetic marker passed directly from mom }; struct hnode // Herbivore node { struct vt X; // Location int Psense[9], Csense[9]; // Sensor network float energy; // Life force bool alive; // Starve or get eaten? int age; // Metabolize, entropy, senescence chromosome EN, HB; // Energy and herbivore chromosomes chromosome SN, DR; // Sensor and direction chromosomes chromosome GM; // GM is a genetic marker passed directly from mom }; struct cnode // Carnivore node { struct vt X; // 2D location vector int Hsense[9]; // Sensor network float energy; // Chi bool alive; // Energy above zero. int age; // Used in reproduce() chromosome EN, CN; // Energy and carnivore chromosomes chromosome SN, DR; // Sensor and direction chromosomes chromosome GM; // GM is a genetic marker passed directly from mom }; struct hnode2 // Herbivore node { struct vt X; // Location int Psense[9], Csense[9]; // Sensor network float energy; // Life force bool alive; // Starve or get eaten? int age; // Metabolize, entropy, senescence chromosome EN, HB; // Energy and herbivore chromosomes chromosome SN, DR; // Sensor and direction chromosomes chromosome GM; }; struct cnode2 // Carnivore node { struct vt X; // 2D location vector int Hsense[9]; // Sensor network float energy; // Chi bool alive; // Energy above zero. int age; // Used in reproduce() chromosome EN, CN; // Energy and carnivore chromosomes chromosome SN, DR; // Sensor and direction chromosomes chromosome GM; }; // Sample buffers struct pnode *PS = (struct pnode *) malloc( 1000 * 1000 * sizeof( struct pnode )); struct hnode *HS = (struct hnode *) malloc( 1000 * 1000 * sizeof( struct hnode )); struct cnode *CS = (struct cnode *) malloc( 1000 * 1000 * sizeof( struct cnode )); // Second sample buffers struct pnode *PB = (struct pnode *) malloc( 1000 * sizeof( struct pnode )); struct hnode *HB = (struct hnode *) malloc( 1000 * sizeof( struct hnode )); struct cnode *CB = (struct cnode *) malloc( 1000 * sizeof( struct cnode )); // Output sample buffers struct pnode *PL = (struct pnode *) malloc( 1000 * sizeof( struct pnode )); struct hnode2 *HL = (struct hnode2 *) malloc( 1000 * sizeof( struct hnode2 )); struct cnode2 *CL = (struct cnode2 *) malloc( 1000 * sizeof( struct cnode2 )); int Psamp, Hsamp, Csamp, cycle; void metabHisto( void ) { int hist[8]; // Spine histogram float metab; // EN::G5 energy use per cycle float PMoffset = 3.1; float base = PMoffset; // 3.1 to 6.1 3/8 step float step = 0.375; // 3/8 step for(int i=0; i<8; i++) hist[i] = 0; for(int j=0; jEN & 3) + PMoffset; for(int i=0; i<8; i++) // Scan histogram bins { if( (metab > (base + i*step)) && (metab < (base + (i+1)*step)) ) hist[i]++; } } printf("\nView the metabolic rate histogram\n"); for(int i=0; i<8; i++) { printf("%3d ", hist[i]); } printf("\n"); } void absorbPHisto( void ) { int hist[8]; // Spine histogram float absorb; // EN::G4 absorption coefficient float base = 0.0667; // 1/15 float step = 0.0583; // (8/15 - 1/15) /8 for(int i=0; i<8; i++) hist[i] = 0; for(int j=0; jEN & (7 << 7)) >> 7) + 1.0) / 15.0; for(int i=0; i<8; i++) // Scan histogram bins { if( (absorb > (base + i*step)) && (absorb < (base + (i+1)*step)) ) hist[i]++; } } printf("\nView the plant energy absorption coefficient histogram\n"); for(int i=0; i<8; i++) { printf("%3d ", hist[i]); } printf("\n"); } void absorbHHisto( void ) { int hist[8]; // Spine histogram float absorb; // EN::G4 absorption coefficient float base = 1.0/12.0; //0.0667; // 1/15 float step = (7.0/12.0) / 8.0; //0.0583; // (8/15 - 1/15) /8 for(int i=0; i<8; i++) hist[i] = 0; for(int j=0; jEN & (7 << 7)) >> 7) + 1.0) / 12.0; for(int i=0; i<8; i++) // Scan histogram bins { if( (absorb > (base + i*step)) && (absorb < (base + (i+1)*step)) ) hist[i]++; } } printf("\nView the herbivore energy absorption coefficient histogram\n"); for(int i=0; i<8; i++) { printf("%3d ", hist[i]); } printf("\n"); } void absorbCHisto( void ) { int hist[8]; // Spine histogram float absorb; // EN::G4 absorption coefficient float base = 1.0/9.0; // 0.0667; // 1/15 float step = (7.0/9.0) / 8.0; // 0.0583; // (8/15 - 1/15) /8 for(int i=0; i<8; i++) hist[i] = 0; for(int j=0; jEN & (7 << 7)) >> 7) + 1.0) / 9.0; for(int i=0; i<8; i++) // Scan histogram bins { if( (absorb > (base + i*step)) && (absorb < (base + (i+1)*step)) ) hist[i]++; } } printf("\nView the carnivore energy absorption coefficient histogram\n"); for(int i=0; i<8; i++) { printf("%3d ", hist[i]); } printf("\n"); } void spineHisto( void ) { int hist[8]; // Spine histogram float spine; // PL::G1 spine probability float base = 0.0154; // 1/65 float step = 0.1212; // (64/65 - 1/65) /8 for(int i=0; i<8; i++) hist[i] = 0; for(int j=0; jPL & (63 << 18)) >> 18) + 1.0) / 65.0; for(int i=0; i<8; i++) // Scan histogram bins { if( (spine > (base + i*step)) && (spine < (base + (i+1)*step)) ) hist[i]++; } } printf("\nView the spine probability histogram\n"); for(int i=0; i<8; i++) { printf("%3d ", hist[i]); } printf("\n"); } int main( void ) { // loadSamples(); // Into PS, HS, CS buffers (PS +i)->EN, PL, GM // loadGMSamples(); // Into PS etc.. loadSpot(); // Load spot samples into PS, HS, CS printf("On cycle #%d you sampled:\n", cycle ); printf("%d plants, %d herbivores, and %d carnivores\n\n", Psamp, Hsamp, Csamp ); // seePL(); // Display plant genes // seeHB(); // Display herbivore genes // seeGM(); // Examine genetic markers // compareGenes(); seeHBDR(); // Display average herbivore direction probability vector seeCNDR(); // Display average carnivore direction probability vector seeEnergy(); // Show min, max energy values of plants, herbivores, carnivores seeSpeed(); // Display herbivore and carnivore average speeds compare(); // Compare competing genes from spot sample poisonHisto(); // Create poison strength histogram resistHisto(); // Create poison resistance histogram camoHisto(); // Create camouflage effectiveness histogram sightHisto(); // Create visual acuity histogram spineHisto(); // Create spine probability histogram eatHisto(); // Create spine eating ability histogram // metabHisto(); // Create not fine grained enough. absorbPHisto(); // Plant absorption coefficient histogram absorbHHisto(); absorbCHisto(); } /* int main( void ) { loadSamples(); // non GM samples convert(); // Add GM chromosome to herbivores and carnivores storeGMSamples(); // GM samples loadGMSamples(); } */ void seeHBDR( void ) { float dirProb[9]; float sums[9]; // Collect sum of each coordinate of the vector for(int i=1; i<9; i++) sums[i] = 0.0; printf("Do herbivores have any favored directions?\n"); printf(" E NE N NW W SW S SE\n"); for(int i=0; iDR & 7); // SE DR::G8 dirProb[7] = (float) (((HS+i)->DR & (7 << 3)) >> 3); // S dirProb[6] = (float) (((HS+i)->DR & (7 << 6)) >> 6); // SW dirProb[5] = (float) (((HS+i)->DR & (7 << 9)) >> 9); // W dirProb[4] = (float) (((HS+i)->DR & (7 << 12)) >> 12); // NW dirProb[3] = (float) (((HS+i)->DR & (7 << 15)) >> 15); // N dirProb[2] = (float) (((HS+i)->DR & (7 << 18)) >> 18); // NE dirProb[1] = (float) (((HS+i)->DR & (7 << 21)) >> 21); // E DR::G1 // DR::G0 is speed for(int j=1; j<9; j++) tt += dirProb[j]; // Sum probabilities for(int j=1; j<9; j++) if( tt > 0.0 ) // -nan guard dirProb[j] /= tt; // Normalize direction probabilities else dirProb[j] = 0.0; for(int j=1; j<9; j++) sums[j] += dirProb[j]; } for(int j=1; j<9; j++) // E NE N NW W SW S SE TODO printf("%6.3f ", (float) ( sums[j] / Hsamp ) ); printf("\n"); } void seeCNDR( void ) { float dirProb[9]; float sums[9]; // Collect sum of each coordinate of the vector for(int i=1; i<9; i++) sums[i] = 0.0; printf("\n"); printf("Do carnivores have any favored directions?\n"); printf(" E NE N NW W SW S SE\n"); for(int i=0; iDR & 7); // SE DR::G8 dirProb[7] = (float) (((CS+i)->DR & (7 << 3)) >> 3); // S dirProb[6] = (float) (((CS+i)->DR & (7 << 6)) >> 6); // SW dirProb[5] = (float) (((CS+i)->DR & (7 << 9)) >> 9); // W dirProb[4] = (float) (((CS+i)->DR & (7 << 12)) >> 12); // NW dirProb[3] = (float) (((CS+i)->DR & (7 << 15)) >> 15); // N dirProb[2] = (float) (((CS+i)->DR & (7 << 18)) >> 18); // NE dirProb[1] = (float) (((CS+i)->DR & (7 << 21)) >> 21); // E DR::G1 // DR::G0 is speed for(int j=1; j<9; j++) tt += dirProb[j]; // Sum probabilities for(int j=1; j<9; j++) if( tt > 0.0 ) dirProb[j] /= tt; // Normalize direction probabilities else dirProb[j] = 0.0; for(int j=1; j<9; j++) sums[j] += dirProb[j]; } for(int j=1; j<9; j++) // E NE N NW W SW S SE TODO printf("%6.3f ", (float) ( sums[j] / Csamp ) ); printf("\n"); } void seeEnergy( void ) { float PMoffset = 3.1, HMoffset = 4.2, CMoffset = 5.3; float PSoffset = 19.1, HSoffset = 56.1, CSoffset = 8.0; int PAoffset = 1, HAoffset = 1, CAoffset = 3; int PNoffset = 3, HNoffset = 19, CNoffset = 39; // float pmin = 1e5, pmax = -1e5; // float hmin = 1e5, hmax = -1e5; // float cmin = 1e5, cmax = -1e5; int pMin = 5000, pMax = -5000; int page = 0, hage = 0, cage = 0; float metab, absorb, satiety; int maturity, Psenescence, Hsenescence, Csenescence; float sumPsen = 0, sumHsen = 0, sumCsen = 0; float sumMat = 0, sumEnergy = 0, sumEn = 0, sumAbs = 0, sumSat = 0; for(int i=0; iEN & 3) + PMoffset; // EN::G4 7.5 - 10.5 absorb = (float) ((((PS+i)->EN & (7 << 3)) >> 3) + 1.0) / 15.0; // EN::G3 0.71 - 0.53 satiety = (float) (((PS+i)->EN & (63 << 6)) >> 6) + PSoffset; // EN::G2 19.1 - 82.1 maturity = (int) (((PS+i)->EN & (3 << 12)) >> 12) + PAoffset; // EN::G2 1 - 4 Psenescence = (int) (((PS+i)->EN & (15 << 22)) >> 22) + 5; // EN::G0 5 - 12 page += (PS+i)->age; if( (PS+i)->age < pMin ) pMin = (PS+i)->age; if( (PS+i)->age > pMax ) pMax = (PS+i)->age; sumEn += metab; // Metabolic energy requirement sumAbs += absorb; sumSat += satiety; sumMat += maturity; sumPsen += Psenescence; sumEnergy += (PS+i)->energy; // if( (PS+i)->energy < pmin ) pmin = (PS+i)->energy; // From 1e5 // if( (PS+i)->energy > pmax ) pmax = (PS+i)->energy; // From -1e5 } printf("\n"); printf("The average metabolism of plants is %6.4f\n", (float) sumEn/Psamp ); printf("The average absorption coefficient of plants is %6.4f\n", sumAbs/Psamp ); printf("The average energy of plants is %8.4f\n", sumEnergy/Psamp ); printf("The average satiety level of plants is %6.2f\n", sumSat/Psamp ); // printf("The average maturity age of plants %6.2f\n", sumMat/Psamp ); printf("The average plant senescent age is %6.2f\n", sumPsen/Psamp ); printf("Plant minimum age %2d maximum age %2d\n", pMin, pMax ); printf("Plant average age %6.3f\n", (float) page/Psamp ); printf("\n"); int hMin = 5000, hMax = -5000; sumMat = 0, sumEnergy = 0, sumEn = 0, sumAbs = 0, sumSat = 0; for(int i=0; iEN & 7) + HMoffset; // EN::G4 4.2 - 11.2 absorb = (float) ((((HS+i)->EN & (7 << 3)) >> 3) + 1.0) / 10.0; // EN::G3 0.1 - 0.8 satiety = (float) (((HS+i)->EN & (63 << 6)) >> 6) + HSoffset; // EN::G2 56.1 - 119.1 maturity = (int) (((HS+i)->EN & (3 << 12)) >> 12) + 1; // EN::G1 1 - 4 Hsenescence = (int) (((HS+i)->EN & (31 << 15)) >> 15) + HNoffset; // EN::G0 19 - 50 hage += (HS+i)->age; if( (HS+i)->age < hMin ) hMin = (HS+i)->age; if( (HS+i)->age > hMax ) hMax = (HS+i)->age; sumEn += metab; // Metabolic energy requirement sumAbs += absorb; sumSat += satiety; sumMat += maturity; sumHsen += Hsenescence; sumEnergy += (HS+i)->energy; // if( (HS+i)->energy < hmin ) hmin = (HS+i)->energy; // From 1e5 // if( (HS+i)->energy > hmax ) hmax = (HS+i)->energy; // From -1e5 } printf("The average metabolism of herbivores is %6.4f\n", (float) sumEn/Hsamp ); printf("The average absorption coefficient of herbivores is %6.4f\n", sumAbs/Hsamp ); printf("The average energy of herbivores is %8.4f\n", sumEnergy/Hsamp ); printf("The average satiety level of herbivores is %6.2f\n", sumSat/Hsamp ); printf("The average herbivore senescent age is %6.2f\n", sumHsen/Hsamp ); printf("Herbivore minimum age %2d maximum age %2d\n", hMin, hMax ); printf("Herbivore average age %6.2f\n", (float) hage/Hsamp ); printf("\n"); int cMin = 5000, cMax = -5000; sumMat = 0, sumEnergy = 0, sumEn = 0, sumAbs = 0, sumSat = 0; for(int i=0; iEN & 7) + CMoffset; // EN::G4 5.3 - 12.3 absorb = (float) ((((CS+i)->EN & (7 << 3)) >> 3) + 1.0) / 8.0; // EN::G3 0.125 - 1.0 satiety = (float) (((CS+i)->EN & (7 << 6)) >> 6) + CSoffset; // EN::G2 8.0 - 15.0 maturity = (int) (((CS+i)->EN & (7 << 12)) >> 12) + 3; // EN::G1 3 - 10 Csenescence = (int) (((CS+i)->EN & (31 << 15)) >> 15) + CNoffset; // EN::G0 39 - 70 cage += (CS+i)->age; if( (CS+i)->age < cMin ) cMin = (CS+i)->age; if( (CS+i)->age > cMax ) cMax = (CS+i)->age; sumEn += metab; // Metabolic energy requirement sumAbs += absorb; sumSat += satiety; sumMat += maturity; sumCsen += Csenescence; sumEnergy += (CS+i)->energy; // if( (CS+i)->energy < cmin ) cmin = (CS+i)->energy; // From 1e5 // if( (CS+i)->energy > cmax ) cmax = (CS+i)->energy; // From -1e5 } printf("The average metabolism of carnivores is %6.4f\n", (float) sumEn/Csamp ); printf("The average absorption coefficient of carnivores is %6.4f\n", sumAbs/Csamp ); printf("The average energy of carnivores is %8.4f\n", sumEnergy/Csamp ); printf("The average satiety level of carnivores is %6.2f\n", sumSat/Csamp ); printf("The average carnivore senescent age is %6.2f\n", sumCsen/Csamp ); printf("Carnivore minimum age %2d maximum age %2d\n", cMin, cMax ); printf("Carnivore average age %6.2f\n", (float) cage/Csamp ); printf("\n"); } // min = 3; // { min <= speed <= 10 } // Cspeed = (int) (((CS+i)->HB & (7 << 28)) >> 28) + Cmin; // min = 1; // { min <= speed <= 8 } // Hspeed = (int) (((HS+i)->DR & (7 << 28)) >> 28) + Hmin; void seeSpeed( void ) { int Hmin = 1, Cmin = 3; int Hspeed = 0, Cspeed = 0; float Havg, Cavg; for(int i=0; iDR & (7 << 28)) >> 28) + Hmin; } for(int i=0; iDR & (7 << 28)) >> 28) + Cmin; } printf("Average speed of herbivores %6.2f\n", (float) Hspeed/Hsamp); printf("Average speed of carnivores %6.2f\n", (float) Cspeed/Csamp); } void poisonHisto( void ) { int hist[8]; // Poison histogram float strength; // PL::G2 poison strength float base = 0.0154; // 1/65 float step = 0.1212; // (64/65 - 1/65) /8 for(int i=0; i<8; i++) hist[i] = 0; for(int j=0; jPL & (63 << 12)) >> 12) + 1.0) / 65.0; for(int i=0; i<8; i++) // Scan histogram bins { if( (strength > (base + i*step)) && (strength < (base + (i+1)*step)) ) hist[i]++; } } printf("\nView the poison strength histogram\n"); for(int i=0; i<8; i++) { printf("%3d ", hist[i]); } printf("\n"); } void resistHisto( void ) { int hist[8]; // Resistance histogram float resist; // HB::G0 Resist poisons float base = 0.0154; // 1/65 float step = 0.1212; // (64/65 - 1/65) /8 for(int i=0; i<8; i++) hist[i] = 0; for(int j=0; jHB & (63 << 12)) >> 12) + 1.0) / 65.0; for(int i=0; i<8; i++) // Scan histogram bins { if( (resist > (base + i*step)) && (resist < (base + (i+1)*step)) ) hist[i]++; } } printf("\nView the poison resistance histogram\n"); for(int i=0; i<8; i++) { printf("%3d ", hist[i]); } printf("\n"); } void sightHisto( void ) { int hist[8]; // Resistance histogram float see; // SN::G5 sight ability float base = 0.0154; // 1/65 float step = 0.1212; // (64/65 - 1/65) /8 for(int i=0; i<8; i++) // Clear histogram bins hist[i] = 0; for(int j=0; jSN & 63) + 1.0) / 65.0; // Extract gene for(int i=0; i<8; i++) // Fill histogram bins { if( (see > (base + i*step)) && (see < (base + (i+1)*step)) ) hist[i]++; } } printf("\nView the visual acuity histogram\n"); for(int i=0; i<8; i++) { printf("%3d ", hist[i]); } printf("\n"); } void camoHisto( void ) { int hist[8]; // Resistance histogram float camo; // HB::G2 Herbivore camouflage float base = 0.0154; // 1/65 float step = 0.1212; // (64/65 - 1/65) /8 for(int i=0; i<8; i++) hist[i] = 0; for(int j=0; jHB & 63) + 1.0) / 65.0; for(int i=0; i<8; i++) // Scan histogram bins { if( (camo > (base + i*step)) && (camo < (base + (i+1)*step)) ) hist[i]++; } } printf("\nView the camouflage effectiveness histogram\n"); for(int i=0; i<8; i++) { printf("%3d ", hist[i]); } printf("\n"); } void eatHisto( void ) { int hist[8]; // Spine eating histogram float eat; // HB::G1 Eat spines float base = 0.0154; // 1/65 float step = 0.1212; // (64/65 - 1/65) /8 for(int i=0; i<8; i++) hist[i] = 0; for(int j=0; jHB & (63 << 6)) >> 6) + 1.0) / 65.0; for(int i=0; i<8; i++) // Scan histogram bins { if( (eat > (base + i*step)) && (eat < (base + (i+1)*step)) ) hist[i]++; } } printf("\nView the spine eating ability histogram\n"); for(int i=0; i<8; i++) { printf("%3d ", hist[i]); } printf("\n"); } // Check if PS,HS,CS is full when compare() is called inside ecoDraw.c void compare( void ) // Compare competing genes from spot sample { int psum = 0, hsum = 0, ssum = 0, camSum = 0, seeSum = 0, eatSum = 0, page = 0, hage = 0, cage = 0; float pmin = 1e5, pmax = -1e5, hmin = 1e5, hmax = -1e5, cmin = 1e6, cmax = -1e5; for(int i=0; iPL & (63 << 12)) >> 12) + 1.0) / 65.0; if( strength < 0.9846 ) psum++; // PL::G1 spine presence float spine = (float) ((((PS+i)->PL & (63 << 18)) >> 18) + 1.0) / 65.0; if( spine < 0.9846 ) ssum++; page += (PS+i)->age; if( (PS+i)->energy < pmin ) pmin = (PS+i)->energy; if( (PS+i)->energy > pmax ) pmax = (PS+i)->energy; } for(int i=0; iHB & (63 << 12)) >> 12) + 1.0) / 65.0; // HB::G0 if( resist < 0.9846 ) hsum++; hage += (HS+i)->age; if( (HS+i)->energy < hmin ) hmin = (HS+i)->energy; if( (HS+i)->energy > hmax ) hmax = (HS+i)->energy; // HB::G1 ability to eat spines float eat = (float) ((((HS+i)->HB & (63 << 6)) >> 6) + 1.0) / 65.0; // HB::G1 if( eat < 0.9846 ) eatSum++; // HB::G2 camouflage effectiveness float camo = (float) (((HS+i)->HB & 63) + 1.0) / 65.0; // HB::G2 if( camo < 0.9846 ) camSum++; } for(int i=0; iSN & 63) + 1.0) / 65.0; // {0.0156 .. 0.9846} if( see < 0.9846 ) seeSum++; cage += (CS+i)->age; if( (CS+i)->energy < cmin ) cmin = (CS+i)->energy; if( (CS+i)->energy > cmax ) cmax = (CS+i)->energy; } // printf("Plant average age %6.3f\n", (float) page/Psamp ); printf("Plant minimum energy %6.2f maximum energy %8.2f\n", pmin, pmax ); // printf("Herbivore average age %6.2f\n", (float) hage/Hsamp ); printf("Herbivore minimum energy %6.2f maximum energy %8.2f\n", hmin, hmax ); // printf("Carnivore average age %6.2f\n", (float) cage/Csamp ); printf("Carnivore minimum energy %6.2f maximum energy %8.2f\n", cmin, cmax ); printf("\n"); printf("%6.2f%% of plants' poison level is under 98.46%%\n", 100 * (float) psum/Psamp ); printf("%6.2f%% of herbivores' poison resistance is under 98.46%%\n", 100 * (float) hsum/Hsamp ); printf("%6.2f%% of herbivores' camouflage is worse than 98.46%% effective\n", 100 * (float) camSum/Hsamp ); printf("%6.2f%% of carnivores have sight worse than 98.46%%\n", 100 * (float) seeSum/Csamp ); printf("%6.2f%% of plants having spines is under 98.46%%\n", 100 * (float) ssum/Psamp ); printf("%6.2f%% of herbivores ability to eat spines is under 98.46%%\n", 100 * (float) eatSum/Hsamp ); } void compareGenes( void ) // Compare competing genes from random sample { int psum = 0, hsum = 0; int camSum = 0, seeSum = 0; for(int i=0; i<1000; i++) { // PL::G2 poison strength float strength = (float) ((((PS+i)->PL & (63 << 12)) >> 12) + 1.0) / 65.0; if( strength < 0.9846 ) psum++; } for(int i=0; i<1000; i++) { // HB::G0 resist poison float resist = (float) ((((HS+i)->HB & (63 << 12)) >> 12) + 1.0) / 65.0; // HB::G0 if( resist < 0.9846 ) hsum++; // HB::G2 camouflage effectiveness float camo = (float) (((HS+i)->HB & 63) + 1.0) / 65.0; // HB::G2 if( camo < 0.9846 ) camSum++; } for(int i=0; i<1000; i++) { // SN::G4 sight ability float see = (float) (((CS+i)->SN & 63) + 1.0) / 65.0; // {0.0156 .. 0.9846} if( see < 0.9846 ) seeSum++; } printf("%6.2f%% of plants' poison level is under 98.46%%\n", 100 * (float) psum/1000 ); printf("%6.2f%% of herbivores' poison resistance is under 98.46%%\n", 100 * (float) hsum/1000 ); printf("%6.2f%% of herbivores' camouflage is worse than 98.46%% effective\n", 100 * (float) camSum/1000 ); printf("%6.2f%% of carnivores have sight worse than 98.46%%\n", 100 * (float) seeSum/1000 ); } void displayBits( chromosome seq ) { int j=0; chromosome mask = 1 << 31; // Shift mask all the way left for(int i=0; i<32; i++) { if( seq & mask ) // Compare mask bit to present bit printf("1"); else printf("0"); if( j++ == 3 ) // Separate into bit quartets { printf(" "); // Green bar print out j=0; } seq <<= 1; // Shift sequence to the left } // printf("\n"); printf(" "); } void loadGMSamples( void ) { FILE *fp; // fp = fopen( "sampleNGM.dat", "r" ); fp = fopen( "sample9GM.dat", "r" ); for(int i=0; i<1000; i++) fscanf(fp, "%u %u %u %f %d", &(PS+i)->EN, &(PS+i)->PL, &(PS+i)->GM, &(PS+i)->energy, &(PS+i)->age ); for(int i=0; i<1000; i++) fscanf(fp, "%u %u %u %u %u %f %d", &(HS+i)->EN, &(HS+i)->SN, &(HS+i)->HB, &(HS+i)->DR, &(HS+i)->GM, &(HS+i)->energy, &(HS+i)->age ); for(int i=0; i<1000; i++) fscanf(fp, "%u %u %u %u %u %f %d", &(CS+i)->EN, &(CS+i)->SN, &(CS+i)->CN, &(CS+i)->DR, &(CS+i)->GM, &(CS+i)->energy, &(CS+i)->age ); fclose( fp ); } void loadSpot( void ) { // int Psamp, Hsamp, Csamp; FILE *fp = fopen( "spot.dat", "r" ); fscanf( fp, "%d", &cycle ); fscanf( fp, "%d %d %d", &Psamp, &Hsamp, &Csamp ); for(int i=0; iEN, &(PS+i)->PL, &(PS+i)->GM, &(PS+i)->energy, &(PS+i)->age ); for(int i=0; iEN, &(HS+i)->SN, &(HS+i)->HB, &(HS+i)->DR, &(HS+i)->GM, &(HS+i)->energy, &(HS+i)->age ); for(int i=0; iEN, &(CS+i)->SN, &(CS+i)->CN, &(CS+i)->DR, &(CS+i)->GM, &(CS+i)->energy, &(CS+i)->age ); fclose( fp ); } void seeEN( void ) { float energy, absorb, satiety; int maturity, Psenescence; float PSoffset = 19.1, PMoffset = 7.5; int PAoffset = 3, PNoffset = 3; printf("Report energy levels\n"); printf("Psensescence, maturity, satiety, absorb, energy\n"); // 3b:3b:6b:3b:5b = 20b Decryption for Plant Energy chromosome for(int i=800; i<1000; i++) { displayBits( (PS+i)->EN ); energy = (float) ((PS+i)->EN & 7) + PMoffset; // metab // EN::G4 absorb = (float) ((((PS+i)->EN & (7 << 7)) >> 7) + 1.0) / 15.0; // EN::G3 satiety = (float) (((PS+i)->EN & (63 << 10)) >> 10) + PSoffset; // EN::G2 maturity = (int) (((PS+i)->EN & (7 << 19)) >> 19) + PAoffset; // EN::G1 Psenescence = (int) (((PS+i)->EN & (7 << 22)) >> 22) + PNoffset; // EN::G0 printf("%3d %3d %8.4f %8.4f %8.4f\n", Psenescence, maturity, satiety, absorb, energy ); } } void seePL( void ) { float strength, spine; printf("How defensive are my plants?\n"); printf("spine, strength\n"); for(int i=100; i<200; i++) { displayBits( (PS+i)->PL ); strength = (float) ((((PS+i)->PL & (63 << 12)) >> 12) + 1.0) / 65.0; // PL::G2 poison strength spine = (float) ((((PS+i)->PL & (63 << 18)) >> 18) + 1.0) / 65.0; // PL::G1 spine probability printf("%8.4f %8.4f\n", spine, strength ); } } void seeHB( void ) { float camo, eat, resist; int speed; int sum = 0, poisonSum = 0, spineSum = 0; printf("Herbivore genes exposed!\n"); printf("speed, resist, eat, camo\n"); for(int i=0; i<20; i++) { displayBits( (HS+i)->HB ); // camo = (float) (((HS+i)->HB & (31 << 10)) >> 10) / 31.0; // HB::G4 camouflage effectiveness // camo = (float) (((HS+i)->HB & (31 << 10)) >> 10) / 50.0; // Herbivore camouflage in eat() camo = (float) (((HS+i)->HB & 63) + 1.0) / 65.0; // {0.0156 .. 0.9846} // HB::G2 // I think camo should be less fine. 5 bit or even 4 bits would be enough // then 1/17 .. 16/17 0.04882 0.9412 // eat = (float) (((HS+i)->HB & (31 << 15)) >> 15) / 31.0; // HB::G3 ability to eat spines // resist = (float) (((HS+i)->HB & (31 << 20)) >> 20)/31.0; // HB::G2 resist poison eat = (float) ((((HS+i)->HB & (63 << 6)) >> 6) + 1.0) / 65.0; // HB::G1 resist = (float) ((((HS+i)->HB & (63 << 12)) >> 12) + 1.0) / 65.0; // HB::G0 // HB::G2 escape range is unused. So we have the top seven bits to play with. // speed = (int) ((HS+i)->HB & (3 << 30)) >> 30; // HB::G0 speed gene 2 b // speed = (int) (((HS+i)->HB & (15 << 27)) >> 27) + 12; // in eat() 4b not 2b speed = (int) (((HL+i)->DR & (15 << 24)) >> 24) + 12; // DR::G0 moved 1-1-25 4b // speed = (int) (((HL+i)->HB & (7 << 28)) >> 28) + min; // in moveH() min = 1 // speed = (int) (((CL+i)->CN & (7 << 28)) >> 28) + min; // in moveC() min = 3 // speed = (int) (((CL+i)->CN & (15 << 27)) >> 27) + 12; // in eat() CN::G1 // One of these days PL, HB, and CN need to be revamped. // What gets used and what is needed? printf("%d %8.4f %8.4f %8.4f\n", speed, resist, eat, camo ); } printf("\n"); for(int i=0; i<1000; i++) { sum++; eat = (float) ((((HS+i)->HB & (63 << 6)) >> 6) + 1.0) / 65.0; // HB::G1 resist = (float) ((((HS+i)->HB & (63 << 12)) >> 12) + 1.0) / 65.0; // HB::G0 if( eat > 0.98 ) spineSum++; if( resist > 0.98 ) poisonSum++; } printf("%8.4f of herbivores ability to eat spines is above 0.98\n", (float) spineSum/1000.0 ); printf("%8.4f of herbivores ability to eat poison is above 0.98\n", (float) poisonSum/1000.0 ); printf("%d %d\n", spineSum, poisonSum ); } void seeSN( void ) { float see; printf("How well can your carnivore see?\n"); for(int i=0; i<20; i++) { displayBits( (CS+i)->SN ); // see = (float) ((CS+i)->SN & 255) / 255.0; // SN::G4 sight ability see = (float) (((CS+i)->SN & 63) + 1.0) / 65.0; // {0.0156 .. 0.9846} printf("%6.2f\n", see ); } } void seeCN( void ) { int speed; printf("How fast are my cheetahs?\n"); for(int i=0; i<20; i++) { displayBits( (CS+i)->CN ); // speed = (int) (((CS+i)->CN & (15 << 27)) >> 27) + 12; // CN::G0 12 - 27 speed = (int) (((HL+i)->DR & (15 << 24)) >> 24) + 12; // DR::G0 moved 1-1-25 4b printf("%d\n", speed ); } } void seeGM( void ) { int sum = 0, msum = 0; int psum = 0, hsum = 0, csum = 0; /* printf("Have I the mark of GM upon me?\n"); for(int i=20; i<30; i++) { displayBits( (CS+i)->GM ); printf("\n"); } */ for(int i=0; i<1000; i++) if( (PS+i)->GM == 3855 ) psum++; for(int i=0; i<1000; i++) if( (HS+i)->GM == 61680 ) hsum++; for(int i=0; i<1000; i++) { if( (CS+i)->GM == 61680 ) csum++; // if( (PS+i)->GM != 0 ) psum++; // if( (HS+i)->GM != 0 ) hsum++; // if( (CS+i)->GM != 0 ) csum++; } msum = psum + hsum + csum; printf("%9.6f of the organisms are marked.\n", (float) msum/3000 ); sum /= 3; printf("%8.5f %% of the plants are marked.\n", 100* (float) psum/1000 ); printf("%7.4f of the herbivores are marked.\n", (float) hsum/1000 ); printf("%7.4f of the carnivores are marked.\n", (float) csum/1000 ); } void loadSamples( void ) { FILE *fp; // fp = fopen( "sampleN.dat", "r" ); fp = fopen( "sample9.dat", "r" ); for(int i=0; i<1000; i++) { fscanf(fp, "%u %u %u %f %d", &(PS+i)->EN, &(PS+i)->PL, &(PS+i)->GM, &(PS+i)->energy, &(PS+i)->age ); } for(int i=0; i<1000; i++) fscanf(fp, "%u %u %u %u %f %d", &(HS+i)->EN, &(HS+i)->SN, &(HS+i)->HB, &(HS+i)->DR, &(HS+i)->energy, &(HS+i)->age ); for(int i=0; i<1000; i++) fscanf(fp, "%u %u %u %u %f %d", &(CS+i)->EN, &(CS+i)->SN, &(CS+i)->CN, &(CS+i)->DR, &(CS+i)->energy, &(CS+i)->age ); fclose( fp ); } void loadSampTwo( void ) // Fill second sample buffers { FILE *fp; fp = fopen( "sample9.dat", "r" ); // Naming scheme ?? Hmm... for(int i=0; i<1000; i++) fscanf(fp, "%u %u %u %f %d", &(PB+i)->EN, &(PB+i)->PL, &(PB+i)->GM, &(PB+i)->energy, &(PB+i)->age ); for(int i=0; i<1000; i++) fscanf(fp, "%u %u %u %u %f %d", &(HB+i)->EN, &(HB+i)->SN, &(HB+i)->HB, &(HB+i)->DR, &(HB+i)->energy, &(HB+i)->age ); for(int i=0; i<1000; i++) fscanf(fp, "%u %u %u %u %f %d", &(CB+i)->EN, &(CB+i)->SN, &(CB+i)->CN, &(CB+i)->DR, &(CB+i)->energy, &(CB+i)->age ); fclose( fp ); } void convert( void ) { for(int i=0; i<1000; i++) { (PL+i)->EN = (PS+i)->EN; (PL+i)->PL = (PS+i)->PL; // (PL+i)->GM = (PS+i)->GM; (PL+i)->energy = (PS+i)->energy; (PL+i)->age = (PS+i)->age; (HL+i)->EN = (HS+i)->EN; (HL+i)->SN = (HS+i)->SN; (HL+i)->HB = (HS+i)->HB; (HL+i)->DR = (HS+i)->DR; (HL+i)->energy = (HS+i)->energy; (HL+i)->age = (HS+i)->age; (CL+i)->EN = (CS+i)->EN; (CL+i)->SN = (CS+i)->SN; (CL+i)->CN = (CS+i)->CN; (CL+i)->DR = (CS+i)->DR; (CL+i)->energy = (CS+i)->energy; (CL+i)->age = (CS+i)->age; } } void storeGMSamples( void ) // Store 1000 samples of P, H, C population { FILE *fp; // fp = fopen( "sampleNGM.dat", "w" ); fp = fopen( "sample9GM.dat", "w" ); for(int i=0; i<1000; i++) { (PL+i)->GM = 0; fprintf(fp, "%u %u %u %f %d\n", (PL+i)->EN, (PL+i)->PL, (PL+i)->GM, (PL+i)->energy, (PL+i)->age ); } for(int i=0; i<1000; i++) { (HL+i)->GM = 0; fprintf(fp, "%u %u %u %u %u %f %d\n", (HL+i)->EN, (HL+i)->SN, (HL+i)->HB, (HL+i)->DR, (HL+i)->GM, (HL+i)->energy, (HL+i)->age ); } for(int i=0; i<1000; i++) { (CL+i)->GM = 0; fprintf(fp, "%u %u %u %u %u %f %d\n", (CL+i)->EN, (CL+i)->SN, (CL+i)->CN, (CL+i)->DR, (CL+i)->GM, (CL+i)->energy, (CL+i)->age ); } fclose( fp ); } //////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// #ifdef NOTES PL::G2 < 0.9846 and CL::G2 < 0.9846 0.9846 is MAX // PL::G2 poison strength (PS+i)->PL strength = (float) ((((PS+i)->PL & (63 << 12)) >> 12) + 1.0) / 65.0; // HB::G0 resist poison (HS+i)->HB resist = (float) ((((HS+i)->HB & (63 << 12)) >> 12) + 1.0) / 65.0; Now compare two genes which are not being expressed: PL::G1 vs HB::G1 spine::eat float strength = (float) ((((PS+i)->PL & (63 << 12)) >> 12) + 1.0) / 65.0; // PL::G2 float spine = (float) ((((PS+i)->PL & (63 << 18)) >> 18) + 1.0) / 65.0; // PL::G1 float camo = (float) (((HS+i)->HB & 63) + 1.0) / 65.0; // HB::G2 float eat = (float) ((((HS+i)->HB & (63 << 6)) >> 6) + 1.0) / 65.0; // HB::G1 float resist = (float) ((((HS+i)->HB & (63 << 12)) >> 12) + 1.0) / 65.0; // HB::G0 #endif #ifdef NOTES Sensor genes: ->SN in herbivores and carnivores 8 bits sight camouflage effectiveness would be tested here 8 bits hearing hear predator or prey 8 bits smell smell poison or prey 32-24 = 8 4 bits taste taste poison, touch spines 4 bits touch pursue vibration of ground?? or hide if sensed 2^4 = 16 (HL+i)->SN for sensor genes { sight, hearing, smell, taste, touch } 8b:8b:8b:4b:4b } (PL+i)->EN for energy genes { metabolism, absorption, satiety, maturity, senescence } 7b:3b:9b:3b:10b from geneCruft.c Now: 3b:3b:6b:3b:5b so offset by 7,10,19,22 is correct but your masks will be 7, 7, 63, 7, 31 // float PMoffset = 7.5, HMoffset = 4.2, CMoffset = 5.3; // EN::G4 7.5 - 14.5 (PL+i)->energy -= (float) ((PL+i)->EN & 7) + PMoffset; // EN::G4 7.5 - 14.5 (HL+i)->energy -= (float) ((HL+i)->EN & 7) + HMoffset; // EN::G4 4.2 - 11.2 (CL+i)->energy -= (float) ((CL+i)->EN & 7) + CMoffset; // EN::G4 5.3 - 12.3 absorb = (float) ((((PL+i)->EN & (7 << 7)) >> 7) + 1.0) / 15.0; // EN::G3 0.71 - 0.53 absorb = (float) ((((HL+i)->EN & (7 << 7)) >> 7) + 1.0) / 10.0; // EN::G3 0.1 - 0.8 absorb = (float) ((((CL+i)->EN & (7 << 7)) >> 7) + 1.0) / 8.0; // EN::G3 0.125 - 1.0 // float PSoffset = 19.1, HSoffset = 56.1, CSoffset = 8.0; satiety = (float) (((PL+i)->EN & (63 << 10)) >> 10) + PSoffset; // EN::G2 19.1 - 82.1 satiety = (float) (((HL+i)->EN & (63 << 10)) >> 10) + HSoffset; // EN::G2 56.1 - 119.1 satiety = (float) (((CL+i)->EN & (7 << 10)) >> 10) + CSoffset; // EN::G2 8.0 - 15.0 // int PAoffset = 3, HAoffset = 1, CAoffset = 3; maturity = (int) (((PL+i)->EN & (7 << 19)) >> 19) + PAoffset; // EN::G1 3 - 10 maturity = (int) (((HL+i)->EN & (3 << 19)) >> 19) + HAoffset; // EN::G1 1 - 4 maturity = (int) (((CL+i)->EN & (7 << 19)) >> 19) + CAoffset; // EN::G1 3 - 10 // int PNoffset = 3, HNoffset = 19, CNoffset = 39; Psenescence = (int) (((PL+i)->EN & (7 << 22)) >> 22) + PNoffset; // EN::G0 3 - 10 Hsenescence = (int) (((HL+i)->EN & (31 << 22)) >> 22) + HNoffset; // EN::G0 19 - 50 Csenescence = (int) (((CL+i)->EN & (31 << 22)) >> 22) + CNoffset; // EN::G0 39 - 70 (PL+i)->PL for plant genes { pollination%, poison%, poison strength, spine%, spine length } 6b:6b:6b:6b:2b strength = (float) ((P->PL & (63 << 12)) >> 12) / 94.0; // PL::G3 poison strength spine = (float) ((((PS+i)->PL & (63 << 18)) >> 18) + 1.0) / 65.0; // PL::G1 spine probability (HL+i)->HB for herbivore genes { run%, hide%, camo%, eat spine%, eat poison% } 5b:5b:5b:5b:5b:5b:2b (HL+i)->HB for herbivore genes { resist, eat, camo } // 3 x 6b = 18 bits Rebuild for more later. 6b:6b:6b 18 bits total for the HB chromosome float camo = (float) (((HS+i)->HB & 63) + 1.0) / 65.0; // HB::G2 camouflage effectiveness float eat = (float) ((((HS+i)->HB & (63 << 6)) >> 6) + 1.0) / 65.0; // HB::G1 ability to eat spines float resist = (float) ((((HS+i)->HB & (63 << 12)) >> 12) + 1.0) / 65.0; // HB::G0 resist poison (int) speed = (int) (((HL+i)->DR & (15 << 24)) >> 24) + 12; DR::G0 moved 1-1-25 4b 1/17 = 0.058 16/17 = 0.941 1/33 = 0.0303 32/33 = 0.970 camo = (HB + 1.0) / 33.0 see = (SN + 1.0) / 256.0?? or 33.0? 1/65 = 0.0154 64/65 = 0.985 see = (CN::SN + 1.0) / 65.0 (CL+i)->CN for carnivore genes { attack%, attack range, sleep%, hide%, camo%, pursuit time } 6b:2b:6b:6b:6b:3b:2b //speed = (int) (((CL+i)->CN & (15 << 27)) >> 27) + 12; // CN::G0 12 - 27 speed = (int) (((HL+i)->DR & (15 << 24)) >> 24) + 12; DR::G0 moved 1-1-25 4b (HL+i)->SN for sensor genes { touch range, hearing range, seeing range, smelling range } 8b:8b:8b:4b:4b see = (float) ((CL+i)->SN & 255) / 255.0; // SN::G4 sight ability (CL+i)->DR for direction genes { E, W, N, S, NE, SE, NW, SW } 4b:4b:4b:4b:4b:4b:4b:4b dirProb[8] = (float) (DR & 15); // SE DR::G7 dirProb[7] = (float) ((DR & (15 << 4)) >> 4); // S dirProb[6] = (float) ((DR & (15 << 8)) >> 8); // SW dirProb[5] = (float) ((DR & (15 << 12)) >> 12); // W dirProb[4] = (float) ((DR & (15 << 16)) >> 16); // NW dirProb[3] = (float) ((DR & (15 << 20)) >> 20); // N dirProb[2] = (float) ((DR & (15 << 24)) >> 24); // NE dirProb[1] = (float) ((DR & (15 << 28)) >> 28); // E DR::G0 FILE *file = fopen("example.txt", "r"); if (file == NULL) { #endif #ifdef NOTES You need to display the GM chromosome. Then you can test matureNutrients() to see how well the random plants are doing against those in 'sample9.dat' camo = (float) (((HS+i)->HB & (31 << 10)) >> 10) / 50.0; // HB::G4 Herbivore camouflage see = (float) ((CS+i)->SN & 255) / 255.0; // SN::G4 sight ability camo = (float) ((((HS+i)->HB & (63 << 10)) >> 10) + 1.0) / 65.0; // 0.9524 61,0.9683 62,0.9841 see = (float) (((CS+i)->SN & 63) + 1.0) / 65.0; {1/65 = 0.0154 .. 64/65 = 0.9846} 1/17 = 0.058 16/17 = 0.941 1/33 = 0.0303 32/33 = 0.970 1/65 = 0.0154 64/65 = 0.985 #endif #ifdef NOTES I want to make a genetic similarity metric. If I convert chromosomes into vectors from their genes I can compare two vectors with their dot product. If the dot produst is 1.0 they are colinear. G[i] = (EN & (PLEN.mask[i] << PLEN.shift[i])) >> PLEN.shift[i]; gene[i] = (chromosome & (gmap.mask[i] << gmap.shift[i])) >> gmap.shift[i] ; Build a function to do this. DONE. Then add offset and divisor if not zero chromosome gene[10]; // float gene[10]; struct gmap PLEN; int ct = 5; void function( chromosome EN, struct gmap PN, int ct, chromosome *gene) { for(int i=0; i> PN.shift[i]; } Now gene[i] holds the raw data from the chromosome. We need to normalize the information. New array - float v[10]; v or something more interesting? descriptive? for(int i=0; i #include #include struct gmap { int mask[8], shift[8]; }; // Add offset and divisor TYPE?? typedef unsigned int chromosome; chromosome gene[10]; // 10 * 3b = 30b 10 genes with 8 steps of variability struct gmap PLEN, HLEN, CLEN; void displayBits( chromosome ); void extract( chromosome EN, struct gmap PN, int ct, chromosome *gene) { for(int i=0; i> PN.shift[i]; } // + PN.offset[i] ) / PN.divisor[i]; if divisor != 0 float float G[10]; void normalGenes( chromosome *gene, float *G ) { float tt = 0; for(int i=1; i<10; i++) { tt += gene[i]; // Sum gene values G[i] = gene[i]; // Copy to vector } for(int i=1; i<10; i++) // Normalize gene vector G[i] /= tt; } #ifdef NOTES We could employ two genes to express one trait. Say PL::G1 is 4 bits wide and PL::G9 is 6 bits wide Combine them like so: PL::G1 + PL::G9 / 64.0; Start with an integer from 0 to 15 add a factor from 0.0 to 0.984375 Now where could I use something like this? Or do I set them both as integers and add them PL::G1 + PL::G9 where they are both four bit numbers So 0 to 15 plus 0 to 15 for 0 to 30 possible #endif int main( void ) { chromosome G[10]; float absorb, metab, satiety; int maturity, senescence; srand( (unsigned) time( NULL ) ); chromosome EN = rand(); // RAND_MAX; // rand(); // I should use pollination. // I do use poison strength and spine %. // I don't need poison % or spine length. // We could have a time factor. cut off a number each cycle. // Sleep is good. If sated or sleep is over 50 to 60% then no eating. // Eating range could be used in eat(), the number of rings outward. // Choices controls the number of random selections eat() makes. // Camouflage seems useless unless herbivores can 'see' carnivores // Hmm.. // Rebuild PL to have 6b:6b:6b for pollination, poison strength, spine %. struct gmap PL; PL.mask[2] = 63; PL.shift[2] = 0; // pollination % - poor, then better, then max? PL.mask[1] = 63; PL.shift[1] = 6; // poison strength ** PL.mask[0] = 63; PL.shift[0] = 12; // spine % struct gmap SN; SN.mask[4] = 255; SN.shift[4] = 0; // sight ** see herbie see carnie SN.mask[3] = 255; SN.shift[3] = 8; // hearing hear a carnivore? SN.mask[2] = 255; SN.shift[2] = 16; // scent smell carnivore or plant?? SN.mask[1] = 15; SN.shift[1] = 24; // taste taste poison?? SN.mask[0] = 15; SN.shift[0] = 28; // touch touch spine?? struct gmap HB; // 3 x 6b = 18 bits Rebuild for more later. HB.mask[2] = 63; HB.shift[2] = 0; // camouflage effectiveness HB.mask[1] = 63; HB.shift[1] = 6; // Ability to eat spiny plants HB.mask[0] = 63; HB.shift[0] = 12; // Ability to resist plant poison struct gmap CN; // What traits can a carnivore employ for survival? CN.mask[3] = 63; CN.shift[3] = 0; // eating range I used 4 bits for 15 CN.mask[2] = 63; CN.shift[2] = 6; // # choices in eat CN.mask[1] = 63; CN.shift[1] = 12; // sleep CN.mask[0] = 63; CN.shift[0] = 18; // camouflage struct gmap DR; DR.mask[8] = 7; DR.shift[8] = 0; // SE ** DR.mask[7] = 7; DR.shift[7] = 3; // S ** DR.mask[6] = 7; DR.shift[6] = 5; // SW ** DR.mask[5] = 7; DR.shift[5] = 9; // W ** DR.mask[4] = 7; DR.shift[4] = 12; // NW ** DR.mask[3] = 7; DR.shift[3] = 15; // N ** DR.mask[2] = 7; DR.shift[2] = 18; // NE ** DR.mask[1] = 7; DR.shift[1] = 21; // E ** DR.mask[0] = 15; DR.shift[0] = 24; // Speed ** PLEN.mask[0] = 7; PLEN.shift[0] = 15; // Senescence ** PLEN.mask[1] = 7; PLEN.shift[1] = 12; // Maturity ** PLEN.mask[2] = 63; PLEN.shift[2] = 6; // Satiety ** PLEN.mask[3] = 7; PLEN.shift[3] = 3; // Absorption ** PLEN.mask[4] = 7; PLEN.shift[4] = 0; // Metabolism ** HLEN.mask[0] = 31; HLEN.shift[0] = 15; HLEN.mask[1] = 3; HLEN.shift[1] = 12; HLEN.mask[2] = 63; HLEN.shift[2] = 6; HLEN.mask[3] = 7; HLEN.shift[3] = 3; HLEN.mask[4] = 7; HLEN.shift[4] = 0; CLEN.mask[0] = 31; CLEN.shift[0] = 15; CLEN.mask[1] = 7; CLEN.shift[1] = 12; CLEN.mask[2] = 7; CLEN.shift[2] = 6; CLEN.mask[3] = 7; CLEN.shift[3] = 3; CLEN.mask[4] = 7; CLEN.shift[4] = 0; // float PMoffset = 7.5, HMoffset = 4.2, CMoffset = 5.3; // EN::G5 7.5 - 14.5 // (PL+i)->energy -= (float) ((PL+i)->EN & 7) + PMoffset; // EN::G5 7.5 - 14.5 for(int i=0; i<5; i++) { // Extract masked portion G[i] = EN & (PLEN.mask[i] << PLEN.shift[i]); displayBits( G[i] ); // Extract raw data by shifting back to zero again G[i] = (EN & (PLEN.mask[i] << PLEN.shift[i])) >> PLEN.shift[i]; displayBits( EN ); displayBits( G[i] ); printf("\n"); } for(int i=4; i>-1; i--) printf("%u ", G[i] ); printf("\n"); // Convert raw data into a floating point factor metab = (float) G[4] + 7.5; // PMoffset absorb = (float) (G[3] + 1.0 ) / 15.0; // Store offset, type, and divisor?? TODO satiety = (float) G[2] + 19.1; // PSoffset maturity = (int) G[1] + 3; senescence = (int) G[0] + 3; // PNoffset printf("metab absorb satiety maturity senescence\n"); // 1/15 to 8/15ths 0.07 .. 0.53 printf("%6.2f \t%4.2f \t%6.2f %d \t\t%d\n", metab, absorb, satiety, maturity, senescence ); extract( EN, PLEN, 5, gene ); printf("PLEN "); for(int i=0; i<5; i++) printf("G%1d %2u ", i, gene[i] ); printf("\n"); extract( rand(), HLEN, 5, gene ); printf("HLEN "); for(int i=0; i<5; i++) printf("G%1d %2u ", i, gene[i] ); printf("\n"); extract( EN, CLEN, 5, gene ); printf("CLEN "); for(int i=0; i<5; i++) printf("G%1d %2u ", i, gene[i] ); printf("\n"); extract( EN, DR, 8, gene ); printf("DR "); for(int i=0; i<8; i++) printf("G%1d %3u ", i, gene[i] ); printf("\n"); extract( EN, HB, 7, gene ); printf("HB "); for(int i=0; i<7; i++) printf("G%1d %3u ", i, gene[i] ); printf("\n"); extract( EN, SN, 5, gene ); printf("SN "); for(int i=0; i<5; i++) printf("G%1d %3u ", i, gene[i] ); printf("\n"); } void displayBits( chromosome seq ) // Chromosome bit sequence { int j=0; chromosome mask = 1 << 31; // Shift mask all the way left for(int i=0; i<32; i++) { if( seq & mask ) // Compare mask bit to sequence bit printf("1"); else printf("0"); if( j++ == 3 ) // Separate into bit quartets { printf(" "); // Green bar print out j=0; } seq <<= 1; // Shift sequence to the left } printf("\n"); } #endif #ifdef NOTES Now compare two genes which are not being expressed: PL::G1 vs HB::G1 spine::eat float strength = (float) ((((PS+i)->PL & (63 << 12)) >> 12) + 1.0) / 65.0; // PL::G2 float spine = (float) ((((PS+i)->PL & (63 << 18)) >> 18) + 1.0) / 65.0; // PL::G1 float camo = (float) (((HS+i)->HB & 63) + 1.0) / 65.0; // HB::G2 float eat = (float) ((((HS+i)->HB & (63 << 6)) >> 6) + 1.0) / 65.0; // HB::G1 float resist = (float) ((((HS+i)->HB & (63 << 12)) >> 12) + 1.0) / 65.0; // HB::G0 struct gmap HB; HB.mask[2] = 63; HB.shift[2] = 0; // camouflage effectiveness HB.mask[1] = 63; HB.shift[1] = 6; // Ability to eat spiny plants HB.mask[0] = 63; HB.shift[0] = 12; // Ability to resist plant poison #endif #ifdef NOTES Bicubic Interpolation of RGB Pixels involves calculating the color value of each new pixel based on a weighted average of the 16 nearest neighboring pixels in a 4x4 grid. Here is a detailed breakdown of the process: 1) Identify the Pixel to be Interpolated: Determine the location of the new pixel in the resized image. 2) Determine the Neighborhood: Locate the 4x4 grid of pixels surrounding the new pixel in the original image. This grid includes the pixel itself and its eight neighbors. 3) Calculate the Weights: For each of the 16 pixels in the neighborhood, calculate a weight based on the distance from the target pixel. Closer pixels are given higher weights. 4) Interpolate the Pixel Value: Compute the new pixel color value by summing the product of each pixel color value and its corresponding weight. This sum is then divided by the sum of all weights to get the final interpolated value. This process is applied separately for each of the R, G, and B color channels. Repeat for All Pixels: Repeat steps 1-4 for every pixel in the new image. This method ensures that the resized image maintains smooth transitions and preserves details, making it a preferred choice for high-quality image resizing tasks. #endif #ifdef NOTES Create a plant poison histogram strength = (float) ((((PL+i)->PL & (63 << 12)) >> 12) + 1.0) / 65.0; // PL::G2 poison strength 1/65 = 0.0154 64/65 = 0.9846 for a range of 0.9692 /8 = 0.1212 sized bins 0.0154 to 0.1212+0.0154 #endif