Save optimum variables

Dear all, I have the following optimization problem implemented:

using JuMP, CPLEX, Printf
n=50

function Problem(c)
    modelo = Model(with_optimizer(CPLEX.Optimizer))

    (n, n) = size(c)

    @variable(modelo, x[1:n,1:n], Bin)
    @objective(modelo, Min, sum(sum(c[i,j]*x[i,j] for j=1:n) for i=1:n) )
    @constraints modelo begin
        c1[i=1:n], sum(x[i,j] for j=1:n) == 10
        c2[j=1:n], sum(x[i,j] for i=1:n) == 10
    end

    status=optimize!(modelo)
    X=value.(x)
    return X
end

for i=1:10
     c=-2 .+ round.(7*rand(n,n))
     XStar = Problem(c)
end

There is a simple way to save the optimum variables “XStar” in 10 different archives (.jl or .dat) whose name depends of counter “i”?
Best regards.

Are you looking for binary (e.g., JLD2) or text representation (e.g., CSV)?

1 Like

For binary. I need to manipulate this solution in a future.

If “in future” you mean the same version of Julia, then:

using Serialization
X = rand(3, 3)
i = 1
open("my_file_$(i)", "w") do io
    serialize(io, X)
end

Y = open(deserialize, "my_file_$(i)", "r")

X == Y  # true

Otherwise you will need to look into JLD2 (GitHub - JuliaIO/JLD2.jl: HDF5-compatible file format in pure Julia) or similar.

1 Like