Esempio n. 1
0
        static void Main(string[] args)
        {
            var net = new Net(Function.Sigmoid, new Layer(2), new Layer(5), new Layer(1));

            TeachNet(net);

            //net.Save(@"d:\temp\fonet.xml");

            Console.WriteLine("Data: {0,0} (0)");
            var data = net.Calculate(new float[] { 0, 0 });

            Console.WriteLine("Result: {0}", data[0]);

            Console.WriteLine("Data: {0,1} (1)");
            data = net.Calculate(new float[] { 0, 1 });
            Console.WriteLine("Result: {0}", data[0]);

            Console.WriteLine("Data: {1,0} (1)");
            data = net.Calculate(new float[] { 1, 0 });
            Console.WriteLine("Result: {0}", data[0]);

            Console.WriteLine("Data: {1,1} (0)");
            data = net.Calculate(new float[] { 1, 1 });
            Console.WriteLine("Result: {0}", data[0]);

            Console.ReadLine();
        }
Esempio n. 2
0
        private double feedForward(double[][][,] values, double[][][,] errors, double[][,] input, double[] expected)
        {
            Net.Calculate(input, values);

            var lidx   = Net.LayerCount - 1;
            var result = values[lidx];
            var len    = result.GetLength(0);
            var output = new double[len];

            for (int j = 0; j < len; j++)
            {
                output[j] = result[j][0, 0];
            }

            var llayer = Net[lidx];

            for (int p = 0; p < llayer.OutputDepth; p++)
            {
                var ej    = m_LossFunction.Derivative(p, output, expected);
                var value = result[p][0, 0];
                var deriv = (llayer.ActivationFunction != null) ? llayer.ActivationFunction.DerivativeFromValue(value) : 1;
                errors[lidx][p][0, 0] = ej * deriv / m_BatchSize;
            }

            var loss = m_LossFunction.Value(output, expected) / m_BatchSize;

            if (m_Regularizator != null)
            {
                loss += (m_Regularizator.Value(Net.Weights) / m_BatchSize);
            }

            return(loss);
        }
Esempio n. 3
0
        private double feedForward(double[][,] input, Class cls)
        {
            Net.Calculate(input, m_Values);

            var lidx   = Net.LayerCount - 1;
            var result = m_Values[lidx];
            var errors = m_Errors[lidx];
            var len    = result.GetLength(0);
            var output = new double[len];

            for (int j = 0; j < len; j++)
            {
                output[j] = result[j][0, 0];
            }

            var expect = m_ExpectedOutputs[cls];
            var llayer = Net[lidx];

            for (int p = 0; p < llayer.OutputDepth; p++)
            {
                var ej    = m_LossFunction.Derivative(p, output, expect);
                var value = result[p][0, 0];
                var deriv = (llayer.ActivationFunction != null) ? llayer.ActivationFunction.DerivativeFromValue(value) : 1;
                errors[p][0, 0] = ej * deriv / m_BatchSize;
            }

            return(m_LossFunction.Value(output, expect) / m_BatchSize);
        }
        public GamePlayerMoveResult MakeMove(PieceColour pieceColour, GameState gameState)
        {
            // build list of previous game states
            var       previousGameStates = new HashSet <GameState>();
            GameState lastGameState      = gameState;

            while ((lastGameState = lastGameState.PreviousGameState) != null)
            {
                previousGameStates.Add(lastGameState);
            }

            var currentMoves = gameState.CalculateAvailableMoves(pieceColour).ToList();

            if (!currentMoves.Any())
            {
                return(new GamePlayerMoveResult(null, MoveStatus.NoLegalMoves));
            }

            var distinctMoves = currentMoves.Where(gm => !previousGameStates.Contains(gm.GameState)).ToList();

            if (!distinctMoves.Any())
            {
                return(new GamePlayerMoveResult(null, MoveStatus.Resign));
            }

            var moveWeights = new List <Tuple <double, GameMove> >();

            foreach (var move in distinctMoves)
            {
                var moveMetric = move.CalculateGameMoveMetrics(pieceColour);

                var inputs = new double[] {
                    moveMetric.CreatedFriendlyKings,
                    moveMetric.FriendlyMovesAvailable,
                    moveMetric.OpponentMovesAvailable,
                    moveMetric.NextMoveFriendlyPiecesAtRisk,
                    moveMetric.NextMoveOpponentPiecesAtRisk,
                    moveMetric.NextMoveFriendlyKingsCreated,
                    moveMetric.NextMoveOpponentKingsCreated,
                    moveMetric.TotalPieces / 100.0,
                    moveMetric.TotalFriendlyPieces / 100.0,
                    moveMetric.TotalOpponentPieces / 100.0,
                    moveMetric.TotalMinionPieces / 100.0,
                    moveMetric.TotalFriendlyMinionPieces / 100.0,
                    moveMetric.TotalOpponentMinionPieces / 100.0,
                    moveMetric.TotalKingPieces / 100.0,
                    moveMetric.TotalFriendlyKingPieces / 100.0,
                    moveMetric.TotalOpponentKingPieces / 100.0,
                    moveMetric.FriendlyMinionsHome / 100.0,
                    moveMetric.OpponentMinionsHome / 100.0,
                    moveMetric.FriendlyMinionsAway / 100.0,
                    moveMetric.OpponentMinionsAway / 100.0,
                    moveMetric.OpponentPiecesTaken / 100.0
                };

                double weightedResult = Net.Calculate(inputs).Single();

                var resultTuple = new Tuple <double, GameMove>(weightedResult, move);
                moveWeights.Add(resultTuple);
            }

            var selectedMove      = moveWeights.OrderByDescending(m => m.Item1).First().Item2;
            var selectedGameState = selectedMove.PerformMove();
            var result            = new GamePlayerMoveResult(selectedGameState, MoveStatus.SuccessfulMove);

            return(result);
        }