Beispiel #1
0
        public static DataView Compute()
        {
            Antigene Ag = new Antigene(Helper.CellSize);

            int step = 0;

            Antibody bestantibody = null;

            List <Population> populations = new List <Population>();

            populations.Add(new Population());

            populations[0].Init();

            while (step != Helper.NumberOfGeneration)
            {
                bestantibody = populations[step].FindBestAntibody(Ag);

                populations.Add(populations[step].GenerateNewPopulation());

                step++;

                var aff = Helper.Affinity(bestantibody, Ag);
                if (aff == 0)
                {
                    break;
                }
                else
                {
                }
            }


            DataView dv = new DataView {
                mAg          = Ag,
                mAb          = bestantibody,
                mPopulations = populations
            };

            return(dv);
            //}

            //public static void Main(string[] args)
            //{

            //	Antigene Ag = new Antigene(Helper.CellSize);

            //	int step = 0;

            //	Console.WriteLine("Generations: " + Helper.NumberOfGeneration);

            //	Antibody bestantibody = null;

            //	Population population = new Population();

            //	population.Init();

            //	while (step != Helper.NumberOfGeneration) {

            //		bestantibody = population.FindBestAntibody(Ag);

            //		population = population.GenerateNewPopulation();

            //		step++;

            //		var aff = Helper.Affinity(bestantibody, Ag);
            //		if (aff == 0) {
            //			Console.WriteLine(step);
            //			break;
            //		} else {
            //			//Console.WriteLine(step);
            //		}
            //	}

            //	//Console.WriteLine("\nMemory cell:");
            //	//Helper.Print(bestantibody);
        }
Beispiel #2
0
        public Antibody FindBestAntibody(Antigene pAg)
        {
            for (int i = 0; i < Helper.NumberOfAntibody; i++)
            {
                ABs.Add(new Antibody(Helper.CellSize));
            }

            List <KeyValuePair <int, int> > aff = new List <KeyValuePair <int, int> >();

            for (int i = 0; i < Helper.NumberOfAntibody; i++)
            {
                aff.Add(new KeyValuePair <int, int>(Helper.Affinity(ABs[i], pAg), i));
            }

            aff.Sort((x, y) => {
                return(x.Key.CompareTo(y.Key));
            });

            //TODO remove bad items
            aff = aff.GetRange(0, amount);


            //TODO cloning good items
            // Clonning
            List <int> c_amount = new List <int>();

            for (int _i = 0; _i < aff.Count; _i++)
            {
                double qwe = (int)((beta * Helper.NumberOfAntibody) / (aff[_i].Value + 1));
                c_amount.Add((int)qwe);
            }

            int Nc = 0;             // sum of the all clones

            for (int i = 0; i < c_amount.Count; i++)
            {
                Nc += c_amount[i];
            }

            List <Antibody> C         = new List <Antibody>();
            List <int>      C_indexes = new List <int>();

            for (int i = 0; i < aff.Count; i++)
            {
                while (c_amount[i] != 0)
                {
                    C.Add(ABs[aff[i].Value].Copy());
                    C_indexes.Add(aff[i].Value + 1);
                    c_amount[i]--;
                }
            }

            // Mutation of all clones
            for (int i = 0; i < Nc; i++)
            {
                double p_mut = p_mutmax * ((beta * C_indexes[i]) / Helper.NumberOfAntibody);
                C[i].Mutate(p_mut);
            }

            // Affinity between antibody and antigene
            List <KeyValuePair <int, int> > aff_star = new List <KeyValuePair <int, int> >();

            for (int i = 0; i < Nc; i++)
            {
                aff_star.Add(new KeyValuePair <int, int>(Helper.Affinity(C[i], pAg), i));
            }

            aff_star.Sort((x, y) => {
                return(x.Key.CompareTo(y.Key));
            });


            // Replacing antibody with its best clone
            if (aff_star[0].Key < Helper.Affinity(ABs[0], pAg))
            {
                ABs[0] = C[aff_star[0].Value].Copy();
            }

            BestAffinity = Helper.Affinity(ABs[0], pAg);

            return(ABs[0]);
        }