Using watsonx.ai to Solve a Decision Optimization Problem for Telephone Production

Annalise Sumpon
IBM Data Science in Practice
7 min readJun 6, 2024

Introduction

Decision optimization plays a crucial role in production planning, helping companies maximize profits while adhering to various constraints. In this article, we will demonstrate how to use watsonx.ai’s Prompt Lab module to generate decision optimization code for a simple linear programming problem involving telephone production. We will then run the generated code in the Decision Optimization module in the watsonx.ai platform to view the results.

Problem Description

We have a telephone company that produces and sells two kinds of telephones: desk phones and cellular phones. Each type of phone is assembled and painted by the company. The objective is to maximize profit, and the company has to produce at least 100 of each type of phone. There are limits in terms of the company’s production capacity, and the company has to calculate the optimal number of each type of phone to produce, while not exceeding the capacity of the plant.

The problem can be mathematically formulated as follows:

Maximize: 12*desk_production + 20*cell_production 

Subject to:
desk_production >= 100
cell_production >= 100
0.2*desk_production + 0.4*cell_production <= 400
0.5*desk_production + 0.4*cell_production <= 490

The problem can be mathematically formulated with the objective of maximizing profit, represented by the equation, where 12 and 20 are the profit coefficients per desk phone and cellular phone, respectively. The constraints include minimum production requirements of 100 units for each type of phone. Additionally, there are production capacity limits: the assembly process, which requires 0.2 hours per desk phone and 0.4 hours per cellular phone, must not exceed 400 hours, and the painting process, which requires 0.5 hours per desk phone and 0.4 hours per cellular phone, must not exceed 490 hours.

Structuring the Prompt for watsonx.ai

Introduction to Prompt Engineering

Prompt engineering is the process of designing and structuring prompts to effectively communicate with large language models (LLMs). A well-structured prompt ensures that the LLM understands the problem context and generates the desired output.

Components of a Good Prompt

  1. Problem Description: Clearly outline the problem context and objectives.
  2. Constraints and Objectives: Define all constraints and the objective function in mathematical terms.
  3. Desired Output: Specify the expected output format (e.g., Python code using DOcplex).

Example Prompt for watsonx

Problem description: telephone production
A telephone company produces and sells two kinds of telephones, namely desk phones and cellular phones.

Each type of phone is assembled and painted by the company.
The objective is to maximize profit, and the company has to produce at least 100 of each type of phone.

There are limits in terms of the company’s production capacity, and the company has to calculate the optimal number of each type of phone to produce, while not exceeding the capacity of the plant.

maximize: 12*desk_production + 20*cell_production
subject to:
desk_production >= 100
cell_production >= 100
0.2*desk_production + 0.4*cell_production <= 400
0.5*desk_production + 0.4*cell_production <= 490

Use the DOcplex Python library to write the mathematical model in Python. This is done in four steps:
1. Create an instance of `docplex.mp.Model` to hold all model objects
2. Create decision variables
3. Create linear constraints
4. Define the objective

Generating Code with watsonx.ai’s Prompt Lab

Access the Prompt Lab Module:

Log in to your watsonx account and open a new watsonx Project

  1. Choose New Task
  2. Search for Prompt Lab.
  3. Navigate to the Prompt Lab module.
  4. In Prompt Lab, choose the “Freeform” tab at the top
  5. You will need to choose a model in the Model Library. Click the model name in the top right corner.
  6. Today, we will be using “Mixtral 8x7b v-1”. Search for the model in the model library.
  7. Choose “Mixtral 8x7b v-1”. (Tip: Feel free to choose any other model that is optimized for code generation! Mixtral is used as an example.)
Steps 1–3
Steps 4–5
Steps 5–7

Enter the Prompt:

8. Click into the text workspace.

9. Input the structured prompt into the Prompt Lab module.

Steps 8–9

Receive the Generated Code:

10. Open the hyperparameters pane.

11. Adjust the max tokens to 500.

12. Click the blue Generate button in the bottom right. The Prompt Lab will display the generated DOcplex code.

Steps 10–12

Example Generated Code

from docplex.mp.model import Model

# Create a model instance
mdl = Model(name='Telephone production')
# Create decision variables
desk_production = mdl.integer_var(name='desk_production', lb=100)
cell_production = mdl.integer_var(name='cell_production', lb=100)
# Create constraints
mdl.add_constraint(desk_production >= 100, 'min_desk_production')
mdl.add_constraint(cell_production >= 100, 'min_cell_production')
mdl.add_constraint(0.2 * desk_production + 0.4 * cell_production <= 400, 'capacity_1')
mdl.add_constraint(0.5 * desk_production + 0.4 * cell_production <= 490, 'capacity_2')
# Define the objective
mdl.maximize(12 * desk_production + 20 * cell_production)
# Solve the model
mdl.solve()
# Print the solution
print("desk_production = ", desk_production.solution_value)
print("cell_production = ", cell_production.solution_value)
print("Profit = ", mdl.objective_value)

Running the Generated Code in the watsonx Decision Optimization Module

Create a New Decision Optimization Project:

13. Choose New Task and search for Decision Optimization.

14. Navigate to the Decision Optimization module.

15. Name your Decision Optimization project.

16. Click the blue Create button in the right corner.

Steps 13–16

Upload the Generated Code:

17. Click Build model in the sidebar.

18. Choose Python as your Code mode.

19. Copy the generated Python code from the Prompt Lab module and paste it into the new project in the Decision Optimization module.

Steps 17–19

Run the Optimization Model:

  1. Execute the code to solve the model by clicking the blue Run button in the top right corner.
  2. The CPLEX engine will process the code and provide the optimal solution based on the constraints and objectives defined in the Log.
Step 20

Analyzing the Results

After running the model, the Log will display the results, including the optimal production quantities for desk and cellular phones. The objective is to maximize profit while meeting the production constraints.

Scaling for Future Use

While using LLMs like within watsonx for generating optimization code is a powerful tool for getting started with small use cases, it is important to focus on scaling up over time. Here are some tips that I think are important for scaling and making the most of LLMs in the long term:

  1. Start Small: Begin with small, manageable problems to understand the capabilities and limitations of LLMs. This allows you to build confidence in the system and understand how to structure your prompts effectively.
  2. Mathematical Formulation: Always have a clear mathematical formulation of your optimization problem. This will make sure that you fully understand the problem and can validate the generated code against your own understanding.
  3. Use LLMs to Augment Processes: Use LLMs to augment and streamline the process of writing code, rather than replacing traditional methods entirely. This hybrid approach allows you to leverage the strengths of LLMs while maintaining control over the final implementation and minimizing any code errors.
  4. Iterative Improvement: Continuously refine your prompts and models based on the results and feedback. As you gain more experience, you can tackle more complex problems and scale your use of LLMs. You could even use a piece-wise approach when generating constraints and other rules.

By following these guidelines, you can effectively use LLMs to enhance your decision optimization processes and scale your operations over time.

In conclusion,

We demonstrated how to use watsonx to generate decision optimization code for a telephone production problem using the Prompt Lab module, and how to run the generated code in the Decision Optimization module. By following these steps, you can leverage the power of watsonx and DOcplex to solve complex optimization problems and enhance production planning in various industries. Have fun learning more!

Additional Resources

--

--