Building NutrientCraft Webapp

NutrientCraft

Here is my web app, NutrientCraft.com

A couple of months ago I was inspired by Rob Rhinehart’s soylent experiment in which he claims to have stopped eating food by consuming only the ingredients the body requires. From his blog, “There are no meats, fruits, vegetables, or breads here… I researched every substance the body needs to survive.. and purchased all of them in nearly raw chemical form from a variety of sources.”

I like the idea of 100% efficiency, however Rob’s approach is too extreme for me. There is, for example, the possibility that nutritional science is incomplete or the that the production of the raw nutrients is flawed. I wanted something in between a traditionally healthy diet and a futuristic nutrient goo.

I was unable to find a tool that could take a list of whole foods and fit their nutrients into a diet, or target set of nutrients. For example, if you are trying to create a list of food portions to meet your daily vitamin C requirements, you will probably find the list of foods has too little or too much of another nutrient. So if you try to balance for this next nutrient, vitamin C will now be unbalanced.

The challenge to me seemed similar to “bin packing” algorithms, like this one. I treated all foods as n-dimensional vectors of nutrients (percent of daily nutrient requirements). I then set a target vector of (1,1,1…), or 100% of daily nutrient requirements. I figured the problem is NP-hard like the bin packing problem, so I wrote a brute force algorithm that tries every possible combination until it finds the best. The best combination of vectors is the one with the smallest angle between itself and the target vector. When I had something working, I ran it on the full National Nutrient Database and it spit out the nastiest meal I’ve ever seen, but it was EFFICENT! Here is my first full database test, graphed with matplotlib:
diet optimization
diet optimization legend
I enjoy web development as a hobby, so I thought it was a great opportunity to make a web app. I used Google App Engine with Python for the backend. I used GAE Boilerplate for the basic infrastructure of the site (users, social logins, etc) so I could focus on the app interface with Javascript. SlickGrid is an amazing spreadsheet library for JavaScript that really came in handy.