Genetic Algorithms: Automatic DIY Soylent


Certainly, the full URL is


I found the main problem. The nutrient profile was hard-coded in one spot in the code. When I switched over to actually optimizing your ingredients over your nutrient profile it got much more reasonable results. I made a few other minor checks for handling missing values and such, so it should be more robust. If you’ve got the recipe and profile set correctly, and you find it’s giving you an oddly expensive solution try increasing the “w” parameter at the top of the file. I’ve had varying success with w ranging from 0.0001 to 0.1 in factors of 10. Higher w = cheaper formula, but it’s at the expense of nutrients.

I’m fiddling with merging multiple loaded recipes and then optimizing over all of their ingredients together, but it’s not finished in this commit. I’m slightly distracted by video games today. Now, if you’ll excuse me I’ve got souls to reap.


I made another commit too to allow custom macro editing:

I’ll look at merging in your latest stuff tomorrow.


This stuff is so over my head but for some reason still find it intriguing to read.


funny thing with AutoSoylent (or at least my bulid of it) is that it produces an answer worse than it started with. (for my value of worse)

if I feed it :

it returns an answer that costs more, and is over max on some values, under min on others


Not sure why, but it takes a very large number of iterations to converge on that one. I managed to get a reasonable output, but it took 1.5 million iterations as opposed to the 50k cap that was set. Try upping the max iterations on the line that looks like this "while (!done && iteration < 50000) { " or removing the iteration cap altogether. It will of course take much longer to run.

Here’s what I got:

Robobread finalized, Soylent alternative for $66 per month

That did the trick.

Next batch of feature requests:

  • (may already be there — I’ll have to check my branch) optionally not override cals/macro-percentages if not supplied
  • in my branch — command line args for recipe and nutrient profile
  • command line arg for number of iterations
  • optionally set rounding on pills and portions — 1.3 pills is tricky
  • optionally pin amounts (e.g: assuming 403ml of coconut milk, solve the remaining ingredients)


My intent was never to produce a stand-alone application. I set out to design a better solver that could be added as a convenience feature to the DIY Soylent site. I’ve done that. I may do some more work at some point to get this to play nice with a GUI, but I’m not planning on putting any more time into the console app. Particularly since I already ordered the ingredients for my own recipe ( ).


1.3 pills per day is about 9 pills a week. Take one each morning and an extra one on Sunday and Wednesday nights, or something like that.


Don’t read too much into the insignificant digits. That precision is only there because it’s computationally easier to do continuous optimization than discrete optimization. Some hand tweaking will be necessary. The DIY site is great for that. I rounded my pill values up to the nearest half a pill, and that seems to work fine nutritionally. It’s not going to hurt me to get a little extra calcium and vitamin k.


Newbie here (joined yesterday). My first thought (before I saw this thread) was to use simulated annealing. I’ve have good luck with that in other domains. My idea was that with so many variables there will not be one single global best solution, but approximately 136.7 gazzilion different ways to find an acceptable local good-enough solution.

One could also score for “favorability” of ingredients. (Say I prefer the flavor or texture of one carb source over another, I could weight the more favored ingredient over the less favored one.)

Anyway, I think I’ll dive into an annealing ap just for fun and see what happens.

ON EDIT: @nickp Now that I’ve glanced at your code I see you are optimizing a single recipe ingredient list. For some reason I was thinking of applying annealing to the whole inventory of possible ingredients and letting the ap discover new recipes from scratch. On reflection, I’m not so sure that would work in a reasonable amount of execution time. I may code it in C++ if it’s too slow in JS. (Or I might end up with a recipe with 1 g each of 209 different ingredients!)


JS should be plenty fast enough if you use the right libraries - Chrome does jit compilation automatically, and the developer tools let you profile your code. You can also use something like for profiling.


Good work. You should use an framework like AngularJS to better separate your business logic and the user interface (HTML).

Maybe I can be of help with that, but I’m very busy the next month or so.


Thanks for that info. I’m a C++ and PHP guy. Aside from a little AJAX work, what I know about JS I could write on the back of a postage stamp.


But would it run? :smiley:


Seems like an interesting packing problem, which is the category in which the more well-known knapsack problem lives.

Some of you have this working in a small amount of time, but with a large ingredient database and balancing all of the micro and macronutrients you may find it to have quite a large complexity. Factor in price and taste, and this could be even more complex.

Is there a downloadable version of the DIY ingredient database available?


With a full database of ingredients to choose from this is the kind of computer problem that could run for thousands of hours!

There is a downloadable DB from USDA. It can be downloaded as separate DB tables in ASCII, or as a flat file for Excel, or as a delimited ASCII file at:


Is there any chance someone could give a quick explanation on how to run this program? I am pretty computer savvy but not a programmer. It would be great to be able to use this tool for my soylent experiments. I am on windows for the record.

Cheers for all the hard work making it!


Which program are you talking about? The one by @Alrecenk?


Yeah that’s the one. Also the changes Nickp has made.

I have tried the “Genetic Soylent” calculator over at but I would really like to try these others out as I can run it locally and get more precision on my recipe. Plus it will run the calculations faster as far as I can tell from this thread,