aboutsummaryrefslogtreecommitdiff
path: root/genetic.c
blob: 5c908f2f7a2956e89e55ee289751b1988259c1cd (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "m_pd.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define TRUE  1
#define FALSE 0
#define MAX_ARRAY_SIZE 256
//typedef short boolean;

static t_class *genetic_class;

typedef struct _individual {
	boolean genes[MAX_ARRAY_SIZE];
	float fitness;
	t_int length;
} 	t_individual;

typedef struct _genetic {
  t_object  x_obj;
  float cumulativeprobability;
  t_int individuallength;
  t_individual population[MAX_ARRAY_SIZE];
  t_int populationsize;
  t_individual targetindividual;
  t_atom out[MAX_ARRAY_SIZE];
  t_int chunksize;
  t_outlet *outlist;
} t_genetic;

void randomizeIndividuals(t_genetic *x) {
	int i,k;
	for (k=0;k<x->populationsize;k++) {
	for (i=0;i<x->individuallength;i++) {
	x->population[k].genes[i] = ((t_float) rand())/RAND_MAX*2;	
	}
	x->population[k].length = x->individuallength;
	}
//	x->population[0].genes[0] = 1;
//	x->population[0].genes[1] = 0;
}

void printTarget(t_genetic *x) {
	unsigned int out[MAX_ARRAY_SIZE];
	int i;	
	for (i=0;i< x->targetindividual.length/x->chunksize;i++) {
	fast_b2short(&out[i],&x->targetindividual.genes[i*x->chunksize],x->chunksize);	
	SETFLOAT(&x->out[i],out[i]);
	}
	outlet_list(x->outlist,&s_list, i, &x->out[0]);
}


void randomTargetIndividual(t_genetic *x) {
	int i;
	for (i=0;i<x->individuallength;i++) {
	x->targetindividual.genes[i] = ((t_float) rand())/RAND_MAX*2;	
	}
	x->targetindividual.length = x->individuallength;
	//printTarget(&x->targetindividual,x);
}

/*float evaluateFitness(t_genetic *x,int index) {
	int fitness = 0;
	int i;
//	post("Fitness eval");
	for (i=0;i<x->individuallength;i++) {
	//if (x->targetindividual.genes[i] == x->population[index].genes[i])
	//if (x->population[index].genes[i] == TRUE)
	fitness += x->population[0].genes[0];
	//fitness++;
	}
	return fitness/x->individuallength;
	//return 1;
}
*/
void startReproduction(t_genetic *x) {
	int selectedIndividuals[MAX_ARRAY_SIZE];
	int count = 0;
	int i;
	float r;
	t_individual childs[MAX_ARRAY_SIZE];
	int childindex = 0;
	for (count =0; count < x->populationsize/4; count++) {
		float sum = 0;
		i = 0;
		r = ((t_float) rand())/RAND_MAX * x->cumulativeprobability;
		while (r > sum) {
				sum += x->population[i].fitness;
				i++;
		}
		selectedIndividuals[count] = i;
	}
	post("parents: %d",count);
	i=0;
	while (i<count) {
	int k = 0;
	for (k=0;k<8;k++) {
	int l;
	r = ((t_float) rand())/RAND_MAX * x->individuallength;
	for (l=0;l<x->individuallength;l++) {
	if (l<r) childs[childindex].genes[l] = x->population[selectedIndividuals[i]].genes[l];
	else childs[childindex].genes[l] = x->population[selectedIndividuals[i+1]].genes[l];
	}
	// mutation
	r = ((t_float) rand())/RAND_MAX * x->individuallength;
	childs[childindex].genes[(int)r] = 1-childs[childindex].genes[(int)r];
	childindex++;
	}
	i+=2;
	}
	for (i=0;i<childindex;i++) {
	x->population[i] = childs[i];	
	}
	post("New childs: %d",childindex);
	
}


int evaluatePopulation(t_genetic *x) {
	int i,k;
	int f;
	int highestfitnessindex = 0;
	float highestfitness = 0;
	float mean;
	x->cumulativeprobability = 0;
	for (k=0; k < x->populationsize; k++) {
		float fitness = 0;
		for (i=0;i<x->individuallength;i++) {
		//x->population[k].genes[i] = 0;
		if (x->targetindividual.genes[i] == x->population[k].genes[i])
		fitness++;
//		fitness += x->population[k].genes[i];
		}
		x->population[k].fitness = fitness/x->individuallength;
		x->population[k].fitness = (fitness/x->individuallength)*(fitness/x->individuallength);

		if (x->population[k].fitness > highestfitness) {
		// post("fitness of %d: %f",k,fitness);
		highestfitness = x->population[k].fitness;
		highestfitnessindex = k;
		}
		x->cumulativeprobability+=x->population[k].fitness;
	}
	//recalculate 
	mean = x->cumulativeprobability/x->populationsize;
	x->cumulativeprobability = 0;
	for (k=0;k < x->populationsize; k++) {
		if (x->population[k].fitness < mean)
		x->population[k].fitness = 0;
		else x->cumulativeprobability += x->population[k].fitness;	
	}
	post("Highest fitness: %f",highestfitness);
	return highestfitnessindex;
}


void genetic_setTarget(t_genetic *x,t_symbol *s, int argc, t_atom *argv) {
int i;
post("target called: %d",argc);
	for (i=0;i< x->individuallength/x->chunksize;i++) {
		if (i >= argc) break;
		fast_d2bl(atom_getint(&argv[i]),&x->targetindividual.genes[i*x->chunksize],x->chunksize);

	}
	printTarget(x);
}

void genetic_randomize(t_genetic *x) {
post("Randomize called");	
randomizeIndividuals(x);
}

void genetic_bang(t_genetic *x)
{
	int selected = 1;
	unsigned int out[MAX_ARRAY_SIZE];
	int i;	
	selected = evaluatePopulation(x);
	//post("Selected individual: %d",selected);
	for (i=0;i< x->individuallength/x->chunksize;i++) {
		//out[i] = ((t_float) rand())/RAND_MAX*2;
		fast_b2short(&out[i],&x->population[selected].genes[i*x->chunksize],x->chunksize);
		//fast_b2short8(&out[i],&x->population[selected].genes[i*8]);	
		SETFLOAT(&x->out[i],out[i]);
	} 
	//	fast_b2d(&out,&x->population[0].genes[0]);
//	fast_b2short(&out,&x->population[0].genes[0]);
//	fast_b2d(&out,&test[0]);
//	out = 100;
//	SETFLOAT(&x->out[1],out);
	outlet_list(x->outlist,&s_list, i, &x->out[0]);
	startReproduction(x);
}


/*
 * take argument rule, size, and offset
 */
void *genetic_new(t_float popsize,t_float indsize, t_float chunksize)
{
  t_genetic *x = (t_genetic *)pd_new(genetic_class);
  //inlet_new(&x->x_obj, &x->x_obj.ob_pd,gensym("list"), gensym("randomize"));
  if ((popsize > 0) && (popsize <= 256))
  x->populationsize = popsize;
  else x->populationsize = 256;
  if ((indsize > 0) && (indsize <= 256))
  x->individuallength = indsize;
  else x->individuallength = 256;
  if ((chunksize <= 16) && (chunksize > 0))
  x->chunksize = (int) chunksize;
  else x->chunksize = 8;
  randomizeIndividuals(x);
  randomTargetIndividual(x);
  inlet_new(&x->x_obj, &x->x_obj.ob_pd,gensym("list"), gensym("target"));
  x->outlist = outlet_new(&x->x_obj,&s_list);
  return (void *)x;
}