public void GetData() { bikeStationsDic = new Dictionary <Network, List <BikeStation> >(); BikeAPIManager bikeApiManager = new BikeAPIManager(); networks = bikeApiManager.GetAllCities().Result; int totalNetworks = networks.Count(); int i = 1; foreach (Network network in networks) { List <BikeStation> bikeStations = bikeApiManager.GetCityBikeStations(network).Result; bikeStationsDic.Add(network, bikeStations); log.Info($"GetData() Processing {i++}/{totalNetworks}: {network.Name}"); } }
/// <summary> /// Runs each Neural Network of the population through the fitness function and assigns the fitnesses. /// Overloaded for running on multiple threads. default is unthreaded. /// </summary> public void Run() { Dictionary <int, double> fitnesses = new Dictionary <int, double>(); double totalfitness = 0; if (threaded) { Thread thread1 = new Thread(() => { for (int i = 0; i < Networks.Count / 4; i++) { NeuralNetwork NN = Networks.ElementAt(i); double fitness = run(NN); totalfitness += fitness; lock (fitnesses) { fitnesses.Add(i, fitness); } //Console.WriteLine(i + " -> " + fitness); } }); Thread thread2 = new Thread(() => { for (int i = Networks.Count / 4; i < Networks.Count / 2; i++) { NeuralNetwork NN = Networks.ElementAt(i); double fitness = run(NN); totalfitness += fitness; lock (fitnesses) { fitnesses.Add(i, fitness); } //Console.WriteLine(i + " -> " + fitness); } }); Thread thread3 = new Thread(() => { for (int i = Networks.Count / 2; i < 3 * Networks.Count / 4; i++) { NeuralNetwork NN = Networks.ElementAt(i); double fitness = run(NN); totalfitness += fitness; lock (fitnesses) { fitnesses.Add(i, fitness); } //Console.WriteLine(i + " -> " + fitness); } }); Thread thread4 = new Thread(() => { for (int i = 3 * Networks.Count / 4; i < Networks.Count; i++) { NeuralNetwork NN = Networks.ElementAt(i); double fitness = run(NN); totalfitness += fitness; lock (fitnesses) { fitnesses.Add(i, fitness); } //Console.WriteLine(i + " -> " + fitness); } }); thread1.Start(); thread2.Start(); thread3.Start(); thread4.Start(); thread1.Join(); thread2.Join(); thread3.Join(); thread4.Join(); } else { int count = 0; foreach (NeuralNetwork NN in Networks) { double fitness = run(NN); totalfitness += fitness; fitnesses.Add(count, fitness); //Console.WriteLine(count + " -> " + fitness); count++; } } AvgPopFitness = totalfitness / Networks.Count; BestNetwork = null; for (int i = 0; i < Networks.Count(); i++) { fitnesses.TryGetValue(i, out double fitness); if (BestNetwork == null || fitness > BestNetwork.Fitness) { BestNetwork = Networks.ElementAt(i); } Networks.ElementAt(i).Fitness = fitness; } //Console.WriteLine("Run: " + isConsistent()); }