Skip to content
Anders Peterson edited this page May 10, 2019 · 3 revisions

There is an updated version of this page at https://www.ojalgo.org/2019/05/the-diet-problem/


The diet problem is one of the earliest real-life problems to be solved using linear programming. This example shows how to use ojAlgo to model and solve a (miniature) instance of this problem. Background information as well as the actual numbers used in this example can be found at NEOS.

It should be easy for you to adapt this example code to whatever optimization problem you want to model.

Example code

import org.ojalgo.OjAlgoUtils;
import org.ojalgo.netio.BasicLogger;
import org.ojalgo.optimisation.Expression;
import org.ojalgo.optimisation.ExpressionsBasedModel;
import org.ojalgo.optimisation.Optimisation;
import org.ojalgo.optimisation.Variable;

/**
 * An example of how to implement a simple optimisation problem - The Diet Problem. It's one of the earliest
 * real-life problems to be solved using linear programming.
 *
 * @author apete
 */
public class TheDietProblem {

    public static void main(final String[] args) {

        BasicLogger.debug();
        BasicLogger.debug(TheDietProblem.class.getSimpleName());
        BasicLogger.debug(OjAlgoUtils.getTitle());
        BasicLogger.debug(OjAlgoUtils.getDate());
        BasicLogger.debug();

        // Create variables expressing servings of each of the considered foods
        // Set lower and upper limits on the number of servings as well as the weight (cost of a
        // serving) for each variable.
        final Variable tmpBread = Variable.make("Bread").lower(0).upper(10).weight(0.05);
        final Variable tmpCorn = Variable.make("Corn").lower(0).upper(10).weight(0.18);
        final Variable tmpMilk = Variable.make("Milk").lower(0).upper(10).weight(0.23);

        // Create a model and add the variables to it.
        final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel();
        tmpModel.addVariable(tmpBread);
        tmpModel.addVariable(tmpCorn);
        tmpModel.addVariable(tmpMilk);

        // Create a vitamin A constraint.
        // Set lower and upper limits and then specify how much vitamin A a serving of each of the
        // foods contain.
        final Expression tmpVitaminA = tmpModel.addExpression("Vitamin A").lower(5000).upper(50000);
        tmpVitaminA.set(tmpBread, 0).set(tmpCorn, 107).set(tmpMilk, 500);

        // Create a calories constraint...
        final Expression tmpCalories = tmpModel.addExpression("Calories").lower(2000).upper(2250);
        tmpCalories.set(tmpBread, 65).set(tmpCorn, 72).set(tmpMilk, 121);

        // Solve the problem - minimise the cost
        Optimisation.Result tmpResult = tmpModel.minimise();

        // Print the result
        BasicLogger.debug();
        BasicLogger.debug(tmpResult);
        BasicLogger.debug();

        // Modify the model to require an integer valued solution.
        BasicLogger.debug("Adding integer constraints...");
        tmpBread.integer(true);
        tmpCorn.integer(true);
        tmpMilk.integer(true);

        // Solve again
        tmpResult = tmpModel.minimise();

        // Print the result, and the model
        BasicLogger.debug();
        BasicLogger.debug(tmpResult);
        BasicLogger.debug();
        BasicLogger.debug(tmpModel);
        BasicLogger.debug();
    }

}

Console output


TheDietProblem
ojAlgo
2015-01-11


OPTIMAL 3.1499999999992 @ [10.0, 1.94444444444, 10.0]

Adding integer constraints...

OPTIMAL 3.16 @ [10.0, 2.0, 10.0]

############################################
0 <= Bread: 10.000000 (0.050000) <= 10.000000
0 <= Corn: 2.000000 (0.180000) <= 10.000000
0 <= Milk: 10.000000 (0.230000) <= 10.000000
5000.000000 <= Vitamin A: 5214.0 <= 50000.000000
2000.000000 <= Calories: 2004.0 <= 2250.000000
############################################