Introduction

Optimization with Julia

Author
Affiliation

Dr. Tobias Vlćek

University of Hamburg - Fall 2024

Julia Programming Language

Why Julia?

  • Designed to be:
    • as general as Python
    • as statistics-friendly as R
    • as fast as C++!
Tip

Allows for fast data workflows, particularly in scientific computing!

Syntax

  • Dynamically-typed syntax just like Python
  • Similar to R, Matlab and Python - not like C++
  • In comparison, accessible and easy to learn!
  • No need to worry about memory management!

JuMP

  • Package for algebraic modeling in Julia
  • Simplifies solving complex optimization problems
  • Provides a high-level, user-friendly interface
  • Useful for operations research and data science
Tip

JuMP is an alternative to Pyomo, GAMS, and AMPL!

Algebraic Modeling

What is algebraic modeling?

  • A “mathematical language” for optimization problems
  • Allows for describing complex systems and constraints
  • Based on linear algebra (Equations and Inequalities)

How to learn algebraic modeling?

  • Practice, practice, and practice!
  • Understand standard models and their approach
  • Develop an understanding of constraints
  • Understand the structure of a models solution space
  • Use an available algorithms to determine solutions

Central Questions

  • What is to be decided?
  • What is relevant to the decision?
  • What information is given and relevant?
  • What parameters (data) are needed?
  • Which variables and of which type are needed?
Tip

Modeling is a creative process!

Model Components

  1. Objective function
  2. Constraints
  3. Variables

Linear Optimization Model

Basic Model Formulation

\[ \begin{aligned} &\text{maximize} \quad F = \sum_{j\in \mathcal{J}} c_j \times X_j \end{aligned} \]

subject to

\[ \begin{aligned} &\sum_{j\in \mathcal{J}} a_{i,j} \times X_j \le b_i && \forall i \in \mathcal{I} \\ &X_j \ge 0 && \forall j \in \mathcal{J} \end{aligned} \]

Model Components

\[ \begin{aligned} \mathcal{I} &: \text{set of $i \in \mathcal{I}$,}\\ \mathcal{J} &: \text{set of $j \in \mathcal{J}$,}\\ F &: \text{Objective function variable,}\\ X_{j} &: \text{decision variables,}\\ c_{j} &: \text{objective function coefficients,}\\ a_{i,j} &: \text{parameters,}\\ b_{i} &: \text{parameters} \end{aligned} \]

What is modelling good for?

  • Good Question! A lot of things:
    • Modeling real-world problems
    • Solving complex systems
    • Optimizing resource allocation
    • Decision-making under constraints
    • Simulation and prediction

Installing Julia

Download and Install Julia

To prepare for the upcoming lectures, we start by installing the Julia Programming Language and an Integrated Development Environment (IDE) to work with Julia.

Installing Julia

Tip

If you are ever asked to add something to your “PATH”, do so!

VS Code

  • Next, we are going to install VS Code
  • Alternatively, you can install VS Codium
  • It is essentially VS Code but without any tracking by MS

Installing VS Code

Verify the Installation

  • Start the IDE and take a look around
  • Search for the field “Extensions” on the left sidebar
  • Click it and search for “Julia”
  • Download and install “Julia (Julia Language Support)”

Create a new file

  • Create a new file with a “.jl” ending
  • Save it somewhere on your computer
  • e.g., in a folder that you will use in the course
print("Hello World!")
Hello World!
  • Run the file by clicking “run” in the upper right corner
  • OR by pressing “Control+Enter” or “STRG+Enter”

Everything working?

  • If the terminal opens with a Hello World! → perfect!
  • If not, it is likely that the IDE cannot find the path to Julia
  • Try to determine the path and save it to VS Code
  • After saving it, try to run the file again

How to get started?

  • Following three parts are dedicated to learning the basics
  • Start with the very basics and gradually move on
  • Focus in the first two parts on the programming language
  • Third part dedicated to Mathematical Optimization

Working with VS Code

Notebooks in VS Code

  • The easiest way is by using VS Code
  • Install the Jupyter Extension
  • Now, you can open .ipynb files
  • Here you can run the code in the cells

Downloading the Notebooks

  • You will find the notebooks next to the sections pages
  • On each page, you will find a button Jupyter on the right
  • Click it to download the notebook and save it
  • I’d recommend storing the notebooks in a separate directory for this workshop

Working with IJulia

IJulia

  • IJulia is an interface between Julia and Jupyter Notebooks
  • Popular tool for data analysis and visualization
  • You can use IJulia to run Julia code in the notebooks

Installing IJulia

  • Open the VS Code IDE and start a terminal
  • Start Julia by typing julia in the terminal
  • Install IJulia by typing ] to open the package manager
  • Install IJulia by typing add IJulia
  • Press Enter

Running IJulia

using IJulia; notebook()
  • Start IJulia by typing the above code in the Julia prompt
  • This will open a new browser window
  • You can now run code in the notebooks
Note

You can also run the notebooks in VS Code, if you prefer!

Get started with the sections

  • Download the first notebook and open it
  • Start with the first problem and solve it step by step
  • You can find the sections here on the website
  • You can ask questions anytime!

Literature

Literature

For interesting literature to learn more about Julia, take a look at the literature list of this course.