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 {
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(); } }