public NeuronalNetwork CloneFullMesh() { //TODO make this mess pretty if (!this.fullMeshGenerated) { throw new NeuronalNetworkNotFullmeshedException(); } NeuronalNetwork copy = new NeuronalNetwork(); foreach (InputNeuron input in inputNeurons) { copy.AddInputNeuron((InputNeuron)input.NameCopy()); } foreach (WorkingNeuron wn in hiddenNeurons) { copy.AddHiddenNeuron((WorkingNeuron)wn.NameCopy()); } foreach (WorkingNeuron wn in outputNeurons) { copy.AddOutputNeuron((WorkingNeuron)wn.NameCopy()); } copy.GenerateFullMesh(); for (int i = 0; i < hiddenNeurons.Count; i++) { List <Connection> connectionsOrginal = hiddenNeurons[i].GetConnections(); List <Connection> connectionsCopy = copy.hiddenNeurons[i].GetConnections(); if (connectionsOrginal.Count != connectionsCopy.Count) { throw new NotSameAmountOfNeuronsException(); } for (int k = 0; k < connectionsOrginal.Count; k++) { connectionsCopy[k].weight = connectionsOrginal[k].weight; } } for (int i = 0; i < outputNeurons.Count; i++) { List <Connection> connectionsOrginal = outputNeurons[i].GetConnections(); List <Connection> connectionsCopy = copy.outputNeurons[i].GetConnections(); if (connectionsOrginal.Count != connectionsCopy.Count) { throw new NotSameAmountOfNeuronsException(); } for (int k = 0; k < connectionsOrginal.Count; k++) { connectionsCopy[k].weight = connectionsOrginal[k].weight; } } return(copy); }
public static void Test() { Console.WriteLine("Begin NN Test!"); NeuronalNetwork nn = new NeuronalNetwork(); InputNeuron in1 = new InputNeuron(); InputNeuron in2 = new InputNeuron(); InputNeuron in3 = new InputNeuron(); WorkingNeuron out1 = new WorkingNeuron(0); WorkingNeuron out2 = new WorkingNeuron(0); WorkingNeuron out3 = new WorkingNeuron(0); nn.AddInputNeuron(in1); nn.AddInputNeuron(in2); nn.AddInputNeuron(in3); nn.GenerateHiddenNeurons(3, 1); nn.AddOutputNeuron(out1); nn.AddOutputNeuron(out2); nn.AddOutputNeuron(out3); nn.GenerateFullMesh(); nn.RandomizeAllWeights(); NeuronalNetwork nn2 = nn.CloneFullMesh(); for (int i = 0; i < 3; i++) { Debug.Assert(nn2.GetOutputNeuronFromIndex(i).GetValue() == nn.GetOutputNeuronFromIndex(i).GetValue()); } Console.WriteLine("NN Test success! <(^.^)>"); }