public Bee CreateBee(int employeesNumber) { var sorted = _factory.EmployeesList.OrderBy(o => o.Abilities).ToList(); Bee bee = new Bee(); for (int i = 0; i < employeesNumber; i++) { var tempList = new List <int>(); var tempMachinesList = new List <Machines>(); Employees employee = sorted[i]; for (int u = 0; u < employee.VectorOfAbilities.Length; u++) { if (employee.VectorOfAbilities[u] == '1') { tempList.Add(u); tempMachinesList.Add(_factory.MachinesList[u]); } } tempMachinesList.RemoveAll(item => bee.Trail.Keys.ToList().Contains(item)); if (tempMachinesList.Count != 0) { bee.Trail.Add(tempMachinesList[_randomNumber.Next(tempMachinesList.Count)], employee); } else { bee = CreateBee(employeesNumber); } } bee.Profit = bee.Profit = ObjectiveFunctionCounter_1.CountValueOfTheFunction(bee, 11, 2); return(bee); }
public Specimen CreateSpecimen(int employeesNumber) { var sorted = _factory.EmployeesList.OrderBy(o => o.Abilities).ToList(); Specimen specimen = new Specimen(); for (int i = 0; i < employeesNumber; i++) { var tempList = new List <int>(); var tempMachinesList = new List <Machines>(); Employees employee = sorted[i]; for (int u = 0; u < employee.VectorOfAbilities.Length; u++) { if (employee.VectorOfAbilities[u] == '1') { tempList.Add(u); tempMachinesList.Add(_factory.MachinesList[u]); } } tempMachinesList.RemoveAll(item => specimen.Genome.Keys.ToList().Contains(item)); if (tempMachinesList.Count != 0) { specimen.Genome.Add(tempMachinesList[_randomNumber.Next(tempMachinesList.Count)], employee); } else { specimen = CreateSpecimen(employeesNumber); } } specimen.Profit = specimen.Profit = ObjectiveFunctionCounter_1.CountValueOfTheFunction(specimen, 11, 2); return(specimen); }
public Specimen CrossoverTwoSpecimens(List <Specimen> Population, int iteration) { Specimen specimen_1 = new Specimen(); Specimen specimen_2 = new Specimen(); int specimensFromEarlierPopulation = (int)(_percentageOfChildrenFromPreviousGeneration * Population.Count); List <Specimen> newPopulation = new List <Specimen>(Population.GetRange(0, specimensFromEarlierPopulation)); specimen_1 = Population[_randomNumber.Next(specimensFromEarlierPopulation)]; specimen_2 = Population[_randomNumber.Next(specimensFromEarlierPopulation)]; Specimen result_specimen = Crossover(specimen_1, specimen_2); result_specimen.Profit = ObjectiveFunctionCounter_1.CountValueOfTheFunction(result_specimen, 11, 3); return(result_specimen); }
public Bee SearchBetterTrail(Bee bee) { Bee newBee = new Bee(bee.Trail); int counter = 0; var trailList = bee.Trail.ToList(); int k1 = _randomNumber.Next(trailList.Count); var machine_1_Number = _factory.MachinesList.IndexOf(trailList[k1].Key); var employeesListAbleToWorkOnMachine_1 = _factory.EmployeesList.FindAll( s => s.VectorOfAbilities[machine_1_Number] == '1' && s.EmployeeID != trailList[k1].Value.EmployeeID); int k2 = _randomNumber.Next(employeesListAbleToWorkOnMachine_1.Count); var machine_2 = trailList.Find(s => s.Value.EmployeeID == employeesListAbleToWorkOnMachine_1[k2].EmployeeID).Key; int machine_2_Number = _factory.MachinesList.IndexOf(machine_2); while (trailList[k1].Value.VectorOfAbilities[machine_2_Number] != '1' && counter < employeesListAbleToWorkOnMachine_1.Count) { k2 = _randomNumber.Next(employeesListAbleToWorkOnMachine_1.Count); while (k1 == k2) { k2 = _randomNumber.Next(employeesListAbleToWorkOnMachine_1.Count); } machine_2 = trailList.Find(s => s.Value.EmployeeID == employeesListAbleToWorkOnMachine_1[k2].EmployeeID).Key; machine_2_Number = _factory.MachinesList.IndexOf(machine_2); counter++; } if (counter != employeesListAbleToWorkOnMachine_1.Count) { newBee.Trail.Remove(trailList[k1].Key); newBee.Trail.Remove(machine_2); newBee.Trail.Add(trailList[k1].Key, employeesListAbleToWorkOnMachine_1[k2]); newBee.Trail.Add(machine_2, trailList[k1].Value); var Trail_1 = newBee.Trail.ToList(); Trail_1.OrderBy(o => o.Value.Abilities); var c = newBee.Trail.OrderBy(o => o.Value.Abilities); newBee.Trail = c.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value); newBee.Profit = ObjectiveFunctionCounter_1.CountValueOfTheFunction(newBee, 11, 3); } return(newBee.Profit > bee.Profit ? newBee : bee); }