Lecture II - First Steps in Julia

Applied Optimization with Julia

Author
Affiliation

Dr. Tobias Vlćek

University of Hamburg

Quick Recap on the Technical Setup

Goals for Today

After this lecture, you will have:

  • A working Julia and VS Code setup on your computer
  • Made your first Git commit
  • Learned how to submit assignments and earn bonus points
  • Started with the first tutorials

Download and Install Julia

The Julia programming language logo

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 Julia download page

. . .

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

VS Code

The VS Codium logo

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

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
println("Hello World!")
Hello World!

Run the File

  • Run the file by clicking “run” in the upper right corner
  • OR by pressing Ctrl+Enter — labelled Strg on German keyboards
  • On macOS it is Ctrl+Enter or Cmd+Enter, depending on your keybindings1

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

Don’t worry if it is not running right away. We will fix this together!

Learning Julia

Julia as a Programming Language

  • The following three lectures are dedicated to learning the basics
  • Start with the very basics and gradually move on
  • Focus in the first two lectures on the programming language
  • Third lecture dedicated to Mathematical Optimization

A First Taste of Julia

Question: What will your savings be worth in 10 years?

. . .

savings = 1000       # Euros in your account
interest_rate = 0.03 # 3% interest per year
value = savings * (1 + interest_rate)^10
println("Value after 10 years: ", round(value; digits=2))
Value after 10 years: 1343.92

. . .

  • Don’t worry if this looks new — you will be able to read and write code like this after the tutorials!

Working with VS Code and Julia

Notebooks in VS Code

  • The easiest way to work on the tutorials is by using VS Code
  • For detailed instructions, just open the first tutorial
  • It explains step-by-step how to use .jl or .ipynb files as notebooks

. . .

If you use .jl files, you can also put them under version control with Git, as you will see later in this lecture.

Downloading the Notebooks

  • You will find the tutorial notebooks next to the tutorial pages
  • On each page, you will find a button Julia on the right
  • Click it to download the jl file and save it
  • If .jl files do not work for you, you can also click on Jupyter
  • This will download an .ipynb file which you can use directly as notebook
  • I’d really recommend storing the files in a separate directory for this course

Learning by Doing

  • The best way to learn a programming language is by doing
  • We will therefore solve problems the coming weeks
  • The goal is to get you familiar with the language
  • You can discuss the problems with your fellow students
  • You can hand in your solutions to receive bonus points!

Working with Git

What is Git?

  • Git is a version control system that tracks changes in your code
  • Can be used for collaboration and keeping track of your work
  • Allows you to save “snapshots” of your project at different stages
  • You can always go back to previous versions if something goes wrong
  • No need to create files like tutorial_v1.jl and tutorial_v2.jl

Installing Git

  • Head to git-scm.com and download Git
  • Follow the installation instructions on the website for your OS

If you have any questions, feel free to ask!

Git Extension in VS Code

  • VS Code has built-in Git support!
  • Look for the “Source Control” icon in the left sidebar (looks like a branch)
  • For enhanced features, install the GitGraph extension

. . .

Git is not required for the course, but strongly recommended: once you get used to it, it becomes invaluable, especially if you are working with a lot of code!

Initialize a Repository

  • Open your project folder (of our lecture) in VS Code
  • Click on “Source Control” in the left sidebar
  • Click “Initialize Repository” button
  • Your folder is now a Git repository!

. . .

You can also synchronize your repository with GitHub or other hosting services. Then, your code is saved in a remote location, making it accessible from anywhere and allowing collaboration with others.

Making Your First Commit

  • Make changes to your files (e.g., work on a tutorial .jl file)
  • Go to Source Control panel
  • You’ll see your changes listed under “Changes”
  • Click the “+” next to files to stage them
  • Add a commit message describing your changes
  • Click the checkmark ✓ to commit

Viewing History

  • Use the “Git Graph” extension for a visual representation
  • Click the “Git Graph” button in the Source Control panel
  • See your commit history as a branching diagram

. . .

Start using Git from day one! Even for small projects, it’s a good habit to develop.

Assignments & Grading

Submission of Assignments

  • You can work in groups of up to three people
  • Submit the assignment via Moodle
  • You will submit your assignment by uploading a notebook
  • The assignment is due the day before the next tutorial

. . .

Don’t forget to save your notebook before uploading it to Moodle!

Grading of Assignments

  • Each assignment is worth 0.5 points
  • You can get a maximum of 6.0 points from the assignments
  • The points will be added to your exam points
  • You need to pass the exam first, to receive any bonus points!

. . .

The assignments are not mandatory, but highly recommended!

Five Tutorials for this Week

Topics of the Tutorials

  • Variables: Learn how to assign values to variables
  • Vectors: Learn how to create and manipulate vectors
  • Comparisons: Learn how to compare values
  • Loops: Learn how to use loops to repeat code
  • Dictionaries: Learn how to store and look up key-value pairs

. . .

Dictionaries will become important later: they are how we will store and look up the data of our optimization models.

Get Started with the Tutorials

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

. . .

NoteAnd that’s it for this lecture!

The remaining time we will already start working on the first problems.

Literature

Literature

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

Footnotes

  1. Strictly speaking, this executes the current line or cell. For longer files, use the command “Julia: Execute active File in REPL”.↩︎