-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutate.c
25 lines (24 loc) · 845 Bytes
/
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
#include "a4.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void mutate(Individual *individual, double rate) {
int n = individual->image.width * individual->image.height;
int max_color = individual->image.max_color;
int num = (int)(rate / 100 * n);
for (int i = 0; i < num; i++) {
int random = rand() % n;
individual->image.data[random].r =
(unsigned char)(rand() % (max_color + 1));
individual->image.data[random].g =
(unsigned char)(rand() % (max_color + 1));
individual->image.data[random].b =
(unsigned char)(rand() % (max_color + 1));
}
}
void mutate_population(Individual *individual, int population_size,
double rate) {
for (int i = population_size / 4; i < population_size; i++) {
mutate(&(individual[i]), rate);
}
}