示例#1
0
        /// <summary>
        /// Main program.
        /// </summary>
        static void Main(string[] args)
        {
            // Run the genetic algorithm and get the best brain.
            double[] output = Setup();

            // Convert the best brain's output into a program.
            string program = ConvertDoubleArrayToBF(output);
            Console.WriteLine(program);
            Console.WriteLine("------");

            try
            {
                // Run the program.
                Interpreter bf = new Interpreter(program, null, (b) =>
                {
                    Console.Write((char)b);
                });

                bf.Run(_maxIterationCount);
            }
            catch
            {
            }

            Console.ReadKey();
        }
示例#2
0
        /// <summary>
        /// Fitness function to evaluate the current genetic algorithm. We decode the weights, run the resulting program, and score the output.
        /// </summary>
        /// <param name="weights">Array of double (genes), where each value cooresponds to a Brainfuck program command.</param>
        /// <returns>double, indicating the score</returns>
        private static double fitnessFunction(double[] weights)
        {
            double fitness = 0;
            string console = "";
            bool noErrors = false;

            // Get the resulting Brainfuck program.
            string program = ConvertDoubleArrayToBF(weights);

            try
            {
                // Run the program.
                Interpreter bf = new Interpreter(program, null, (b) =>
                {
                    console += (char)b;
                });
                bf.Run(_maxIterationCount);

                // It runs!
                noErrors = true;
            }
            catch
            {
            }

            // Order bonus.
            for (int i = 0; i < _targetString.Length; i++)
            {
                if (console.Length > i)
                {
                    fitness += 256 - Math.Abs(console[i] - _targetString[i]);
                }
            }

            // Is this a new best fitness?
            if (fitness > _bestFitness)
            {
                _bestFitness = fitness;
                _bestOutput = console;
                _bestNoErrors = noErrors;
                _bestLastChangeDate = DateTime.Now;
                _bestProgram = program;
            }

            return fitness;
        }
示例#3
0
        /// <summary>
        /// Fitness function to evaluate the current genetic algorithm. We decode the weights, run the resulting program, and score the output.
        /// </summary>
        /// <param name="weights">Array of double (genes), where each value cooresponds to a Brainfuck program command.</param>
        /// <returns>double, indicating the score</returns>
        private static double fitnessFunction(double[] weights)
        {
            double fitness = 0;
            StringBuilder console = new StringBuilder();
            bool noErrors = false;
            Interpreter bf = null;

            // Get the resulting Brainfuck program.
            string program = ConvertDoubleArrayToBF(weights);

            try
            {
                // Run the program.
                bf = new Interpreter(program, null, (b) =>
                {
                    console.Append((char)b);

                    // If we've printed out more than our target string, then kill the program. This saves us the time of running the remaining iterations. Ok maybe this is cheating, but at least it's still in the fitness function.
                    if (console.Length >= _targetString.Length)
                    {
                        bf.m_Stop = true;
                    }
                });
                bf.Run(_maxIterationCount);

                // It runs!
                noErrors = true;
            }
            catch
            {
            }

            // Order bonus.
            for (int i = 0; i < _targetString.Length; i++)
            {
                if (console.Length > i)
                {
                    fitness += 256 - Math.Abs(console[i] - _targetString[i]);
                }
            }

            // Is this a new best fitness?
            if (fitness > _bestFitness)
            {
                _bestFitness = fitness;
                _bestOutput = console.ToString();
                _bestNoErrors = noErrors;
                _bestLastChangeDate = DateTime.Now;
                _bestProgram = program;
                _bestTicks = bf.m_Ticks;
            }

            return fitness;
        }