Creating Constraints Quickly

I have been using Mathematica with my optimisation problem and am now keen to try the optimisation tools in Julia (SDDP in particular). Am new Julia so plz bear with me.

In my optimisation problem, my constraints are actually measure of something in a hypothetical future time series - ie a few (given) assumptions as to what happens in the next 12mths, and my decision variables have to be chosen such that (1) generates least cost and (2) at each point in the next 12mths that dependent variable is above a target (lets say 10).

In mathematica, i was able to generate that time series relatively easy as I specify all the relationships “outside” and then bring each of them in in the optimisation code ie
obj = var * 50
timeseries = [w[1] = 100 + var, w[2] = w[1]+5+var/6, w[3]=w[2]-10+var/6…]
optsol = Nminimize[{obj, timeseries>10},var]

My questions on Julia:

  • Is it not possible to predefine all the relationships like i did above with obj and timeseries (as constraint) and add them to the optimizer model? It seems like with Julia I need to add each variable and relationship “inside” the model.
  • How do I generate each constraint above quickly. Specifically, my time series could have 100 data points. Do I need to code up @variable(mode;, var[n]) n times or is there a quick way to add them.

Thanks

Hi @cs1231, welcome to the forum :smile:

I’m glad that you’re coming to Julia and JuMP to use SDDP.jl. Feel free to ask any questions you have about it.

Re your other question. You could do something like:

julia> using JuMP, HiGHS

julia> function main()
           model = Model(HiGHS.Optimizer)
           @variable(model, x)
           @objective(model, Min, 50 * x)
           @variable(model, w[1:3] >= 10)
           @constraint(model, w[1] == 100 + x)
           @constraint(model, w[2] == w[1] + 5 + x / 6)
           @constraint(model, w[3] == w[2] - 10 + x / 6)
           optimize!(model)
           @assert is_solved_and_feasible(model)
           return value(x), value.(w)
       end
main (generic function with 1 method)

julia> x, w = main()
Running HiGHS 1.8.0 (git hash: fcfb53414): Copyright (c) 2024 HiGHS under MIT licence terms
Coefficient ranges:
  Matrix [2e-01, 1e+00]
  Cost   [5e+01, 5e+01]
  Bound  [1e+01, 1e+01]
  RHS    [5e+00, 1e+02]
Presolving model
0 rows, 0 cols, 0 nonzeros  0s
0 rows, 0 cols, 0 nonzeros  0s
Presolve : Reductions: rows 0(-3); columns 0(-4); elements 0(-8) - Reduced to empty
Solving the original LP from the solution after postsolve
Model   status      : Optimal
Objective value     : -3.1875000000e+03
HiGHS run time      :          0.00
(-63.75, [36.25, 30.625, 10.0])

julia> x
-63.75

julia> w
3-element Vector{Float64}:
 36.25
 30.625
 10.0

Note that it’s much easier if you can provide a minimal reproducible example of the Julia code you are trying to write. It’s hard to offer advice like “how do I generate each constraint quickly”. It depends on your problem.

For example, you could do:

julia> using JuMP, HiGHS

julia> function time_series(x)
           w = Vector{Any}(undef, 3)
           w[1] = 100 + x
           w[2] = w[1] + 5 + x / 6
           w[3] = w[2] - 10 + x / 6
           return w
       end
time_series (generic function with 1 method)

julia> function main()
           model = Model(HiGHS.Optimizer)
           @variable(model, x)
           @objective(model, Min, 50 * x)
           @variable(model, w[1:3] >= 10)
           @constraint(model, w .== time_series(x))
           optimize!(model)
           @assert is_solved_and_feasible(model)
           return value(x), value.(w)
       end
main (generic function with 1 method)

julia> x, w = main()
Running HiGHS 1.8.0 (git hash: fcfb53414): Copyright (c) 2024 HiGHS under MIT licence terms
Coefficient ranges:
  Matrix [1e+00, 1e+00]
  Cost   [5e+01, 5e+01]
  Bound  [1e+01, 1e+01]
  RHS    [1e+02, 1e+02]
Presolving model
0 rows, 0 cols, 0 nonzeros  0s
0 rows, 0 cols, 0 nonzeros  0s
Presolve : Reductions: rows 0(-3); columns 0(-4); elements 0(-6) - Reduced to empty
Solving the original LP from the solution after postsolve
Model   status      : Optimal
Objective value     : -3.1875000000e+03
HiGHS run time      :          0.00
(-63.74999999999999, [36.25000000000001, 30.625, 10.0])