# Sum of JuMP.value

**URL:** <https://discourse.julialang.org/t/sum-of-jump-value/88673>\
**Category:** Optimization (Mathematical)\
**Created:** [October 13, 2022, 3:37pm UTC](https://discourse.julialang.org/t/sum-of-jump-value/88673 "2022-10-13T15:37:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emma456](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@Emma456](https://discourse.julialang.org/u/Emma456)\
**Post date:** [October 13, 2022, 3:37pm UTC](https://discourse.julialang.org/t/sum-of-jump-value/88673/1 "2022-10-13T15:37:04Z")

</div>

Hello,

I would like to display in an excel document the sum of students assigned for each course:

```julia
if JuMP.value.(x[i,j])==1

```

for each j, display the sum of x[i,j]

Could someone help me?

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [October 13, 2022, 4:02pm UTC](https://discourse.julialang.org/t/sum-of-jump-value/88673/2 "2022-10-13T16:02:03Z")

</div>

```julia
students_in_each_course = zeros(J)
for j in 1:J
    for i in 1:I
        if value(x[i,j]) == 1
            students_in_each_course[j] += 1
        end
    end
end

println(students_in_each_course)

```

---

<div class="post-metadata">

**Author:** ![Emma456](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@Emma456](https://discourse.julialang.org/u/Emma456)\
**Post date:** [October 13, 2022, 5:18pm UTC](https://discourse.julialang.org/t/sum-of-jump-value/88673/3 "2022-10-13T17:18:53Z")

</div>

Thank you very much!

---

<div class="post-metadata">

**Author:** ![Emma456](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@Emma456](https://discourse.julialang.org/u/Emma456)\
**Post date:** [October 13, 2022, 5:23pm UTC](https://discourse.julialang.org/t/sum-of-jump-value/88673/4 "2022-10-13T17:23:14Z")

</div>

Do you have any idea how I could display my solution in the following form:  
from Julia in excel?

![Capture d’écran 2022-10-13 à 19.21.25](https://global.discourse-cdn.com/julialang/original/3X/4/6/46e9fa1bcd2fe16b9cb1a367b693a5bec87b87a0.png)

for the moment I have this:

```julia
XLSX.openxlsx("choix élèves julia.xlsx", mode="rw") do xf
    !XLSX.hassheet(xf, "ATTRIBUTION") && XLSX.addsheet!(xf, "ATTRIBUTION")
    sheet = xf["ATTRIBUTION"]
    sheet["A1", dim=2] = collect(1:3)

```

---

<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:** [October 13, 2022, 8:24pm UTC](https://discourse.julialang.org/t/sum-of-jump-value/88673/5 "2022-10-13T20:24:48Z")

</div>

> if value(x[i,j]) == 1

Never do this. Even if `x` is integer or binary, the result is not guaranteed to be exactly one. Do instead

```julia
if isapprox(value(x[i, j]), 1; atol = 1e-6) # <-- or some other tolerance

```

See this part of the docs: [Getting started with Julia · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_julia/#Floating-point-numbers)

However, since it seems you probably are using binary variables, you can also do:

```julia
students_in_each_course = [sum(value.(x[:, j])) for j in 1:J]

```

> Do you have any idea how I could display my solution in the following form

What, exactly, do you want to represent? The A column is the name of the course, the B column is the name of the students assigned to each course, and the last row of each course is the number of students?

---

<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:** [October 14, 2022, 2:14am UTC](https://discourse.julialang.org/t/sum-of-jump-value/88673/6 "2022-10-14T02:14:36Z")

</div>

I’d do something like this:

```julia
using HiGHS
using JuMP
using XLSX

function main(students, courses)
    model = Model(HiGHS.Optimizer)
    @variable(model, x[students, courses], Bin)
    @constraint(model, [i in students], sum(x[i, :]) == 1)
    @constraint(model, [j in courses], sum(x[:, j]) >= 1)
    optimize!(model)
    XLSX.openxlsx("output.xlsx", mode = "w") do io
        if !XLSX.hassheet(io, "ATTRIBUTION")
            XLSX.addsheet!(io, "ATTRIBUTION")
        end
        sheet = io["ATTRIBUTION"]
        row = 1
        for course in courses
            sheet["A$row"] = "Course $course"
            count = 0
            for student in students
                if isapprox(value(x[student, course]), 1; atol = 1e-6)
                    sheet["B$row"] = "Student $student"
                    row += 1
                    count += 1
                end
                sheet["B$row"] = "Row count = $count"
            end
            row += 2
        end
    end
    return
end

students, courses = 1:3, 1:2
main(students, courses)

```

which yields

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

It might not be exactly what you want, but hopefully it points you in the right direction.
