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.
- Download and unzip the DO-samples from the Decision Optimization GitHub on to your machine.
- Create a project in IBM Watson Studio. Select Create an empty project.
- Click Add to Project.
- Select Decision Optimization.
- Select the From file tab in the New Decision Optimization experiment window that opens.
- Associate a Machine Learning service instance with your project and reload the page.
- Click Add file. Then browse and choose Diet.zip from the Model_Builder folder in the DO-samples that you downloaded.
- Click Create. A Decision Optimization model is created with the same name as the sample.
In the Prepare data
view, you can see the data assets
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 (
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.
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.
- Run the model using the Run button on the top right of the Model view.
Once the model is solved, the page refreshes with the Solution view displayed. The solution contains a list of foods and their quantities, along with the nutrients that they provide. KPIs are also shown in this Solution view.
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.