# How do I reset a variable value in a loop?

**URL:** <https://discourse.julialang.org/t/how-do-i-reset-a-variable-value-in-a-loop/49821>\
**Category:** Optimization (Mathematical)\
**Tags:** question, optimization\
**Created:** [November 9, 2020, 4:03am UTC](https://discourse.julialang.org/t/how-do-i-reset-a-variable-value-in-a-loop/49821 "2020-11-09T04:03:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlos](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@carlos](https://discourse.julialang.org/u/carlos)\
**Post date:** [November 9, 2020, 4:03am UTC](https://discourse.julialang.org/t/how-do-i-reset-a-variable-value-in-a-loop/49821/1 "2020-11-09T04:03:12Z")

</div>

Let’s say I have a model like this:

> using JuMP  
> using CPLEX
> 
> model = Model(CPLEX.Optimizer)
> 
> for k = 1:3
> 
> ```
> @variables(model, begin
> sigma
> x[1:3]>=0
> end)
> 
> @constraints(model,begin
> sigma == sum(x[i] for i=1:3) + k
> x[1]+x[2]<=100
> x[2]+x[3]<=50
> end)
> 
> @objective(model,Max,sigma)
> 
> optimize!(model)
> 
> ```
> 
> end

I get next error:

> An object of name sigma is already attached to this model. If this is intended, consider using the anonymous construction syntax, e.g., x = @variable(model, [1:N], …) where the name of the object does not appear inside the macro.

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [November 9, 2020, 9:08am UTC](https://discourse.julialang.org/t/how-do-i-reset-a-variable-value-in-a-loop/49821/2 "2020-11-09T09:08:14Z")

</div>

I am not sure what you really want to do, but I assume you like to solve the model three times for different values of parameter `k`. If so, you could simply put

```julia
model = Model(CPLEX.Optimizer)

```

inside the for-loop.

If you declare the model outside, clearly, in the second iteration of the for-loop the variables and constraints already exist in your model which causes the error message.

---

<div class="post-metadata">

**Author:** ![carlos](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@carlos](https://discourse.julialang.org/u/carlos)\
**Post date:** [November 9, 2020, 9:19am UTC](https://discourse.julialang.org/t/how-do-i-reset-a-variable-value-in-a-loop/49821/3 "2020-11-09T09:19:47Z")

</div>

Thank you.
