Example #1
0
        /// <summary>
        /// Save network to file
        /// </summary>
        /// <param name="network">Network to save</param>
        /// <param name="filename">FileName</param>
        public static void SaveNetwork(SNP_Network network, string filename)
        {
            Console.WriteLine("----------- Saving Network -----------");
            string json = ConvertNetworkToJson(network);

            SaveToFile(json, filename);
        }
Example #2
0
        /// <summary>
        ///  compare the fitnesses of the two networks, save the best genes
        /// </summary>
        private void CalculateFitness()
        {
            fitnessSum = 0;
            DNA best = Population[0];

            for (int i = 0; i < Population.Count; i++)
            {
                fitnessSum += Population[i].CalculateFitness(i);
                if (Generation > 1 && Population[i].Fitness >= 0 && !float.IsNaN(Population[i].Fitness) && !Population[i].Equals(null) && Generation > 1 && Population[i].Fitness <= 1)
                {
                    Console.WriteLine("Current Fitness: {0},", Population[i].Fitness);
                    FitnessList.Add(Population[i].Fitness);
                }
                if (Population[i].Fitness >= best.Fitness && Generation > 1)
                {
                    best = Population[i];
                    best.Genes.minifiedPrint();
                    best.Genes.OutputSet.ForEach(x => Console.Write("{0}\t", x));
                    Console.WriteLine("\nCurrent best fitness: {0}", best.Fitness);
                    // FitnessList.Add(best.Fitness);
                }
            }
            BestFitness = best.Fitness;
            BestGenes   = ReflectionCloner.DeepFieldClone(best.Genes);
        }
Example #3
0
        /// <summary>
        /// This code will loop over the entire network and remove any spikes which match the correct rules.
        /// </summary>
        /// <param name="networkRef">the network which this neuron belongs to and for which this method is being executed</param>
        /// <param name="Connections">connections that this neuron has</param>
        /// <returns></returns>
        public bool?RemoveSpikes(SNP_Network networkRef)
        {
            int index;

            if (this.ActiveDelay == 0)
            {
                if (PersistedState.Equals(true))
                {
                    this.SpikeCount = "";
                    PersistedState  = false;
                    return(true);
                }
                else if (PersistedState.Equals(null))
                {
                    this.SpikeCount = "";
                    PersistedState  = false;
                    return(null);
                }
                else
                {
                    index = MatchRules();
                    if (this.Rules[index].IsMatched(this.SpikeCount).Equals(null))
                    {
                        if (this.Rules[index].Delay > 0)
                        {
                            this.ActiveDelay    = this.Rules[index].Delay;
                            this.PersistedState = null;
                            return(false);
                        }
                        this.SpikeCount = "";
                        return(null);
                    }
                    else if (this.Rules[index].IsMatched(this.SpikeCount).Equals(false))
                    {
                        return(false);
                    }
                    else if (this.Rules[index].IsMatched(this.SpikeCount).Equals(true))
                    {
                        if (this.Rules[index].Delay > 0)
                        {
                            this.ActiveDelay    = this.Rules[index].Delay;
                            this.PersistedState = true;
                            return(false);
                        }
                        this.SpikeCount = "";
                        return(true);
                    }
                }
            }
            else
            {
                this.ActiveDelay--;
                return(false);
            }
            // this should never happen.
            Console.Error.WriteLine("Foreach loop failed.");
            return(false);
        }
Example #4
0
        /// <summary>
        /// Once the spikes have been removed, each network can spike across all of its connections
        /// </summary>
        /// <param name="networkRef">the network which this neuron belongs to and for which this method is being executed</param>
        /// <param name="Connections">connections that this neuron has</param>
        public void FireSpike(SNP_Network networkRef, List <int> Connections)
        {
            /*
             * If List has more than one rule
             * Check if the neuron has enough spike to satisfy some or all of the rules
             * If only one rule can proceed, complete that spike
             * If more than one rule can proceed at any one time use the Random() function to determine which will fire
             * Fire the spike on that chosen rule definition
             * We do not need to worry about the removal of spikes as that is done in RemoveSpikes()
             */
            int index = MatchRules();

            foreach (int connection in Connections)
            {
                if (this.Rules[index].IsMatched(this.SpikeCount).Equals(true))
                {
                    if (this.Rules[index].Fire)
                    {
                        networkRef.Neurons[connection - 1].SpikeCount = networkRef.Neurons[connection - 1].SpikeCount + "a";
                    }
                }
            }
        }
Example #5
0
 /// <summary>
 /// Convert the provided SN P Network to a JSON format for exporting
 /// </summary>
 /// <param name="network">SNP network to convert</param>
 /// <returns>JSON String</returns>
 public static string ConvertNetworkToJson(SNP_Network network)
 {
     return(JsonConvert.SerializeObject(network, Formatting.Indented));
 }