internal void MutateNetworks() { for (int netI = 0; netI < networks.Count; netI++) { MutationInfo currentInfo = networks[netI].Network.MutationInfo; if (currentInfo == null) { throw new NullReferenceException(); } networks[netI].Network.MutateNetwork(out List <NetworkPosition> addedNeuronPositions); int addedLastI; if (currentInfo.mutateOutput) { if (addedNeuronPositions[addedLastI = addedNeuronPositions.Count - 1].layerIndex == networks[netI].Network.Lenght - 1) { List <int> networkIndex = GetNetworksConnectedTo(netI, addedNeuronPositions[addedLastI].neuronIndex, out List <int> connectionIndexes); for (int connectedI = 0; connectedI < networkIndex.Count; connectedI++) { networks[networkIndex[connectedI]].Connections[connectionIndexes[connectedI]].AdjustForPreviousNeuronChange(addedNeuronPositions[addedLastI].neuronIndex); } } } for (int connectionI = 0; connectionI < networks[netI].Connections.Count; connectionI++) { networks[netI].Connections[connectionI].MutateWeigths(currentInfo); } } }
/// <summary> /// Starts a new family tree with a random biased network /// </summary> public EvolutionaryLine(Game startingState, int startingNetworkCount, MutationInfo mutationInfo, TemporalNetwork.ActivationFunctions activationFunction, int inputLenght, int outputLenght, int layersCount, int minNeurons, int maxNeurons) { networks = new List <FittedNetwork <Game> >(); for (int i = 0; i < startingNetworkCount; i++) { networks.Add(new FittedNetwork <Game>(startingState, mutationInfo, activationFunction, inputLenght, outputLenght, layersCount, minNeurons, maxNeurons)); } }
internal void MutateWeigths(MutationInfo mutationInfo) { for (int i = 0; i < weigths.Count; i++) { for (int j = 0; j < weigths[i].Count; j++) { weigths[i][j] += GetVariation(mutationInfo.maxVariation, mutationInfo.valueMutationPercentage); } } }
public TemporalNetwork(ActivationFunctions activation, List <TemporalLayer> layers, MutationInfo mutationInfo = null) { ActivationFunction = activation; TemporalLayers = layers; if (mutationInfo != null) { this.MutationInfo = mutationInfo; } else { this.MutationInfo = new MutationInfo(); } }
public static TemporalNetwork FromString(string str, MutationInfo mutationInfo = null) { str = str.Replace("activation: ", ""); string activationS = str.Remove(str.IndexOf("\nlayer:")); str = str.Remove(0, str.IndexOf("layer")); string[] srs = str.Split(new string[] { "layer:\n" }, StringSplitOptions.RemoveEmptyEntries); List <TemporalLayer> layers = new List <TemporalLayer>(); foreach (var layer in srs) { layers.Add(TemporalLayer.FromString(layer)); } return(new TemporalNetwork((ActivationFunctions)Enum.Parse(typeof(ActivationFunctions), activationS), layers, mutationInfo)); }
public TemporalNetwork(ActivationFunctions activationFunction, int inputLenght, List <List <NeuronTypes> > neuronTypes, List <List <Interconnection> > connections = null, MutationInfo mutationInfo = null) { TemporalLayers = new List <TemporalLayer> { new TemporalLayer(inputLenght, neuronTypes[0]) }; for (int i = 1; i < neuronTypes.Count; i++) { TemporalLayers.Add(new TemporalLayer(neuronTypes[i - 1].Count, neuronTypes[i])); } ActivationFunction = activationFunction; if (mutationInfo != null) { this.MutationInfo = mutationInfo; } else { this.MutationInfo = new MutationInfo(); } if (connections == null) { for (int layerIndex = 0; layerIndex < TemporalLayers.Count; layerIndex++) { TemporalLayers[layerIndex].Connections = new List <Interconnection>(); } } else { int lastIndex = -1; for (int layerIndex = 0; layerIndex < Math.Min(connections.Count, TemporalLayers.Count); layerIndex++) { TemporalLayers[layerIndex].Connections.AddRange(connections[layerIndex]); lastIndex = layerIndex; } for (int i = lastIndex; i < TemporalLayers.Count; i++) { TemporalLayers[i].Connections = new List <Interconnection>(); } } }
public TemporalNetwork(ActivationFunctions activation, int inputLenght, int outputLenght, int layerCount, int minNeurons, int maxNeurons, MutationInfo mutationInfo = null) { if (mutationInfo != null) { this.MutationInfo = mutationInfo; } else { this.MutationInfo = new MutationInfo(); } ActivationFunction = activation; Random r = new Random(DateTime.Now.Millisecond + inputLenght * outputLenght); List <TemporalLayer> layers = new List <TemporalLayer>(); for (int layerIndex = 0; layerIndex < layerCount; layerIndex++) { int previousLayerLenght = layerIndex == 0 ? inputLenght : layers[layerIndex - 1].Lenght; int currentNeurons = r.Next(minNeurons, maxNeurons + 1) * Convert.ToInt32(layerIndex < layerCount - 1); layers.Add(new TemporalLayer(previousLayerLenght, GetRandomNeuronTypes(currentNeurons))); } layers.Add(new TemporalLayer(layers.Count > 0 ? layers[layers.Count - 1].Lenght : inputLenght, GetRandomNeuronTypes(outputLenght))); TemporalLayers = layers; }
/*public NetworkEvolution(List<EvolutionaryLine> networks, MutationInfo mutationInfo, int inputLenght, int outputLenght, TemporalNetwork.ActivationFunctions activation, int startingLayersCount, int minNeurons, int maxNeurons) * { * this.mutationInfo = mutationInfo; * this.inputLenght = inputLenght; * this.outputLenght = outputLenght; * this.activation = activation; * this.startingLayersCount = startingLayersCount; * this.minNeurons = minNeurons; * this.maxNeurons = maxNeurons; * }*/ /// <summary> /// Used when all the networks are evaluated, important: has to be awaited for correct function /// </summary> /// <param name="maxSelectedNetworks">The max number of networks to be selectionated independent of their score</param> /// <param name="minScore">If set to -1 min score will be dynamic</param> public async void AdvanceGeneration(int maxSelectedNetworks, int timesToCrossOver, int timesToMutate, MutationInfo info, int minSpecies = 20, double minScore = double.MinValue, bool canDie = true) { List <Task> taskList = new List <Task>(); List <double> maxScores = GetMaxScores(); maxScores.Sort(); maxScores.Reverse(); int dividerMultiplier = 1; if (canDie) { minScore += (maxScores[0] / (dividerMultiplier * Math.E) + double.MinValue) * Convert.ToInt32(minScore == double.MinValue); } else { minScore = 0; } for (int specieI = 0; specieI < species.Count; specieI++) { species[specieI].SelectNetworks(minScore, out bool survives); if (species.Count > maxSelectedNetworks) { species.RemoveRange(maxSelectedNetworks, species.Count - maxSelectedNetworks); } if (!survives) { species.RemoveAt(specieI); specieI--; } else { taskList.Add(Task.Run(() => species[specieI].CrossoverNetworks(timesToCrossOver, startingState))); } } while (species.Count < minSpecies) { species.Add(new EvolutionaryLine <Game>(startingState, startingSpecieNetworksCount, info, activation, inputLenght, outputLenght, startingLayersCount, minNeurons, maxNeurons)); } bool hasFinished = false; int mlbetweenChecks = 200; while (!hasFinished) { for (int i = 0; i < taskList.Count; i++) { hasFinished = taskList[i].IsCompleted && hasFinished; if (taskList[i].IsCompleted) { taskList.RemoveAt(i); i--; } } mlbetweenChecks = 1000; Console.WriteLine($"{taskList.Count} species mutations left"); await Task.Delay(mlbetweenChecks); } for (int i = 0; i < species.Count; i++) { taskList.Add(Task.Run(() => species[i].MutateNetworks(timesToMutate))); } hasFinished = false; mlbetweenChecks = 200; while (!hasFinished) { for (int i = 0; i < taskList.Count; i++) { hasFinished = taskList[i].IsCompleted && hasFinished; if (taskList[i].IsCompleted) { taskList.RemoveAt(i); i--; } } mlbetweenChecks = 1000; Console.WriteLine($"{taskList.Count} species crossovers left"); await Task.Delay(mlbetweenChecks); } }
public FittedNetwork(Game game, MutationInfo mutationInfo, TemporalNetwork.ActivationFunctions activationFunction, int inputLenght, int outputLenght, int layersCount, int minNeurons, int maxNeurons) { this.game = game; network = new TemporalNetwork(activationFunction, inputLenght, outputLenght, layersCount, minNeurons, maxNeurons, mutationInfo); fitnessScore = 0; }
public static EvolutionaryLine <Game> FromString(string str, Game startingState, MutationInfo mutationInfo) { string[] networksString = str.Split(new string[] { "\nnetwork:\n" }, StringSplitOptions.RemoveEmptyEntries); List <FittedNetwork <Game> > networks = new List <FittedNetwork <Game> >(); for (int i = 0; i < networksString.Length; i++) { networks.Add(new FittedNetwork <Game>(TemporalNetwork.FromString(networksString[i]), startingState)); } return(new EvolutionaryLine <Game>(networks)); }
/// <summary> /// MutationInfo will be set to null /// </summary> public static NetworkEvolution <Game> FromString(string str, Game startingState, MutationInfo mutationInfo) { string[] species = str.Split(new string[] { "\nspecie:\n" }, StringSplitOptions.RemoveEmptyEntries); string[] randomParameters = species[0].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < randomParameters.Length; i++) { randomParameters[i] = randomParameters[i].Remove(0, randomParameters[i].IndexOf(":") + 1); } int inputLenght = Convert.ToInt32(randomParameters[0]); int outputLenght = Convert.ToInt32(randomParameters[1]); int startingSpecieNetworkCount = Convert.ToInt32(randomParameters[2]); int startingLayersCount = Convert.ToInt32(randomParameters[3]); int minNeurons = Convert.ToInt32(randomParameters[4]); int maxNeurons = Convert.ToInt32(randomParameters[5]); Enum.TryParse(randomParameters[6], out TemporalNetwork.ActivationFunctions activation); List <EvolutionaryLine <Game> > networks = new List <EvolutionaryLine <Game> >(); for (int i = 1; i < species.Length; i++) { networks.Add(EvolutionaryLine <Game> .FromString(species[i], startingState, mutationInfo)); } return(new NetworkEvolution <Game>(networks, inputLenght, outputLenght, startingSpecieNetworkCount, activation, startingLayersCount, minNeurons, maxNeurons)); }
public NetworkEvolution(Game startingState, int speciesCount, int initialSpecieNetworkCount, MutationInfo mutationInfo, TemporalNetwork.ActivationFunctions activation, int inputLenght, int outputLenght, int layersCount, int minNeurons, int maxNeurons) { startingSpecieNetworksCount = initialSpecieNetworkCount; this.startingState = startingState; this.activation = activation; this.inputLenght = inputLenght; this.outputLenght = outputLenght; this.startingLayersCount = layersCount; this.minNeurons = minNeurons; this.maxNeurons = maxNeurons; species = new List <EvolutionaryLine <Game> >(); for (int i = 0; i < speciesCount; i++) { species.Add(new EvolutionaryLine <Game>(startingState, startingSpecieNetworksCount, mutationInfo, activation, inputLenght, outputLenght, layersCount, minNeurons, maxNeurons)); } }