Commit 6c336844 authored by Philipp Wellner's avatar Philipp Wellner
Browse files

Added files for day one

parent e94eb8ae
Pipeline #24222 passed with stage
in 5 seconds
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mathematical Optimization First Exercise with Xpress and SCIP\n",
"\n",
"Exercises will be done in Jupyter notebooks. In this tutorial, you will\n",
"\n",
"1. model an introductory linear program using Xpress python\n",
"2. model the same program with SCIP\n",
"\n",
"#### Problem formulation\n",
"\n",
"Try to find the optimal solution to the following problem:\n",
"\n",
"$$\n",
"\\begin{align*}\n",
" \\min x + y \\\\\n",
" x + 2y \\geq 5\\\\\n",
" 4x + y \\geq 6\n",
"\\end{align*}\n",
"$$\n",
"You might remember this problem from the registration process. Do you also remember the solution?\n",
"\n",
"#### Xpress\n",
"Let's start with the Xpress implementation\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# import the Xpress Python module.\n",
"import xpress as xp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Code cells like the one above are editable, markdown cells like this are not. You need to execute a cell in order for the code in it to take action. One way of doing so is to click on the cell, and then select Cells --> Run Cells from the menu. Do this now for the cell above.\n",
" \n",
"Let's now start with your first line of code. The code below creates a first variable x. Use the same function to create a variable y in the code cell below.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create two variables x and y\n",
"x = xp.var('x',lb=-xp.infinity)\n",
"#...TODO...\n",
"\n",
"print(f\"1st variable: name {x.name} lb {x.lb} ub {x.ub} \")\n",
"print(f\"2nd variable: name {y.name} lb {y.lb} ub {y.ub} \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's take a big leap and create the whole problem. Since it is tiny, we will use a compact delcaration.Checkout the documentation of the Xpress Python interface:\n",
" https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/chModeling.html \n",
"\n",
"Go to 'Creating a problem' and look for the example at the bottom of the page. Try to adapt it to our model.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create a problem using the one-liner notation that first \n",
"# adds the variables, then the constraints, and finally, the objective function\n",
"p = xp.problem(\n",
"#...TODO...\n",
")\n",
"\n",
"p.write(\"register\",\"lp\")\n",
"with open(\"register.lp\", \"r\") as file_:\n",
" print(\"\".join(file_.readlines()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Search the documentation on how you solve a model (or take an educated guess).\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# solve it\n",
"#...TODO..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now store the optimal objective and solution vector into two local variables obj and sol.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# check solution\n",
"#...TODO...\n",
"\n",
"print(\"solution {} with objective value {}.\".format(sol, obj))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Congratulations, you solved your first model with the Xpress Python interface\n",
"#### SCIP\n",
"\n",
"Let us do the same with **SCIP** now\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pyscipopt import Model\n",
"\n",
"# initialize model\n",
"model = Model()\n",
"# make scip output visible in jupyter notebook\n",
"model.redirectOutput()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, we add the variables. See how the first variable is added and add the second accordingly."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# add variables\n",
"x = model.addVar('x',lb=None)\n",
"#...TODO...\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we add an objective and the two constraints. Consult the online documentation at http://scip-interfaces.github.io/PySCIPOpt/docs/html/md_README.html on how to do this."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# add objective \n",
"#...TODO...\n",
"\n",
"# add constraints\n",
"#...TODO...\n",
"\n",
"# check problem\n",
"model.writeProblem()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Solve the model and print the optimal solution vector and its objective value. For the latter, use the getSolVal function; consult the documentation on its syntax."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# solve problem\n",
"#...TODO...\n",
"\n",
"# check solution\n",
"#...TODO...\n",
"\n",
"print(\"solution {} with objective value {}.\".format(sol, obj))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment