# Rolling Horizon Implementation

**URL:** <https://discourse.julialang.org/t/rolling-horizon-implementation/23048>\
**Category:** Optimization (Mathematical)\
**Created:** [April 11, 2019, 6:52pm UTC](https://discourse.julialang.org/t/rolling-horizon-implementation/23048 "2019-04-11T18:52:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seanny123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/seanny123/32/5271_2.png) [@Seanny123](https://discourse.julialang.org/u/Seanny123)\
**Post date:** [April 11, 2019, 6:52pm UTC](https://discourse.julialang.org/t/rolling-horizon-implementation/23048/1 "2019-04-11T18:52:51Z")

</div>

I have an optimization problem with a long time period (96 steps). Reading some papers [1][2], it seems a common way to reduce the optimization time of a Mixed Integer Linear Programming problem is to use something called [Rolling Horizon](https://aimms.com/english/developers/resources/examples/functional-examples/rolling-horizon/), which is synonymous to [Receding Horizon Control and Model Predictive Control](https://math.stackexchange.com/a/3059910/59407).

I’m new to mathematical optimization and JuliaOpt, but I was wondering if there were any examples using this approach to solve an optimization problem?

[1] “[Reducing Computation Time with a Rolling Horizon Approach Applied to a MILP Formulation of Multiple Urban Energy Hub System](https://www.semanticscholar.org/paper/Reducing-Computation-Time-with-a-Rolling-Horizon-to-Marquant-Evins/34974f0cc2030419ed59502aeaa876bc0e20bdb4#citing-papers)” by Marquant et al.  
[2] “[Optimization of a network of compressors in parallel: Operational and maintenance planning – The air separation plant case](https://www.semanticscholar.org/paper/Optimization-of-a-network-of-compressors-in-%3A-and-%E2%80%93-Kopanos-Xenos/f3957a1cbb69198c08dff44d2e51eb652c2fd2d4)” by Kopanos et al.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [April 11, 2019, 7:51pm UTC](https://discourse.julialang.org/t/rolling-horizon-implementation/23048/2 "2019-04-11T19:51:00Z")

</div>

Typically this is just a roll-your-own type model. You would typically do something like

```julia
function solve_stage_t(incoming_state)
    model = Model()
    # ... definition using `incoming_state`
    optimize!(model)
    return outgoing_state
end

state = [initial_state]
for t in 1:T
    push!(state, solve_stage_t(state[end]))
end 

```

If rebuilding the model every step is a bottleneck (don’t assume this! Try solving the problem first and then time it.), you might want to look at [GitHub - tkoolen/Parametron.jl: Efficiently solving instances of a parameterized family of (possibly mixed-integer) linear/quadratic optimization problems in Julia](https://github.com/tkoolen/Parametron.jl) or [https://github.com/JuliaStochOpt/ParameterJuMP.jl/](https://github.com/JuliaStochOpt/ParameterJuMP.jl/).

---

<div class="post-metadata">

**Author:** ![daro.slai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daro.slai/32/48757_2.png) [@daro.slai](https://discourse.julialang.org/u/daro.slai)\
**Post date:** [July 20, 2023, 2:01pm UTC](https://discourse.julialang.org/t/rolling-horizon-implementation/23048/3 "2023-07-20T14:01:10Z")

</div>

Hi! What was the final implementation for this?  
Cause I’m having a similar issue depending on the solver I use.  
Here’s the draft of my MWE:

```julia
include(makeModels.jl) # <-- I create the spvModel
function rollingHorizon(EMSdata,
    W, # weights
    Tw, # time window [days]
    steps, # number of steps to move the window
    Δt) # time length of each step [hr]
    # Initialize
    tend = 24*Tw; # [hr]
    Dt=0:Δt:tend; Dt=Dt*3600; # time array in seconds
    s=modelSettings(nEV=1:2, dTime=collect(Dt));
    results=Vector{Dict}(undef, steps); # allocate memory
    for ts in 1:steps
        # build+solve model
        model=solvePolicies(KNITRO.Optimizer, s, data); 
        results[ts]=getResults(model);
        # update data
        data=update_data(results[ts], s, data);
        # move time window
        Dt = Dt .+ Δt*3600.0;
        s.dTime=collect(Dt);
    end
    return results
end

```

Doing this with `Ipopt` worked and with `KNITRO` didn’t. Even if I do `model=InfiniteModel();` in the middle.
