Plot feasible region
Description: Plot the feasible region and optimal solution for a simple two variable model using AMPL’s Python API.
Tags: lecture, LP, simple
Notebook author: Gyorgy Matyasfalvi <gyorgy@ampl.com>, Sarah Wells <sarah@ampl.com>
Model author: Gyorgy Matyasfalvi, Sarah Wells
References:
Solving the problem
Create an AMPL instance and specify model and data
To solve the optimization problem, we need to create an instance of AMPL and specify the model and data. The AMPL instance allows us to send the model and data we defined in the previous section to AMPL for processing.
Solve the problem
Using the amplpy API, we can directly set solver options and invoke the solve command to find the optimal solution to the optimization problem. Once we have solved the problem, we can retrieve the optimal values of make and print them to the console.
HiGHS 1.5.1: HiGHS 1.5.1: optimal solution; objective 17
0 simplex iterations
0 barrier iterations
Plotting of feasible region
To plot the feasible region, we need to retrieve the “constraint matrix” defined by the usage
parameter and the right-hand side values of the constraints defined by the limit
parameter. Once we have obtained these values, we can use them to generate the necessary data for plotting the feasible region.
Solving for iced_tea
To plot the feasible regions, we need to solve for iced_tea
in each of our constraints, which corresponds to the y
axis. Each constraint is associated with one of our “dry” ingredients. We will use a dictionary of lambda expressions, each of which expresses iced_tea
, to help us with this.
Draw graph
We now have all the necessary information to plot our graphs. The y
axis corresponds to iced_tea
and, the x
axis to lemonade
. The lemons and tea bags define a natural upper bound for the number of lemonades and iced teas we can make. This will help us to bound our graph. We will plot each function in constraint_dict
, and the intersection of regions between the axes and the functions will give us the feasible region. We finish by plotting the optimal solution as well.