private void readFile() { string[] lines = File.ReadAllLines("USPopulation.txt"); if (lines.Length != LAST_YEAR - FIRST_YEAR + 1) { MessageBox.Show("Wrong number of lines in input file"); Application.Current.Shutdown(); } int year = FIRST_YEAR; foreach (string line in lines) { try { years.Add(year, int.Parse(line)); dataList.Add(new KeyValuePair <int, int>(year, int.Parse(line))); PopulationList.Add("The population in " + year + " is " + line); } catch (Exception) { MessageBox.Show("Incorrect data in input file"); Application.Current.Shutdown(); } year++; } }
public GeneticAlgorithm(int[] shape, int populationSize, Random rnd, double minWeightValue, double maxWeightValue, double[] inputExample) { PopulationSize = populationSize; Rnd = rnd; MinWeightValue = minWeightValue; MaxWeightValue = maxWeightValue; InputExample = inputExample; Shape = shape; for (int i = 0; i < populationSize; i++) { PopulationList.Add(new NeuronNet(shape, Rnd)); } foreach (NeuronNet neuronNet in PopulationList) //randomize individuals { neuronNet.SetInputLayer(inputExample); neuronNet.RandomizeWeights(MinWeightValue, MaxWeightValue); } }
/// <summary> /// /// </summary> /// <param name="Worker"></param> private void AddWorkerToList(Worker worker) { // Vérifie si le nombre de slots de populaton est suffisant // avant de créer l'unité try { if (CheckFreeSlotInPopulation(worker)) { PopulationList.Add(worker); PopulationChanged(this, new PopulationChangedEventArgs { ActualPopulation = PopulationList.Sum(x => x.PopulationSlots), Unit = worker }); } } catch (NotEnoughUnitSlotsAvailableException) { throw; } }
public void Add(T element) { PopulationList.Add(element); }
private void NearGenesCrossWithOffspring(NeuronNet individualA, NeuronNet individualB) { NeuronNet offspring = new NeuronNet(Shape, Rnd); double extraRange = 0.1; offspring.SetInputLayer(InputExample); if (individualA.Layers.Count != individualB.Layers.Count) { throw new Exception("Different layer size. Can not cross"); } for (int i = 0; i < individualA.Layers.Count; i++) { if (individualA.Layers[i].Neurons.Count != individualB.Layers[i].Neurons.Count) { throw new Exception("Different neuron size. Can not cross"); } for (int j = 0; j < individualA.Layers[i].Neurons.Count; j++) { double auxBiasA = individualA.Layers[i].Neurons[j].Bias; double auxBiasB = individualB.Layers[i].Neurons[j].Bias; double maximumBias; double minimumBias; if (auxBiasA > auxBiasB) { maximumBias = auxBiasA + (auxBiasA * extraRange); minimumBias = auxBiasB - (auxBiasA * extraRange); } else { maximumBias = auxBiasB + (auxBiasA * extraRange); minimumBias = auxBiasA - (auxBiasA * extraRange); } offspring.Layers[i].Neurons[j].Bias = Rnd.NextDouble() * (maximumBias - minimumBias) + minimumBias; if (individualA.Layers[i].Neurons[j].Dendrites.Count != individualB.Layers[i].Neurons[j].Dendrites.Count) { throw new Exception("Different dendrite count. Can not cross"); } for (int k = 0; k < individualA.Layers[i].Neurons[j].Dendrites.Count; k++) { double auxDendriteWeightA = individualA.Layers[i].Neurons[j].Dendrites[k].Weight; double auxDendriteWeightB = individualB.Layers[i].Neurons[j].Dendrites[k].Weight; double maximum; double minimum; if (auxDendriteWeightA > auxDendriteWeightB) { maximum = auxDendriteWeightA + (auxDendriteWeightA * extraRange); minimum = auxDendriteWeightB - (auxDendriteWeightA * extraRange); } else { maximum = auxDendriteWeightB + (auxDendriteWeightA * extraRange); minimum = auxDendriteWeightA - (auxDendriteWeightA * extraRange); } offspring.Layers[i].Neurons[j].Dendrites[k].Weight = Rnd.NextDouble() * (maximum - minimum) + minimum; } } } PopulationList.Add(offspring); }