# Creating Variables from a Vector of Tuples and Naming Them

**URL:** <https://discourse.julialang.org/t/creating-variables-from-a-vector-of-tuples-and-naming-them/103525>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [September 5, 2023, 2:39am UTC](https://discourse.julialang.org/t/creating-variables-from-a-vector-of-tuples-and-naming-them/103525 "2023-09-05T02:39:54Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [September 5, 2023, 3:14am UTC](https://discourse.julialang.org/t/creating-variables-from-a-vector-of-tuples-and-naming-them/103525/4 "2023-09-05T03:14:10Z")

</div>

I guess one option is to start with the nested for loops. If it’s easy to write and understand, that’s all you need. It’ll be faster than PuLP. But you might still run into scaling issues. (Although perhaps your fine with the runtime.)

If it becomes a problem, here’s another option:

```julia
using JuMP
import DataFrames
df = DataFrames.DataFrame(
    origin_node = ["A", "A", "B", "C"],
    destination_node = ["C", "C", "C", "A"],
    coal_group = ["x", "y", "y", "y"],
)
model = Model()
df.x = @variable(model, x[1:size(df, 1)] >= 0, base_name = "mass_flow")
locations = union(df.origin_node, df.destination_node)
for (index, gdf) in pairs(DataFrames.groupby(df, :coal_group))
    @constraint(
        model, 
        [l in locations],
        sum(r.x for r in eachrow(gdf) if r.destination_node == l) ==
        sum(r.x for r in eachrow(gdf) if r.origin_node == l),
    )
end

```

---

_[View the full topic](https://discourse.julialang.org/t/creating-variables-from-a-vector-of-tuples-and-naming-them/103525)._
