# How to define a subset of some variables integers?

**URL:** https://discourse.julialang.org/t/how-to-define-a-subset-of-some-variables-integers/77443
**Category:** Optimization (Mathematical)
**Tags:** question, jump, optimization
**Created:** [March 5, 2022, 2:26pm UTC](https://discourse.julialang.org/t/how-to-define-a-subset-of-some-variables-integers/77443 "2022-03-05T14:26:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [March 5, 2022, 2:26pm UTC](https://discourse.julialang.org/t/how-to-define-a-subset-of-some-variables-integers/77443/1 "2022-03-05T14:26:34Z")

</div>

I need to define some variables as integer based on input. The following code doesn’t work as the name `x` is already used, and at the same time I don’t know the syntax to add it as a constraint:

```julia
for a in 1:nActivities
    if activities.integer[a] == 1
        @variable(profitModel, 0 <= x[a], Int)
    else
        @variable(profitModel, 0 <= x[a])
    end
end

```

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [March 5, 2022, 4:57pm UTC](https://discourse.julialang.org/t/how-to-define-a-subset-of-some-variables-integers/77443/2 "2022-03-05T16:57:29Z")

</div>

The possibility of extension of a variable set already defined was considered and discarded [here](https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests). (But there is a workaround if you need one, you just need to create anonymous variables, call `set_name` over them, and `append!` them to the already existing `model[:var_name]` vector.)

However, if you can define all variables first, then you can just change part of them to integer after, see [here](https://jump.dev/JuMP.jl/dev/manual/variables/#Integer-variables).

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [March 5, 2022, 5:53pm UTC](https://discourse.julialang.org/t/how-to-define-a-subset-of-some-variables-integers/77443/3 "2022-03-05T17:53:12Z")

</div>

Indeed. I solved by using:

```julia
@variables profitModel begin
    x[1:nA] >= 0
end

for a in 1:nA
    if activities.integer[a] == 1
        set_integer(x[a])
    end
end

```

Thanks
