Closed hadialaddin closed 5 years ago
There's no limit on the number of variables used in the results. I think that the problem you're giving it just happens to have an optimal solution with the three variables it's returning.
Quick and dirty solution to your problem, you could solve in multiple stages:
1.) Solve the problem
2.) If the count of results is < {{x}}, add max
constraints on the variables returned. For example, your solution returned:
{ 'omlet': 2.93935441, 'banana': 28.45265985, 'baked salmon': 4.50321154 }
try adding to the existing model something like:
{
"constraints": {
...
"omlet": {"max": 2.9},
"banana": {"max": 28},
"baked salmon": {"max": 4.3}
}
}
or whatever.
At the moment, I'm unfortunately not clever enough to know how to tell it to return a minimum of {{x}} variables in the solution.
This help?
I tried to put them under constraints as suggested, but it didn't have any effect. It always goes beyond the max I define for each. Could it be because I have over 300 variables?
Can you share your model?
On Mon, Oct 28, 2019 at 9:48 PM Hadi Aladdin notifications@github.com wrote:
I tried to put them under constraints as suggested, but it didn't have any effect. It always goes beyond the max I define for each. Could it be because I have over 300 variables?
— You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/JWally/jsLPSolver/issues/97?email_source=notifications&email_token=AAS6F55AUWEEFKGSRE5XD3TQQ6P6RA5CNFSM4JGB4WJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECPBUEI#issuecomment-547232273, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAS6F54OQNREVRIGZBYJK3LQQ6P6RANCNFSM4JGB4WJQ .
Here it is:
model = {
"optimize": "serving_calories",
"opType": "max",
"constraints": {
...maximum_servings,
"serving_calories": {"max": data.calories},
"serving_carbs": {"max": data.carbs},
"serving_fats": {"max": data.fats},
"serving_proteins": {"max": data.proteins}
},
"variables": meal_options,
"options": {
"tolerance": 0.1 // 10% margin of tolerance for the values
}
};
And note that I iterate over thousands of objects to fill:
meal_options[doc.id] = {"serving_calories": doc.serving_calories, "serving_carbs": doc.serving_carbs, "serving_fats": doc.serving_fats, "serving_proteins": doc.serving_proteins}
which I use in the model as variables (working fine).
And "maximum_servings" is an object that has each variable and the max array for it as you suggested in this format:
{
'398830': { max: 3 },
'400465': { max: 3 },
'401208': { max: 3 },
'404326': { max: 3 },
'406171': { max: 3 },
'407355': { max: 3 },
'407666': { max: 3 },
.
.
.
.
.
}
Try this:
Original Model:
{
"name": "Stigler Diet",
"background": "https://developers.google.com/optimization/lp/glop",
"optimize": "Price",
"opType": "min",
"constraints" : {
"Calories": { "min": 3 },
"Protein": { "min": 70 },
"Calcium": { "min": 0.8 },
"Iron": { "min": 12 },
"VitaminA": { "min": 5 },
"Thiamine": { "min": 1.8 },
"Riboflavin": { "min": 2.7 },
"Niacin": { "min": 18 },
"AscorbicAcid": { "min": 75 }
},
"variables": {
"Wheat Flour (Enriched)": { "Price": 1, "Calories": 44.7, "Protein": 1411, "Calcium": 2, "Iron": 365, "VitaminA": 0, "Thiamine": 55.4, "Riboflavin": 33.3, "Niacin": 441, "AscorbicAcid": 0 },
"Macaroni": { "Price": 1, "Calories": 11.6, "Protein": 418, "Calcium": 0.7, "Iron": 54, "VitaminA": 0, "Thiamine": 3.2, "Riboflavin": 1.9, "Niacin": 68, "AscorbicAcid": 0 },
"Wheat Cereal (Enriched)": { "Price": 1, "Calories": 11.8, "Protein": 377, "Calcium": 14.4, "Iron": 175, "VitaminA": 0, "Thiamine": 14.4, "Riboflavin": 8.8, "Niacin": 114, "AscorbicAcid": 0 },
"Corn Flakes": { "Price": 1, "Calories": 11.4, "Protein": 252, "Calcium": 0.1, "Iron": 56, "VitaminA": 0, "Thiamine": 13.5, "Riboflavin": 2.3, "Niacin": 68, "AscorbicAcid": 0 },
"Corn Meal": { "Price": 1, "Calories": 36.0, "Protein": 897, "Calcium": 1.7, "Iron": 99, "VitaminA": 30.9, "Thiamine": 17.4, "Riboflavin": 7.9, "Niacin": 106, "AscorbicAcid": 0 },
"Hominy Grits ": { "Price": 1, "Calories": 28.6, "Protein": 680, "Calcium": 0.8, "Iron": 80, "VitaminA": 0, "Thiamine": 10.6, "Riboflavin": 1.6, "Niacin": 110, "AscorbicAcid": 0 },
"Rice": { "Price": 1, "Calories": 21.2, "Protein": 460, "Calcium": 0.6, "Iron": 41, "VitaminA": 0, "Thiamine": 2, "Riboflavin": 4.8, "Niacin": 60, "AscorbicAcid": 0 },
"Rolled Oats": { "Price": 1, "Calories": 25.3, "Protein": 907, "Calcium": 5.1, "Iron": 341, "VitaminA": 0, "Thiamine": 37.1, "Riboflavin": 8.9, "Niacin": 64, "AscorbicAcid": 0 },
"White Bread (Enriched)": { "Price": 1, "Calories": 15.0, "Protein": 488, "Calcium": 2.5, "Iron": 115, "VitaminA": 0, "Thiamine": 13.8, "Riboflavin": 8.5, "Niacin": 126, "AscorbicAcid": 0 },
"Whole Wheat Bread": { "Price": 1, "Calories": 12.2, "Protein": 484, "Calcium": 2.7, "Iron": 125, "VitaminA": 0, "Thiamine": 13.9, "Riboflavin": 6.4, "Niacin": 160, "AscorbicAcid": 0 },
"Rye Bread": { "Price": 1, "Calories": 12.4, "Protein": 439, "Calcium": 1.1, "Iron": 82, "VitaminA": 0, "Thiamine": 9.9, "Riboflavin": 3, "Niacin": 66, "AscorbicAcid": 0 },
"Pound Cake": { "Price": 1, "Calories": 8.0, "Protein": 130, "Calcium": 0.4, "Iron": 31, "VitaminA": 18.9, "Thiamine": 2.8, "Riboflavin": 3, "Niacin": 17, "AscorbicAcid": 0 },
"Soda Crackers": { "Price": 1, "Calories": 12.5, "Protein": 288, "Calcium": 0.5, "Iron": 50, "VitaminA": 0, "Thiamine": 0, "Riboflavin": 0, "Niacin": 0, "AscorbicAcid": 0 },
"Milk": { "Price": 1, "Calories": 6.1, "Protein": 310, "Calcium": 10.5, "Iron": 18, "VitaminA": 16.8, "Thiamine": 4, "Riboflavin": 16, "Niacin": 7, "AscorbicAcid": 177 },
"Evaporated Milk (can)": { "Price": 1, "Calories": 8.4, "Protein": 422, "Calcium": 15.1, "Iron": 9, "VitaminA": 26, "Thiamine": 3, "Riboflavin": 23.5, "Niacin": 11, "AscorbicAcid": 60 },
"Butter": { "Price": 1, "Calories": 10.8, "Protein": 9, "Calcium": 0.2, "Iron": 3, "VitaminA": 44.2, "Thiamine": 0, "Riboflavin": 0.2, "Niacin": 2, "AscorbicAcid": 0 },
"Oleomargarine": { "Price": 1, "Calories": 20.6, "Protein": 17, "Calcium": 0.6, "Iron": 6, "VitaminA": 55.8, "Thiamine": 0.2, "Riboflavin": 0, "Niacin": 0, "AscorbicAcid": 0 },
"Eggs": { "Price": 1, "Calories": 2.9, "Protein": 238, "Calcium": 1.0, "Iron": 52, "VitaminA": 18.6, "Thiamine": 2.8, "Riboflavin": 6.5, "Niacin": 1, "AscorbicAcid": 0 },
"Cheese (Cheddar)": { "Price": 1, "Calories": 7.4, "Protein": 448, "Calcium": 16.4, "Iron": 19, "VitaminA": 28.1, "Thiamine": 0.8, "Riboflavin": 10.3, "Niacin": 4, "AscorbicAcid": 0 },
"Cream": { "Price": 1, "Calories": 3.5, "Protein": 49, "Calcium": 1.7, "Iron": 3, "VitaminA": 16.9, "Thiamine": 0.6, "Riboflavin": 2.5, "Niacin": 0, "AscorbicAcid": 17 },
"Peanut Butter": { "Price": 1, "Calories": 15.7, "Protein": 661, "Calcium": 1.0, "Iron": 48, "VitaminA": 0, "Thiamine": 9.6, "Riboflavin": 8.1, "Niacin": 471, "AscorbicAcid": 0 },
"Mayonnaise": { "Price": 1, "Calories": 8.6, "Protein": 18, "Calcium": 0.2, "Iron": 8, "VitaminA": 2.7, "Thiamine": 0.4, "Riboflavin": 0.5, "Niacin": 0, "AscorbicAcid": 0 },
"Crisco": { "Price": 1, "Calories": 20.1, "Protein": 0, "Calcium": 0, "Iron": 0, "VitaminA": 0, "Thiamine": 0, "Riboflavin": 0, "Niacin": 0, "AscorbicAcid": 0 },
"Lard": { "Price": 1, "Calories": 41.7, "Protein": 0, "Calcium": 0, "Iron": 0, "VitaminA": 0.2, "Thiamine": 0, "Riboflavin": 0.5, "Niacin": 5, "AscorbicAcid": 0 },
"Sirloin Steak": { "Price": 1, "Calories": 2.9, "Protein": 166, "Calcium": 0.1, "Iron": 34, "VitaminA": 0.2, "Thiamine": 2.1, "Riboflavin": 2.9, "Niacin": 69, "AscorbicAcid": 0 },
"Round Steak": { "Price": 1, "Calories": 2.2, "Protein": 214, "Calcium": 0.1, "Iron": 32, "VitaminA": 0.4, "Thiamine": 2.5, "Riboflavin": 2.4, "Niacin": 87, "AscorbicAcid": 0 },
"Rib Roast": { "Price": 1, "Calories": 3.4, "Protein": 213, "Calcium": 0.1, "Iron": 33, "VitaminA": 0, "Thiamine": 0, "Riboflavin": 2, "Niacin": 0, "AscorbicAcid": 0 },
"Chuck Roast": { "Price": 1, "Calories": 3.6, "Protein": 309, "Calcium": 0.2, "Iron": 46, "VitaminA": 0.4, "Thiamine": 1, "Riboflavin": 4, "Niacin": 120, "AscorbicAcid": 0 },
"Plate": { "Price": 1, "Calories": 8.5, "Protein": 404, "Calcium": 0.2, "Iron": 62, "VitaminA": 0, "Thiamine": 0.9, "Riboflavin": 0, "Niacin": 0, "AscorbicAcid": 0 },
"Liver (Beef)": { "Price": 1, "Calories": 2.2, "Protein": 333, "Calcium": 0.2, "Iron": 139, "VitaminA": 169.2, "Thiamine": 6.4, "Riboflavin": 50.8, "Niacin": 316, "AscorbicAcid": 525 },
"Leg of Lamb": { "Price": 1, "Calories": 3.1, "Protein": 245, "Calcium": 0.1, "Iron": 20, "VitaminA": 0, "Thiamine": 2.8, "Riboflavin": 3.9, "Niacin": 86, "AscorbicAcid": 0 },
"Lamb Chops (Rib)": { "Price": 1, "Calories": 3.3, "Protein": 140, "Calcium": 0.1, "Iron": 15, "VitaminA": 0, "Thiamine": 1.7, "Riboflavin": 2.7, "Niacin": 54, "AscorbicAcid": 0 },
"Pork Chops": { "Price": 1, "Calories": 3.5, "Protein": 196, "Calcium": 0.2, "Iron": 30, "VitaminA": 0, "Thiamine": 17.4, "Riboflavin": 2.7, "Niacin": 60, "AscorbicAcid": 0 },
"Pork Loin Roast": { "Price": 1, "Calories": 4.4, "Protein": 249, "Calcium": 0.3, "Iron": 37, "VitaminA": 0, "Thiamine": 18.2, "Riboflavin": 3.6, "Niacin": 79, "AscorbicAcid": 0 },
"Bacon": { "Price": 1, "Calories": 10.4, "Protein": 152, "Calcium": 0.2, "Iron": 23, "VitaminA": 0, "Thiamine": 1.8, "Riboflavin": 1.8, "Niacin": 71, "AscorbicAcid": 0 },
"Ham, smoked": { "Price": 1, "Calories": 6.7, "Protein": 212, "Calcium": 0.2, "Iron": 31, "VitaminA": 0, "Thiamine": 9.9, "Riboflavin": 3.3, "Niacin": 50, "AscorbicAcid": 0 },
"Salt Pork": { "Price": 1, "Calories": 18.8, "Protein": 164, "Calcium": 0.1, "Iron": 26, "VitaminA": 0, "Thiamine": 1.4, "Riboflavin": 1.8, "Niacin": 0, "AscorbicAcid": 0 },
"Roasting Chicken": { "Price": 1, "Calories": 1.8, "Protein": 184, "Calcium": 0.1, "Iron": 30, "VitaminA": 0.1, "Thiamine": 0.9, "Riboflavin": 1.8, "Niacin": 68, "AscorbicAcid": 46 },
"Veal Cutlets": { "Price": 1, "Calories": 1.7, "Protein": 156, "Calcium": 0.1, "Iron": 24, "VitaminA": 0, "Thiamine": 1.4, "Riboflavin": 2.4, "Niacin": 57, "AscorbicAcid": 0 },
"Salmon, Pink (can) ": { "Price": 1, "Calories": 5.8, "Protein": 705, "Calcium": 6.8, "Iron": 45, "VitaminA": 3.5, "Thiamine": 1, "Riboflavin": 4.9, "Niacin": 209, "AscorbicAcid": 0 },
"Apples": { "Price": 1, "Calories": 5.8, "Protein": 27, "Calcium": 0.5, "Iron": 36, "VitaminA": 7.3, "Thiamine": 3.6, "Riboflavin": 2.7, "Niacin": 5, "AscorbicAcid": 544 },
"Bananas": { "Price": 1, "Calories": 4.9, "Protein": 60, "Calcium": 0.4, "Iron": 30, "VitaminA": 17.4, "Thiamine": 2.5, "Riboflavin": 3.5, "Niacin": 28, "AscorbicAcid": 498 },
"Lemons ": { "Price": 1, "Calories": 1.0, "Protein": 21, "Calcium": 0.5, "Iron": 14, "VitaminA": 0, "Thiamine": 0.5, "Riboflavin": 0, "Niacin": 4, "AscorbicAcid": 952 },
"Oranges": { "Price": 1, "Calories": 2.2, "Protein": 40, "Calcium": 1.1, "Iron": 18, "VitaminA": 11.1, "Thiamine": 3.6, "Riboflavin": 1.3, "Niacin": 10, "AscorbicAcid": 1998 },
"Green Beans": { "Price": 1, "Calories": 2.4, "Protein": 138, "Calcium": 3.7, "Iron": 80, "VitaminA": 69, "Thiamine": 4.3, "Riboflavin": 5.8, "Niacin": 37, "AscorbicAcid": 862 },
"Cabbage": { "Price": 1, "Calories": 2.6, "Protein": 125, "Calcium": 4.0, "Iron": 36, "VitaminA": 7.2, "Thiamine": 9, "Riboflavin": 4.5, "Niacin": 26, "AscorbicAcid": 5369 },
"Carrots": { "Price": 1, "Calories": 2.7, "Protein": 73, "Calcium": 2.8, "Iron": 43, "VitaminA": 188.5, "Thiamine": 6.1, "Riboflavin": 4.3, "Niacin": 89, "AscorbicAcid": 608 },
"Celery": { "Price": 1, "Calories": 0.9, "Protein": 51, "Calcium": 3.0, "Iron": 23, "VitaminA": 0.9, "Thiamine": 1.4, "Riboflavin": 1.4, "Niacin": 9, "AscorbicAcid": 313 },
"Lettuce": { "Price": 1, "Calories": 0.4, "Protein": 27, "Calcium": 1.1, "Iron": 22, "VitaminA": 112.4, "Thiamine": 1.8, "Riboflavin": 3.4, "Niacin": 11, "AscorbicAcid": 449 },
"Onions": { "Price": 1, "Calories": 5.8, "Protein": 166, "Calcium": 3.8, "Iron": 59, "VitaminA": 16.6, "Thiamine": 4.7, "Riboflavin": 5.9, "Niacin": 21, "AscorbicAcid": 1184 },
"Potatoes": { "Price": 1, "Calories": 14.3, "Protein": 336, "Calcium": 1.8, "Iron": 118, "VitaminA": 6.7, "Thiamine": 29.4, "Riboflavin": 7.1, "Niacin": 198, "AscorbicAcid": 2522 },
"Spinach": { "Price": 1, "Calories": 1.1, "Protein": 106, "Calcium": 0, "Iron": 138, "VitaminA": 918.4, "Thiamine": 5.7, "Riboflavin": 13.8, "Niacin": 33, "AscorbicAcid": 2755 },
"Sweet Potatoes": { "Price": 1, "Calories": 9.6, "Protein": 138, "Calcium": 2.7, "Iron": 54, "VitaminA": 290.7, "Thiamine": 8.4, "Riboflavin": 5.4, "Niacin": 83, "AscorbicAcid": 1912 },
"Peaches (can)": { "Price": 1, "Calories": 3.7, "Protein": 20, "Calcium": 0.4, "Iron": 10, "VitaminA": 21.5, "Thiamine": 0.5, "Riboflavin": 1, "Niacin": 31, "AscorbicAcid": 196 },
"Pears (can)": { "Price": 1, "Calories": 3.0, "Protein": 8, "Calcium": 0.3, "Iron": 8, "VitaminA": 0.8, "Thiamine": 0.8, "Riboflavin": 0.8, "Niacin": 5, "AscorbicAcid": 81 },
"Pineapple (can)": { "Price": 1, "Calories": 2.4, "Protein": 16, "Calcium": 0.4, "Iron": 8, "VitaminA": 2, "Thiamine": 2.8, "Riboflavin": 0.8, "Niacin": 7, "AscorbicAcid": 399 },
"Asparagus (can)": { "Price": 1, "Calories": 0.4, "Protein": 33, "Calcium": 0.3, "Iron": 12, "VitaminA": 16.3, "Thiamine": 1.4, "Riboflavin": 2.1, "Niacin": 17, "AscorbicAcid": 272 },
"Green Beans (can)": { "Price": 1, "Calories": 1.0, "Protein": 54, "Calcium": 2, "Iron": 65, "VitaminA": 53.9, "Thiamine": 1.6, "Riboflavin": 4.3, "Niacin": 32, "AscorbicAcid": 431 },
"Pork and Beans (can)": { "Price": 1, "Calories": 7.5, "Protein": 364, "Calcium": 4, "Iron": 134, "VitaminA": 3.5, "Thiamine": 8.3, "Riboflavin": 7.7, "Niacin": 56, "AscorbicAcid": 0 },
"Corn (can)": { "Price": 1, "Calories": 5.2, "Protein": 136, "Calcium": 0.2, "Iron": 16, "VitaminA": 12, "Thiamine": 1.6, "Riboflavin": 2.7, "Niacin": 42, "AscorbicAcid": 218 },
"Peas (can)": { "Price": 1, "Calories": 2.3, "Protein": 136, "Calcium": 0.6, "Iron": 45, "VitaminA": 34.9, "Thiamine": 4.9, "Riboflavin": 2.5, "Niacin": 37, "AscorbicAcid": 370 },
"Tomatoes (can)": { "Price": 1, "Calories": 1.3, "Protein": 63, "Calcium": 0.7, "Iron": 38, "VitaminA": 53.2, "Thiamine": 3.4, "Riboflavin": 2.5, "Niacin": 36, "AscorbicAcid": 1253 },
"Tomato Soup (can)": { "Price": 1, "Calories": 1.6, "Protein": 71, "Calcium": 0.6, "Iron": 43, "VitaminA": 57.9, "Thiamine": 3.5, "Riboflavin": 2.4, "Niacin": 67, "AscorbicAcid": 862 },
"Peaches, Dried": { "Price": 1, "Calories": 8.5, "Protein": 87, "Calcium": 1.7, "Iron": 173, "VitaminA": 86.8, "Thiamine": 1.2, "Riboflavin": 4.3, "Niacin": 55, "AscorbicAcid": 57 },
"Prunes, Dried": { "Price": 1, "Calories": 12.8, "Protein": 99, "Calcium": 2.5, "Iron": 154, "VitaminA": 85.7, "Thiamine": 3.9, "Riboflavin": 4.3, "Niacin": 65, "AscorbicAcid": 257 },
"Raisins, Dried ": { "Price": 1, "Calories": 13.5, "Protein": 104, "Calcium": 2.5, "Iron": 136, "VitaminA": 4.5, "Thiamine": 6.3, "Riboflavin": 1.4, "Niacin": 24, "AscorbicAcid": 136 },
"Peas, Dried": { "Price": 1, "Calories": 20.0, "Protein": 1367, "Calcium": 4.2, "Iron": 345, "VitaminA": 2.9, "Thiamine": 28.7, "Riboflavin": 18.4, "Niacin": 162, "AscorbicAcid": 0 },
"Lima Beans, Dried": { "Price": 1, "Calories": 17.4, "Protein": 1055, "Calcium": 3.7, "Iron": 459, "VitaminA": 5.1, "Thiamine": 26.9, "Riboflavin": 38.2, "Niacin": 93, "AscorbicAcid": 0 },
"Navy Beans, Dried": { "Price": 1, "Calories": 26.9, "Protein": 1691, "Calcium": 11.4, "Iron": 792, "VitaminA": 0, "Thiamine": 38.4, "Riboflavin": 24.6, "Niacin": 217, "AscorbicAcid": 0 },
"Coffee": { "Price": 1, "Calories": 0, "Protein": 0, "Calcium": 0, "Iron": 0, "VitaminA": 0, "Thiamine": 4, "Riboflavin": 5.1, "Niacin": 50, "AscorbicAcid": 0 },
"Tea": { "Price": 1, "Calories": 0, "Protein": 0, "Calcium": 0, "Iron": 0, "VitaminA": 0, "Thiamine": 0, "Riboflavin": 2.3, "Niacin": 42, "AscorbicAcid": 0 },
"Cocoa": { "Price": 1, "Calories": 8.7, "Protein": 237, "Calcium": 3, "Iron": 72, "VitaminA": 0, "Thiamine": 2, "Riboflavin": 11.9, "Niacin": 40, "AscorbicAcid": 0 },
"Chocolate": { "Price": 1, "Calories": 8.0, "Protein": 77, "Calcium": 1.3, "Iron": 39, "VitaminA": 0, "Thiamine": 0.9, "Riboflavin": 3.4, "Niacin": 14, "AscorbicAcid": 0 },
"Sugar": { "Price": 1, "Calories": 34.9, "Protein": 0, "Calcium": 0, "Iron": 0, "VitaminA": 0, "Thiamine": 0, "Riboflavin": 0, "Niacin": 0, "AscorbicAcid": 0 },
"Corn Syrup ": { "Price": 1, "Calories": 14.7, "Protein": 0, "Calcium": 0.5, "Iron": 74, "VitaminA": 0, "Thiamine": 0, "Riboflavin": 0, "Niacin": 5, "AscorbicAcid": 0 },
"Molasses ": { "Price": 1, "Calories": 9.0, "Protein": 0, "Calcium": 10.3, "Iron": 244, "VitaminA": 0, "Thiamine": 1.9, "Riboflavin": 7.5, "Niacin": 146, "AscorbicAcid": 0 },
"Strawberry Preserves": { "Price": 1, "Calories": 6.4, "Protein": 11, "Calcium": 0.4, "Iron": 7, "VitaminA": 0.2, "Thiamine": 0.2, "Riboflavin": 0.4, "Niacin": 3, "AscorbicAcid": 0 }
}
}
Which results in the following:
{
"feasible": true,
"Navy Beans, Dried": 0.06102856,
"Liver (Beef)": 0.00189256,
"Spinach": 0.00500766,
"Wheat Flour (Enriched)": 0.02951906,
"Cabbage": 0.01121444,
"result": 0.10866228
}
So in the model, I'm going to give every variable a {{self}} attribute of 1 via the following hacky code:
var result_01 = solver.Solve(model);
console.log(result_01);
// Iterate over variables add a {{self}} attribute of 1
for(variable in model.variables){
//
// If the variable's name is "apple", it *should*
// now look like this:
//
// "apple": {"carbs": 0.4, "apple": 1, "etc": 888}
model.variables[variable][variable] = 1;
//
// If this variable was part of the prior result_01 set,
// add a constraint on it @ 95% of rhe prior result (arbitrary)
if(result_01[variable]){
model.constraints[variable] = {"max": result_01[variable] * 0.95};
}
};
var result_02 = solver.Solve(model);
console.log(result_02);
Which adds the following constraints to the model:
"Wheat Flour (Enriched)": {
"max": 0.028043106999999998
},
"Liver (Beef)": {
"max": 0.001797932
},
"Cabbage": {
"max": 0.010653718
},
"Spinach": {
"max": 0.004757277
},
"Navy Beans, Dried": {
"max": 0.057977132
}
Which results in the following solution with more variables used:
{ feasible: true,
result: 0.11049072,
bounded: true,
'Navy Beans, Dried': 0.05797713,
'Corn Meal': 0.0010777,
Spinach: 0.00475728,
'Wheat Flour (Enriched)': 0.02804311,
Cabbage: 0.01065372,
'Lima Beans, Dried': 0.00472442,
Potatoes: 0.00062186,
'Sweet Potatoes': 0.00160229,
'Evaporated Milk (can)': 0.00103322 }
Does LP Solver limit the number of resulting variables to 3? I give it 500 variables and it always tries to generate a max of 3 variables to fit the constraints, by increasing the quantities. This makes it give me something like:
{ 'omlet': 2.93935441, 'banana': 28.45265985, 'baked salmon': 4.50321154 }
Any way I can make it generate 3-6 variables as results instead of 3 only to avoid getting a very high number for one variable (such as 'banana' above?)