<jupyter_start><jupyter_text>Jupyter Notebook Integration[![magics.ipynb](https://img.shields.io/badge/github-%23121011.svg?logo=github)](https://github.com/ampl/amplcolab/blob/master/miscellaneous/magics.ipynb) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ampl/amplcolab/blob/master/miscellaneous/magics.ipynb) [![Kaggle](https://kaggle.com/static/images/open-in-kaggle.svg)](https://kaggle.com/kernels/welcome?src=https://github.com/ampl/amplcolab/blob/master/miscellaneous/magics.ipynb) [![Gradient](https://assets.paperspace.io/img/gradient-badge.svg)](https://console.paperspace.com/github/ampl/amplcolab/blob/master/miscellaneous/magics.ipynb) [![Open In SageMaker Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/ampl/amplcolab/blob/master/miscellaneous/magics.ipynb)Description: Jupyter Notebook Integration with amplpyTags: amplpy, exampleNotebook author: <NAME> author: N/AReferences: N/A<jupyter_code># Install dependencies
!pip install -q amplpy ampltools
# Google Colab & Kaggle interagration
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()<jupyter_output><empty_output><jupyter_text>Use `%%ampl_eval` to pass the model to AMPL<jupyter_code>%%ampl_eval
set SIZES;
param capacity >= 0;
param value {SIZES};
var Qty {SIZES} binary;
maximize TotVal: sum {s in SIZES} value[s] * Qty[s];
subject to Cap: sum {s in SIZES} s * Qty[s] <= capacity;<jupyter_output><empty_output><jupyter_text>Set data<jupyter_code>ampl.set['SIZES'] = [5, 4, 6, 3]
ampl.param['value'] = [10, 40, 30, 50]
ampl.param['capacity'] = 10<jupyter_output><empty_output><jupyter_text>Use `%%ampl_eval` to display values<jupyter_code>%%ampl_eval
display SIZES;
display value;
display capacity;<jupyter_output>set SIZES := 5 4 6 3;

value [*] :=
3  50
4  40
5  10
6  30
;

capacity = 10<jupyter_text>Use amplpy to retrive values<jupyter_code>print('SIZES:', ampl.set['SIZES'].getValues().toList())
print('value:', ampl.param['value'].getValues().toDict())
print('capacity:', ampl.param['capacity'].value())<jupyter_output>SIZES: [5.0, 4.0, 6.0, 3.0]
value: {3.0: 50.0, 4.0: 40.0, 5.0: 10.0, 6.0: 30.0}
capacity: 10.0<jupyter_text>Use `%%ampl_eval` to solve the model<jupyter_code>%%ampl_eval
option solver gurobi;
option gurobi_options 'outlev=1';
solve;<jupyter_output>Gurobi 9.5.0: outlev=1
Set parameter OutputFlag to value 1
Set parameter InfUnbdInfo to value 1
Gurobi Optimizer version 9.5.0 build v9.5.0rc5 (mac64[x86])
Thread count: 4 physical cores, 8 logical processors, using up to 8 threads
Optimize a model with 1 rows, 4 columns and 4 nonzeros
Model fingerprint: 0x1dc33b55
Variable types: 0 continuous, 4 integer (4 binary)
Coefficient statistics:
  Matrix range     [3e+00, 6e+00]
  Objective range  [1e+01, 5e+01]
  Bounds range     [1e+00, 1e+00]
  RHS range        [1e+01, 1e+01]
Found heuristic solution: objective 50.0000000
Presolve removed 1 rows and 4 columns
Presolve time: 0.00s
Presolve: All rows and columns removed

Explored 0 nodes (0 simplex iterations) in 0.00 seconds (0.00 work units)
Thread count was 1 (of 8 available processors)

Solution count 2: 90 50 

Optimal solution found (tolerance 1.00e-04)
Best objective 9.000000000000e+01, best bound 9.000000000000e+01, gap 0.0000%
Set parameter Presolve to value 0
Set parameter Method t[...]
{'fraction_non_alphanumeric': 0.13528138528138528, 'fraction_numerical': 0.053841991341991344, 'mean_word_length': 5.871747211895911, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 11, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '665177', 'license': 'MIT', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1344, 'n_words': 327, 'path': 'miscellaneous/magics.ipynb', 'repo_name': 'ampl/amplcolab', 'size': '3709'}