public MealDto GetRandomMeal(MealGoalsDto goals, int alimentCount) { MealDto meal; var aliments = _alimentService.GetAll(); do { meal = GenerateMealWithRandomAliments(alimentCount, aliments); meal = ComputeBestQuantities(meal, goals); } while (!MatchGoals(meal)); return(meal); }
private MealDto ComputeBestQuantities(MealDto meal, MealGoalsDto goals) { var calories = meal.MealParts.Select(p => p.Aliment.Calories).ToList(); var protides = meal.MealParts.Select(p => p.Aliment.Protides).ToList(); var lipides = meal.MealParts.Select(p => p.Aliment.Lipides).ToList(); var glucides = meal.MealParts.Select(p => p.Aliment.Glucides).ToList(); var combos = GetAllPossibleCombos(meal.MealParts.Count); double minRes = double.MaxValue; List <double> bestValue = null; foreach (var combo in combos) { double ct = 0; double pt = 0; double lt = 0; double gt = 0; for (int i = 0; i < combo.Count; i++) { ct += combo[i] * calories[i]; pt += combo[i] * protides[i]; lt += combo[i] * lipides[i]; gt += combo[i] * glucides[i]; } var result = 100 * Math.Abs(ct - goals.Calories) / goals.Calories + 100 * Math.Abs(pt - goals.Protides) / goals.Protides + 100 * Math.Abs(lt - goals.Lipides) / goals.Lipides + 100 * Math.Abs(gt - goals.Glucides) / goals.Glucides; if (result < minRes) { minRes = result; bestValue = combo; } } for (int i = 0; i < meal.MealParts.Count; i++) { meal.MealParts[i].Quantity = bestValue[i]; } meal.Accuracy = Math.Max(0, 100 - minRes); return(meal); }
public Task <MealDto> GetRandomMealAsync(MealGoalsDto goals, int alimentCount) { return(Task.Factory.StartNew(() => { MealDto meal; var aliments = _alimentService.GetAll(); do { meal = GenerateMealWithRandomAliments(alimentCount, aliments); meal = ComputeBestQuantities(meal, goals); } while (!MatchGoals(meal)); return meal; })); }