Exemplo n.º 1
0
        public void NextStep(object actionLock)
        {
            BitStorage brainInput  = new BitStorage(Brain.InputCount);
            int        bitPosition = 0;

            // Prepare brain input using sensors
            foreach (ISensor sensor in Sensors)
            {
                sensor.Prepare(this);
                for (int i = 0; i < sensor.Length; i++)
                {
                    brainInput[i + bitPosition] = sensor[i];
                }

                bitPosition += sensor.Length;
            }

            BitStorage brainOutput = new BitStorage(Brain.OutputCount);

            Brain.Process(brainInput, brainOutput);

            // Make actions using brain output
            bitPosition = 0;
            lock (actionLock)
            {
                foreach (IAction action in Actions)
                {
                    action.Load(brainOutput.Copy(bitPosition, action.Length));
                    action.Do(this);
                    bitPosition += action.Length;
                }
            }

            Age++;
        }
Exemplo n.º 2
0
        public void SetBlock(int x, int y, int z, int blockStateId)
        {
            if (Pallete != null)
            {
                if (Pallete.Contains(blockStateId))
                {
                    blockStateId = Pallete.IndexOf(blockStateId);
                }
                else
                {
                    Pallete.Add(blockStateId);
                    blockStateId = Pallete.Count - 1;
                    byte bitsNeeded = Utils.BitsNeededToStore(blockStateId);
                    if (bitsNeeded > Data.BitsPerVar)
                    {
                        if (bitsNeeded > PalleteBitsPerVarMax)
                        {
                            BitStorage newStorage = new BitStorage(PalleteBitsPerVarMax, Volume);
                            for (int i = 0; i < Volume; i++)
                            {
                                newStorage.Set(i, Pallete[Data.Get(i)]);
                            }

                            Pallete = null;
                            Data    = newStorage;
                        }
                        else
                        {
                            BitStorage newStorage = new BitStorage(bitsNeeded, Volume);
                            for (int i = 0; i < Volume; i++)
                            {
                                newStorage.Set(i, Data.Get(i));
                            }

                            Data = newStorage;
                        }
                    }
                }
            }

            int oldBlock = GetBlock(x, y, z);

            if (oldBlock == 0 && blockStateId != 0)
            {
                SolidBlockCount++;
            }
            else if (oldBlock != 0 && blockStateId == 0)
            {
                SolidBlockCount--;
            }

            Data.Set(GetBlockIndex(x, y, z), blockStateId);
        }
Exemplo n.º 3
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();
            }
        }
Exemplo n.º 4
0
        public void NextStep(bool print)
        {
            Parallel.For(0, brains.Count, b =>
            {
                Brain brain       = brains[b];
                brain.Correct     = 0;
                BitStorage output = brain.Output;

                foreach (var pair in numbers)
                {
                    BitStorage input = pair.Key;
                    bool isPrime     = pair.Value;

                    brain.Process(input, output);
                    bool result = output[0];

                    if (result == isPrime)
                    {
                        brain.Correct++;
                    }
                }
            });

            brains = brains.OrderBy(b => b.Correct).ToList();

            Brain bestBrain   = brains[brains.Count - 1];
            Brain secondBrain = brains[brains.Count - 2];
            Brain worstBrain  = brains[0];

            brains.Remove(worstBrain);

            if (print)
            {
                Console.WriteLine("best : " + bestBrain.Correct + ", second: " + secondBrain.Correct + ", worst: " + worstBrain.Correct + ", max: " + maxCorrect);
            }

            if (bestBrain.Correct > (maxCorrect * 85) / 100)
            {
                AddNumber();
            }

            Genome genome   = sex.MakeChild(bestBrain.Genome, secondBrain.Genome, random.Next(1, 100));
            Brain  newBrain = new Brain();

            newBrain.Load(genome);
            brains.Add(newBrain);
        }
Exemplo n.º 5
0
        public void SetSkyLight(int x, int y, int z, int skyLight)
        {
            int chunkSectionIdx = GetChunkSectionIdx(y);

            if (SkyLightSections[chunkSectionIdx] == null)
            {
                if (skyLight == 0)
                {
                    return;
                }

                SkyLightSections[chunkSectionIdx] = new BitStorage(4, ChunkSection.Volume);
                SkyLightMask |= 1 << chunkSectionIdx;
            }

            SkyLightSections[chunkSectionIdx].Set(GetChunkRelBlockIdx(x, y, z), skyLight);
        }
Exemplo n.º 6
0
    static void Main(string[] args)
    {
        BitStorage bitStore = new BitStorage();

        Console.WriteLine("Bits: " + bitStore.bits.ToString());
        bitStore.Add(1);
        Console.WriteLine("Bits added(1): " + bitStore.bits.ToString());
        bitStore.Add(2);
        Console.WriteLine("Bits added(2): " + bitStore.bits.ToString());

        Console.WriteLine("Bits has (1): " + bitStore.Has(1).ToString());
        Console.WriteLine("Bits has (4): " + bitStore.Has(4).ToString());

        bitStore.Remove(1);
        Console.WriteLine("Bits removed(1): " + bitStore.bits.ToString());
        Console.WriteLine("Bits has (1): " + bitStore.Has(1).ToString());
        Console.WriteLine("Bits has (2): " + bitStore.Has(2).ToString());
    }
Exemplo n.º 7
0
        void Start()
        {
            int            memorySize     = 2;
            BrainStructure brainStructure = new BrainStructure()
            {
                InputCount     = 16,
                LevelSizes     = new int[] { 3 + memorySize },
                MemoryBitCount = memorySize
            };

            Genome genome = new Genome(brainStructure);

            genome.SetRandomValues();
            Console.WriteLine("Genome: " + genome.GetView());

            DigitalBrain brain = new DigitalBrain();

            brain.Load(genome);

            BitStorage input  = new BitStorage(brainStructure.InputCount);
            BitStorage output = new BitStorage(brainStructure.OutputCount);

            for (int i = 0; i < 5; i++)
            {
                input.SetRandomValues();
                Console.WriteLine("\r\nInput: " + input.GetView());
                for (int j = 0; j < 1; j++)
                {
                    //int bitToChange = Common.Random.Next(genome.Length);
                    //genome[bitToChange] ^= true;
                    //Console.WriteLine("Genome: " + genome.GetView());

                    //brain.Load(genome);

                    for (int k = 0; k < 3; k++)
                    {
                        brain.Process(input, output);
                        Console.WriteLine("Output: " + output.GetView() + " Memory: " + brain.Memory.GetView());
                    }
                }
            }
        }
Exemplo n.º 8
0
        void TestCompilation()
        {
            var bs = new BrainStructure()
            {
                InputCount     = 100,
                MemoryBitCount = 16,
                LevelSizes     = new int[] { 50, 24 }
            };
            Genome genome = new Genome(bs);

            genome.SetRandomValues();

            var brain = new DigitalBrain();

            brain.Load(genome);

            var output = new BitStorage(bs.OutputCount);
            var input  = new BitStorage(bs.InputCount);

            input.SetRandomValues();
            Console.WriteLine(input.GetView());

            int count = 10000;
            var start = DateTime.Now;

            Parallel.For(0, count, i =>
            {
                brain.Process(input, output);
            });

            Console.WriteLine(output.GetView() + " " + count / (DateTime.Now - start).TotalSeconds);

            brain.Compile();
            start = DateTime.Now;
            Parallel.For(0, count, i =>
            {
                brain.Process(input, output);
            });

            Console.WriteLine(output.GetView() + " " + count / (DateTime.Now - start).TotalSeconds);
        }
Exemplo n.º 9
0
        public void AddNumber()
        {
            maxCorrect++;
            max += 2;
            bool isPrime = IsPrime(max);

            BitStorage input = new BitStorage(16);

            for (int bit = 0; bit < 16; bit++)
            {
                input[bit] = ((max >> bit) & 1) > 0;
            }
            numbers.Add(input, isPrime);

            Console.WriteLine("Adding " + max + " - " + isPrime);
            if (isPrime)
            {
                primes.Add(max);
            }
            else
            {
                nonPrimes.Add(max);
            }
        }
Exemplo n.º 10
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];
                }
            }
        }
Exemplo n.º 11
0
 public ChunkSection(BitStorage data, short solidBlockCount = 0, List <int> pallete = null)
 {
     Data            = data;
     SolidBlockCount = solidBlockCount;
     Pallete         = pallete;
 }
Exemplo n.º 12
0
 public ChunkSection()
 {
     Data    = new BitStorage(4, Volume);
     Pallete = new List <int>();
     Pallete.Add(0);
 }
Exemplo n.º 13
0
        public ChunkDataPacket(Client client, ChunkColumn column) : base(client)
        {
            PacketId = 0x22;
            MCSerializer serializer = new MCSerializer();


            serializer.WriteInt(column.X);
            serializer.WriteInt(column.Z);
            serializer.WriteBool(true);
            serializer.WriteVarInt(column.SectionMask);
            NbtWriter  heightmapWriter  = new NbtWriter(serializer.Stream, "root");
            BitStorage heightmapStorage = new BitStorage(9, 256);

            for (int y = 0; y < 256; y++)
            {
                for (int z = 0; z < 16; z++)
                {
                    for (int x = 0; x < 16; x++)
                    {
                        if (column.GetBlock(x, y, z).StateId != 0)
                        {
                            heightmapStorage.Set(ChunkColumn.GetHeightmapIdx(x, z), y);
                        }
                    }
                }
            }

            heightmapWriter.WriteLongArray("MOTION_BLOCKING", heightmapStorage.Data);
            heightmapWriter.EndCompound();
            heightmapWriter.Finish();

            for (int y = 0; y < 256; y += 4)
            {
                for (int z = 0; z < 16; z += 4)
                {
                    for (int x = 0; x < 16; x += 4)
                    {
                        serializer.WriteInt((int)column.GetBiome(x, y, z));
                    }
                }
            }

            MCSerializer dataSerializer = new MCSerializer();
            int          primaryBitMask = column.SectionMask;

            for (int y = 0; y < 16; y++)
            {
                if ((primaryBitMask & 0b1) == 1)
                {
                    column.Sections[y].Write(dataSerializer);
                }

                primaryBitMask >>= 1;
            }

            byte[] encodedChunkData = dataSerializer.GetBytes();
            serializer.WriteVarInt(encodedChunkData.Length);
            serializer.WriteBytes(encodedChunkData);

            // TODO: Block Entities
            serializer.WriteVarInt(0);
            Data = serializer.GetBytes();
        }
Exemplo n.º 14
0
        void Performance()
        {
            var brains = new List <IBrain>();
            var bs     = new BrainStructure()
            {
                InputCount     = 100,
                MemoryBitCount = 16,
                LevelSizes     = new int[] { 50, 24 }
            };

            for (int i = 0; i < 100; i++)
            {
                Genome genome = new Genome(bs);
                genome.SetRandomValues();

                var brain = new DigitalBrain();
                brain.Load(genome);
                brains.Add(brain);
            }

            Console.WriteLine("Compiling");
            int nr     = 0;
            var cStart = DateTime.Now;

            Parallel.ForEach(brains, brain =>
            {
                (brain as DigitalBrain).Compile();
                lock (brains)
                {
                    Console.SetCursorPosition(0, 0);
                    Console.WriteLine("Compiling " + ++nr);
                }
            });

            Console.WriteLine("Compilations per second " + nr / (DateTime.Now - cStart).TotalSeconds);
            return;

            var      output = new BitStorage(bs.OutputCount);
            int      op     = 0;
            DateTime start  = DateTime.Now;

            for (int i = 0; i <= 10000; i++)
            {
                var input = new BitStorage(bs.InputCount);
                input.SetRandomValues();
                Parallel.ForEach(brains, brain =>
                {
                    brain.Process(input, output);
                });

                op += brains.Count;

                if (i % 10 == 0)
                {
                    double seconds     = (DateTime.Now - start).TotalSeconds;
                    double performance = op;
                    performance /= seconds;

                    Console.SetCursorPosition(0, 0);
                    Console.WriteLine(seconds.ToString("f2") + " => " + i + " ; " + performance.ToString("f2"));
                }
            }
            Console.WriteLine("Press any key");
            Console.ReadKey(true);
        }