Exemplo n.º 1
0
        public Gene crossOver(Gene gene)
        {
            Gene Child = new Gene();

            int seed = unchecked(DateTime.Now.Ticks.GetHashCode());
            Random rnd = new Random(seed);

            int place = rnd.Next(1, this.binString.Length - 1);
            Child.binString = this.getStringFirstPart(place) + gene.getStringSecondPart(place);
            return Child;
        }
Exemplo n.º 2
0
        private void GA_Click(object sender, EventArgs e)
        {
            //num of population
            int population = 50;
            int iteration = 100;
            double crossOverRate = 0.5 ;
            double mutationRate = 0.1 ;

            string file = @"GA" + tbMinSim.Text + DateTime.Now.ToString("HH_mm_ss") + ".csv";
            System.IO.StreamWriter outputFile;
            outputFile = new System.IO.StreamWriter(file, false);

            decimal minSim = Convert.ToDecimal(tbMinSim.Text);

            List<Gene> genes = new List<Gene>();

            List<int> taintIds = new List<int>();

            int totalBenign = 0;
            for (int i = 0; i < simTable.Count; i++)
            {
                if (simTable[i].familyName == "Benign")
                {
                    totalBenign++;
                }
            }

            int numOfTaintTrait = simTable[0].similarity.Count;
            //generate first generation
            for(int i = 0 ; i < population ; i++)
            {
                int seed = unchecked(DateTime.Now.Ticks.GetHashCode());
                Gene newGene = new Gene();
                Random rnd = new Random(seed);
                for (int j = 0; j < numOfTaintTrait; j++)
                {
                    if ((taintTraitQuality[j + 1].numOfMalware - taintTraitQuality[j + 1].numOfBenign) < 10 || taintTraitQuality[j + 1].numOfBenign > 10)
                    {
                        newGene.binString += '0';
                    }
                    else
                    {
                        newGene.binString += rnd.Next(0, 2).ToString();
                    }

                }
                newGene.computeFitness(ref simTable, minSim, totalBenign);
                genes.Add(newGene);
            }

            for(int i = 0 ; i < iteration ; i++)
            {
                genes.Sort((x, y) => x.fitness.CompareTo(y.fitness));

                for(int k = 0 ; k < Convert.ToInt32(population*crossOverRate) ; k++ )
                {
                    genes.RemoveAt(0);
                }
                int seed = unchecked(DateTime.Now.Ticks.GetHashCode());

                Random rnd = new Random(seed);

                while (genes.Count < population)
                {
                    Gene newGene = new Gene();
                    int select1, select2;
                    select1 = rnd.Next(0, genes.Count);
                    select2 = rnd.Next(0, genes.Count);
                    while (select1 == select2)
                    {
                        select2 = rnd.Next(0, genes.Count);
                    }
                    newGene = genes[select1].crossOver(genes[select2]);
                    newGene.computeFitness(ref simTable, minSim, totalBenign);
                    genes.Add(newGene);
                }
                //for (int j = 0; j < genes.Count; j++ )
                //{
                //    if (((float)rnd.Next(5000) / 5000) > mutationRate)
                //    {
                //        genes[j].mutation();
                //        genes[j].computeFitness(ref simTable, minSim, totalBenign);
                //    }
                //}
                genes.Sort((x, y) => y.fitness.CompareTo(x.fitness));
                Console.WriteLine("tp:" + genes[0].tp.ToString() + "  dRate :" + genes[0].dRate.ToString() + " fit:"+genes[0].fitness.ToString());
                outputFile.WriteLine((i + 1).ToString() + "," + genes[0].fp.ToString() + "," + genes[0].tp.ToString() + "," + genes[0].dRate.ToString() + "," + genes[0].fitness.ToString());
            }
            genes.Sort((x, y) => y.tp.CompareTo(x.tp));
            outputFile.WriteLine(genes[0].binString + ",1," + genes[0].fp.ToString() + "," + genes[0].tp.ToString() + "," + genes[0].dRate.ToString() + "," + genes[0].fitness.ToString());
            outputFile.WriteLine(genes[1].binString + ",2," + genes[1].fp.ToString() + "," + genes[1].tp.ToString() + "," + genes[1].dRate.ToString() + "," + genes[1].fitness.ToString());
            outputFile.WriteLine(genes[2].binString + ",3," + genes[2].fp.ToString() + "," + genes[2].tp.ToString() + "," + genes[2].dRate.ToString() + "," + genes[2].fitness.ToString());
            outputFile.Close();
        }