public NeuronLayer(int neurons, IActivationFunction activationFunction)
 {
     NumberOfNeurons    = neurons;
     Neurons            = new MatrixFloat(1, NumberOfNeurons);
     Gradient           = new MatrixFloat(1, NumberOfNeurons);
     ActivationFunction = activationFunction;
 }
Пример #2
0
        public Grid(IOsnowaContextManager contextManager, bool contextShouldBePresent = false)
        {
            if (!contextManager.HasContext)
            {
                if (contextShouldBePresent)
                {
                    throw new ArgumentException("Missing context where it's expeteced to be present");
                }
            }
            else
            {
                _positionFlags         = contextManager.Current.PositionFlags;
                _walkability           = contextManager.Current.Walkability;
                _pathfindingDataHolder = contextManager.Current.PathfindingData;
            }

            contextManager.ContextReplaced += newContext =>
            {
                _positionFlags         = newContext.PositionFlags;
                _walkability           = newContext.Walkability;
                _pathfindingDataHolder = newContext.PathfindingData;
            };
            MinPosition = Position.Zero;

            _passingLightFlagAsUlong = Convert.ToUInt64(PositionFlag.PassingLight);
        }
Пример #3
0
 public OsnowaContext(int xSize, int ySize)
 {
     PositionFlags       = new PositionFlags(xSize, ySize);
     PathfindingData     = new PathfindingDataHolder(xSize, ySize);
     TileMatricesByLayer = new MatrixByte[(int)TilemapLayer.TotalLayersCount];
     Walkability         = new MatrixFloat(xSize, ySize);
     for (int i = 0; i < TileMatricesByLayer.Length; i++)
     {
         TileMatricesByLayer[i] = new MatrixByte(xSize, ySize);
     }
     VisibleEntities = new HashSet <IPositionedEntity>();
 }
Пример #4
0
 public WeightLayer(NeuronLayer prev, NeuronLayer next)
 {
     Weights = new MatrixFloat(prev.NumberOfNeurons, next.NumberOfNeurons);
     for (int row = 0; row < prev.NumberOfNeurons; row++)
     {
         for (int column = 0; column < next.NumberOfNeurons; column++)
         {
             Weights[row, column] = Helper.random.Range(-1f, 1f);
         }
     }
     Gradient = new MatrixFloat(prev.NumberOfNeurons, next.NumberOfNeurons);
 }
Пример #5
0
        public Input(int neurons, IActivationFunction activationFunction)
        {
            NumberOfNeurons    = neurons;
            Neurons            = new MatrixFloat(1, NumberOfNeurons);
            Gradient           = new MatrixFloat(1, NumberOfNeurons);
            _bias              = new MatrixFloat(1, NumberOfNeurons);
            ActivationFunction = activationFunction;

            for (int i = 0; i < _bias.Columns; i++)
            {
                _bias[0, i] = 1;
            }
        }
Пример #6
0
        public void Init(IExampleContextManager contextManager, BuildingIngredientConfig config, WorldGeneratorConfig worldGeneratorConfig, IRandomNumberGenerator rng,
                         ValueMap civilizationMap, ValueMap soilMap, ValueMap vegetationMapValues, ValueMap debugMapValues)
        {
            _rng           = rng;
            _config        = config;
            _soilMap       = soilMap;
            _walkability   = contextManager.Current.Walkability;
            _vegetationMap = vegetationMapValues;
            base.Init(contextManager.Current, config, worldGeneratorConfig);

            Values            = new ValueMap(1, worldGeneratorConfig.XSize, worldGeneratorConfig.YSize);
            _tileMatricesByte = GameContext.TileMatricesByLayer;
            _tileset          = worldGeneratorConfig.Tileset;
            _gameConfig       = worldGeneratorConfig.GameConfig;

            _grid       = new Grid(contextManager, true);
            _pathfinder = Pathfinder.Create(contextManager);
        }
Пример #7
0
        public override IEnumerator Recalculating()
        {
            MatrixFloat walkabilityMap = _context.Walkability;

            foreach (Position cellMiddle in _waterMap.AllCellMiddles())
            {
                if (Math.Abs(_waterMap.Get(cellMiddle) - WaterIngredientGenerator.Ground) < 0.01f)
                {
                    Values.Set(cellMiddle, 1f);
                    walkabilityMap.Set(cellMiddle, 1f);
                }
                else
                {
                    Values.Set(cellMiddle, 0f);
                    walkabilityMap.Set(cellMiddle, 0f);
                }
            }

            yield return(new WaitForSeconds(0.1f));
        }
Пример #8
0
        public void ApplyGradToBias()
        {
            var update = _bias.Transpose().Multiply(Gradient);

            _bias -= update;
        }
        private static void Main(string[] args)
        {
            var activation = new Relu();

            var n = new FeedForward();

            n.AddLayer(new NeuronLayer(2, activation));
            n.AddLayer(new NeuronLayer(3, activation));
            n.AddLayer(new NeuronLayer(1, activation));

            n.Construct();

            var str = "";

            for (int i = 0; i < n.Output.Length; i++)
            {
                str += n.Output[i].ToString() + "\n";
            }

            var inp = new MatrixFloat[] {
                new MatrixFloat(new float[, ] {
                    { 1, 1 }
                }),
                new MatrixFloat(new float[, ] {
                    { 0, 1 }
                }),
                new MatrixFloat(new float[, ] {
                    { 1, 0 }
                }),
                new MatrixFloat(new float[, ] {
                    { 0, 0 }
                }),
            };

            var expected = new MatrixFloat[] {
                new MatrixFloat(new float[, ] {
                    { 0 }
                }),
                new MatrixFloat(new float[, ] {
                    { 1 }
                }),
                new MatrixFloat(new float[, ] {
                    { 1 }
                }),
                new MatrixFloat(new float[, ] {
                    { 0 }
                })
            };

            for (int i = 0; i < 500000; i++)
            {
                var error = 0f;
                for (int k = 0; k < 4; k++)
                {
                    var np1 = inp[k];
                    for (int j = 0; j < np1.Columns; j++)
                    {
                        n.Input[j] = np1[0, j];
                    }
                    var exp = expected[k];

                    n.Forward();

                    for (int z = 0; z < exp.Columns; z++)
                    {
                        var tmp = (n.Output[z] - exp[0, z]);
                        error += tmp * tmp;
                    }

                    n.Backward(exp);

                    n.Clear();
                }
                Console.WriteLine(error);
            }

            Console.ReadKey();
        }