Advertisement
Guest User

Weasel Program

a guest
Aug 25th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. int randomNumber(int high) {
  7.     return rand() % high;
  8. }
  9.  
  10. int generateOffspring(char* currentGen, int currentLen, char* letters, char** offspring, unsigned long children, int probability) {
  11.     for (unsigned int i = 0; i < children; i++) {
  12.         for (unsigned int c = 0; c < currentLen; c++) {
  13.             if (randomNumber(probability) < 1) {
  14.                 offspring[i][c] = letters[randomNumber(27)];
  15.             }
  16.             else {
  17.                 offspring[i][c] = currentGen[c];
  18.             }
  19.         }
  20.     }
  21.  
  22.     return 0;
  23. }
  24.  
  25. int findLeastDistance(char* string, char** strings, int len, int count) {
  26.     unsigned int matches = 0;
  27.     unsigned int bestMatches = 0;
  28.     unsigned int bestMatch = 0;
  29.  
  30.     for (unsigned int i = 0; i < count; i++) {
  31.         for (unsigned int c = 0; c < len; c++) {
  32.             if (string[c] == strings[i][c]) {
  33.                 matches++;
  34.             }
  35.         }
  36.  
  37.         if (matches > bestMatches) {
  38.             bestMatches = matches;
  39.             bestMatch = i;
  40.         }
  41.  
  42.         matches = 0;
  43.     }
  44.  
  45.     return bestMatch;
  46. }
  47.  
  48. int main(int argc, char *argv[]) {
  49.     system("PAUSE");
  50.     time_t t;
  51.     srand((unsigned)time(&t));
  52.     unsigned int probability;
  53.  
  54.     if (argc == 2) {
  55.         probability = (int)argv[1];
  56.     }
  57.     else {
  58.         probability = 10000;
  59.     }
  60.  
  61.     unsigned int children = 100;
  62.  
  63.     char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
  64.     char target[] = "METHINKS IT IS LIKE A WEASEL";
  65.     char currentGen[29];
  66.  
  67.     for (unsigned int i = 0; i < 29; i++) {
  68.         currentGen[i] = letters[randomNumber(27)];
  69.     }
  70.  
  71.     char** offspring[children];
  72.  
  73.     for (unsigned int i = 0; i < children; i++) {
  74.         offspring[i] = malloc(29);
  75.     }
  76.  
  77.     unsigned int bestMutation;
  78.     unsigned int counter = 0;
  79.    
  80.     for (;;) {
  81.         generateOffspring(currentGen, 28, letters, offspring, children, probability);
  82.  
  83.         bestMutation = findLeastDistance(target, offspring, 28, children);
  84.  
  85.         if (strcmp(currentGen, offspring[bestMutation])) {
  86.             printf("Gen %d: %s\n", counter, offspring[bestMutation]);
  87.         }
  88.  
  89.         strcpy(currentGen, offspring[bestMutation]);
  90.  
  91.         if (!strcmp(currentGen, target)) {
  92.             break;
  93.         }
  94.  
  95.         counter++;
  96.     }
  97.  
  98.     for (unsigned int i = 0; i < children; i++) {
  99.         free(offspring[i]);
  100.     }
  101.  
  102.     free(currentGen);
  103.  
  104.     printf("\nEnd\n");
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement