Skip to content

greg-neustroev/Julia-optimization-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Julia Optimization Tutorial

This repository contains code for a tutorial on mathematical optimization in Julia that I prepared for PhD students and employees of the algorithmics research group of TU Delft.

Before You Get Started

Install Julia and an optimizer such as Gurobi or HiGHS. The latter is open-source but unfortunately does not support some types of mathematical programming, for example, conic optimization.

In my examples, I also use VSCode as an IDE, but if you prefer you can use a different environment.

Julia for Visual Studio Code is a useful extension of VSCode if you work with Julia. In VSCode, press press Ctrl + Shift + X (or + + X if you are using MacOS) and search the extension by its name.

Julia REPL in VSCode

In VSCode, open/create a project, and press Ctrl + Shift + P (or + + P if you are using MacOS) to open the command palette and find Julia: Start REPL. This will open Julia REPL in the terminal panel (usually at the botom of the IDE).

You can also run REPL from terminal. Go to the directory of your project, and run julia:

cd <project-directory>
julia

Note: julia must be part of your environment variables to call it from the command line. Add julia to PATH if this does not work.

To quit REPL, use:

julia> exit()

When in Julia REPL, you can open Julia's package manager by pressing ]. The terminal will change to something like:

(@v1.9) pkg>

The expression in brackets shows the environment that is currently active. To quit the package mode, press (backspace).

Starting a Julia Environment

In the package mode, run the following commands:

pkg> activate .   # activates a new environment 
pkg> instantiate  # install missing packages and create the manifest

Little Tricks

Julia supports Unicode characters. For example, these are valid commands in Julia:

julia> π
π = 3.1415926535897...

julia> 2  [1, 2]
true

julia> sin(2π / 3) == 3 / 2
false

julia> sin(2π / 3)  3 / 2
true

In Julia REPL or in VSCode with Julia extension, you can use LaTeX commands for basic Unicode characters, such as \pi and \in to insert the Unicode characters. Note that you need to press Tab after you finished typing the command to convert it to the Unicode character.

Note: \euler produces ℯ which is a special character for Euler's constant:

julia> ℯ
ℯ = 2.7182818284590...

You can apply an operator or a function element-wise by adding a period to it, either in the beginning (for operators), or in the end (for functions):

julia> [1, 2] .+ [3, 4]
2-element Vector{Int64}:
 4
 6

julia> .√[4, 9]
2-element Vector{Float64}:
 2.0
 3.0

julia> abs.([-2, -3])
2-element Vector{Int64}:
 2
 3

Functions that end in an exclamation point change their arguments:

julia> x = [1, 5, 2, 4, 3]
5-element Vector{Int64}:
 1
 5
 2
 4
 3

julia> sort(x)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> x
5-element Vector{Int64}:
 1
 5
 2
 4
 3

julia> sort!(x)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> x
5-element Vector{Int64}:
 1
 2
 3
 4
 5

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published