<jupyter_start><jupyter_text>Production Planning Problem Problem descriptionA company needs to decide their production plan for the next month. They produce four products: A, B, C and D. Each product has a known demand, the production cannot exceed this demand as it would be lost. Moreover, the company has a limited time of 2000 minutes to produce all the products. What should be the production level for each product so that they can maximize their profit.| Product | Demand | Profit | Processing time ||---------|--------|--------|-----------------||    A    |   50   |   40   |       15        ||    B    |   70   |   20   |       15        ||    C    |  100   |   10   |        5        ||    D    |   20   |   70   |       25        |  Model formulationWe would use a Mathematical programming approach to formulate our optimization model. As our production should be determine by units, the decision must be a integer number. In this case, we are working with an Integer Programming problem. Sets$Products$: set of products Parameters$demand_p$: demand of product $p \in Products$$profit_p$: profit per unit of product $p \in Products$$proTime_p$: processing time per unit of product $p \in Products$$capacity$: total time capacity available for production Decision variables$production_p \in \mathbb{Z}^+$: production level of product $p \in Products$ Objective function$$\max totalProfit=\sum_{p \in Products} production_p * profit_p$$ Constraints$$production_p \leq demand_p   \forall p \in Products$$$$\sum_{p \in Products} production_p * proTime_p \leq capacity$$  Python implementationWe will follow the next steps by using the IBM® Decision Optimization CPLEX® Modeling for Python:1. Import docplex library2. Create a Model instance3. Define sets and parameters4. Define decision variables5. Define constraints6. Define linear expressions7. Define objective function8. Define de solving command  1. Import docplex libraryFirst of all, we will import the docplex library, and specifically the model module. If you need to use other library, for example, for data processing or data visualization, you should import it in this step as well. For this problem, we are no using any additional library.<jupyter_code>import docplex
from docplex.mp.model import Model<jupyter_output><empty_output><jupyter_text>2. Create a Model instanceYou need a Model instance to define all the elements needed to create the production problem<jupyter_code>model = Model(name="production_problem")<jupyter_output><empty_output><jupyter_text>3. Define sets and parameters**Sets:** The first you need to do is define your sets as they are going to be the indexes for your parameters and variable decisions. **Parameters:** These are the known data from your problem that allows to make decisions.<jupyter_code># Define sets
products = {"A","B","C","D"}

# Define parameters
#demand
demand = {"A":50,"B":70,"C":100,"D":20}
#profit
profit = {"A":40,"B":20,"C":10,"D":70}
#processing time
pro_time = {"A":15,"B":15,"C":5,"D":25}
#total capacity
capacity = 2000<jupyter_output><empty_output><jupyter_text>4. Define decision variables<jupyter_code>production = model.integer_var_dict(products,name="prod")<jupyter_output><empty_output><jupyter_text>5. Define constraintsFor this problem we have two constraints:1. The company cannot sale products over the known demand2. The company has limited time capacity to produce all the products.<jupyter_code>#1. The company cannot sale products over the known demand
for p in products:
    model.add_constraint(production[p]<=demand[p])
#2. The company has limited time capacity to produce all the products.
model.add_constraint(model.sum(production[p]*pro_time[p] for p in products)<=capacity)<jupyter_output><empty_output><jupyter_text>6. Define linear expressionDefining linear expression is a good practice when using complex or long objective functions. Dividing an expression let you keep track of the values each expression is getting and, in some cases, find formulation problems.For this example, the objective function is simple, btu we are using linear expressions to show how to build them and use them.<jupyter_code>profit_prod = model.sum(production[p]*profit[p] for p in products)<jupyter_output><empty_output><jupyter_text>7. Define the objective functionThe objective function is an important step in your formulation as it is the one that would guide your problem solution. Results might change as the objective function is changed, so be careful when defining it. For this example, we are try to get the highest profit possible, so we choose the maximization as our objective.<jupyter_code>model.maximize(profit_prod)<jupyter_output><empty_output><jupyter_text>8. Solve your modelBy using the solve() method from the model instance, the optimiaztion motor is called<jupyter_code>solution=model.solve()<jupyter_output><empty_output><jupyter_text>Post processingOnce the optimization model is solved, you can retrieve the results to pritn them.<jupyter_code>print('The maximum profit would be: %a' %solution.get_objective_value())
for p in sorted(products):
    print('The company needs to produce %a units of product %s' %(production[p].solution_value,p))<jupyter_output>The maximum profit would be: 4730
The company needs to produce 50.0 units of product A
The company needs to produce 17.0 units of product B
The company needs to produce 99.0 units of product C
The company needs to produce 20.0 units of product D
{'fraction_non_alphanumeric': 0.08255537250594912, 'fraction_numerical': 0.016840563792787845, 'mean_word_length': 4.887931034482759, 'pattern_counts': {'":': 12, '<': 38, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '556372', 'license': 'Apache-2.0', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1407, 'n_words': 664, 'path': 'Production_Planning/CPLEX_Python/Production planning docplex.ipynb', 'repo_name': 'SmartBP/Modeling_Training', 'size': '5463'}