Пример #1
0
        public static void WriteNs(NeuralNet NN)
        {
            FileStream   fs = new FileStream(NPath, FileMode.Create, FileAccess.Write, FileShare.None);
            StreamWriter sw = new StreamWriter(fs);

            foreach (Neuron n in NN.Neurons)
            {
                sw.WriteLine("Neuron" + ' ' + n.layer);
                if (n.layer == 0)
                {
                    for (int i = 0; i <= 7; i++)
                    {
                        for (int ii = 0; ii <= 7; ii++)
                        {
                            sw.Write(Math.Abs(n.weights[i, ii]).ToString());
                            if (ii < 7)
                            {
                                sw.Write(" ");
                            }
                        }
                        sw.WriteLine();
                    }
                }
                else
                {
                    int count = 1;
                    //Layweights are all the same?
                    foreach (KeyValuePair <Neuron, double> kvp in n.layWeights)
                    {
                        sw.Write(Math.Abs(kvp.Value).ToString());
                        if (count < n.layWeights.Count())
                        {
                            sw.Write(" ");
                        }
                        count++;
                    }
                    sw.WriteLine();
                }
            }
            sw.Close(); fs.Close();
        }
Пример #2
0
        public static void Play(Board b)
        {
            Board  b2 = GoDiePointers.DeepClone(b);
            Player PW = new Player(true); NeuralNet NNW = new NeuralNet(PW, 3, 10);

            Data.ReadNs(NNW);
            Player PB = new Player(false); NeuralNet NNB = new NeuralNet(PB, 3, 10);

            Data.ReadNs(NNB);
            foreach (Piece p in b2.Pieces)
            {
                if (p is Empty)
                {
                    continue;
                }
                if (p.Player.IsW == true)
                {
                    p.Player = PW;
                }
                else
                {
                    p.Player = PB;
                }
            }
            List <Neuron> BestNeurons = GoDiePointers.DeepClone(NNW.Neurons);

            Random random             = new Random();

            //Amount of weights to change
            int changeCount = 5;

            for (int j = 0; j < changeCount; j++)
            {
                //For neurons
                //It increases/decreases the weight by rand.next(x, y)% [normally]
                //It currently is used as an input for the sigmoid (as a randomizing factor)
                double randomVal = random.Next(-14, 14);
                //For pieces
                double pieceRVal  = random.Next(1, 19) / 10.00;
                int    randNeuron = random.Next(0, NNW.Neurons.Count);
                int    randthing  = random.Next(1, 2);
                int    X          = random.Next(0, 7);
                int    Y          = random.Next(0, 7);
                try
                {
                    if (randthing == 1)
                    {
                        if (BestNeurons[randNeuron].layer == 0)
                        {
                            NNW.Neurons[randNeuron].weights[X, Y] =
                                Sigmoid.sigmoid(randomVal);
                        }
                        else
                        {
                            KeyValuePair <Neuron, double> kvp = NNW.Neurons[randNeuron].layWeights.ElementAt(random.Next(0, NNW.Neurons[randNeuron].layWeights.Count));
                            NNW.Neurons[randNeuron].layWeights[kvp.Key] =
                                Sigmoid.sigmoid(randomVal);
                        }
                    }
                    if (randthing == 2)
                    {
                        if (BestNeurons[randNeuron].layer == 0)
                        {
                            NNB.Neurons[randNeuron].weights[X, Y] = Sigmoid.sigmoid(randomVal);
                        }
                        else
                        {
                            KeyValuePair <Neuron, double> kvp = NNB.Neurons[randNeuron].layWeights.ElementAt(random.Next(0, NNB.Neurons[randNeuron].layWeights.Count));
                            NNB.Neurons[randNeuron].layWeights[kvp.Key] =
                                Sigmoid.sigmoid(randomVal);
                        }
                    }

                    /*
                     * Disabled for now
                     * also, it has a 50% chance of selecting the empty squares with the current x/y randomizer
                     * //Changing class values?
                     * if (randthing == 3)
                     * {
                     *  b.Pieces[X, Y].CVal = (int)(pieceRVal * (GoDiePointers.DeepClone(b.Pieces[X, Y].CVal)));
                     * }
                     * //Repeat to equalize chances of neuron vs piece
                     * if (randthing == 4)
                     * {
                     *  b2.Pieces[Y, X].CVal = (int)(pieceRVal * (GoDiePointers.DeepClone(b.Pieces[X, Y].CVal)));
                     * }
                     */
                }
                catch (Exception ex) { Console.WriteLine(ex); return; }
            }

            //At movecap, end playing, and write whoever had a higher score to the weight list file
            int moveCap = 100;
            int i       = 1;

            //While it has not moved too many times, and while no-one has won, play
            //Run in parallel?

            //Using two boards to allow for different piece cvals, unless I want to put that into the NN class?
            while (i <= moveCap && !b.WWin && !b.BWin && !b2.WWin && !b2.BWin && !b.Stale && !b2.Stale)
            {
                if (b.WTurn)
                {
                    b2.Pieces = NNW.Move(b, true).Pieces; Board.PrintBoard(b2); b2.WTurn = false; i++;
                }
                if (!b2.WTurn)
                {
                    b.Pieces = NNB.Move(b2, false).Pieces; Board.PrintBoard(b); b.WTurn = true; i++;
                }
                else
                {
                    Console.WriteLine("NN Failure"); break;
                }
            }
            //Will need to check whether pieces read/write properly in the future

            //If white won, write white's data
            if (b.WWin || b2.WWin) /*Data.WritePieces(b);*/ Data {
Пример #3
0
        public static void ReadNs(NeuralNet NN)
        {
            NN.Neurons = new List <Neuron>();
            FileStream   fs = new FileStream(NPath, FileMode.Open, FileAccess.Read, FileShare.None);
            StreamReader sr = new StreamReader(fs);

            while (!sr.EndOfStream)
            {
                string   line      = sr.ReadLine();
                string[] splitLine = line.Split(' ');

                if (splitLine[0] == "Neuron" && splitLine[1] == "0")
                {
                    //fs.Position++;
                    int.TryParse(splitLine[1], out int result);
                    Neuron n = new Neuron(NN, 0, result);
                    for (int j = 0; j <= 7; j++)
                    {
                        line      = sr.ReadLine();
                        splitLine = line.Split(' ');
                        for (int i = 0; i <= 7; i++)
                        {
                            try
                            {
                                double.TryParse(splitLine[i], out double result2);
                                n.weights[j, i] = result2;
                            }
                            catch { }
                        }
                    }
                }
                if (splitLine[0] == "Neuron" && splitLine[1] != "0")
                {
                    Dictionary <Neuron, double> layerwets = new Dictionary <Neuron, double>();
                    int.TryParse(splitLine[1], out int result);
                    line      = sr.ReadLine();
                    splitLine = line.Split(' ');

                    //MAGIC CODE, DO NOT TOUCH

                    for (int i = (splitLine.Count() * (result - 1)); i <= (splitLine.Count() * result) - 1; i++)
                    {
                        if (!layerwets.ContainsKey(NN.Neurons[i]))
                        {
                            if (result == NN.Neurons[i].layer + 1)
                            {
                                double.TryParse(splitLine[i - (splitLine.Count() * (result - 1))], out double result2);
                                layerwets.Add(NN.Neurons[i], result2);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Failure to copy neuron " + NN.Neurons[i].ToString() + " " + NN.Neurons[i].layer.ToString());
                        }
                    }
                    Neuron n = new Neuron(NN, layerwets, 0, result);
                    if (result == 3)
                    {
                        NN.Output = n;
                    }
                }
            }
            sr.Close(); fs.Close();
        }
Пример #4
0
        public static void activeUI()
        {
            Console.WriteLine("Command?");
            string command = Console.ReadLine();

            try
            {
                if (command[0] == 'm')
                {
                    bool x = int.TryParse(command[1].ToString(), out int result);
                    bool y = int.TryParse(command[2].ToString(), out int result2);
                    bool z = int.TryParse(command[3].ToString(), out int result3);
                    bool q = int.TryParse(command[4].ToString(), out int result4);
                    if (x && y && z && q)
                    {
                        ActiveBoard = (ActiveBoard.Pieces[result, result2]).Move(ActiveBoard, result3, result4);
                    }
                }
                if (command.ToLower() == "learn")
                {
                    NeuralNet.Play(ActiveBoard);
                }
                if (command[0] == 'p' || command[0] == 'P')
                {
                    if (ActiveBoard.WTurn)
                    {
                        NeuralNet NN = new NeuralNet(player1, 3, 10);
                        Data.ReadNs(NN);
                        ActiveBoard = NN.Move(ActiveBoard, NN.Player.IsW);
                    }
                    else
                    {
                        NeuralNet NN = new NeuralNet(player2, 3, 10);
                        Data.ReadNs(NN);
                        ActiveBoard = NN.Move(ActiveBoard, NN.Player.IsW);
                    }
                }
                if (command == "initialize")
                {
                    NeuralNet NN = new NeuralNet(player2, 3, 10);
                    NN.initNN();
                    Data.WriteNs(NN);
                }
                if (command == "load")
                {
                    NeuralNet NN = new NeuralNet(player2, 3, 10);
                    Data.ReadNs(NN);
                    Data.WriteNs(NN);
                }
                //'s' to stop learning
                if (command.Length >= 2 && command[0] == 'p' && command[1] == 'm')
                {
                    bool x = int.TryParse(command[2].ToString(), out int result);
                    bool y = int.TryParse(command[3].ToString(), out int result2);
                    bool z = int.TryParse(command[4].ToString(), out int result3);
                    bool q = int.TryParse(command[5].ToString(), out int result4);
                    if (x && y && z && q)
                    {
                        ActiveBoard.Pieces[result, result2].Move(ActiveBoard, result3, result4);
                    }
                }
            }
            catch (Exception ex) { Console.WriteLine("Failure"); Console.WriteLine(ex); }
            finally
            {
                Board.PrintBoard(ActiveBoard);
                activeUI();
            }
        }