# BoundsError when re-solving a model

**URL:** https://discourse.julialang.org/t/boundserror-when-re-solving-a-model/12050
**Category:** Optimization (Mathematical)
**Tags:** question
**Created:** [June 28, 2018, 11:37pm UTC](https://discourse.julialang.org/t/boundserror-when-re-solving-a-model/12050 "2018-06-28T23:37:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![varun7rs](https://avatars.discourse-cdn.com/v4/letter/v/c2a13f/32.png) [@varun7rs](https://discourse.julialang.org/u/varun7rs)
#### Post date: [June 28, 2018, 11:37pm UTC](https://discourse.julialang.org/t/boundserror-when-re-solving-a-model/12050/1 "2018-06-28T23:37:12Z")

</div>

I’ve implemented a neighbourhood search technique wherein I deepcopy the original model, hot-start it and then solve it repeatedly. While the first iteration went smooth, during the second iteration however, I get this error while solving the modified MIP

```julia
ERROR: LoadError: BoundsError: attempt to access 15469-element Array{Float64,1} at index [15470]
Stacktrace:
 [1] set_constrLB!(::CPLEX.Model, ::Array{Float64,1}) at /home/user/.julia/v0.6/CPLEX/src/cpx_constrs.jl:243
 [2] setconstrLB!(::CPLEX.CplexMathProgModel, ::Array{Float64,1}) at /home/user/.julia/v0.6/CPLEX/src/CplexSolverInterface.jl:123
 [3] #build#119(::Bool, ::Bool, ::JuMP.ProblemTraits, ::Function, ::JuMP.Model) at /home/user/.julia/v0.6/JuMP/src/solvers.jl:337
 [4] (::JuMP.#kw##build)(::Array{Any,1}, ::JuMP.#build, ::JuMP.Model) at ./<missing>:0
 [5] #solve#116(::Bool, ::Bool, ::Bool, ::Array{Any,1}, ::Function, ::JuMP.Model) at /home/user/.julia/v0.6/JuMP/src/solvers.jl:168

```

I’m not sure where the error is. Besides, the error is thrown after the MIP is passed to the solver.

---

<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: [June 29, 2018, 1:02pm UTC](https://discourse.julialang.org/t/boundserror-when-re-solving-a-model/12050/2 "2018-06-29T13:02:37Z")

</div>

Hi @varun7rs, can you provide a [minimum working example](https://stackoverflow.com/help/mcve) that triggers this bug? It’s a little hard to find out what is going wrong otherwise.

---

<div class="post-metadata">

### Author: ![varun7rs](https://avatars.discourse-cdn.com/v4/letter/v/c2a13f/32.png) [@varun7rs](https://discourse.julialang.org/u/varun7rs)
#### Post date: [June 29, 2018, 5:04pm UTC](https://discourse.julialang.org/t/boundserror-when-re-solving-a-model/12050/3 "2018-06-29T17:04:58Z")

</div>

Hi @odow, thanks for your reply. The original problem was too complicated to post. This is a condensed version of the problem I’m solving but I encounter a different error now. The stacktrace seems a little too similar to the previous error however.

Just to briefly explain what the heuristic does, we solve a relaxation of a MIP and then employ a rounding technique (the one in the MWE is a simplified version. In my problem I use the ACO heuristic for rounding). To improve the rounded solutions, we use a neighbourhood search technique.

```julia
using JuMP
using CPLEX

profit = [5, 3, 2, 7, 4]
weight = [2, 8, 4, 2, 5]
capacity = 10

const ϵ = 0.1
K = length(profit)

type MyProblem
  model
  x
  z
end

function createproblem(profit, weight, capacity)

  K = length(profit)

  m = Model(solver=CplexSolver())
  @variable(m, x[1:K], Bin)
  @variable(m, z ≥ 0.0)
  @objective(m, Max, dot(profit, x))
  @constraint(m, dot(weight, x) <= capacity)
  return MyProblem(m, x, z)
end

# A simple rounding heuristic

p = createproblem(profit, weight, capacity)
status = solve(p.model, relaxation = true)
xR, zR = getvalue(p.x), getvalue(p.z)
obj = getobjectivevalue(p.model)

x = zeros(Float64,K) # Fixed variables

for k = 1:K
  if xR[k] ≥ 1-ϵ
    x[k] = 1.0
  end
end

# A Neighbourhood search heuristic

start = time(); TILIM = 300.0;
θ = 0.005 * obj; M = 1e+15;

i = 0
while (time()-start) < TILIM

  i += 1
  temp = deepcopy(p)
  # Hot-start
  for k = 1:K
    setvalue(temp.x[k], x[k])
  end

  # Cut-off
  @constraint(temp.model, dot(profit, temp.x) ≥ obj + θ - temp.z)
  @objective(temp.model, Min, sum((x[k] == 0.0) ? temp.x[k] : 0.0 for k = 1:K)
                            + sum((x[k] == 1.0) ? (1-temp.x[k]) : 0.0 for k = 1:K)
                            + M * temp.z)

  println("Iteration $i")
  solve(temp.model)

  if getvalue(temp.z) == 0.0
    x = getvalue(temp.x); z = getvalue(temp.z)
    obj = dot(profit, x)
  else
    θ = θ / 2.0
  end
end

```

---

<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: [June 29, 2018, 5:14pm UTC](https://discourse.julialang.org/t/boundserror-when-re-solving-a-model/12050/4 "2018-06-29T17:14:26Z")

</div>

`deepcopy` is not intended to be used on JuMP models. It can lead to unexpected behaviour as you observe.

Why do you need the copy instead of just using the model `p` everywhere?

---

<div class="post-metadata">

### Author: ![varun7rs](https://avatars.discourse-cdn.com/v4/letter/v/c2a13f/32.png) [@varun7rs](https://discourse.julialang.org/u/varun7rs)
#### Post date: [June 29, 2018, 5:55pm UTC](https://discourse.julialang.org/t/boundserror-when-re-solving-a-model/12050/5 "2018-06-29T17:55:06Z")

</div>

Since I modify model `p` at every iteration(I add a cut-off constraint which is modified over the course of the heuristic), I wanted to have a different copy of the model. And since I had trouble doing [this](https://discourse.julialang.org/t/solving-a-model-and-its-modified-copy/9778), I used `deepcopy`. Is there another way around this problem?

For now, I’ve settled with re-building the problem at each iteration by calling `createproblem` but I’m worried if model building would consume a lot of time for larger problem instances

---

<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: [June 30, 2018, 4:55am UTC](https://discourse.julialang.org/t/boundserror-when-re-solving-a-model/12050/6 "2018-06-30T04:55:44Z")

</div>

Instead of the deepcopy, you should just rebuild the model using `createproblem`.

The upcoming release of JuMP will support constraint deletion which should help this situation a lot. Then you can just delete the cut and add a new one in each iteration to the same model.

Some solvers will also support constraint coefficient modification, so you don’t even have to delete and then re-add, you can just modify the coefficients of the constraint in-place.

---

<div class="post-metadata">

### Author: ![varun7rs](https://avatars.discourse-cdn.com/v4/letter/v/c2a13f/32.png) [@varun7rs](https://discourse.julialang.org/u/varun7rs)
#### Post date: [June 30, 2018, 5:51pm UTC](https://discourse.julialang.org/t/boundserror-when-re-solving-a-model/12050/7 "2018-06-30T17:51:12Z")

</div>

Thanks a lot for the help.
