private void Mutate(Individual child) { var rnd = new Random(); var mutation = rnd.Next(0, child.Genes.Length); using (var db = new DietDbContext()) { var totalDataSet = db.FoodItems.Count(); //todo: enhance per category e.g. breakfast, snack, lunch, supper var id = rnd.Next(1, totalDataSet); var recipe = db.FoodItems.Find(id); if (recipe == null) { Console.WriteLine("Recipe null"); } // if we haven't used this variation already, add it to the set if (!child.Genes.Any(g => string.Equals(g.Name, recipe.Name, StringComparison.OrdinalIgnoreCase))) { child.Genes[mutation].Name = recipe.Name; child.Genes[mutation].Fitness = recipe.CalcTotalFitness(); //Console.WriteLine("Mutation Occurred. Individual added to population"); return; } // we have used it, try another variation Mutate(child); } }
public static Population GetRecipes() { var rnd = new Random(); var mealPop = new Population(); using (var db = new DietDbContext()) { var totalDataSet = db.FoodItems.Count(); for (var j = 1; j < 11; j++) { var mealIndividual = new Individual(); var i = 0; while (i < mealIndividual.Genes.Length) { //todo: add categorisation for breakfast, snack, lunch, supper int id = rnd.Next(1, totalDataSet); var recipe = db.FoodItems.Find(id); if (recipe == null) { Console.WriteLine("Recipe null"); } if (mealIndividual.Genes.Any(g => string.Equals(g.Name, recipe.Name, StringComparison.OrdinalIgnoreCase))) { continue; } mealIndividual.Genes[i].Name = recipe.Name; mealIndividual.Genes[i].Fitness = recipe.CalcTotalFitness(); i++; } //todo: enhance meal set per category e.g. breakfast, snack, lunch, supper mealPop.AddIndividual(mealIndividual); Console.WriteLine("Meal set {0} added", j); } }; Console.WriteLine("Population has been filled."); return(mealPop); }
static async Task AddRecipes(string search) { var yummlyService = new YummlyService(); var recipes = await yummlyService.SearchRecipes(search); using (var db = new DietDbContext()) { foreach (var item in recipes.Matches) { // check if in database if (db.FoodItems.Any(x => x.RecipeId == item.Id)) { continue; } var recipe = await yummlyService.GetRecipe(item.Id); var foodItem = new FoodItem { Name = item.RecipeName, RecipeId = recipe.Id, RawData = JsonConvert.SerializeObject(recipe), Ingredients = JsonConvert.SerializeObject(item.Ingredients) }; // save required nutrient values var fatKCal = recipe.NutritionEstimates .FirstOrDefault(x => string.Equals("FAT_KCAL", x.Attribute, StringComparison.OrdinalIgnoreCase)); if (fatKCal != null) { foodItem.FAT_KCAL = fatKCal.Value; } var enerKCal = recipe.NutritionEstimates .FirstOrDefault(x => string.Equals("ENERC_KCAL", x.Attribute, StringComparison.OrdinalIgnoreCase)); if (enerKCal != null) { foodItem.ENERC_KCAL = enerKCal.Value; } var prot = recipe.NutritionEstimates .FirstOrDefault(x => string.Equals("PROCNT", x.Attribute, StringComparison.OrdinalIgnoreCase)); if (prot != null) { foodItem.PROCNT = prot.Value; } var chol = recipe.NutritionEstimates .FirstOrDefault(x => string.Equals("CHOLE", x.Attribute, StringComparison.OrdinalIgnoreCase)); if (chol != null) { foodItem.CHOLE = chol.Value; } var tCarb = recipe.NutritionEstimates .FirstOrDefault(x => string.Equals("CHOCDF", x.Attribute, StringComparison.OrdinalIgnoreCase)); if (tCarb != null) { foodItem.CHOCDF = tCarb.Value; } var sugar = recipe.NutritionEstimates .FirstOrDefault(x => string.Equals("SUGAR", x.Attribute, StringComparison.OrdinalIgnoreCase)); if (sugar != null) { foodItem.SUGAR = sugar.Value; } var fibre = recipe.NutritionEstimates .FirstOrDefault(x => string.Equals("FIBTG", x.Attribute, StringComparison.OrdinalIgnoreCase)); if (fibre != null) { foodItem.FIBTG = fibre.Value; } var tFat = recipe.NutritionEstimates .FirstOrDefault(x => string.Equals("FAT", x.Attribute, StringComparison.OrdinalIgnoreCase)); if (fatKCal != null) { foodItem.FAT = tFat.Value; } var satFat = recipe.NutritionEstimates .FirstOrDefault(x => string.Equals("FASAT", x.Attribute, StringComparison.OrdinalIgnoreCase)); if (satFat != null) { foodItem.FASAT = satFat.Value; } var traFat = recipe.NutritionEstimates .FirstOrDefault(x => string.Equals("FATRN", x.Attribute, StringComparison.OrdinalIgnoreCase)); if (traFat != null) { foodItem.FATRN = traFat.Value; } var water = recipe.NutritionEstimates .FirstOrDefault(x => string.Equals("WATER", x.Attribute, StringComparison.OrdinalIgnoreCase)); if (water != null) { foodItem.WATER = water.Value; } await db.AddAsync(foodItem); Console.WriteLine(item.RecipeName); } await db.SaveChangesAsync(); } Console.WriteLine("Done"); Console.ReadKey(); }