Exemple #1
0
double* matrix_vector_mult(double mat[5][5], double *vec, int rows, int cols)
{ // in matrix form: result = mat * vec;
	int i;
		for (i = 0; i < rows; i++) {
        	mult_result[i] = vectors_dot_prod(mat[i], vec, cols);
		}
	return mult_result;
} 
Exemple #2
0
double* matrix_vector_mult(double mat[5][5], double *vec, int rows, int cols)
{ // in matrix form: result = mat * vec;
	int i;
	double *result = (double*) malloc (cols* sizeof (double));
		for (i = 0; i < rows; i++) {
        	result[i] = vectors_dot_prod(mat[i], vec, cols);
		}
	return result; 
} 
Exemple #3
0
static void mv(void *args)
{
	struct work_struct *s = args;
	int i;

	//printf("[%d]MV size = %d, threads = %d \n", s->id, matrixSize, CPUCORES);

	for (i = s->id; i < matrixSize; i += CPUCORES){
		cc[i][0] = vectors_dot_prod(aa[i], bb);
	}
	//printf("%d", s->id);
	 if (s->id)
		job_exit();
}
Exemple #4
0
int  main () {

  time_t time;
  srand ((unsigned) (&time)); 	
  lineage lin; 
  lin.generation = (population*) malloc (100000 * sizeof(population)); 
  int count;
  double y[5] = {1, 1, 1, 1, 1};  
  double optimal_phenotype[5] = {10, 5, 10, 0, 5}; 
  int org; 
  for (org = 0; org < 1; org++) { 

    for (count = 0; count < 5; count++) {
  
 
    //  lin.generation[0].organism[org].allele[count].gene_size = 10;
    //  lin.generation[0].organism[org].allele[count].regulator = malloc (lin.generation[0].organism[org].allele[count].gene_size * sizeof(int));
    //  lin.generation[0].organism[org].allele[count].protein = malloc (lin.generation[0].organism[org].allele[count].gene_size * sizeof(int));
      lin.generation[0].organism[org].allele[count].reg_direction = rand()%2;
      lin.generation[0].organism[org].allele[count].pro_direction = rand()%2;  
		
      int i;
      int p_rand[10];
      int r_rand[10]; 
      printf ("GENE %d\n", count); 

      for (i = 0; i < 10; i++) {
      p_rand[i] = rand()%4;
      r_rand[i] = rand()%4;
      lin.generation[0].organism[org].allele[count].regulator[i] = r_rand[i];
      lin.generation[0].organism[org].allele[count].protein[i] = p_rand[i];	
	
      printf ("%d\t%d\n", lin.generation[0].organism[org].allele[count].regulator[i], lin.generation[0].organism[org].allele[count].protein[i] );
	
      }
  }
  int h;	
  int j; 
  int k; 
  for (h = 0; h < 5; h++) {
    for (j = 0; j < 5; j++) {
      for (k = 0; k < 10; k++) {
        if (lin.generation[0].organism[org].allele[h].regulator[k] == lin.generation[0].organism[org].allele[j].protein[k]) {
         lin.generation[0].organism[org].GRN[h][j] = (lin.generation[0].organism[org].GRN[h][j] + 1);
/*        if (lin.generation[0].organism[org].allele[h].reg_direction == 1) {
         lin.generation[0].organism[org].GRN[h][j] = (lin.generation[0].organism[org].GRN[h][j] * (-1));
        }
  */      }
      }
       lin.generation[0].organism[org].GRN[h][j] = ((1/50)* ( exp (lin.generation[0].organism[org].GRN[h][j] - 3)));
       if (lin.generation[0].organism[org].allele[h].reg_direction == 1) {
         lin.generation[0].organism[org].GRN[h][j] = (lin.generation[0].organism[org].GRN[h][j] * (-1)); 
        }
    }
	}
	
  int q, r; 
  for (q = 0; q < 5; q++) {
    for (r = 0; r < 5; r++) {
    printf ("%f ", lin.generation[0].organism[org].GRN[q][r]);
    }
  printf ("\n"); 
  }


 // double *Mat[] = {lin.generation[0].organism[org].GRN[0], lin.generation[0].organism[org].GRN[1], lin.generation[0].organism[org].GRN[2], lin.generation[0].organism[org].GRN[3], lin.generation[0].organism[org].GRN[4]};


  int i; 
/* double *ans =  matrix_vector_mult (Mat, y, 5, 5);
  printf ("\n"); 
  for (i = 0; i < 5; i++) {
    printf ("%f\n", ans[i]); 
  }
*/

  printf("\nMatrix iteration:\n");
  lin.generation[0].organism[org].phenotype = mat_power(lin.generation[0].organism[org].GRN, y, 5, 5, 100);
  for (i = 0; i < 5; i++) {
    printf ("%f\n", lin.generation[0].organism[org].phenotype[i]);
  }

  double euc_dist = sqrt (vectors_dot_prod (lin.generation[0].organism[org].phenotype, lin.generation[0].organism[org].phenotype, 5) + vectors_dot_prod (optimal_phenotype, optimal_phenotype, 5) - (2 * vectors_dot_prod (optimal_phenotype, lin.generation[0].organism[org].phenotype, 5) ) ); 
  printf ("norm = %f\n", euc_dist); 
  
  lin.generation[0].organism[org].fitness = exp ((-1) * (euc_dist));

  printf ("fitness-%d = %f\n", org, lin.generation[0].organism[org].fitness);    

 // free(ans); 
  }

for (int tst = 1; tst < 100; tst++) {
lin.generation[0].organism[tst] = lin.generation[0].organism[0]; 
}
for (count = 0; count < 5; count ++){
  printf( "copy gene %d\n", count); 
  for (int i = 0; i < 10; i++) {
    printf ("%d\t%d\n", lin.generation[0].organism[1].allele[count].regulator[i], lin.generation[0].organism[org].allele[count].protein[i] );
  }
}
/* END OF GEN-0 creation --> Next mutate and recombine to make GEN-1 through GEN-N */

  int gen;
  int hap; 

  for (gen = 1; gen < 100000; gen ++) {
    double fitnesses[100]; 
      for (org = 0; org < 100; org++) {
        fitnesses[org] = lin.generation[gen-1].organism[org].fitness;
      }
      for (org = 0; org < 100; org++) {
        int parent1 = weight_rand(fitnesses);
        int parent2 = weight_rand(fitnesses);
  //      printf ("organism %d has parent 1 = %d and parent 2 = %d\n" , org, parent1, parent2);     
        for (hap = 0; hap < 5; hap++){
          if (hap == 0 || hap == 2 || hap == 3) {
            lin.generation[gen].organism[org].allele[hap] = lin.generation[gen-1].organism[parent1].allele[hap]; 
        }
          if (hap == 1 || hap == 4) {
            lin.generation[gen].organism[org].allele[hap] = lin.generation[gen-1].organism[parent2].allele[hap]; 
          }
          for (int nuc = 0; nuc < 10; nuc++) {
            int mut_reg1 = rand()%1000;
            int mut_reg2 = rand()%4; 
            int mut_pro1 = rand()%1000;
            int mut_pro2 = rand()%4; 
            
            if (mut_reg1 == 1 && mut_reg2 == 0) {  
              lin.generation[gen].organism[org].allele[hap].regulator[nuc] = 0; 
            } 
            if (mut_reg1 == 1 && mut_reg2 == 1) {
              lin.generation[gen].organism[org].allele[hap].regulator[nuc] = 1; 
            }
            if (mut_reg1 == 1 && mut_reg2 == 2) {
              lin.generation[gen].organism[org].allele[hap].regulator[nuc] = 2; 
            }
            if (mut_reg1 == 1 && mut_reg2 == 3) {
              lin.generation[gen].organism[org].allele[hap].regulator[nuc] = 3;
            }

            if (mut_pro1 == 1 && mut_pro2 == 0) {
              lin.generation[gen].organism[org].allele[hap].protein[nuc] = 0; 
            }
            if (mut_pro1 == 1 && mut_pro2 == 1) {
              lin.generation[gen].organism[org].allele[hap].protein[nuc] = 1; 
            }
            if (mut_pro1 == 1 && mut_pro2 == 2) {
              lin.generation[gen].organism[org].allele[hap].protein[nuc] = 2;
            }
            if (mut_pro1 == 1 && mut_pro2 == 3) {
              lin.generation[gen].organism[org].allele[hap].protein[nuc] = 3;
            }

//      printf ("%d\t%d\n", lin.generation[0].organism[0].allele[hap].protein[nuc], lin.generation[gen].organism[org].allele[hap].protein[nuc] );
          }
//      printf ("\n"); 
            int mut_dir = rand()%1000;
          //  int mut_dir2 = rand()%10000;  
          if (mut_dir == 1 && lin.generation[gen].organism[org].allele[hap].reg_direction == 1) {
            lin.generation[gen].organism[org].allele[hap].reg_direction = 0; 
          }
          if (mut_dir == 1 && lin.generation[gen].organism[org].allele[hap].reg_direction == 0) {
            lin.generation[gen].organism[org].allele[hap].reg_direction = 1;
          }
      /*    if (mut_dir2 == 1 && lin.generation[gen].organism[org].allele[hap].pro_direction %2 == 1) {
            lin.generation[gen].organism[org].allele[hap].reg_direction = 0;
          }
          if (mut_dir2 == 1 && lin.generation[gen].organism[org].allele[hap].pro_direction %2 == 0) {
            lin.generation[gen].organism[org].allele[hap].reg_direction = 1;
          }
    */   

        }
      
        int h, j, k; 
        for (h = 0; h < 5; h++) {
          for (j = 0; j < 5; j++) {
            for (k = 0; k < 10; k++) {
              if (lin.generation[gen].organism[org].allele[h].regulator[k] == lin.generation[gen].organism[org].allele[j].protein[k]) {
                lin.generation[gen].organism[org].GRN[h][j] = (lin.generation[gen].organism[org].GRN[h][j] + 1);
   /*           if (lin.generation[gen].organism[org].allele[h].reg_direction == 1) {
                lin.generation[gen].organism[org].GRN[h][j] = (abs (lin.generation[gen].organism[org].GRN[h][j]) * (-1));
              }   
     */         }
            } 
             
 //         lin.generation[gen].organism[org].GRN[h][j] = ((1/50) * ( exp (lin.generation[gen].organism[org].GRN[h][j] - 3)));
          if (lin.generation[gen].organism[org].allele[h].reg_direction == 0) {
            lin.generation[gen].organism[org].GRN[h][j] = (lin.generation[gen].organism[org].GRN[h][j] * (-1));
          }
        }   
       }
      /* don't need pmat now that matrix_vec_mult has mat[5][5] instead **mat now */
  //       double *pmat[] = {lin.generation[gen].organism[org].GRN[0], lin.generation[gen].organism[org].GRN[1], lin.generation[gen].organism[org].GRN[2], lin.generation[gen].organism[org].GRN[3], lin.generation[gen].organism[org].GRN[4]};
       lin.generation[gen].organism[org].phenotype = mat_power (lin.generation[gen].organism[org].GRN, y, 5, 5, 100);  

       double euc_dist = sqrt (vectors_dot_prod (lin.generation[gen].organism[org].phenotype, lin.generation[gen].organism[org].phenotype, 5) + vectors_dot_prod (optimal_phenotype, optimal_phenotype, 5) - (2 * vectors_dot_prod (optimal_phenotype, lin.generation[gen].organism[org].phenotype, 5) ) );
//  printf ("norm = %f\n", euc_dist);

       lin.generation[gen].organism[org].fitness = exp ((-1) * pow ((euc_dist), 1));
       if ( gen % 100 == 0 && org == 0 ){
       printf ("GENERATION %d fitness-%d = %f\n", gen, org, lin.generation[gen].organism[org].fitness);  
         int q, r;
          for (q = 0; q < 5; q++) {
            for (r = 0; r < 5; r++) {
              printf ("%f ", lin.generation[gen].organism[org].GRN[q][r]);
            }
            printf ("\n");
          }
   /*   for (hap = 0; hap < 5; hap++) {
        printf ("Gene %d\n", hap); 
        for (int i = 0; i < 10; i++) {

          printf ("%d\t%d\n", lin.generation[gen].organism[org].allele[hap].regulator[i], lin.generation[gen].organism[org].allele[hap].protein[i] );

          }
       } */ 
     }
    }
  }
  FILE *f; 
  f = fopen ("long-run-lin1.dat", "wb"); 
  fwrite (lin.generation, sizeof(population), 10000000, f); 
  fclose(f);  
  free (lin.generation); 
}  
Exemple #5
0
int  main () {

  time_t time;
  srand ((unsigned) (&time)); 	
//  population pop;
  lineage lin;  
  lin.generation = (population*) malloc (1000000 * sizeof(population)); 
  int count;
  double y[5] = {1, 1, 1, 1, 1};  
  double optimal_phenotype[5] = {1, 1, 1, 1, 1}; 
  int org; 
  for (org = 0; org < 1; org++) { 
		
      int i;
      printf ("GENE %d\n", count); 

      lin.generation[0].organism[org].allele[0].regulator[1] ; 
      lin.generation[0].organism[org].allele[0].regulator[2] ;
      lin.generation[0].organism[org].allele[0].regulator[2] ;
      lin.generation[0].organism[org].allele[0].regulator[1] ;
      lin.generation[0].organism[org].allele[0].regulator[2] ;
      lin.generation[0].organism[org].allele[0].regulator[2] ;
      lin.generation[0].organism[org].allele[0].regulator[2] ;
      lin.generation[0].organism[org].allele[0].regulator[0] ;
      lin.generation[0].organism[org].allele[0].regulator[0] ;
      lin.generation[0].organism[org].allele[0].regulator[0] ;

   
      lin.generation[0].organism[org].allele[1].regulator[1] ;
      lin.generation[0].organism[org].allele[1].regulator[2] ;
      lin.generation[0].organism[org].allele[1].regulator[1] ;
      lin.generation[0].organism[org].allele[1].regulator[1] ;
      lin.generation[0].organism[org].allele[1].regulator[3] ;
      lin.generation[0].organism[org].allele[1].regulator[1] ;
      lin.generation[0].organism[org].allele[1].regulator[1] ;
      lin.generation[0].organism[org].allele[1].regulator[0] ;
      lin.generation[0].organism[org].allele[1].regulator[1] ;
      lin.generation[0].organism[org].allele[1].regulator[0] ;


      lin.generation[0].organism[org].allele[2].regulator[0] ;
      lin.generation[0].organism[org].allele[2].regulator[0] ;
      lin.generation[0].organism[org].allele[2].regulator[2] ;
      lin.generation[0].organism[org].allele[2].regulator[3] ;
      lin.generation[0].organism[org].allele[2].regulator[3] ;
      lin.generation[0].organism[org].allele[2].regulator[1] ;
      lin.generation[0].organism[org].allele[2].regulator[2] ;
      lin.generation[0].organism[org].allele[2].regulator[0] ;
      lin.generation[0].organism[org].allele[2].regulator[0] ;
      lin.generation[0].organism[org].allele[2].regulator[0] ;


      lin.generation[0].organism[org].allele[3].regulator[1] ;
      lin.generation[0].organism[org].allele[3].regulator[0] ;
      lin.generation[0].organism[org].allele[3].regulator[2] ;
      lin.generation[0].organism[org].allele[3].regulator[1] ;
      lin.generation[0].organism[org].allele[3].regulator[3] ;
      lin.generation[0].organism[org].allele[3].regulator[2] ;
      lin.generation[0].organism[org].allele[3].regulator[2] ;
      lin.generation[0].organism[org].allele[3].regulator[1] ;
      lin.generation[0].organism[org].allele[3].regulator[1] ;
      lin.generation[0].organism[org].allele[3].regulator[0] ;

      lin.generation[0].organism[org].allele[4].regulator[1] ;
      lin.generation[0].organism[org].allele[4].regulator[0] ;
      lin.generation[0].organism[org].allele[4].regulator[1] ;
      lin.generation[0].organism[org].allele[4].regulator[1] ;
      lin.generation[0].organism[org].allele[4].regulator[2] ;
      lin.generation[0].organism[org].allele[4].regulator[2] ;
      lin.generation[0].organism[org].allele[4].regulator[2] ;
      lin.generation[0].organism[org].allele[4].regulator[0] ;
      lin.generation[0].organism[org].allele[4].regulator[0] ;
      lin.generation[0].organism[org].allele[4].regulator[0] ;

      lin.generation[0].organism[org].allele[0].protein[2] ;
      lin.generation[0].organism[org].allele[0].protein[1] ;
      lin.generation[0].organism[org].allele[0].protein[3] ;
      lin.generation[0].organism[org].allele[0].protein[2] ;
      lin.generation[0].organism[org].allele[0].protein[1] ;
      lin.generation[0].organism[org].allele[0].protein[3] ;
      lin.generation[0].organism[org].allele[0].protein[1] ; 
      lin.generation[0].organism[org].allele[0].protein[2] ;
      lin.generation[0].organism[org].allele[0].protein[2] ;
      lin.generation[0].organism[org].allele[0].protein[3] ;
     

      lin.generation[0].organism[org].allele[1].protein[2] ;
      lin.generation[0].organism[org].allele[1].protein[1] ;
      lin.generation[0].organism[org].allele[1].protein[0] ;
      lin.generation[0].organism[org].allele[1].protein[2] ;
      lin.generation[0].organism[org].allele[1].protein[0] ;
      lin.generation[0].organism[org].allele[1].protein[3] ;
      lin.generation[0].organism[org].allele[1].protein[3] ;
      lin.generation[0].organism[org].allele[1].protein[3] ;
      lin.generation[0].organism[org].allele[1].protein[3] ;
      lin.generation[0].organism[org].allele[1].protein[3] ;
      
      lin.generation[0].organism[org].allele[2].protein[0] ;
      lin.generation[0].organism[org].allele[2].protein[1] ;
      lin.generation[0].organism[org].allele[2].protein[0] ;
      lin.generation[0].organism[org].allele[2].protein[2] ;
      lin.generation[0].organism[org].allele[2].protein[1] ;
      lin.generation[0].organism[org].allele[2].protein[3] ;
      lin.generation[0].organism[org].allele[2].protein[0] ;
      lin.generation[0].organism[org].allele[2].protein[1] ;
      lin.generation[0].organism[org].allele[2].protein[3] ;
      lin.generation[0].organism[org].allele[2].protein[2] ; 
      
      lin.generation[0].organism[org].allele[3].protein[3] ;
      lin.generation[0].organism[org].allele[3].protein[1] ;
      lin.generation[0].organism[org].allele[3].protein[3] ;
      lin.generation[0].organism[org].allele[3].protein[2] ;
      lin.generation[0].organism[org].allele[3].protein[2] ;
      lin.generation[0].organism[org].allele[3].protein[0] ;
      lin.generation[0].organism[org].allele[3].protein[0] ;
      lin.generation[0].organism[org].allele[3].protein[3] ;
      lin.generation[0].organism[org].allele[3].protein[2] ;
      lin.generation[0].organism[org].allele[3].protein[2] ;
      
      lin.generation[0].organism[org].allele[4].protein[2] ;
      lin.generation[0].organism[org].allele[4].protein[1] ;
      lin.generation[0].organism[org].allele[4].protein[0] ;
      lin.generation[0].organism[org].allele[4].protein[0] ;
      lin.generation[0].organism[org].allele[4].protein[0] ;
      lin.generation[0].organism[org].allele[4].protein[0] ;
      lin.generation[0].organism[org].allele[4].protein[3] ;
      lin.generation[0].organism[org].allele[4].protein[3] ;
      lin.generation[0].organism[org].allele[4].protein[2] ;
      lin.generation[0].organism[org].allele[4].protein[1] ;
 




    
      printf ("%d\t%d\n", lin.generation[0].organism[org].allele[count].regulator[i], lin.generation[0].organism[org].allele[count].protein[i] );
	
      
  
  int h;	
  int j; 
  int k; 
  for (h = 0; h < 5; h++) {
    for (j = 0; j < 5; j++) {
      for (k = 0; k < 10; k++) {
        if (lin.generation[0].organism[org].allele[h].regulator[k] == lin.generation[0].organism[org].allele[j].protein[k]) {
         lin.generation[0].organism[org].GRN[h][j] = (lin.generation[0].organism[org].GRN[h][j] + 1);
/*        if (lin.generation[0].organism[org].allele[h].reg_direction == 1) {
         lin.generation[0].organism[org].GRN[h][j] = (lin.generation[0].organism[org].GRN[h][j] * (-1));
        }
  */      }
      }
/*       if ((lin.generation[0].organism[org].allele[h].reg_direction % 2) == (lin.generation[0].organism[org].allele[j].pro_direction % 2)) {
         lin.generation[0].organism[org].GRN[h][j] = (lin.generation[0].organism[org].GRN[h][j] * (-1)); 
       }
  */  }
	}
	
  int q, r; 
  for (q = 0; q < 5; q++) {
    for (r = 0; r < 5; r++) {
    printf ("%f ", lin.generation[0].organism[org].GRN[q][r]);
    }
  printf ("\n"); 
  }


 // double *Mat[] = {lin.generation[0].organism[org].GRN[0], lin.generation[0].organism[org].GRN[1], lin.generation[0].organism[org].GRN[2], lin.generation[0].organism[org].GRN[3], lin.generation[0].organism[org].GRN[4]};


  int i; 
/* double *ans =  matrix_vector_mult (Mat, y, 5, 5);
  printf ("\n"); 
  for (i = 0; i < 5; i++) {
    printf ("%f\n", ans[i]); 
  }
*/

  printf("\nMatrix iteration:\n");
  lin.generation[0].organism[org].phenotype = mat_power(lin.generation[0].organism[org].GRN, y, 5, 5, 2);
  for (i = 0; i < 5; i++) {
    printf ("%f\n", lin.generation[0].organism[org].phenotype[i]);
  }

  double euc_dist = sqrt (vectors_dot_prod (lin.generation[0].organism[org].phenotype, lin.generation[0].organism[org].phenotype, 5) + vectors_dot_prod (optimal_phenotype, optimal_phenotype, 5) - (2 * vectors_dot_prod (optimal_phenotype, lin.generation[0].organism[org].phenotype, 5) ) ); 
  printf ("norm = %f\n", euc_dist); 
  
  lin.generation[0].organism[org].fitness = exp ((-1) * (euc_dist));

  printf ("fitness-%d = %f\n", org, lin.generation[0].organism[org].fitness);    

 // free(ans); 
  }

for (int tst = 1; tst < 100; tst++) {
lin.generation[0].organism[tst] = lin.generation[0].organism[0]; 
}

/* END OF GEN-0 creation --> Next mutate and recombine to make GEN-1 through GEN-N */


//  float sum_of_weight = 0;
//  for (int i = 0; i < 10; i++) {
//     sum_of_weight += lin.generation[0].organism[i].fitness;
//     printf ("sum of weight = %f\n" , sum_of_weight);
//  }
//    float rnd = fmod((double)rand()/((double)RAND_MAX), sum_of_weight);
//  printf ("rnd = %f\n", rnd);
//  for (int i = 0; i < 10; i++) {
//    if (rnd < lin.generation[0].organism[i].fitness){
//      printf ("%d is fit to reproduce\n", i);
//      return i;
 //     break;
 //     }
  //    rnd -= lin.generation[0].organism[i].fitness;
//  }

  int gen;
  int hap; 

  for (gen = 1; gen < 150000; gen ++) {
    float fitnesses[100]; 
      for (org = 0; org < 100; org++) {
        fitnesses[org] = lin.generation[gen-1].organism[org].fitness;
      }
      for (org = 0; org < 100; org++) {
        int parent1 = weight_rand(fitnesses);
        int parent2 = weight_rand(fitnesses);
//        printf ("organism %d has parent 1 = %d and parent 2 = %d\n" , org, parent1, parent2);     
        for (hap = 0; hap < 5; hap++){
          if (hap == 0 || hap == 2 || hap == 3) {
            lin.generation[gen].organism[org].allele[hap] = lin.generation[gen-1].organism[parent1].allele[hap]; 
        }
          if (hap == 1 || hap == 4) {
            lin.generation[gen].organism[org].allele[hap] = lin.generation[gen-1].organism[parent2].allele[hap]; 
          }
          for (int nuc = 0; nuc < 10; nuc++) {
            int mut_reg1 = rand()%10000;
            int mut_reg2 = rand()%4; 
            int mut_pro1 = rand()%10000;
            int mut_pro2 = rand()%4; 
            
            if (mut_reg1 == 1 && mut_reg2 == 0) {  
              lin.generation[gen].organism[org].allele[hap].regulator[nuc] = 0; 
            } 
            if (mut_reg1 == 1 && mut_reg2 == 1) {
              lin.generation[gen].organism[org].allele[hap].regulator[nuc] = 1; 
            }
            if (mut_reg1 == 1 && mut_reg2 == 2) {
              lin.generation[gen].organism[org].allele[hap].regulator[nuc] = 2; 
            }
            if (mut_reg1 == 1 && mut_reg2 == 3) {
              lin.generation[gen].organism[org].allele[hap].regulator[nuc] = 3;
            }

            if (mut_pro1 == 1 && mut_pro2 == 0) {
              lin.generation[gen].organism[org].allele[hap].protein[nuc] = 0; 
            }
            if (mut_pro1 == 1 && mut_pro2 == 1) {
              lin.generation[gen].organism[org].allele[hap].protein[nuc] = 1; 
            }
            if (mut_pro1 == 1 && mut_pro2 == 2) {
              lin.generation[gen].organism[org].allele[hap].protein[nuc] = 2;
            }
            if (mut_pro1 == 1 && mut_pro2 == 3) {
              lin.generation[gen].organism[org].allele[hap].protein[nuc] = 3;
            }

//      printf ("%d\t%d\n", lin.generation[0].organism[0].allele[hap].protein[nuc], lin.generation[gen].organism[org].allele[hap].protein[nuc] );
          }
//      printf ("\n"); 
  /*        int mut_dir = rand()%10000;
            int mut_dir2 = rand()%10000;  
          if (mut_dir == 1 && lin.generation[gen].organism[org].allele[hap].reg_direction %2 == 1) {
            lin.generation[gen].organism[org].allele[hap].reg_direction = 0; 
          }
          if (mut_dir == 1 && lin.generation[gen].organism[org].allele[hap].reg_direction %2 == 0) {
            lin.generation[gen].organism[org].allele[hap].reg_direction = 1;
          }
          if (mut_dir2 == 1 && lin.generation[gen].organism[org].allele[hap].pro_direction %2 == 1) {
            lin.generation[gen].organism[org].allele[hap].reg_direction = 0;
          }
          if (mut_dir2 == 1 && lin.generation[gen].organism[org].allele[hap].pro_direction %2 == 0) {
            lin.generation[gen].organism[org].allele[hap].reg_direction = 1;
          }
    */   

        }
      
        int h, j, k; 
        for (h = 0; h < 5; h++) {
          for (j = 0; j < 5; j++) {
            for (k = 0; k < 10; k++) {
              if (lin.generation[gen].organism[org].allele[h].regulator[k] == lin.generation[gen].organism[org].allele[j].protein[k]) {
                lin.generation[gen].organism[org].GRN[h][j] = (lin.generation[gen].organism[org].GRN[h][j] + 1);
   /*           if (lin.generation[gen].organism[org].allele[h].reg_direction == 1) {
                lin.generation[gen].organism[org].GRN[h][j] = (abs (lin.generation[gen].organism[org].GRN[h][j]) * (-1));
              }   
     */         }
            }    
      /*    if ((lin.generation[gen].organism[org].allele[h].reg_direction) != (lin.generation[gen].organism[org].allele[j].pro_direction)) {
            lin.generation[gen].organism[org].GRN[h][j] = (lin.generation[gen].organism[org].GRN[h][j] * (-1));
          }
      */  }   
       }
      /* don't need pmat now that matrix_vec_mult has mat[5][5] instead **mat now */
  //       double *pmat[] = {lin.generation[gen].organism[org].GRN[0], lin.generation[gen].organism[org].GRN[1], lin.generation[gen].organism[org].GRN[2], lin.generation[gen].organism[org].GRN[3], lin.generation[gen].organism[org].GRN[4]};
       lin.generation[gen].organism[org].phenotype = mat_power (lin.generation[gen].organism[org].GRN, y, 5, 5, 2);  

       double euc_dist = sqrt (vectors_dot_prod (lin.generation[gen].organism[org].phenotype, lin.generation[gen].organism[org].phenotype, 5) + vectors_dot_prod (optimal_phenotype, optimal_phenotype, 5) - (2 * vectors_dot_prod (optimal_phenotype, lin.generation[gen].organism[org].phenotype, 5) ) );
//  printf ("norm = %f\n", euc_dist);

       lin.generation[gen].organism[org].fitness = exp ((-1) * (euc_dist));
       if ( gen % 1000 == 0  ){
       printf ("GENERATION %d fitness-%d = %f\n", gen, org, lin.generation[gen].organism[org].fitness);  
         int q, r;
          for (q = 0; q < 5; q++) {
            for (r = 0; r < 5; r++) {
              printf ("%f ", lin.generation[gen].organism[org].GRN[q][r]);
            }
            printf ("\n");
          } 
       }
    }
  }  
free (lin.generation); 
}  
Exemple #6
0
int  main (int argc, char * argv[]) {

  time_t time;
  srand ((unsigned) (&time)); 	
  double y[5] = {1, 1, 1, 1, 1};  
  double optimal_phenotype[5] = {10, 5, 10, 0, 5}; 

  lineage ancestral_lin;
  lineage lin1;

  ancestral_lin.generation = (population*) malloc (10000 * sizeof(population)); 
  lin1.generation = (population*) malloc (10000 * sizeof(population));

  FILE *op = fopen ("lineage1.dat", "rb");
  fread (ancestral_lin.generation, sizeof (population), 10000, op);

  lin1.generation[0] = ancestral_lin.generation[9999]; 

/* END OF GEN-0 creation --> Next mutate and recombine to make GEN-1 through GEN-N */

  int gen;
  int hap; 

  for (gen = 1; gen < 10000; gen ++) {
    double fitnesses[100]; 
      for (int org = 0; org < 100; org++) {
        fitnesses[org] = lin1.generation[gen-1].organism[org].fitness;
      }
      for (int org = 0; org < 100; org++) {
        int parent1 = weight_rand(fitnesses);
        int parent2 = weight_rand(fitnesses);
  //      printf ("organism %d has parent 1 = %d and parent 2 = %d\n" , org, parent1, parent2);     
        for (hap = 0; hap < 5; hap++){
          if (hap == 0 || hap == 2 || hap == 3) {
            lin1.generation[gen].organism[org].allele[hap] = lin1.generation[gen-1].organism[parent1].allele[hap]; 
        }
          if (hap == 1 || hap == 4) {
            lin1.generation[gen].organism[org].allele[hap] = lin1.generation[gen-1].organism[parent2].allele[hap]; 
          }
          for (int nuc = 0; nuc < 10; nuc++) {
            int mut_reg1 = rand()%10000;
            int mut_reg2 = rand()%4; 
            int mut_pro1 = rand()%10000;
            int mut_pro2 = rand()%4; 
            
            if (mut_reg1 == 1 && mut_reg2 == 0) {  
              lin1.generation[gen].organism[org].allele[hap].regulator[nuc] = 0; 
            } 
            if (mut_reg1 == 1 && mut_reg2 == 1) {
              lin1.generation[gen].organism[org].allele[hap].regulator[nuc] = 1; 
            }
            if (mut_reg1 == 1 && mut_reg2 == 2) {
              lin1.generation[gen].organism[org].allele[hap].regulator[nuc] = 2; 
            }
            if (mut_reg1 == 1 && mut_reg2 == 3) {
              lin1.generation[gen].organism[org].allele[hap].regulator[nuc] = 3;
            }

            if (mut_pro1 == 1 && mut_pro2 == 0) {
              lin1.generation[gen].organism[org].allele[hap].protein[nuc] = 0; 
            }
            if (mut_pro1 == 1 && mut_pro2 == 1) {
              lin1.generation[gen].organism[org].allele[hap].protein[nuc] = 1; 
            }
            if (mut_pro1 == 1 && mut_pro2 == 2) {
              lin1.generation[gen].organism[org].allele[hap].protein[nuc] = 2;
            }
            if (mut_pro1 == 1 && mut_pro2 == 3) {
              lin1.generation[gen].organism[org].allele[hap].protein[nuc] = 3;
            }

//      printf ("%d\t%d\n", lin1.generation[0].organism[0].allele[hap].protein[nuc], lin1.generation[gen].organism[org].allele[hap].protein[nuc] );
          }
//      printf ("\n"); 
            int mut_dir = rand()%10000;
          //  int mut_dir2 = rand()%10000;  
          if (mut_dir == 1 && lin1.generation[gen].organism[org].allele[hap].reg_direction == 1) {
            lin1.generation[gen].organism[org].allele[hap].reg_direction = 0; 
          }
          if (mut_dir == 1 && lin1.generation[gen].organism[org].allele[hap].reg_direction == 0) {
            lin1.generation[gen].organism[org].allele[hap].reg_direction = 1;
          }
      /*    if (mut_dir2 == 1 && lin1.generation[gen].organism[org].allele[hap].pro_direction %2 == 1) {
            lin1.generation[gen].organism[org].allele[hap].reg_direction = 0;
          }
          if (mut_dir2 == 1 && lin1.generation[gen].organism[org].allele[hap].pro_direction %2 == 0) {
            lin1.generation[gen].organism[org].allele[hap].reg_direction = 1;
          }
    */   

        }
      
        int h, j, k; 
        for (h = 0; h < 5; h++) {
          for (j = 0; j < 5; j++) {
            lin1.generation[gen].organism[org].GRN[h][j] = 0;
            for (k = 0; k < 10; k++) {
              if (lin1.generation[gen].organism[org].allele[h].regulator[k] == lin1.generation[gen].organism[org].allele[j].protein[k]) {
                lin1.generation[gen].organism[org].GRN[h][j] = (lin1.generation[gen].organism[org].GRN[h][j] + 1);
   /*           if (lin1.generation[gen].organism[org].allele[h].reg_direction == 1) {
                lin1.generation[gen].organism[org].GRN[h][j] = (abs (lin1.generation[gen].organism[org].GRN[h][j]) * (-1));
              }   
     */         }
            } 
             
 //         lin1.generation[gen].organism[org].GRN[h][j] = ((1/50) * ( exp (lin1.generation[gen].organism[org].GRN[h][j] - 3)));
          if (lin1.generation[gen].organism[org].allele[h].reg_direction == 0) {
            lin1.generation[gen].organism[org].GRN[h][j] = (lin1.generation[gen].organism[org].GRN[h][j] * (-1));
          }
        }   
       }
       lin1.generation[gen].organism[org].phenotype = mat_power (lin1.generation[gen].organism[org].GRN, y, 5, 5, 100);  

       double euc_dist = sqrt (vectors_dot_prod (lin1.generation[gen].organism[org].phenotype, lin1.generation[gen].organism[org].phenotype, 5) + vectors_dot_prod (optimal_phenotype, optimal_phenotype, 5) - (2 * vectors_dot_prod (optimal_phenotype, lin1.generation[gen].organism[org].phenotype, 5) ) );
//  printf ("norm = %f\n", euc_dist);

       lin1.generation[gen].organism[org].fitness = exp ((-1) * pow ((euc_dist), 2));
       if ( gen % 100 == 0 && org == 0 ){
       printf ("GENERATION %d fitness-%d = %f\n", gen, org, lin1.generation[gen].organism[org].fitness);  
         int q, r;
          for (q = 0; q < 5; q++) {
            for (r = 0; r < 5; r++) {
              printf ("%f ", lin1.generation[gen].organism[org].GRN[q][r]);
            }
            printf ("\n");
          }
   /*   for (hap = 0; hap < 5; hap++) {
        printf ("Gene %d\n", hap); 
        for (int i = 0; i < 10; i++) {

          printf ("%d\t%d\n", lin1.generation[gen].organism[org].allele[hap].regulator[i], lin1.generation[gen].organism[org].allele[hap].protein[i] );

          }
       } */ 
     }
    }
  }


  FILE *f; 
  f = fopen (argv[1], "wb"); 
  fwrite (lin1.generation, sizeof(population), 10000, f); 
  fclose(f);
  free (lin1.generation); 
}  
Exemple #7
0
int  main () {

  double meanfit;
  
  double meanfit1;
  double meanfit2;

  FILE *fit; 
  fit = fopen ("fitness.dat", "w");

  FILE *flocation;
  flocation = fopen("location.dat", "w");

  FILE *hybridfit;
  hybridfit = fopen ("Hybrid_Fitnesses.txt", "w");

  FILE *allo1fit;
  allo1fit = fopen("Allo1_Fitness.txt", "w");

  FILE *allo2fit;
  allo2fit = fopen("Allo2_Fitness.txt", "w");

  time_t time;
  srand ((unsigned) (&time));
  /* "time_t" above ensures better random number generation since it uses time.
   */

  //lineage lin;
  //lin.generation = (population*) malloc (N * sizeof(population));
  /* Assigning a lineage struct to to "lin"
   * lin.generation is an array of 1,000,000 populations. 
   */
  population currpop;
  population prevpop;

  population allo1curr;
  population allo1prev;
  population allo2curr;
  population allo2prev;

  int count;

   double y[5]; // = {1, 1, 1, 1, 1};  
  double optimal_phenotype[5]; // = {2, 2, 2, 2, 2}; 
  for (int ph = 0; ph < 5; ph++)
  {
    y[ph] = ((double) rand()/(double) RAND_MAX)*(rand()%10);
    optimal_phenotype[ph] = ((double) rand()/(double) RAND_MAX)*(rand()%10);
  }
  /* y[5] is a 5 dimensional initial vector.
   * Interpreted as the "environment."
   */

  /* the below for loop generates ONLY the first generation (currpop) of the simulation. 
   * All individuals in this generation are identical and random.
   */

    currpop.organism[0].location = 0;   

    for (count = 0; count < 5; count++) {

      currpop.organism[0].allele[count].reg_direction = rand()%2;
      currpop.organism[0].allele[count].pro_direction = rand()%2;  

      int i;
      int p_rand[g];
      int r_rand[g]; 
      printf ("GENE %d\n", count); 

      for (i = 0; i < g; i++) {
      p_rand[i] = rand()%4;
      r_rand[i] = rand()%4;
      currpop.organism[0].allele[count].regulator[i] = r_rand[i];
      currpop.organism[0].allele[count].protein[i] = p_rand[i];	
      printf ("%d\t%d\n", currpop.organism[0].allele[count].regulator[i], currpop.organism[0].allele[count].protein[i] );
      }
  }
  int h;
  int j;
  int k;
  for (h = 0; h < 5; h++) {
    for (j = 0; j < 5; j++) {
      for (k = 0; k < g; k++) {
        if (currpop.organism[0].allele[h].regulator[k] == currpop.organism[0].allele[j].protein[k]) {
         currpop.organism[0].GRN[h][j] = (currpop.organism[0].GRN[h][j]) + 1;
        /*  if (currpop.organism[org].allele[h].reg_direction == 1) {
            currpop.organism[org].GRN[h][j] = (currpop.organism[org].GRN[h][j] * (-1));
            }
         */ }
      }
      if (currpop.organism[0].allele[h].reg_direction == currpop.organism[0].allele[j].pro_direction) {
         currpop.organism[0].GRN[h][j] = (currpop.organism[0].GRN[h][j] * (-1)); 
      }
    }
	}

  for (h = 0; h < 5; h++)
  {
    for (j = 0; j < 5; j++)
    {
    //  currpop.organism[org].GRN[h][j] = pow(0.5 + (exp(6 - currpop.organism[org].GRN[h][j]))/5, -1);

      if (currpop.organism[0].allele[h].reg_direction == 1)
      {
        currpop.organism[0].GRN[h][j] = (currpop.organism[0].GRN[h][j] * (-1)); 
      }
    }
  }
	
  int q, r; 
  for (q = 0; q < 5; q++) {
    for (r = 0; r < 5; r++) {
    printf ("%f ", currpop.organism[0].GRN[q][r]);
    }
  printf ("\n"); 
  }


 // double *Mat[] = {currpop.organism[org].GRN[0], currpop.organism[org].GRN[1], currpop.organism[org].GRN[2], currpop.organism[org].GRN[3], currpop.organism[org].GRN[4]};


  int i; 
/* double *ans =  matrix_vector_mult (Mat, y, 5, 5);
  printf ("\n"); 
  for (i = 0; i < 5; i++) {
    printf ("%f\n", ans[i]); 
  }
*/

  printf("\nMatrix iteration:\n");
  currpop.organism[0].phenotype = mat_power(currpop.organism[0].GRN, y, 5, 5, 10);
  for (i = 0; i < 5; i++) {
    printf ("%f\n", currpop.organism[0].phenotype[i]);
  }

  double euc_dist = sqrt (vectors_dot_prod (currpop.organism[0].phenotype, currpop.organism[0].phenotype, 5) + vectors_dot_prod (optimal_phenotype, optimal_phenotype, 5) - (2 * vectors_dot_prod (optimal_phenotype, currpop.organism[0].phenotype, 5) ) ); 
  printf ("norm = %f\n", euc_dist); 
  
  currpop.organism[0].fitness = exp ((-1) * (pow(euc_dist, 2)));

  printf ("fitness-%d = %f\n", 0, currpop.organism[0].fitness);    

 // free(ans); 
  

for (int tst = 1; tst < pop; tst++) {
currpop.organism[tst] = currpop.organism[0];
}
for (count = 0; count < 5; count ++){
  printf( "copy gene %d\n", count); 
  for (int i = 0; i < g; i++) {
    printf ("%d\t%d\n", currpop.organism[1].allele[count].regulator[i], currpop.organism[0].allele[count].protein[i] );
  }
}

/***************************** Simulation after initial (0th) generation *************************
 *
 * Generations gen = 1 through gen = 100,000.
 */

  int gen;
  int hap; 

  for (gen = 1; gen < N; gen ++) {
    prevpop = currpop; 
    int org;
    meanfit = 0;
    double fitnesses[pop];
     // if (gen % 20000 == 0)
     // {
     //   for (int ph = 0; ph < 5; ph++)
     //   {
     //     optimal_phenotype[ph] = ((double) rand()/(double) RAND_MAX) * (rand()%10);
     //   }
     // }
      for (org = 0; org < pop; org++) {
        fitnesses[org] = prevpop.organism[org].fitness;
       // printf ("org %d fitness - %f\n ", org, fitnesses[org]);
      }
      for (org = 0; org < pop; org++) {
        
        int parent1 = weight_rand(fitnesses);
        //printf("gen-%d  org-%d parent-%d\n", gen, org, parent1);
      //  int geog[pop];
      //    for (int loc = 0; loc < pop; loc++)
      //    {
      //       geog[loc] = abs(prevpop.organism[parent1].location - prevpop.organism[loc].location);
      //    }
        
       //  int parent2 = geography_rand(fitnesses, geog);  
        int parent2 = weight_rand(fitnesses);
  //      printf ("organism %d has parent 1 = %d and parent 2 = %d\n" , org, parent1, parent2);     
        for (hap = 0; hap < 5; hap++){
          int rec = rand()%2;
   //       if (org < (pop/2))
   //       {
   //         currpop.organism[org].allele[hap] = prevpop.organism[parent1].allele[hap]; 
   //       }
          if (/* org > (pop/2) && */rec == 0) {
            currpop.organism[org].allele[hap] = prevpop.organism[parent1].allele[hap];
            currpop.organism[org].allele[hap].reg_direction = prevpop.organism[parent1].allele[hap].reg_direction;
            currpop.organism[org].allele[hap].pro_direction = prevpop.organism[parent1].allele[hap].pro_direction; 
        }
          if (/* org > (pop/2) &&  */rec == 1) {
            currpop.organism[org].allele[hap] = prevpop.organism[parent2].allele[hap]; 
            currpop.organism[org].allele[hap].reg_direction = prevpop.organism[parent2].allele[hap].reg_direction;
            currpop.organism[org].allele[hap].pro_direction = prevpop.organism[parent2].allele[hap].pro_direction; 
          }
          
          for (int nuc = 0; nuc < g; nuc++) {
            int mut_reg1 = rand()%mutate;
            int mut_reg2 = rand()%4; 
            int mut_pro1 = rand()%mutate;
            int mut_pro2 = rand()%4; 
            
            if (mut_reg1 == 1 && mut_reg2 == 0) {  
              currpop.organism[org].allele[hap].regulator[nuc] = 0; 
            } 
            if (mut_reg1 == 1 && mut_reg2 == 1) {
              currpop.organism[org].allele[hap].regulator[nuc] = 1; 
            }
            if (mut_reg1 == 1 && mut_reg2 == 2) {
              currpop.organism[org].allele[hap].regulator[nuc] = 2; 
            }
            if (mut_reg1 == 1 && mut_reg2 == 3) {
              currpop.organism[org].allele[hap].regulator[nuc] = 3;
            }

            if (mut_pro1 == 1 && mut_pro2 == 0) {
              currpop.organism[org].allele[hap].protein[nuc] = 0; 
            }
            if (mut_pro1 == 1 && mut_pro2 == 1) {
              currpop.organism[org].allele[hap].protein[nuc] = 1; 
            }
            if (mut_pro1 == 1 && mut_pro2 == 2) {
              currpop.organism[org].allele[hap].protein[nuc] = 2;
            }
            if (mut_pro1 == 1 && mut_pro2 == 3) {
              currpop.organism[org].allele[hap].protein[nuc] = 3;
            }

//      printf ("%d\t%d\n", currpop.organism[0].allele[hap].protein[nuc], currpop.organism[org].allele[hap].protein[nuc] );
          }
//      printf ("\n"); 
            int mut_dir  = rand()%mutate;
            int mut_dir2 = rand()%mutate;  
          if (mut_dir == 1 && currpop.organism[org].allele[hap].reg_direction == 1) {
            currpop.organism[org].allele[hap].reg_direction = 0; 
          }
          else if (mut_dir == 1 && currpop.organism[org].allele[hap].reg_direction == 0) {
            currpop.organism[org].allele[hap].reg_direction = 1;
          }
          if (mut_dir2 == 1 && currpop.organism[org].allele[hap].pro_direction  == 1) {
            currpop.organism[org].allele[hap].pro_direction = 0;
          }
          else if (mut_dir2 == 1 && currpop.organism[org].allele[hap].pro_direction  == 0) {
            currpop.organism[org].allele[hap].pro_direction = 1;
          }
   

        }
         // int mig_rand = rand()%3;
         // int mig_dist;

         // if (mig_rand == 0)
         // {
         //   mig_dist = 0;
         // }
         // if (mig_rand == 1)
         // {
         //   mig_dist = 1;
         // }
         // if (mig_rand == 2)
         // {
         //   mig_dist = -1;
         // }
        //    currpop.organism[org].location = prevpop.organism[parent1].location + mig_dist;
        //  if (gen % 100 == 0)
        //  {
        //    fprintf (flocation, "%d,", currpop.organism[org].location);
        //    if (org == (pop-1))
        //    {
        //      fprintf(flocation, "\n"); 
        //    }
        //  }
          

        int h, j, k;

        for (h = 0; h < 5; h++)
        {
          for (j = 0; j < 5; j++)
          {
            currpop.organism[org].GRN[h][j] = 0;
          }
        }

        for (h = 0; h < 5; h++)
        {
          for (j = 0; j < 5; j++)
          {
            for (k = 0; k < g; k++)
            {
              if (currpop.organism[org].allele[h].regulator[k] == currpop.organism[org].allele[j].protein[k])
              {
                currpop.organism[org].GRN[h][j] = (currpop.organism[org].GRN[h][j] + 0.001);
              }
            }

            // if (currpop.organism[org].allele[h].reg_direction == 0)
            // {
            //   currpop.organism[org].GRN[h][j] = (currpop.organism[org].GRN[h][j] * (-1));
            // }
          }
         }


  for (h = 0; h < 5; h++)
  {
    for (j = 0; j < 5; j++)
    {
        // currpop.organism[org].GRN[h][j] = pow(0.5 + (exp(6 - currpop.organism[org].GRN[h][j]))/5, -1);
      if (currpop.organism[org].allele[h].reg_direction != currpop.organism[org].allele[j].pro_direction)
      {
        currpop.organism[org].GRN[h][j] = (currpop.organism[org].GRN[h][j] * (-1));
      }
    }
  }
  

  /* don't need pmat now that matrix_vec_mult has mat[5][5] instead **mat now */
  //       double *pmat[] = {currpop.organism[org].GRN[0], currpop.organism[org].GRN[1], currpop.organism[org].GRN[2], currpop.organism[org].GRN[3], currpop.organism[org].GRN[4]};
         currpop.organism[org].phenotype = mat_power (currpop.organism[org].GRN, y, 5, 5, 100); 

       double euc_dist = sqrt (vectors_dot_prod (currpop.organism[org].phenotype, currpop.organism[org].phenotype, 5) + vectors_dot_prod (optimal_phenotype, optimal_phenotype, 5) - (2 * vectors_dot_prod (optimal_phenotype, currpop.organism[org].phenotype, 5) ) );
//  printf ("norm = %f\n", euc_dist);

       currpop.organism[org].fitness = exp (((-1) * (pow ((euc_dist), 2))));
       if (currpop.organism[org].fitness <= 0.0000001)
       {
         currpop.organism[org].fitness = 0.0000001; 
       }

    /*   if (currpop.organism[org].fitness <= 0.000001)
       {
         currpop.organism[org].fitness = 0.000001;
       }*/
       if ( (gen == 1 || gen % 1000 == 0) && org == 1 ){
       printf ("GENERATION %d fitness-%d = %f\n", gen, org, currpop.organism[org].fitness);  
         int q, r;
          for (q = 0; q < 5; q++) {
            for (r = 0; r < 5; r++) {
              printf ("%f ", currpop.organism[org].GRN[q][r]);
            }
            printf ("\n");
          }
    /*  for (hap = 0; hap < 5; hap++) {
        printf ("Gene %d\n", hap); 
        for (int i = 0; i < g; i++) {

          printf ("%d\t%d\n", currpop.organism[org].allele[hap].regulator[i], currpop.organism[org].allele[hap].protein[i] );

          }
       } */
     }
         meanfit += currpop.organism[org].fitness*0.01;
     if (gen % 1 == 0)
     {
         if (org == (pop - 1))
         {
           fprintf (fit, "%f\n,", meanfit);
         }
     }

      }
  }

  lineage allo1;
  lineage allo2;
    allo1.generation = malloc (isolate * sizeof(population)); 
    allo2.generation = malloc (isolate * sizeof(population)); 
  allo1curr = currpop;
  allo2curr = currpop;
  
  
  for (int gee = 0; gee < isolate; gee ++) {
    meanfit1 = 0;
    allo1prev = allo1curr; 
    int org;
    double fitnesses[pop];
      for (int org = 0; org < pop; org++) {
        fitnesses[org] = allo1prev.organism[org].fitness;
       // printf ("org %d fitness - %f\n ", org, fitnesses[org]);
      }
      for (org = 0; org < pop; org++) {
        
        int parent1 = weight_rand(fitnesses);
        
        int parent2 = weight_rand(fitnesses);
        for (hap = 0; hap < 5; hap++){
          int rec = rand()%2;
          if (/* org > (pop/2) && */rec == 0) {
            allo1curr.organism[org].allele[hap] = allo1prev.organism[parent1].allele[hap];
        }
          if (/* org > (pop/2) &&  */rec == 1) {
            allo1curr.organism[org].allele[hap] = allo1prev.organism[parent2].allele[hap]; 
          }
          
          for (int nuc = 0; nuc < g; nuc++) {
            int mut_reg1 = rand()%mutate;
            int mut_reg2 = rand()%4; 
            int mut_pro1 = rand()%mutate;
            int mut_pro2 = rand()%4; 
            
            if (mut_reg1 == 1 && mut_reg2 == 0) {  
              allo1curr.organism[org].allele[hap].regulator[nuc] = 0; 
            } 
            if (mut_reg1 == 1 && mut_reg2 == 1) {
              allo1curr.organism[org].allele[hap].regulator[nuc] = 1; 
            }
            if (mut_reg1 == 1 && mut_reg2 == 2) {
              allo1curr.organism[org].allele[hap].regulator[nuc] = 2; 
            }
            if (mut_reg1 == 1 && mut_reg2 == 3) {
              allo1curr.organism[org].allele[hap].regulator[nuc] = 3;
            }

            if (mut_pro1 == 1 && mut_pro2 == 0) {
              allo1curr.organism[org].allele[hap].protein[nuc] = 0; 
            }
            if (mut_pro1 == 1 && mut_pro2 == 1) {
              allo1curr.organism[org].allele[hap].protein[nuc] = 1; 
            }
            if (mut_pro1 == 1 && mut_pro2 == 2) {
              allo1curr.organism[org].allele[hap].protein[nuc] = 2;
            }
            if (mut_pro1 == 1 && mut_pro2 == 3) {
              allo1curr.organism[org].allele[hap].protein[nuc] = 3;
            }

//      printf ("%d\t%d\n", allo1curr.organism[0].allele[hap].protein[nuc], allo1curr.organism[org].allele[hap].protein[nuc] );
          }
//      printf ("\n"); 
            int mut_dir  = rand()%mutate;
            int mut_dir2 = rand()%mutate;  
          if (mut_dir == 1 && allo1curr.organism[org].allele[hap].reg_direction == 1) {
            allo1curr.organism[org].allele[hap].reg_direction = 0; 
          }
          else if (mut_dir == 1 && allo1curr.organism[org].allele[hap].reg_direction == 0) {
            allo1curr.organism[org].allele[hap].reg_direction = 1;
          }
          if (mut_dir2 == 1 && allo1curr.organism[org].allele[hap].pro_direction  == 1) {
            allo1curr.organism[org].allele[hap].pro_direction = 0;
          }
          else if (mut_dir2 == 1 && allo1curr.organism[org].allele[hap].pro_direction  == 0) {
            allo1curr.organism[org].allele[hap].pro_direction = 1;
          }
   

        }

        int h, j, k;

        for (h = 0; h < 5; h++)
        {
          for (j = 0; j < 5; j++)
          {
            allo1curr.organism[org].GRN[h][j] = 0;
          }
        }

        for (h = 0; h < 5; h++)
        {
          for (j = 0; j < 5; j++)
          {
            for (k = 0; k < g; k++)
            {
              if (allo1curr.organism[org].allele[h].regulator[k] == allo1curr.organism[org].allele[j].protein[k])
              {
                allo1curr.organism[org].GRN[h][j] = (allo1curr.organism[org].GRN[h][j] + 0.001);
              }
            }

            // if (allo1curr.organism[org].allele[h].reg_direction == 0)
            // {
            //   allo1curr.organism[org].GRN[h][j] = (allo1curr.organism[org].GRN[h][j] * (-1));
            // }
          }
         }


  for (h = 0; h < 5; h++)
  {
    for (j = 0; j < 5; j++)
    {
        // allo1curr.organism[org].GRN[h][j] = pow(0.5 + (exp(6 - allo1curr.organism[org].GRN[h][j]))/5, -1);
      if (allo1curr.organism[org].allele[h].reg_direction != allo1curr.organism[org].allele[j].pro_direction)
      {
        allo1curr.organism[org].GRN[h][j] = (allo1curr.organism[org].GRN[h][j] * (-1));
      }
    }
  }
  

  /* don't need pmat now that matrix_vec_mult has mat[5][5] instead **mat now */
  //       double *pmat[] = {allo1curr.organism[org].GRN[0], allo1curr.organism[org].GRN[1], allo1curr.organism[org].GRN[2], allo1curr.organism[org].GRN[3], allo1curr.organism[org].GRN[4]};
         allo1curr.organism[org].phenotype = mat_power (allo1curr.organism[org].GRN, y, 5, 5, 100); 

       double euc_dist = sqrt (vectors_dot_prod (allo1curr.organism[org].phenotype, allo1curr.organism[org].phenotype, 5) + vectors_dot_prod (optimal_phenotype, optimal_phenotype, 5) - (2 * vectors_dot_prod (optimal_phenotype, allo1curr.organism[org].phenotype, 5) ) );

       allo1curr.organism[org].fitness = exp (((-1) * (pow ((euc_dist), 2))));
       if (allo1curr.organism[org].fitness <= 0.0000001)
       {
         allo1curr.organism[org].fitness = 0.0000001; 
       }

         meanfit1 += allo1curr.organism[org].fitness*0.01;
     if (gee % 1 == 0)
     {
         if (org == (pop - 1))
         {
           fprintf (allo1fit, "%f\n,", meanfit1);
         }
     }
       if ( (gee == 1 || gee % 1000 == 0) && org == 1 ){
       printf ("geeERATION %d fitness-%d = %f\n", gee, org, allo1curr.organism[org].fitness);  
         int q, r;
          for (q = 0; q < 5; q++) {
            for (r = 0; r < 5; r++) {
              printf ("%f ", allo1curr.organism[org].GRN[q][r]);
            }
            printf ("\n");
          }
     }

      }
        allo1.generation[gee].organism[0] = allo1curr.organism[0];
  }
  
  for (int eeg = 0; eeg < isolate; eeg ++) {
    meanfit2=0;
    allo2prev = allo2curr;
    int org;
    meanfit = 0;
    double fitnesses[pop];
      for (org = 0; org < pop; org++) {
        fitnesses[org] = allo2prev.organism[org].fitness;
      }
      for (org = 0; org < pop; org++) {
        
        int parent1 = weight_rand(fitnesses);
        int parent2 = weight_rand(fitnesses);
        for (hap = 0; hap < 5; hap++){
          int rec = rand()%2;
          if (rec == 0) {
            allo2curr.organism[org].allele[hap] = allo2prev.organism[parent1].allele[hap];
            allo2curr.organism[org].allele[hap].reg_direction = allo2prev.organism[parent1].allele[hap].reg_direction;
            allo2curr.organism[org].allele[hap].pro_direction = allo2prev.organism[parent1].allele[hap].pro_direction; 
        }
          if (rec == 1) {
            allo2curr.organism[org].allele[hap] = allo2prev.organism[parent2].allele[hap]; 
            allo2curr.organism[org].allele[hap].reg_direction = allo2prev.organism[parent2].allele[hap].reg_direction;
            allo2curr.organism[org].allele[hap].pro_direction = allo2prev.organism[parent2].allele[hap].pro_direction; 
          }
          
          for (int nuc = 0; nuc < g; nuc++) {
            int mut_reg1 = rand()%mutate;
            int mut_reg2 = rand()%4; 
            int mut_pro1 = rand()%mutate;
            int mut_pro2 = rand()%4; 
            
            if (mut_reg1 == 1 && mut_reg2 == 0) {  
              allo2curr.organism[org].allele[hap].regulator[nuc] = 0; 
            } 
            if (mut_reg1 == 1 && mut_reg2 == 1) {
              allo2curr.organism[org].allele[hap].regulator[nuc] = 1; 
            }
            if (mut_reg1 == 1 && mut_reg2 == 2) {
              allo2curr.organism[org].allele[hap].regulator[nuc] = 2; 
            }
            if (mut_reg1 == 1 && mut_reg2 == 3) {
              allo2curr.organism[org].allele[hap].regulator[nuc] = 3;
            }

            if (mut_pro1 == 1 && mut_pro2 == 0) {
              allo2curr.organism[org].allele[hap].protein[nuc] = 0; 
            }
            if (mut_pro1 == 1 && mut_pro2 == 1) {
              allo2curr.organism[org].allele[hap].protein[nuc] = 1; 
            }
            if (mut_pro1 == 1 && mut_pro2 == 2) {
              allo2curr.organism[org].allele[hap].protein[nuc] = 2;
            }
            if (mut_pro1 == 1 && mut_pro2 == 3) {
              allo2curr.organism[org].allele[hap].protein[nuc] = 3;
            }

          }
            int mut_dir  = rand()%mutate;
            int mut_dir2 = rand()%mutate;  
          if (mut_dir == 1 && allo2curr.organism[org].allele[hap].reg_direction == 1) {
            allo2curr.organism[org].allele[hap].reg_direction = 0; 
          }
          else if (mut_dir == 1 && allo2curr.organism[org].allele[hap].reg_direction == 0) {
            allo2curr.organism[org].allele[hap].reg_direction = 1;
          }
          if (mut_dir2 == 1 && allo2curr.organism[org].allele[hap].pro_direction  == 1) {
            allo2curr.organism[org].allele[hap].pro_direction = 0;
          }
          else if (mut_dir2 == 1 && allo2curr.organism[org].allele[hap].pro_direction  == 0) {
            allo2curr.organism[org].allele[hap].pro_direction = 1;
          }
   

        }

        int h, j, k;

        for (h = 0; h < 5; h++)
        {
          for (j = 0; j < 5; j++)
          {
            allo2curr.organism[org].GRN[h][j] = 0;
          }
        }

        for (h = 0; h < 5; h++)
        {
          for (j = 0; j < 5; j++)
          {
            for (k = 0; k < g; k++)
            {
              if (allo2curr.organism[org].allele[h].regulator[k] == allo2curr.organism[org].allele[j].protein[k])
              {
                allo2curr.organism[org].GRN[h][j] = (allo2curr.organism[org].GRN[h][j] + 0.001);
              }
            }

          }
         }


  for (h = 0; h < 5; h++)
  {
    for (j = 0; j < 5; j++)
    {
      if (allo2curr.organism[org].allele[h].reg_direction != allo2curr.organism[org].allele[j].pro_direction)
      {
        allo2curr.organism[org].GRN[h][j] = (allo2curr.organism[org].GRN[h][j] * (-1));
      }
    }
  }
  

         allo2curr.organism[org].phenotype = mat_power (allo2curr.organism[org].GRN, y, 5, 5, 100); 

       double euc_dist = sqrt (vectors_dot_prod (allo2curr.organism[org].phenotype, allo2curr.organism[org].phenotype, 5) + vectors_dot_prod (optimal_phenotype, optimal_phenotype, 5) - (2 * vectors_dot_prod (optimal_phenotype, allo2curr.organism[org].phenotype, 5) ) );

       allo2curr.organism[org].fitness = exp (((-1) * (pow ((euc_dist), 2))));
       if (allo2curr.organism[org].fitness <= 0.0000001)
       {
         allo2curr.organism[org].fitness = 0.0000001; 
       }

         meanfit2 += allo2curr.organism[org].fitness*0.01;
     if (eeg % 1 == 0)
     {
         if (org == (pop - 1))
         {
           fprintf (allo2fit, "%f\n,", meanfit2);
         }
     }
       if ( (eeg == 1 || eeg % 1000 == 0) && org == 1 ){
       printf ("eegERATION %d fitness-%d = %f\n", eeg, org, allo2curr.organism[org].fitness);  
         int q, r;
          for (q = 0; q < 5; q++) {
            for (r = 0; r < 5; r++) {
              printf ("%f ", allo2curr.organism[org].GRN[q][r]);
            }
            printf ("\n");
          }
     }

      }
        allo2.generation[eeg].organism[0] = allo2curr.organism[0];
  }
  
  lineage hybrid;
    hybrid.generation = malloc (isolate * sizeof(population)); 

  for (int hy = 0; hy < isolate; hy++)
  {
    for (int a = 0; a < 5; a++)
    {
      int mate = rand()%2;
      if (mate == 0)
      {
        hybrid.generation[hy].organism[0].allele[a] = allo1.generation[hy].organism[0].allele[a];
      }
      if (mate == 1)
      {
        hybrid.generation[hy].organism[0].allele[a] = allo2.generation[hy].organism[0].allele[a];
      }
    }
        int h, j, k;
//
//        for (h = 0; h < 5; h++)
//        {
//          for (j = 0; j < 5; j++)
//          {
//            hybrid.generation[hy].organism[0].GRN[h][j] = 0;
//          }
//        }

        for (h = 0; h < 5; h++)
        {
          for (j = 0; j < 5; j++)
          {
            for (k = 0; k < g; k++)
            {
              if (hybrid.generation[hy].organism[0].allele[h].regulator[k] == hybrid.generation[hy].organism[0].allele[j].protein[k])
              {
                hybrid.generation[hy].organism[0].GRN[h][j] = (hybrid.generation[hy].organism[0].GRN[h][j] + 0.001);
              }
            }
          }
        }
  for (h = 0; h < 5; h++)
  {
    for (j = 0; j < 5; j++)
    {
      if (hybrid.generation[hy].organism[0].allele[h].reg_direction != hybrid.generation[hy].organism[0].allele[j].pro_direction)
      {
        hybrid.generation[hy].organism[0].GRN[h][j] = (hybrid.generation[hy].organism[0].GRN[h][j] * (-1));
      }
    }
  }
  

         hybrid.generation[hy].organism[0].phenotype = mat_power (hybrid.generation[hy].organism[0].GRN, y, 5, 5, 100); 

       double euc_dist = sqrt (vectors_dot_prod (hybrid.generation[hy].organism[0].phenotype, hybrid.generation[hy].organism[0].phenotype, 5) + vectors_dot_prod (optimal_phenotype, optimal_phenotype, 5) - (2 * vectors_dot_prod (optimal_phenotype, hybrid.generation[hy].organism[0].phenotype, 5) ) );

       hybrid.generation[hy].organism[0].fitness = exp (((-1) * (pow ((euc_dist), 2))));
       fprintf (hybridfit, "%f\n", hybrid.generation[hy].organism[0].fitness);
  
        }
  

  fclose(fit);
  fclose(flocation);
  fclose(hybridfit);
  fclose(allo1fit);
  fclose(allo2fit);

  free(allo1.generation);
  free(allo2.generation);
  free(hybrid.generation);
/*  FILE *f; 
 *  f = fopen ("long-run-lin1.dat", "wb"); 
 *  fwrite (lin.generation, sizeof(population), 10000000, f); 
 *  fclose(f);  
 */ //  free (lin.generation); 
}