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++!

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

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?

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

Screenshot of the julialang.org homepage, headed The Julia Programming Language, with a green Download button next to a Documentation button.

  • Head to julialang.org/downloads and follow the instructions
  • macOS/Linux: run the install command in a terminal
  • Windows: download and open the MSIX App Installer
  • Later, you can then manage Julia with juliaup

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

Zed

  • Next, we are going to install Zed, the editor of this workshop
  • macOS: brew install --cask zed (or the installer from zed.dev)
  • Windows: installer from zed.dev/download
  • Linux: curl -f https://zed.dev/install.sh | sh

On Windows, Zed needs a DirectX 11 GPU with a current graphics driver. If it shows a blank window or an “Unsupported GPU” warning, update the driver first. Still not working? Use VS Code with the “Julia (Julia Language Support)” extension instead: everything in this workshop works there too.

Verify the Installation

  • Start Zed and take a look around
  • Open the command palette: Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS)
  • Choose zed: extensions and search for “Julia”
  • Install the Julia extension for syntax 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!
  • Open a terminal in that folder and run the file with:
  • julia test.jl

Everything working?

  • If the terminal prints Hello World! → perfect!
  • If not, the terminal likely cannot find the path to Julia
  • Open a new terminal after installing Julia (restart Zed if you use its terminal)
  • Then try to run the file again
  • Still not working? The FAQ lists the usual fixes

Installation Checklist

Before you start with the first tutorial, make sure you can:

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

Julia in Zed

Tutorials in Zed

  • The easiest way to work on the tutorials is a .jl file in Zed, run cell by cell
  • For detailed instructions, just open the first tutorial
  • It explains step-by-step how to set up the workshop environment and run the cells

Downloading the Files

  • You will find the tutorial files next to the sections pages
  • On each page, you will find a link Julia on the right (under “Code Links”)
  • Click it to download the .jl file and save it
  • Prefer a notebook? The link Jupyter (under “Other Formats”) downloads the same tutorial as .ipynb
  • I’d recommend storing the files in a separate directory for this workshop

Get started with the sections

  • Download the first .jl file and open it in Zed
  • 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.