# New to Jump and Julia

**URL:** <https://discourse.julialang.org/t/new-to-jump-and-julia/99823>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, optimization\
**Created:** [June 3, 2023, 9:27pm UTC](https://discourse.julialang.org/t/new-to-jump-and-julia/99823 "2023-06-03T21:27:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![A.Marx](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@A.Marx](https://discourse.julialang.org/u/A.Marx)\
**Post date:** [June 3, 2023, 9:27pm UTC](https://discourse.julialang.org/t/new-to-jump-and-julia/99823/1 "2023-06-03T21:27:13Z")

</div>

Hey all,  
I am trying to programm a simple optimization problem which minimizes the cost of a of an energy system with 4 suppliers subject to capacitiy constraints for 6 periods.

The costs are for each period are: costs = Dict(  
“A” =\> [1.0,1.0,1.0,1.0,1.0,1.0],  
“B” =\> [2.0,2.0,2.0,2.0,2.0,2.0],  
“C” =\> [1.0,1.0,1.0,1.0,1.0,1.0],  
“D” =\> [2.0,2.0,2.0,2.0,2.0,2.0],  
)

The capacity contraints for each period are:  
cap = Dict(  
“A” =\> [1.0,2.0,1.0,0.0,0.0,0.0],  
“B” =\> [2.0,1.0,2.0,0.0,0.0,0.0],  
“C” =\> [0.0,0.0,0.0,1.0,2.0,1.0],  
“D” =\> [0.0,0.0,0.0,2.0,1.0,2.0],

generator = keys(costs)  
# Variables  
@variable(dispatch\_model, 0 \<= y[generator],Int)

```
# Objective Function
@objective(dispatch_model,Min, 
sum(costs[g]*y[g] for g in generator)
)

```

# capacity constraint:

@constraint(dispatch\_model, cap[g in generator], (y[g] for g in generator) .\<=cap[g] )

I could write the capacity constraint for each generator, like:  
@constraint(dispatch\_model, y[“A”].\<= cap[“A”])  
@constraint(dispatch\_model, y[“B”].\<= cap[“B”])

but I would prefer to only specify one constraint by looping through the cap dictionary, how do I achieve that in Julia?

---

<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 4, 2023, 12:39am UTC](https://discourse.julialang.org/t/new-to-jump-and-julia/99823/2 "2023-06-04T00:39:34Z")

</div>

Here’s how I would write your model

```julia
costs = Dict(
    "A" => [1.0,1.0,1.0,1.0,1.0,1.0],
    "B" => [2.0,2.0,2.0,2.0,2.0,2.0],
    "C" => [1.0,1.0,1.0,1.0,1.0,1.0],
    "D" => [2.0,2.0,2.0,2.0,2.0,2.0],
)

cap = Dict(
    "A" => [1.0,2.0,1.0,0.0,0.0,0.0],
    "B" => [2.0,1.0,2.0,0.0,0.0,0.0],
    "C" => [0.0,0.0,0.0,1.0,2.0,1.0],
    "D" => [0.0,0.0,0.0,2.0,1.0,2.0],
)

model = Model()
@variable(model, 0 <= y[g in keys(cap)] <= cap[g], Int)
@objective(model, Min, sum(c * y[g] for (c, g) in costs))

```

You can loop over constraints like

```plaintext
@constraint(model, [g in keys(cap)], y[g] <= cap[g])

```

but you can also just use a for loop

```julia
for (key, value) in cap
    @constraint(model, y[key] <= value)
end

```

If you’re new to JuMP, here’s a good place to get started:

- [Introduction · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/introduction/)

If you’re interested in power systems, you might want to read:

- [Power Systems · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/applications/power_systems/)

---

<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 4, 2023, 12:41am UTC](https://discourse.julialang.org/t/new-to-jump-and-julia/99823/3 "2023-06-04T00:41:14Z")

</div>

p.s., I moved your question to the “Optimization (Mathematical)” section, which is where we try to keep JuMP-related questions.

You might want to read [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) which has some suggestions. It also explains how you can format your code in a Discourse post.

---

<div class="post-metadata">

**Author:** ![JackaChou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackachou/32/35269_2.png) [@JackaChou](https://discourse.julialang.org/u/JackaChou)\
**Post date:** [June 6, 2023, 2:48pm UTC](https://discourse.julialang.org/t/new-to-jump-and-julia/99823/4 "2023-06-06T14:48:33Z")

</div>

I run the above code, but some errors happened.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/d/adc5c87bcea4e092c0c79966ba79f2189710c813.png)

---

<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 6, 2023, 8:20pm UTC](https://discourse.julialang.org/t/new-to-jump-and-julia/99823/5 "2023-06-06T20:20:43Z")

</div>

Oops. I guess I should have tried to run the code. the issue is that `cap[g]` is a vector, so perhaps the original author meant to include a time dimension. This works:

```julia
costs = Dict(
    "A" => [1.0,1.0,1.0,1.0,1.0,1.0],
    "B" => [2.0,2.0,2.0,2.0,2.0,2.0],
    "C" => [1.0,1.0,1.0,1.0,1.0,1.0],
    "D" => [2.0,2.0,2.0,2.0,2.0,2.0],
)

cap = Dict(
    "A" => [1.0,2.0,1.0,0.0,0.0,0.0],
    "B" => [2.0,1.0,2.0,0.0,0.0,0.0],
    "C" => [0.0,0.0,0.0,1.0,2.0,1.0],
    "D" => [0.0,0.0,0.0,2.0,1.0,2.0],
)

model = Model()
@variable(model, 0 <= y[g in keys(cap), t=1:6] <= cap[g][t], Int)
@objective(model, Min, sum(sum(c[t] * y[g, t] for t in 1:6) for (g, c) in costs))

```

---

<div class="post-metadata">

**Author:** ![JackaChou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackachou/32/35269_2.png) [@JackaChou](https://discourse.julialang.org/u/JackaChou)\
**Post date:** [June 7, 2023, 12:01am UTC](https://discourse.julialang.org/t/new-to-jump-and-julia/99823/6 "2023-06-07T00:01:30Z")

</div>

Thx, odow, the code you provided worked. I understand the right syntax is the bounds cannot be a vector but an element.
