Jupyter Notebook Integration

magics.ipynb Open In Colab Kaggle Gradient Open In SageMaker Studio Lab Hits

Description: Jupyter Notebook Integration with amplpy

Tags: amplpy, example

Notebook author: Filipe Brandão <fdabrandao@gmail.com>

Model author: N/A

References: N/A

# Install dependencies
%pip install -q amplpy
# Google Colab & Kaggle integration
from amplpy import AMPL, ampl_notebook

ampl = ampl_notebook(
    modules=["gurobi"],  # modules to install
    license_uuid="default",  # license to use
)  # instantiate AMPL object and register magics

Use %%ampl_eval to pass the model to AMPL

%%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;

Set data

ampl.set["SIZES"] = [5, 4, 6, 3]
ampl.param["value"] = [10, 40, 30, 50]
ampl.param["capacity"] = 10

Use %%ampl_eval to display values

%%ampl_eval
display SIZES;
display value;
display capacity;
set SIZES := 5 4 6 3;

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

capacity = 10

Use amplpy to retrive values

print("SIZES:", ampl.set["SIZES"].to_list())
print("value:", ampl.param["value"].to_dict())
print("capacity:", ampl.param["capacity"].value())
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

Use %%ampl_eval to solve the model

%%ampl_eval
option solver gurobi;
option gurobi_options 'outlev=1';
solve;
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 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: 0xe07c951d
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]
Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    9.0000000e+01   0.000000e+00   0.000000e+00      0s

Solved in 0 iterations and 0.00 seconds (0.00 work units)
Optimal objective  9.000000000e+01
Gurobi 9.5.0: optimal solution; objective 90