Example #1
0
        public bool Process(IBitInput input)
        {
            if (input.Length != InputCount)
            {
                throw new Exception("Invalid input bit count");
            }

            bool result     = InitValue;
            int  inputCount = InputCount;

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

                case LogicOperation.Or:
                    result |= input[i];
                    break;

                case LogicOperation.And:
                    result &= input[i];
                    break;

                case LogicOperation.Xor:
                    result ^= input[i];
                    break;
                }
            }

            result ^= Negative;

            return(result);
        }
Example #2
0
        public void Process(IBitInput input, IBitOutput output)
        {
            if (input.Length != InputCount)
            {
                throw new Exception("Invalid input bit count");
            }

            if (output.Length != OutputCount)
            {
                throw new Exception("Invalid output bit count");
            }

            int outputCount = OutputCount;

            for (int bitPos = 0; bitPos < outputCount; bitPos++)
            {
                INeuron neuron = neurons[bitPos];
                output[bitPos] = neuron.Process(input);
            }

            //Parallel.For(0, outputCount, bitPos =>
            //{
            //    INeuron neuron = neurons[bitPos];
            //    output[bitPos] = neuron.Process(input);
            //});
        }
Example #3
0
        public static LogicOperation GetLogicOperation(IBitInput operationBits)
        {
            if (operationBits.Length != LogicOperationBitCount)
            {
                throw new Exception("Invalid bit count");
            }

            int operationNr = (operationBits[0] ? 2 : 0)
                              + (operationBits[1] ? 1 : 0);

            return((LogicOperation)operationNr);
        }
Example #4
0
        public void Load(int inputCount, int outputCount, IBitInput logic)
        {
            levels.Clear();
            if (genome == null)
            {
                brainStructure = new BrainStructure()
                {
                    InputCount = inputCount,
                    LevelSizes = new int[] { outputCount }
                };

                genome = new Genome(brainStructure);
                genome.Load(logic);
            }

            // add memory input and output
            inputCount += brainStructure.MemoryBitCount;

            int genomeOffset = 0;

            for (int levelIndex = 0; levelIndex < brainStructure.LevelSizes.Length; levelIndex++)
            {
                DigitalLevel level = new DigitalLevel();

                outputCount = brainStructure.LevelSizes[levelIndex];
                int logicLength = BrainStructure.GetLevelLogicLength(inputCount, outputCount);

                logic         = genome.Copy(genomeOffset, logicLength);
                genomeOffset += logicLength;

                level.Load(inputCount, outputCount, logic);
                levels.Add(level);
                inputCount = outputCount;
            }

            // Load initial memory status
            if (brainStructure.MemoryBitCount > 0)
            {
                Memory = new BitStorage();
                Memory.Load(genome.Copy(genomeOffset, brainStructure.MemoryBitCount));
            }

            InputCount  = brainStructure.InputCount;
            OutputCount = brainStructure.OutputCount;

            if (autoCompile)
            {
                new Thread(() =>
                {
                    Compile();
                }).Start();
            }
        }
Example #5
0
        /// <summary>
        /// Load data from IBitInput
        /// </summary>
        /// <param name="values">array with data</param>
        public void Load(IBitInput input)
        {
            if (length == 0)
            {
                length = input.Length;
            }

            if (input.Length != length)
            {
                throw new Exception("Invalid input bit count");
            }

            bits = (BitArray)input.GetBitArray().Clone();
        }
Example #6
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);
            }
        }
Example #7
0
        public void Load(int inputCount, IBitInput logic)
        {
            InputCount  = inputCount;
            OutputCount = 1;

            logicOperations = new LogicOperation[inputCount];

            // Load neuron state
            InitValue = logic[0];
            Negative  = logic[1];

            for (int inputNr = 0; inputNr < inputCount; inputNr++)
            {
                IBitInput operationBits = logic.Copy(Settings.NeuronStateBitCount + inputNr * Settings.LogicOperationBitCount, Settings.LogicOperationBitCount);
                logicOperations[inputNr] = Settings.GetLogicOperation(operationBits);
            }
        }
Example #8
0
        public void Process(IBitInput input, IBitOutput output)
        {
            if (input.Length != InputCount)
            {
                throw new Exception("Invalid input bit count");
            }

            if (output.Length != OutputCount)
            {
                throw new Exception("Invalid output bit count");
            }

            // Add memory bits to input
            if (Memory != null)
            {
                var inputWithMemory = new BitStorage(input.Length + Memory.Length);

                for (int i = 0; i < input.Length; i++)
                {
                    inputWithMemory[i] = input[i];
                }

                for (int i = 0; i < Memory.Length; i++)
                {
                    inputWithMemory[i + input.Length] = Memory[i];
                }

                input = inputWithMemory;
            }

            BitStorage result = null;

            if (compiled != null)
            {
                result = new BitStorage(OutputCount + (Memory != null ? Memory.Length : 0));
                compiled.Invoke(null, new object[] { input, result });
            }
            else
            {
                foreach (ILevel level in Levels)
                {
                    result = new BitStorage(level.OutputCount);
                    level.Process(input, result);
                    input = result;
                }
            }

            if (result.Length == OutputCount)
            {
                output.Load(result);
            }
            else
            {
                output.Load(result.Copy(0, OutputCount));

                // Update memory
                for (int i = 0; i < Memory.Length; i++)
                {
                    Memory[i] = result[OutputCount + i];
                }
            }
        }
Example #9
0
 public void Process(IBitInput input, IBitOutput output)
 {
     output[0] = Process(input);
 }
Example #10
0
 public void Load(int inputCount, int outputCount, IBitInput logic)
 {
     Load(inputCount, logic);
 }