using System; namespace Science.Biology.PopulationGenetics { /// /// Chromosome /// public class Chromosome { private int numberOfAllele = 1,numberOfGenes = 1; private int[,] configuration; private double[,] mutation; // mutation between chromosomes private double[] recombination; public int NumberOfAllele { get{return numberOfAllele;} } public int NumberOfGenes { get{return numberOfGenes;} } public int[,] Configuration // gene configuration { get{return configuration;} } public double[,] Mutation // property of mutation { get{return mutation;} } public double[] Recombination // property of Recombination { get{return recombination;} } private Gene[] genes; public Chromosome() { configuration = new int[numberOfAllele,numberOfGenes]; mutation = new double[numberOfAllele,numberOfAllele]; configuration[0,0] = 0; mutation[0,0] = 1.0; } public Chromosome(Gene inputGene1) // ordering inputGenes { genes = new Gene[1]; genes[0] = inputGene1; numberOfGenes = genes.Length; doit(); } public Chromosome(Gene inputGene1, Gene inputGene2) // ordering inputGenes { genes = new Gene[2]; genes[0] = inputGene1; genes[1] = inputGene2; numberOfGenes = genes.Length; doit(); } public Chromosome(Gene inputGene1, Gene inputGene2, Gene inputGene3) // ordering inputGenes { genes = new Gene[3]; genes[0] = inputGene1; genes[1] = inputGene2; genes[2] = inputGene3; numberOfGenes = genes.Length; doit(); } public Chromosome(Gene inputGene1, Gene inputGene2, Gene inputGene3, Gene inputGene4) // ordering inputGenes { genes = new Gene[4]; genes[0] = inputGene1; genes[1] = inputGene2; genes[2] = inputGene3; genes[3] = inputGene4; numberOfGenes = genes.Length; doit(); } public Chromosome(Gene[] inputGenes) // ordering inputGenes { genes = inputGenes; numberOfGenes = genes.Length; doit(); } private void doit() { for(int i = 0; i < numberOfGenes; i++) numberOfAllele *= genes[i].NumberOfAllele; FindConfiguration(); FindMutation(); FindRecombination(); } private void FindConfiguration() { configuration = new int[numberOfAllele,numberOfGenes]; int temp; for(int i = 0; i < numberOfAllele; i++) { temp = i; for(int j = numberOfGenes -1; j >= 0 ; j--) { configuration[i,j] = temp % genes[j].NumberOfAllele; temp -= configuration[i,j]; temp /= genes[j].NumberOfAllele; } } } private void FindMutation() { mutation = new double[numberOfAllele,numberOfAllele]; for(int i = 0; i < numberOfAllele; i++) { for(int j = 0; j < numberOfAllele; j++) { mutation[i,j] = 1.0; for(int k = 0; k < numberOfGenes ; k++) { mutation[i,j] *= genes[k].Mutation [configuration[i,k],configuration[j,k]]; } } } } private double factor3 = 0.0000001; public double RecombinationFactor { set{factor3=value;} } private void FindRecombination() { recombination = new Double[numberOfGenes - 1]; for(int i = 0; i < numberOfGenes - 1; i++) { recombination[i] = factor3*(genes[i+1].StartBase - genes[i].EndBase); } } public int Allele(int[] geneConfiguration) { int y = 0; for(int i = 0; i < numberOfGenes; i++) { y = genes[i].NumberOfAllele*y + geneConfiguration[i]; } return y; } } }