Lecture 3 : Optimization 1

Some Julia Package Stuff

Let's create a julia package!

]
(@v1.8) pkg> add PkgTemplates

julia> using PkgTemplates

help?> Template

julia> t = Template(user = "floswald", interactive = true)
Template keywords to customize:
[press: d=done, a=all, n=none]
   [ ] authors
   [ ] dir
   [ ] host
   [ ] julia
 > [ ] plugins

julia> t("MyNewPackage")
[ Info: Running prehooks
[ Info: Running hooks
  Activating project at `~/.julia/dev/MyNewPackage`
    Updating registry at `~/.julia/registries/General`
    Updating git-repo `https://github.com/JuliaRegistries/General.git`
┌ Warning: The active manifest file at `/Users/74097/.julia/dev/MyNewPackage/Manifest.toml` has an old format that is being maintained.
│ To update to the new format, which is supported by Julia versions ≥ 1.6.2, run `Pkg.upgrade_manifest()` which will upgrade the format without re-resolving.
│ To then record the julia version re-resolve with `Pkg.resolve()` and if there are resolve conflicts consider `Pkg.update()`.
└ @ Pkg.Types /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-5.0/build/default-macmini-x64-5-0/julialang/julia-release-1-dot-8/usr/share/julia/stdlib/v1.8/Pkg/src/manifest.jl:287
  No Changes to `~/.julia/dev/MyNewPackage/Project.toml`
  No Changes to `~/.julia/dev/MyNewPackage/Manifest.toml`
Precompiling project...
  1 dependency successfully precompiled in 1 seconds
[ Info: We haven't cleaned this depot up for a bit, running Pkg.gc()...
      Active manifest files: 113 found
      Active artifact files: 303 found
      Active scratchspaces: 48 found
     Deleted 40 package installations (186.329 MiB)
  Activating project at `~/.julia/environments/v1.8`
[ Info: Running posthooks
[ Info: New package is at /Users/74097/.julia/dev/MyNewPackage
module MyNewPackage

mutable struct MPoint
    x::Number
    y::Number
end

import Base.:+

+(a::MPoint,b::MPoint) = MPoint(a.x + b.x, a.y + b.y)

end   # module
julia> include("src/MyNewPackage.jl")
Main.MyNewPackage
julia> include("src/MyNewPackage.jl")
WARNING: replacing module MyNewPackage.
@testset "MyNewPackage.jl" begin
    a = MyNewPackage.MPoint(3,5)
    b = MyNewPackage.MPoint(1,2)
    @test a + b isa MyNewPackage.MPoint
    p = a + b
    @test p.x == a.x + b.x
    @test p.y == a.y + b.y
end
-(a::MPoint,b::MPoint) = MPoint(a.x - b.x, a.y - b.y)
julia> a = MyNewPackage.MPoint(3,4)
MyNewPackage.MPoint(3, 4)

julia> b = MyNewPackage.MPoint(99,100)
MyNewPackage.MPoint(99, 100)

julia> a - b
MyNewPackage.MPoint(-96, -96)

Debugging A Package

function econ_model(; startval = 1.0)
    # make an Mpoint
    x = MPoint(startval, startval-0.5)
    # ... and evaluate a utility function
    MPoint(log(x.x),log(x.y))
end
julia> MyNewPackage.econ_model()
MyNewPackage.MPoint(0.0, -0.6931471805599453)
julia> MyNewPackage.econ_model(startval = 0.3)
  1. Add println statements.

  2. Add @debug statements. then attaching a logger with

using Logging
debug_logger = ConsoleLogger(stdout, Logging.Debug)
global_logger(debug_logger)  # turns on logging of @debug messages
  1. Use an actual debugger to step through our code.

    1. VSCode exports by default the @enter macro. type: @enter MyNewPackage.econ_model(startval = -0.3)

    2. click on teh play symbol. program hits an error.

    3. set a break point just before

    4. click on replay.

Some Julia-Bootcamp stuff

TopicNotebook
Intro to Macrosclick for notebook
Intro to Differential Equationsclick for notebook
Plottingclick for notebook
Interactiveclick for notebook

Optimization, Finally!

TopicNotebook
Review of Optimization Algorithmsright-click and save notebook