# Print all ellements of a variable-vector in once (broadcasting)

Hi,
I’ve just started with Julia, and I would like to print each element of a vector in one time for a variable (see line 37) in my optimization model under JuMP. It is already working for a defined parameter (D, see line 31).
This would save me writing 3 lines of code (see line 33 to 35).
Does anyone have any ideas?
Fabian

1 using JuMP
2 using LinearAlgebra
3 using HiGHS

5 model = Model(HiGHS.Optimizer)

7 # Parameter
8 D = [10, 10, 10]
9 C_Hydro = [1, 10, 1]
10 C_gas = [10, 1, 10]
11 t = [1, 1, 1]

13 # decision variables

15 @ variable(model , q_out_hydro[i=1:3] >= 0 ) # vector with 3 elements, there is a space between variable and ‘@’ to avoid tagging people
16 @ variable(model , q_in_gas[i=1:3] >= 0) # vector with 3 elements

18 # constraint
19 e2 = 10 * q_in_gas
20 e1 = 2 * q_out_hydro
21 @ constraint(model, energy, D <= e1 .* t + e2 .* t)
22 Cope = dot(C_Hydro, (q_out_hydro .* t)) + dot(C_gas, (q_in_gas .* t))

24 # objective function

26 @ objective(model, Min, Cope)

28 optimize!(model)

30 # print results
31 print(“D:”, D) # output: D:[10, 10, 10]

33 print(“; q_out_hydro:”, value(q_out_hydro) ) # output: q_out_hydro:5.0
34 print(“; q_out_hydro:”, value(q_out_hydro) ) # output: q_out_hydro:0.0
35 print(“; q_out_hydro:”, value(q_out_hydro) ) # output: q_out_hydro:5.0

37 print(“; Q_hydro:”, q_out_hydro[1:3]) # output: Q_hydro:VariableRef[q_out_hydro, q_out_hydro, q_out_hydro]

39 # Output in the Terminal
40 # Running HiGHS 1.5.3 [date: 1970-01-01, git hash: 45a127b78]
41 # Copyright (c) 2023 HiGHS under MIT licence terms
42 # Presolving model
43 # 3 rows, 4 cols, 4 nonzeros
44 # 0 rows, 0 cols, 0 nonzeros
45 # Presolve : Reductions: rows 0(-3); columns 0(-6); elements 0(-6) - Reduced to empty
46 # Solving the original LP from the solution after postsolve
47 # Model status : Optimal
48 # Objective value : 1.1000000000e+01
49 # HiGHS run time : 0.02
50 # D:[10, 10, 10]; q_out_hydro:5.0; q_out_hydro:0.0; q_out_hydro:5.0; Q_hydro:VariableRef[q_out_hydro, q_out_hydro, q_out_hydro]

You can query the values of a vector variable using a dot. For instance,

``````q_out_hydro_vals = value.(q_out_hydro)
``````

You can also use the right-hand-side directly in the print function similar as in your line 31.

1 Like

Hi Fabian,

You probably want to use “broadcasting” which means putting `.` after the function to indicate you want to apply it to all elements:

``````value.(q_out_hydro)
``````

Hope that helps.

There is also a specific category here on Julia Discourse that the JuMP developers actively watch that is a good place to ask JuMP questions:

1 Like

Thank you very much! It was very helpful

Thank you very much! Finally it worked! Have a nice day!

Hi @Fabian1, I’ve moved this question to the optimization section. Feel free to open any other questions 