Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- //Default population size. Also the number of lines on the array
- int size = 500;
- //Declaring functions
- void popstart();
- int fitness(int ** vet , int j);
- int arraymult(int ** vet ,int * vet2, int j);
- //starts filling the population array with size lines and 6 collumns. The last collumn will be the fitness value of the entire line
- void popstart() {
- //seeding
- srand(time(NULL));
- //line and collumn counter
- int i = 0;
- int j = 0;
- //population matrix
- int pop [size][6];
- //starts filling pop with random values ranging from (0 to 177)+5
- for (j=0;j < size;j++) {
- for (i=0;i < 5;i++) {
- pop[j][i] = (((double)rand() / RAND_MAX) * 177.0)+5;
- printf("Generated:%d,%d: %d\n",j,i, pop[j][i]);
- }
- //I start calculating the fitness as soon as possible so I don't loop too many times around the array and take advantage of already being in the line, quickening the process.
- pop[j][6] = fitness(pop,j);
- }
- }
- //function to calculate fitness. Receives pop and the current value of J to know what line it's being calculated on.
- int fitness(int ** vet , int j){
- // fitness will be returned. pmax is the size... I should be using size instead. Penalty is by how much it will hurt the total score.
- int fitness;
- int pmax = size;
- int penalty = 20;
- //weigth array.
- int wg[5] = {2, 4, 5, 7, 12};
- //value array.
- int value[5] = {3, 6, 10, 18, 26};
- //I multiply the values Eg:(vet1[j][1]*vet2[1]) and sum them all.
- int wgcromo = arraymult(vet,wg,j);
- int valuecromo = arraymult(vet,value,j);
- //I see if the current value is inside the range I want. Else I penalize the total score.
- if (wgcromo<pmax) fitness = valuecromo;
- else fitness = valuecromo - ( wgcromo - pmax) * penalty;
- return fitness;
- }
- //I multiply the values Eg:(vet1[j][1]*vet2[1]) and sum them all.
- int arraymult(int ** vet ,int * vet2, int j){
- int result=0;
- for (int i=0;i < 5;i++) {
- result = result +(vet[j][i]*vet2[i]);
- }
- return result;
- }
- int main(){
- popstart(size);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement