-
Notifications
You must be signed in to change notification settings - Fork 1
/
mutate.c
executable file
·34 lines (23 loc) · 1.18 KB
/
mutate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdlib.h>
#include <stdio.h>
#include "a4.h"
void mutate(Individual *individual, double rate){
// Individual (aka image) has a pixel array
int n = (individual->image).width * (individual->image).height; // image size
int selectPixels = (int)(n * (rate/100)); // Selects random pixels
int i;
for (i = 0; i < selectPixels; i++){ // For every random pixel
int numberOfPixelBeingMutated = rand()%n;
PIXEL *toChange = &(((individual->image).data[numberOfPixelBeingMutated]));
toChange->r = (unsigned char)(rand() % (individual->image.max_color + 1)); // Randomize red value
toChange->g = (unsigned char)(rand() % (individual->image.max_color + 1)); // Randomize green value
toChange->b = (unsigned char)(rand() % (individual->image.max_color + 1)); // Randomize blue value
}
}
void mutate_population(Individual *individual, int population_size, double rate){
int begin = population_size / 4; // Start at index population_size/4
int i;
for (i = begin; i < population_size; i++){ // From index beginning to end of population
mutate((individual + i), rate); // Mutate individual, move pointer
}
}