Esempio n. 1
0
        private void NuevoGen(Individ i1, Individ i2, bool mutate)
        {
            // Randomly use gene from one of the parent
            for (int i = 0; i < Gene.Length; i++)
            {
                Gene[i] = (rnd.Next(0, 2) == 0 ? i1.Gene[i] : i2.Gene[i]);
            }

            if (mutate)
            {
                // value should be between 0 and 10
                int variability = 2;

                if (rnd.Next(0, 12 - variability) == 0)
                {
                    Gene[0] = (byte)rnd.Next(0, ProgramText.VAlphabet.Length);
                }

                if (rnd.Next(0, 12 - variability) == 0)
                {
                    Gene[1] = (byte)rnd.Next(0, ProgramText.OAlphabet.Length);
                }

                if (rnd.Next(0, 12 - variability) == 0)
                {
                    Gene[2] = (byte)rnd.Next(0, ProgramText.VAlphabet.Length);
                }

                if (rnd.Next(0, 12 - variability) == 0)
                {
                    Gene[3] = (byte)rnd.Next(0, ProgramText.OAlphabet.Length);
                }

                if (rnd.Next(0, 12 - variability) == 0)
                {
                    Gene[4] = (byte)rnd.Next(0, ProgramText.VAlphabet.Length);
                }

                if (rnd.Next(0, 12 - variability) == 0)
                {
                    Gene[5] = (byte)rnd.Next(0, ProgramText.OAlphabet.Length);
                }

                if (rnd.Next(0, 12 - variability) == 0)
                {
                    Gene[6] = (byte)rnd.Next(0, ProgramText.VAlphabet.Length);
                }

                if (rnd.Next(0, 12 - variability) == 0)
                {
                    Gene[7] = (byte)rnd.Next(0, ProgramText.OAlphabet.Length);
                }

                if (rnd.Next(0, 12 - variability) == 0)
                {
                    Gene[8] = (byte)rnd.Next(0, ProgramText.VAlphabet.Length);
                }
            }
        }
Esempio n. 2
0
        //// Creates Individ with random gene
        //public Individ()
        //{
        //    Gene = new byte[genSize];

        //    NuevoGen();
        //    while (!genContVar())
        //        NuevoGen();

        //    Deviation = int.MaxValue;
        //}



        // Creates Individ with random gene
        public Individ(Individ i1, Individ i2, bool mutate)
        {
            Gene = new byte[genSize];

            if (i1 == null)
            {
                NuevoGen();
            }
            else
            {
                NuevoGen(i1, i2, mutate);
            }

            Deviation = int.MaxValue;
        }
Esempio n. 3
0
 // For sorting by deviation from training results
 static int IndividComparizon(Individ i1, Individ i2)
 {
     if (i1.Deviation < i2.Deviation)
     {
         return(-1);
     }
     else
     if (i1.Deviation > i2.Deviation)
     {
         return(1);
     }
     else
     {
         return(0);
     }
 }
        // Creates individual with random gene, that is compiled successfully
        //public static Individ CreateIndivid()
        //{
        //    Individ ind = new Individ();

        //    CompilerResults results = null;

        //    // Loop until compiled
        //    while (true)
        //    {
        //        // Get code sample with randomly built function
        //        sProgramCodeFinal = ProgramText.GetProgramCode(ind.Gene);

        //        // Try to compile
        //        results = provider.CompileAssemblyFromSource(parameters, sProgramCodeFinal);
        //        if (results.Errors.HasErrors)
        //        {
        //            ind = new Individ();
        //            continue;
        //        }

        //        break;
        //    }

        //    // Get access to the compiled function
        //    Assembly assembly = results.CompiledAssembly;
        //    Type program = assembly.GetType("Program.WorkingClass");
        //    MethodInfo main = program.GetMethod("F");

        //    int[] f_results = new int[TrainingDataBack.a_array.Length];

        //    // Calculate function values with training data
        //    for (int itry = 0; itry < TrainingDataBack.a_array.Length; itry++)
        //    {
        //        InvokeParams[0] = TrainingDataBack.a_array[itry];
        //        InvokeParams[1] = TrainingDataBack.b_array[itry];
        //        f_results[itry] = Convert.ToInt32(main.Invoke(null, InvokeParams));
        //    }

        //    // Calculate deviation with the training data at the place
        //    ind.CalculateDeviation(f_results);

        //    return ind;
        //}

        // // Creates individual with two parent's genes and possible mutation
        public static Individ CreateIndivid(Individ i1 = null, Individ i2 = null, bool mutate = true)
        {
            Individ ind = new Individ(i1, i2, mutate);

            CompilerResults results = null;

            // Loop until compiled
            while (true)
            {
                // Get code sample with randomly built function
                sProgramCodeFinal = ProgramText.GetProgramCode(ind.Gene);

                // Try to compile
                results = provider.CompileAssemblyFromSource(parameters, sProgramCodeFinal);
                if (results.Errors.HasErrors)
                {
                    ind = new Individ(i1, i2, true);
                    continue;
                }

                break;
            }

            // Get access to the compiled function
            Assembly   assembly = results.CompiledAssembly;
            Type       program  = assembly.GetType("Program.WorkingClass");
            MethodInfo main     = program.GetMethod("F");

            int[] f_results = new int[TrainingDataBack.a_array.Length];

            // Calculate function values with training data
            for (int itry = 0; itry < TrainingDataBack.a_array.Length; itry++)
            {
                InvokeParams[0] = TrainingDataBack.a_array[itry];
                InvokeParams[1] = TrainingDataBack.b_array[itry];
                f_results[itry] = Convert.ToInt32(main.Invoke(null, InvokeParams));
            }

            ind.CalculateDeviation(f_results);

            return(ind);
        }