public void Assign(NEAT source) { foreach (var n in nodes) { Destroy(n); } nodes.Clear(); foreach (var c in connections) { Destroy(c.gameObject); } connections.Clear(); source.InputLayer.ForEach(neuron => neuron.Done = false); source.OutputLayer.ForEach(neuron => neuron.Done = false); source.HiddenLayers.Values.ToList().ForEach(neuron => neuron.Done = false); horizontal = 1; i = 0; var position = initialPosittion; foreach (var neuron in source.InputLayer) { ComputeNeuron(neuron, position); position.y += offset; } position = initialPosittion; position.x += offset * (source.HiddenLayers.Count + 1) * 2; position.y += offset * (source.InputLayer.Count - source.OutputLayer.Count) / 2; foreach (var neuron in source.OutputLayer) { ComputeNeuron(neuron, position); position.y += offset; } }
public float Distance(NEAT partner) { if (partner == null) { Debug.Log("NULL partner"); Debug.Break(); } //var synapseIenumerator = AllSynapses.GetEnumerator(); //var partnerSynapseIenumerator = partner.AllSynapses.GetEnumerator(); //synapseIenumerator.MoveNext(); float result = 0; if (AllSynapses.Any() || partner.AllSynapses.Any()) { int disjointGenes = 0; int excessGenes = 0; float weightDifference = 0; float N = Math.Max(AllSynapses.Count, partner.AllSynapses.Count); disjointGenes = 2 * AllSynapses.Keys.Union(partner.AllSynapses.Keys).Count() - AllSynapses.Count - partner.AllSynapses.Count; foreach (var synapseInnovationNo in AllSynapses.Keys.Intersect(partner.AllSynapses.Keys)) { weightDifference += Math.Abs((float)(AllSynapses[synapseInnovationNo].Weight - partner.AllSynapses[synapseInnovationNo].Weight)); //if (Math.Abs((float)(AllSynapses[synapseInnovationNo].Weight - partner.AllSynapses[synapseInnovationNo].Weight)) > Constants.Con.synapse_difference_threshold) // disjointGenes++; } result += (Constants.Con.c1 * excessGenes + Constants.Con.c2 * disjointGenes + Constants.Con.c3 * weightDifference) / N; } //if(HiddenLayers.Any() || partner.HiddenLayers.Any()) OutputLayer.Count > 0 always { int disjointGenes = 0; int excessGenes = 0; float weightDifference = 0; float N = Math.Max(HiddenLayers.Count, partner.HiddenLayers.Count) + OutputLayer.Count; foreach (var neuronPair in OutputLayer.Zip(partner.OutputLayer)) { weightDifference += Mathf.Abs((float)(neuronPair.Key.Bias - neuronPair.Value.Bias)); //if (Mathf.Abs((float)(neuronPair.Key.Bias - neuronPair.Value.Bias)) > Constants.Con.bias_difference_threshold) // disjointGenes++; } disjointGenes = 2 * HiddenLayers.Keys.Union(partner.HiddenLayers.Keys).Count() - HiddenLayers.Count - partner.HiddenLayers.Count; foreach (var neuronInnovationNo in HiddenLayers.Keys.Intersect(partner.HiddenLayers.Keys)) { weightDifference += Math.Abs((float)(HiddenLayers[neuronInnovationNo].Bias - partner.HiddenLayers[neuronInnovationNo].Bias)); //if (Math.Abs((float)(HiddenLayers[neuronInnovationNo].Bias - partner.HiddenLayers[neuronInnovationNo].Bias)) > Constants.Con.bias_difference_threshold) // disjointGenes++; } result += (Constants.Con.c1 * excessGenes + Constants.Con.c2 * disjointGenes + Constants.Con.c3 * weightDifference) / N; } return(result); //if (result < Constants.Con.delta_t) // return true; //return false; }
public NEAT Crossover(NEAT partner) { //assume: partner has higher score NEAT offspring = partner.Copy(); foreach (var synapse in AllSynapses) { if (offspring.AllSynapses.ContainsKey(synapse.Key) && RandomGenerator.Next(2) == 0) { offspring.AllSynapses[synapse.Key].Weight = synapse.Value.Weight; } } return(offspring); }
public NEAT Copy() { NEAT c = new NEAT(InputLayer.Count, OutputLayer.Count, true); foreach (var synapse in AllSynapses) { c.AddSynapse(synapse.Value); } foreach (var neuronpair in OutputLayer.Zip(c.OutputLayer)) { neuronpair.Value.Bias = neuronpair.Key.Bias; } return(c); }