private GroupDiet CreateRandomDailyDiet() { var diet = new GroupDiet(); for (var j = 0; j < _numberOfMealsPerDay; j++) { var meal = new GroupMeal { MealType = (MealType)j }; diet.Meals.Add(meal); } List <int> idsWithDeficiency; while ((idsWithDeficiency = _personalData.Where(p => p.Requirements.CaloriesAllowedRange.Lower > _dietAnalyzer.SummarizeForPerson(diet, p.Id).NutritionValues.Calories).Select(pd => pd.Id).ToList()).Any()) { var meal = diet.Meals.GetRandomItem(); var includedRecipes = diet.Meals.SelectMany(m => m.Recipes).Select(r => r.Recipe).Distinct().ToList(); var split = GetRanodmNotInvlolvedRecipe(includedRecipes, meal.MealType); split.Adjustments.RemoveAll(a => !idsWithDeficiency.Contains(a.PersonId)); meal.Recipes.Add(split); } return(diet); }
private static GroupMeal CopyMeal(GroupMeal sourceMeal) { return(new GroupMeal { MealType = sourceMeal.MealType, Recipes = CopyRecipes(sourceMeal.Recipes) }); }
private void PerformRecipesLevelMutation(GroupMeal meal, double mutationProbability) { if (_random.NextDouble() < mutationProbability) { //add new split var split = new RecipeGroupSplit { Recipe = _recipesForMealType[meal.MealType].GetRandomItem() }; var adjustment = new RecipeAdjustment { AmountMultiplier = RecipeGroupSplit.Multipliers.GetRandomItem(), PersonId = _random.Next(_groupSize) }; var duplicate = meal.Recipes.FirstOrDefault(r => r.Recipe == split.Recipe); if (duplicate == null) { meal.Recipes.Add(split); } else if (duplicate.Adjustments.All(a => a.PersonId != adjustment.PersonId)) { duplicate.Adjustments.Add(adjustment); } } foreach (var recipeGroupSplit in meal.Recipes) { if (_random.NextDouble() < mutationProbability) { //add adjustment to split if (recipeGroupSplit.Adjustments.Count < _groupSize) { recipeGroupSplit.Adjustments.Add(GetRandomMissingAdjustment(recipeGroupSplit.Adjustments)); } } for (var adjIndex = recipeGroupSplit.Adjustments.Count - 1; adjIndex >= 0; adjIndex--) { if (!(_random.NextDouble() < mutationProbability)) { continue; } if (_random.NextDouble() < 0.5) { //remove adjustment from split recipeGroupSplit.Adjustments.RemoveAt(adjIndex); } else { //change multiplier recipeGroupSplit.Adjustments[adjIndex].AmountMultiplier = RecipeGroupSplit.Multipliers.GetRandomItem(); } } } meal.Recipes.RemoveAll(r => !r.Adjustments.Any()); }
private void RemoveRepeatingRecipes(GroupMeal meal) { for (var i = 0; i < meal.Recipes.Count; i++) { for (var j = meal.Recipes.Count - 1; j > i; j--) { if (meal.Recipes[i].Recipe == meal.Recipes[j].Recipe) { meal.Recipes.RemoveAt(j); } } } }
private Tuple <GroupMeal, GroupMeal> CrossOverMeal(GroupMeal parent1, GroupMeal parent2) { var crossOverPoint = _random.Next(1 + Math.Min(parent1.Recipes.Count, parent2.Recipes.Count)); var child1 = new GroupMeal { MealType = parent1.MealType }; var child2 = new GroupMeal { MealType = parent1.MealType }; child1.Recipes.AddRange(parent1.Recipes.Take(crossOverPoint)); child1.Recipes.AddRange(parent2.Recipes.Skip(crossOverPoint)); child2.Recipes.AddRange(parent2.Recipes.Take(crossOverPoint)); child2.Recipes.AddRange(parent1.Recipes.Skip(crossOverPoint)); RemoveRepeatingRecipes(child1); RemoveRepeatingRecipes(child2); return(new Tuple <GroupMeal, GroupMeal>(child1, child2)); }
private static MealData CreateMealData(GroupMeal meal) { return(new MealData { Recipes = CreateRecipesData(meal.Recipes) }); }