示例#1
0
        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());
        }
示例#2
0
 private void DecreaseAjustment(RecipeAdjustment recipeAdjustment)
 {
     recipeAdjustment.AmountMultiplier = RecipeGroupSplit.Multipliers.First();
 }