Solving and analyzing a model: the diet problem

This example shows you how to create and solve a Python-based model using a sample.

About this task

This well-known optimization problem identifies the best mix of foodstuffs to meet dietary requirements while minimizing costs. The data inputs are the nutritional profile and price of different foods and the min and max values for nutrients in a diet. The model is expressed as the minimization of a linear program. The files used in this sample are available in the DO-samples.

Note: To create and run Optimization models you must have both a Machine Learning service added to your project and a deployment space associated with your experiment:
  1. Add a Machine Learning service to your project. You can either do this at the project level (see Creating a Watson Machine Learning Service instance), or this can be done when you first create a new Decision Optimization experiment: click Add a service, select or create a New service, click Associate service in the item bar, then close the pane.
  2. Associate a deployment space with your Decision Optimization experiment (see Deployment spaces). A deployment space can be created or selected when you first create a new Decision Optimization experiment: click Create a deployment space, enter a name for your deployment space and click Create. For existing models, you can also create or select a space in the Overview information pane.


  1. Download and extract all the DO-samples on to your machine. You can also download just the file from the Model_Builder subfolder for your product and version, but in this case do not extract it.
  2. Create a project in IBM Watson Studio. Select Create an empty project, enter a project name and click Create.
  3. In the Overview tab of your project, click add a Machine Learning service and select an existing service instance (or create a new one) and click Select.
  4. Click Add to Project.
  5. Select Decision Optimization experiment.
  6. Select the From file tab in the Decision Optimization experiment pane that opens.
  7. Associate a Machine Learning service instance with your project and reload the page.
  8. Choose a deployment space from the drop-down menu (or create one) and click Create. If you haven't already associated a Machine Learning service with your project, you must first select Add a service to select or create one, before choosing your deployment space for your experiment.
  9. Click Add file. Then browse to find the Model_Builder folder in your downloaded DO-samples. Select the relevant product and version subfolder. Choose the file.
  10. Click Create.
    A Decision Optimization model is created with the same name as the sample.
  11. In the Prepare data view, you can see the data assets imported.
    These tables represent the min and max values for nutrients in the diet (diet_nutrients), the nutrients in different foods (diet_food_nutrients), and the price and quantity of specific foods (diet_food).

    Tables of input data in Prepare data view

  12. Click Run model in the sidebar to view your model.
    The Python model minimizes the cost of the food in the diet while satisfying minimum nutrient and calorie requirements.

    Python model for diet problem displayed in Run Model view.

    Python model for diet problem displayed in Run model view

    Note also how the inputs (tables in the Prepare data view) and the outputs (in this case the solution table to be displayed in the Explore solution view) are specified in this model.

  13. Run the model using the Run button on the top right of the Run model view.


When the run is completed, you can see the results in the Explore solution view. You can also click Engine statistics or Log to see the solution chart and inspect the solver engine log files. The first tab in the Explore solution view shows the objective (or objectives if you have several) with its values and weights. The Solution tables tab provides you with a list of foods and their quantities, along with the nutrients that they provide.

You can also download the solution tables as csv files.

If your model had any conflicting constraints, these would be shown in the Conflicts tab with the Relaxations necessary to solve the model.

In the Visualization view, the solution is displayed as a table and a chart in the Solution page. You can add notes, different types of tables and charts to show input data, solution data or KPIs by selecting and editing the widgets. You can also create different pages in the Visualization view. For example, an Input page is also provided in this sample. See Visualization view.

You're ready to start running comparisons between different scenarios. For example, the basic solution contains a quantity of hot dog. You might want to check an alternate solution for someone who prefers a vegetarian diet.