for fi in food_items: if fi.name == 'asparagus': asparagus = fi elif fi.name == 'chicken': chicken = fi elif fi.name == 'protein filler': pfill = fi elif fi.name == 'carbs filler': cfill = fi elif fi.name == 'fat filler': ffill = fi else: raise Exception( "Unknown food item type: %s" % ( fi) ) potential_meals = nc.meal_planner( initial_meal, [ asparagus ], [ pfill, cfill, ffill ] ) for score, added_item, potential_meal in potential_meals: print "Adding %s to %s yeilded a score of: %f" % ( added_item.name, potential_meal.name, score ) print "Food items were:" for food_item, amount in potential_meal.get_food_items(): print "\t%s, %f" % ( food_item.name, amount ) print "Nutrients were:" pp.pprint( potential_meal.get_nutrients() ) print potential_meals[0][2].nutrient_goals print potential_meals[0][2].nutrient_constraints
def planner( request ): current_meal = None FIs = nc.FoodItems() food_items = FIs.get_food_items() pfill = None cfill = None ffill = None current_score = 0 for fi in food_items: if fi.name == 'protein filler': pfill = fi elif fi.name == 'carbs filler': cfill = fi elif fi.name == 'fat filler': ffill = fi if request.method == 'POST': #import pdb #pdb.set_trace() #pcalories = request.POST.get( 'calories', None ) #pfood_group = None #pfood_choices = #for food_group_element in food_group_ordering: # if food_group_element in request.POST: # pfood_group = food_group_element #pselected_food_items = {} #for key, value in request.POST.items(): # if key[:10] == 'food_item_': # pselected_food_items[key[10:]] = value form = MealForm( request.POST ) #calories = pcalories, # food_group = pfood_group, # selected_food_items=pselected_food_items ) if form.is_valid(): food_group = None selected_food_group = form.data['food_group'] for idx, food_group_element in enumerate( food_group_ordering ): if food_group_element == selected_food_group: if idx + 1 < len( food_group_ordering ): food_group = food_group_ordering[ idx + 1 ] else: food_group = 'done' # Now food_group is either our last item or the next one # we want to process. if food_group == 'done': initial_meal = nc.Meal( 'Current Meal', nutrient_equality_constraints = [ ( 'Energy', energy ) ], nutrient_limit_constraints = [ ( 'Protein', energy*.3*.8/4, energy*.3*1.2/4 ), ( 'Carbohydrate, by difference', energy*.4*.8/4, energy*.4*1.2/4 ), ( 'Total lipid (fat)', energy*.3*.8/9, energy*.3*1.2/9 ) ], nutrient_goals = [ x[:2] for x in nutrient_goals ], min_servings = 0.5, max_servings = 3.0 ) for nutrient, amount in nutrient_limit_hash.items(): initial_meal.add_nutrient_constraint( nutrient, 'limits', ( 0, amount ) ) selected_foods = { form.data['food_choices'] : 'selected_foods_%s' % ( form.data['food_choices'] ) } for food_item_label in [ x for x in form.data.keys() if x.startswith( 'selected_foods_' ) ]: selected_foods[food_item_label[15:]] = form.data[food_item_label] initial_meal.add_food_item( food_item_label[15:], 0 ) result = nc.meal_planner( initial_meal, [ FIs.get_food_item( form.data['food_choices'] ) ], [ pfill, cfill, ffill ] ) if len( result ): current_score, current_ingredient, current_meal = result[0] form = None else: initial_meal = nc.Meal( 'Current Meal', nutrient_equality_constraints = [ ( 'Energy', energy ) ], nutrient_limit_constraints = [ ( 'Protein', energy*.3*.8/4, energy*.3*1.2/4 ), ( 'Carbohydrate, by difference', energy*.4*.8/4, energy*.4*1.2/4 ), ( 'Total lipid (fat)', energy*.3*.8/9, energy*.3*1.2/9 ) ], nutrient_goals = [ x[:2] for x in nutrient_goals ], min_servings = 0.5, max_servings = 3.0 ) for nutrient, amount in nutrient_limit_hash.items(): initial_meal.add_nutrient_constraint( nutrient, 'limits', ( 0, amount ) ) # DEBUG - We're only getting one ingredient at a time. selected_foods = { form.data['food_choices'] : 'selected_foods_%s' % ( form.data['food_choices'] ) } for food_item_label in [ x for x in form.data.keys() if x.startswith( 'selected_foods_' ) ]: selected_foods[food_item_label[15:]] = form.data[food_item_label] initial_meal.add_food_item( food_item_label[15:], 0 ) #import pdb #pdb.set_trace() result = nc.meal_planner( initial_meal, [ FIs.get_food_item( form.data['food_choices'] ) ], [ pfill, cfill, ffill ] ) if len( result ): current_score, current_ingredient, current_meal = result[0] new_ingredients = [] # DEBUG db_food_items = FoodItems.objects.filter( food_group_id__in = food_groups[food_group].keys() )[:21] for db_food_item in db_food_items: new_ingredients.append( FIs.get_food_item( db_food_item.uid ) ) candidate_meals = nc.meal_planner( current_meal, new_ingredients, [ pfill, cfill, ffill ] ) food_choices = [ ( x[1].uid, x[1].name ) for x in candidate_meals[:20] ] form = MealForm( calories = 1230, selected_foods = selected_foods, food_group = food_group, food_choices = food_choices ) else: empty_meal = nc.Meal( 'Current Meal', nutrient_equality_constraints = [ ( 'Energy', energy ) ], nutrient_limit_constraints = [ ( 'Protein', energy*.3*.8/4, energy*.3*1.2/4 ), ( 'Carbohydrate, by difference', energy*.4*.8/4, energy*.4*1.2/4 ), ( 'Total lipid (fat)', energy*.3*.8/9, energy*.3*1.2/9 ) ], nutrient_goals = [ x[:2] for x in nutrient_goals ], min_servings = 0.5, max_servings = 3.0 ) current_meal = empty_meal food_group = food_group_ordering[0] # DEBUG db_food_items = FoodItems.objects.filter( food_group_id__in = food_groups[food_group].keys() )[:21] new_ingredients = [] for db_food_item in db_food_items: new_ingredients.append( FIs.get_food_item( db_food_item.uid ) ) candidate_meals = nc.meal_planner( empty_meal, new_ingredients, [ pfill, cfill, ffill ] ) food_choices = [ ( x[1].uid, x[1].name ) for x in candidate_meals[:20] ] form = MealForm( food_group = food_group, food_choices = food_choices ) current_meal_nutrients = {} if current_meal is not None: for nutrient, amount in current_meal.get_nutrients().items(): goal_amount = -1.0 if nutrient in nutrient_goal_hash: goal_amount = nutrient_goal_hash[nutrient] current_meal_nutrients[nutrient] = "%d%% of DRI - ~%d grams" % ( int( 100.0 * amount / goal_amount ), int( amount ) ) # Don't show stuff if we don't have a goal. return render( request, 'meal_planner/planner.html', { 'form' : form, 'current_meal' : current_meal.get_food_items(), 'current_meal_nutrients' : sorted( current_meal_nutrients.items(), key=lambda x: x[0] ), 'food_group' : food_group, 'current_score' : current_score } )