public nn InitialiseCopy(int hiddenLayerCount, int hiddenNeuronCount) //to add { nn n = new nn(); List <Matrix <float> > newWeights = new List <Matrix <float> >(); for (int i = 0; i < this.weights.Count; i++) { Matrix <float> currentWeight = Matrix <float> .Build.Dense(weights[i].RowCount, weights[i].ColumnCount); for (int x = 0; x < currentWeight.RowCount; x++) { for (int y = 0; y < currentWeight.ColumnCount; y++) { currentWeight[x, y] = weights[i][x, y]; } } newWeights.Add(currentWeight); } List <float> newBiases = new List <float>(); newBiases.AddRange(biases); n.weights = newWeights; n.biases = newBiases; n.InitialiseHidden(hiddenLayerCount, hiddenNeuronCount); return(n); }
private void initialFillPopulationWithRandomValues (nn[] newPopulation, int startingIndex) { if(fileexist) { int n=data.lastgenoms.Length; for(int i=0;i<n;i++) { newPopulation[i]= data.lastgenoms[i]; } currentGeneration=data.lastgeneration; bestAgentSelection=0; currentGenome=0; fileexist=false; } else { while (startingIndex < initialPopulation) { newPopulation[startingIndex] = new nn(); newPopulation[startingIndex].Initialise(enemy.hiddenLayer, enemy.hiddenneurons); startingIndex++; } currentGenome=0; } }
public void ResetWithNetwork(nn net) { NN = net; centertime = 0f; fitnessvalue = 0f; ballhit = 0; powerups = 0; ball.GetComponent <ball>().reset(); }
private void FillPopulationWithRandomValues (nn[] newPopulation, int startingIndex) { while (startingIndex < initialPopulation) { newPopulation[startingIndex] = new nn(); newPopulation[startingIndex].Initialise(enemy.hiddenLayer, enemy.hiddenneurons); startingIndex++; } }
public static int Main() { int N = 15; Func <double, double> fit_func = (f) => { // return Cos(5*f)*Exp(-f*f); return(f * Exp(-f * f)); // return Exp(-f*f); }; Func <double, double> activate_func = (f) => { // return Cos(f); return(Cos(5 * f) * Exp(-f * f)); }; nn NN = new nn(N / 3, activate_func); double a = -3 / 2; double b = 3 / 2; vector x = new vector(N); vector y = new vector(N); System.IO.StreamWriter data = new System.IO.StreamWriter("xydata.txt"); for (int i = 0; i < N; i++) { x[i] = a + (b - a) * i / (N - 1); y[i] = fit_func(x[i]); data.WriteLine("{0} {1}", x[i], y[i]); } data.Close(); for (int i = 0; i < NN.n; i++) { NN.parameters[3 * i + 0] = a + (b - a) * i / (NN.n - 1); NN.parameters[3 * i + 1] = 1; NN.parameters[3 * i + 2] = 1; } NN.train(x, y); vector fit_x = new vector(vector.linspace(a, b, 100)); WriteLine("fit data:"); System.IO.StreamWriter fit = new System.IO.StreamWriter("fitdata.txt"); for (int i = 0; i < fit_x.size; i++) { double feeded = NN.feedforwad(fit_x[i]); // fit.WriteLine($"{feeded}"); fit.WriteLine("{0} {1}", fit_x[i], feeded); } fit.Close(); return(0); }
private void SortPopulation() { for (int i = 0; i < numpopulation.Length; i++) { for (int j = i; j < numpopulation.Length; j++) { if (numpopulation[i].fitness < numpopulation[j].fitness) { nn temp = numpopulation[i]; numpopulation[i] = numpopulation[j]; numpopulation[j] = temp; } } } }
public void Death (float fitness, nn network,float maxfitness) { if (currentGenome < numpopulation.Length -1) { if(fitness>maxfitness) { bestAgentSelection++; } numpopulation[currentGenome].fitness = fitness; currentGenome++; ResetToCurrentGenome(); } else { RePopulate(); //bestAgentSelection=0; } }
private nn[] PickBestPopulation() { nn[] newPopulation = new nn[initialPopulation]; for (int i = 0; i < bestAgentSelection; i++) { newPopulation[naturallySelected] = numpopulation[i]; newPopulation[naturallySelected].fitness = 0; naturallySelected++; int f = Mathf.RoundToInt(numpopulation[i].fitness /10); for (int c = 0; c < f; c++) { genePool.Add(i); } } int last = numpopulation.Length - 1; for (int i = 0; i < worstAgentSelection; i++) { last -= i; int f = Mathf.RoundToInt(numpopulation[last].fitness /10); for (int c = 0; c < f; c++) { genePool.Add(last); } } return newPopulation; }
private void Crossover (nn[] newPopulation) { for (int i = 0; i < numberToCrossover; i+=2) { int AIndex = i; int BIndex = i + 1; if (genePool.Count >= 1) { for (int l = 0; l < 100; l++) { AIndex = genePool[Random.Range(0, genePool.Count)]; BIndex = genePool[Random.Range(0, genePool.Count)]; if (AIndex != BIndex) break; } } nn Child1 = new nn(); nn Child2 = new nn(); Child1.Initialise(enemy.hiddenLayer, enemy.hiddenneurons); Child2.Initialise(enemy.hiddenLayer, enemy.hiddenneurons); Child1.fitness = 0; Child2.fitness = 0; for (int w = 0; w < Child1.weights.Count; w++) { if (Random.Range(0.0f, 1.0f) < 0.5f) { Child1.weights[w] = numpopulation[AIndex].weights[w]; Child2.weights[w] = numpopulation[BIndex].weights[w]; } else { Child2.weights[w] = numpopulation[AIndex].weights[w]; Child1.weights[w] = numpopulation[BIndex].weights[w]; } } for (int w = 0; w < Child1.biases.Count; w++) { if (Random.Range(0.0f, 1.0f) < 0.5f) { Child1.biases[w] = numpopulation[AIndex].biases[w]; Child2.biases[w] = numpopulation[BIndex].biases[w]; } else { Child2.biases[w] = numpopulation[AIndex].biases[w]; Child1.biases[w] = numpopulation[BIndex].biases[w]; } } newPopulation[naturallySelected] = Child1; naturallySelected++; newPopulation[naturallySelected] = Child2; naturallySelected++; } }
public static int Main() { int N = 15; // Func<double,double> activate_func = (f) => // { // // return Cos(f); // // return Sin(f); // return Exp(-f*f); // // return Cos(5*f)*Exp(-f*f); // }; Func <double, double> fit_func = (f) => { // return Cos(5*f)*Exp(-f*f); // return Exp(-f*f); return(f * Exp(-f * f)); }; Func <double, double> derive_func = (f) => { // return -2*f*Exp(-f*f); // return -Exp(-f*f)*(5*Sin(5*f)+2*f*Cos(5*f)); return(Exp(-f * f) - 2 * f * f * Exp(-f * f)); }; Func <double, double> int_func = (f) => { // return Sqrt(PI)/2*errorfunction(f); // return (Sqrt(2)/2)*(2*errorfunction(f))/(Exp(24/4)); return(-1.0 / 2 * Exp(-f * f)); }; nn NN = new nn(N / 3, fit_func, derive_func, int_func); double a = -3 / 2; double b = 3 / 2; vector x = new vector(N); vector y = new vector(N); vector dy = new vector(N); vector Y = new vector(N); System.IO.StreamWriter data = new System.IO.StreamWriter("xydata.txt"); for (int i = 0; i < N; i++) { x[i] = a + (b - a) * i / (N - 1); y[i] = fit_func(x[i]); dy[i] = derive_func(x[i]); Y[i] = int_func(x[i]); data.WriteLine("{0} {1} {2} {3}", x[i], y[i], dy[i], Y[i]); } data.Close(); for (int i = 0; i < NN.n; i++) { NN.parameters[3 * i + 0] = a + (b - a) * i / (NN.n - 1); NN.parameters[3 * i + 1] = 1; NN.parameters[3 * i + 2] = 1; } NN.train(x, y); vector fit_x = new vector(vector.linspace(a, b, 100)); WriteLine("fit data:"); System.IO.StreamWriter fit = new System.IO.StreamWriter("fitdata.txt"); for (int i = 0; i < fit_x.size; i++) { double feeded = NN.feedforwad(fit_x[i]); double derived = NN.ff_derivative(fit_x[i]); double integrated = NN.ff_integrate(fit_x[i]); // fit.WriteLine($"{feeded}"); fit.WriteLine("{0} {1} {2} {3}", fit_x[i], feeded, derived, 0.28 + integrated); } fit.Close(); return(0); }