Weasel

A forum for learning, discussing, developing, and identifying top quality science materials -- physics, chemistry, mathematics, engineering, computer science, geology, biology, etc. Being versant in these areas will help make one versant in exploration of ID and Creation Science.

Weasel

Postby stcordova » Sun Feb 14, 2016 10:31 pm

deleted
Last edited by stcordova on Mon Feb 15, 2016 12:30 am, edited 1 time in total.
stcordova
 
Posts: 447
Joined: Wed Mar 05, 2014 1:41 am

Re: Weasel

Postby stcordova » Mon Feb 15, 2016 12:09 am

deleted
Last edited by stcordova on Mon Feb 15, 2016 12:30 am, edited 1 time in total.
stcordova
 
Posts: 447
Joined: Wed Mar 05, 2014 1:41 am

Re: Weasel

Postby stcordova » Mon Feb 15, 2016 12:16 am

deleted
Last edited by stcordova on Mon Feb 15, 2016 12:31 am, edited 1 time in total.
stcordova
 
Posts: 447
Joined: Wed Mar 05, 2014 1:41 am

Re: Weasel

Postby stcordova » Mon Feb 15, 2016 12:21 am

deleted
Last edited by stcordova on Mon Feb 15, 2016 12:31 am, edited 1 time in total.
stcordova
 
Posts: 447
Joined: Wed Mar 05, 2014 1:41 am

Re: Weasel

Postby stcordova » Mon Feb 15, 2016 12:23 am

Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ITERATION_LIMIT (1*1000)
#define NUMBER_KIDS 25

char *target_string = "METHINKS IT IS LIKE A WEASEL";
int target_strlen =0;
char *kid[NUMBER_KIDS];
int target_hit = 0;

char random_printable_ascii()
   {
    /* use capital letters starting at ascii 65 = A*/
    int random_ascii_number = rand() % (65+26-32)+32;
    return (char) random_ascii_number;
   }

int fitness_score(char *prospective_weasel)
   {
       int fitness =0;
       int i = 0;
       for (i=0; i< target_strlen; i++)
          {
           if (target_string[i]==prospective_weasel[i])
              {
               fitness++;
              }
          }
        return fitness;
       
   }

void make_mutant_kid(char *clone_of_fit_parent, char *prospective_mutant_kid)
   {
      strcpy(prospective_mutant_kid, clone_of_fit_parent);
      prospective_mutant_kid[ rand()%target_strlen] = random_printable_ascii();
   }

void make_a_family_from_fittest()
   {
       int i =0;
       int index_of_fittest = 0;
       int fittest_score = 0;
       
       /* find the fittest from siblings */
       for (i=0; i < NUMBER_KIDS; i++)
          {
           int fitness = 0;
           
           fitness = fitness_score( kid[i]);
           if (fitness > fittest_score)
              {
               fittest_score = fitness;
               index_of_fittest = i;
              }
             
          }
       
        /* put the clone in the first array element */
        strcpy( kid[0], kid[index_of_fittest]);
        printf("fittest=%s\n", kid[0]);   
       /* make mutant kids */
       for (i=1; i < NUMBER_KIDS; i++)
          {
           
           make_mutant_kid(kid[0],kid[i]);
             
          }
       if (fittest_score >= target_strlen)
          {
              target_hit = 1;
          }
         
   }

int main()
{
    target_strlen = strlen(target_string);
    int i = 0;
   
    /* initialization and memory setup */
    char *cloned_kid = (char *) malloc(target_strlen+1);
    cloned_kid[target_strlen]= '\0';
    for (i=0; i < NUMBER_KIDS; i++)
       {
         kid[i] = (char *) malloc(target_strlen) + 1;
         int j=0;
         for (j=0; j<target_strlen; j++)
            {
             kid[i][j] = random_printable_ascii();
            }
         kid[i][target_strlen] = '\0';
         printf("kid %d=%s\n",i,kid[i]);
       }
   
    for (i =0; i< ITERATION_LIMIT && !target_hit; i++)
       {
         make_a_family_from_fittest();
       }
   
    printf("hit target in %d iterations\n", i);
    return 0;
}


stcordova
 
Posts: 447
Joined: Wed Mar 05, 2014 1:41 am


Return to Science, Engineering, Mathematics

cron