<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 AMPLPY
Step1:  Google Colab & Kaggle interagration
Step2:  Quick start
Step 1
Step3:  Import Bokeh (do not run if you do not have Bokeh installed)
Step4:  For Jupyter Notebooks only (do not run if you are not using Bokeh and Jupyter)
Step5:  Step 2
Step6:  Step 3
Step7:  If the AMPL installation directory is not in the system search path, you should create the AMPL object as follows instead
Step8:  Step 5
Step9:  Note
Step10:  Define AMPL sets fom python lists
Step11:  Define data using an amplpy DataFrame
Step12:  Define data using a Pandas DataFrame
Step13:  Define data using a python dictionary
Step14:  Step 7
Step15:  Step 8
Step16:  Step 9
Step17:  Step 10
Step18:  Step 11
Step19:  Step 12
Step20:  Step 13
Step21:  Step 14
Step22:  Step 15
<ASSISTANT_TASK:>
Python Code:
!pip install -q amplpy ampltools pandas bokeh
Explanation: AMPLPY: Setup & Quick Start
Documentation: http://amplpy.readthedocs.io
GitHub Repository: https://github.com/ampl/amplpy
PyPI Repository: https://pypi.python.org/pypi/amplpy
Jupyter Notebooks: https://github.com/ampl/amplpy/tree/master/notebooks
Setup
Install from the PiPY repository:
$ python -m pip install amplpy
Note: For Windows, Linux, and macOS, the amplpy package comes with 33 binary wheels for Python 2.7, 3.5, 3.6, 3.7, 3.8, and 3.9. Please make sure that you are using the latest version of pip before installing amplpy (upgrade using "python -m pip install pip --upgrade"). If a binary wheel for your platform is not available, a C++ compiler and python development libraries will be required.
Aditional packages
In this tutorial, we will also use Pandas and Bokeh. You can install these packages using "pip install pandas bokeh" or "python -m pip install pandas bokeh". Note that Bokeh is not mandatory for this tutorial.
We also recommend the use of Jupyter Notebook, which was used to create this tutorial. You can install Jupyter using  "pip install jupyter" or "python -m pip install jupyter".
Setup
End of explanation
MODULES=['ampl', 'gurobi']
from ampltools import cloud_platform_name, ampl_notebook
from amplpy import AMPL, register_magics
if cloud_platform_name() is None:
    ampl = AMPL() # Use local installation of AMPL
else:
    ampl = ampl_notebook(modules=MODULES) # Install AMPL and use it
register_magics(ampl_object=ampl) # Evaluate %%ampl_eval cells with ampl.eval()
Explanation: Google Colab & Kaggle interagration
End of explanation
import pandas as pd  
Explanation: Quick start
Step 1: Import some packages that we will use
Import Pandas:
End of explanation
from bokeh.layouts import row
from bokeh.plotting import figure, show
Explanation: Import Bokeh (do not run if you do not have Bokeh installed):
End of explanation
from bokeh.io import output_notebook
output_notebook()
Explanation: For Jupyter Notebooks only (do not run if you are not using Bokeh and Jupyter):
End of explanation
from amplpy import AMPL, Environment, DataFrame
Explanation: Step 2: Import the amplpy components that we will use
End of explanation
ampl = AMPL()
ampl.eval('option version;')
Explanation: Step 3: Create an AMPL object
End of explanation
ampl.setOption('solver', 'gurobi')
Explanation: If the AMPL installation directory is not in the system search path, you should create the AMPL object as follows instead:
python
ampl = AMPL(Environment('full path to the AMPL installation directory'))
Note that you may need to use raw strings (e.g., r'C:\ampl\ampl.mswin64') or escape the slashes (e.g., 'C:\\ampl\\ampl.mswin64') if the path includes backslashes.
Step 4: Select the solver
End of explanation
ampl.eval('''
set NUTR;
set FOOD;
param cost {FOOD} > 0;
param f_min {FOOD} >= 0;
param f_max {j in FOOD} >= f_min[j];
param n_min {NUTR} >= 0;
param n_max {i in NUTR} >= n_min[i];
param amt {NUTR,FOOD} >= 0;
var Buy {j in FOOD} >= f_min[j], <= f_max[j];
minimize Total_Cost:  sum {j in FOOD} cost[j] * Buy[j];
subject to Diet {i in NUTR}:
   n_min[i] <= sum {j in FOOD} amt[i,j] * Buy[j] <= n_max[i];
''')
Explanation: Step 5: Define the model
End of explanation
foods = ['BEEF', 'CHK', 'FISH', 'HAM', 'MCH', 'MTL', 'SPG', 'TUR']
nutrients = ['A', 'C', 'B1', 'B2', 'NA', 'CAL']
Explanation: Note: Alternatively you can read the model from a file using "ampl.read(filename)".
Step 6: Define the initial data
End of explanation
ampl.getSet('FOOD').setValues(foods)
ampl.getSet('NUTR').setValues(nutrients)
Explanation: Define AMPL sets fom python lists
End of explanation
ampl.setData(DataFrame(
    index=[('FOOD', foods)], 
    columns=[
        ('cost', [3.59, 2.59, 2.29, 2.89, 1.89, 1.99, 1.99, 2.49]), 
        ('f_min', [2, 2, 2, 2, 2, 2, 2, 2]), 
        ('f_max', [10, 10, 10, 10, 10, 10, 10, 10])
    ]
))
Explanation: Define data using an amplpy DataFrame
End of explanation
df = pd.DataFrame({
    'n_min': [700, 700, 700, 700, 0, 16000], 
    'n_max': [20000, 20000, 20000, 20000, 50000, 24000]
}, 
    index=nutrients
)
ampl.setData(DataFrame.fromPandas(df))
Explanation: Define data using a Pandas DataFrame
End of explanation
amounts = [
    [ 60,    8,   8,  40,   15,  70,   25,   60],
    [ 20,    0,  10,  40,   35,  30,   50,   20],
    [ 10,   20,  15,  35,   15,  15,   25,   15],
    [ 15,   20,  10,  10,   15,  15,   15,   10],
    [928, 2180, 945, 278, 1182, 896, 1329, 1397],
    [295,  770, 440, 430,  315, 400,  379,  450]
]
df = DataFrame(('NUTR', 'FOOD'), 'amt')
df.setValues({
    (nutrient, food): amounts[i][j]
    for i, nutrient in enumerate(nutrients)
    for j, food in enumerate(foods)
})
ampl.setData(df)
Explanation: Define data using a python dictionary
End of explanation
ampl.solve()
Explanation: Step 7: Solve the model
End of explanation
ampl.getVariable('Buy').getValues().toPandas()
Explanation: Step 8: Create a Pandas DataFrame with the values of the variable 'Buy'
End of explanation
totalcost = ampl.getObjective('Total_Cost')
print("Objective is:", totalcost.value())
Explanation: Step 9: Display the objective value
End of explanation
cost = ampl.getParameter('cost')
cost.setValues({'BEEF': 5.01, 'HAM': 4.55})
print("Increased costs of beef and ham.")
Explanation: Step 10: Increase the costs of beef and ham
End of explanation
ampl.solve()
Explanation: Step 11: Solve the model with the new costs
End of explanation
print("New objective value:", totalcost.value())
Explanation: Step 12: Display the new objective value
End of explanation
Buy = ampl.getVariable('Buy')
print("Buy['BEEF'].val = {}".format(Buy['BEEF'].value()))
Explanation: Step 13: Display the value of Buy['BEEF']
End of explanation
diet = ampl.getConstraint('Diet')
for nutr in nutrients:
    print("Diet['{}'].dual = {}".format(nutr, diet[nutr].dual()))
Explanation: Step 14: Display the dual value of each diet constraint
End of explanation
rows = [tuple(row) for row in Buy.getValues()]
factors = [index for index, value in rows]
x = [value for index, value in rows]
dot = figure(
    title="Categorical Dot Plot", tools='', toolbar_location=None,
    y_range=factors, x_range=[0,12]
)
dot.segment(0, factors, x, factors, line_width=2, line_color='green')
dot.circle(x, factors, size=15, fill_color='orange', line_color='green', line_width=3)
show(dot)
Explanation: Step 15: Display the values of the variable 'Buy' using Bokeh
End of explanation
<END_TASK>