Esempio n. 1
0
        static void AddNeuronResult(DigitalNeuron neuron, StringBuilder code, string input)
        {
            int start = code.Length;
            int count = 0;

            code.Append(neuron.InitValue ? "true" : "false");

            int inputCount      = neuron.InputCount;
            var logicOperations = neuron.LogicOperations;

            for (int i = 0; i < inputCount; i++)
            {
                switch (logicOperations[i])
                {
                case LogicOperation.None:
                    continue;

                case LogicOperation.Or:
                    code.Append(" | " + input + "[" + i + "])");
                    count++;
                    break;

                case LogicOperation.And:
                    code.Append(" & " + input + "[" + i + "])");
                    count++;
                    break;

                case LogicOperation.Xor:
                    code.Append(" ^ " + input + "[" + i + "])");
                    count++;
                    break;
                }
            }

            if (neuron.Negative)
            {
                code.Append(" ^ true");
            }
            else
            {
                code.Append(" ^ false");
            }

            for (int i = 0; i < count; i++)
            {
                code.Insert(start, '(');
            }
        }
Esempio n. 2
0
        public void Load(int inputCount, int outputCount, IBitInput logic)
        {
            InputCount  = inputCount;
            OutputCount = outputCount;

            neurons = new List <INeuron>(outputCount);
            int neuronLogicLength = logic.Length / outputCount;

            for (int i = 0; i < outputCount; i++)
            {
                DigitalNeuron neuron      = new DigitalNeuron();
                IBitInput     neuronLogic = logic.Copy(i * neuronLogicLength, neuronLogicLength);
                neuron.Load(inputCount, neuronLogic);
                neurons.Add(neuron);
            }
        }
Esempio n. 3
0
        public static MethodInfo CompileProcessor(IBrain brain)
        {
            var code = new StringBuilder(Start);

            string input = "input";
            string output;

            for (int i = 0; i < brain.Levels.Count; i++)
            {
                var level = brain.Levels[i];

                // if last layer
                if (i == brain.Levels.Count - 1)
                {
                    output = "output";
                }
                else
                {
                    output = "n" + i;
                    code.AppendLine("bool[] " + output + " = new bool[" + level.OutputCount + "];");
                }

                for (int bitPos = 0; bitPos < level.OutputCount; bitPos++)
                {
                    DigitalNeuron neuron = level.Neurons[bitPos] as DigitalNeuron;
                    code.Append(output + "[" + bitPos + "] = ");
                    AddNeuronResult(neuron, code, input);
                    code.AppendLine(";");
                }

                input = output;
            }

            code.Append(End);
            return(CompileProcessor(code));
        }