Convert VariableRef to String

We can use string(variable) to convert integer variable to string. Can we convert VariableRef to string? VariableRef we are getting from math model.

I tried with parse(AbstractString,VariableRef) or string(VariableRef) but it is not working. Please check the code mentioned below:

using JuMP
using GLPK
using MathOptInterface

const MOI = MathOptInterface
mathoptformat_model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS)
FILE_NAME=“E:\file1.mps”
MOI.read_from_file(mathoptformat_model, FILE_NAME)
m = Model(GLPK.Optimizer)
MOI.copy_to(m, mathoptformat_model)
optimize!(m)
println(“Optimal Solution:”,objective_value(m))

for var in all_variables(m)
var_name = parse(AbstractString, var) # or string(var)
if var_name[1] == “C”
println(var_name)
end
end

Please suggest.

1 Like

https://jump.dev/JuMP.jl/stable/manual/variables/#Variable-names

Use name(var).

You can also simplify a lot of this.

using JuMP, GLPK
model = read_from_file("E:\file1.mps")
set_optimizer(model, GLPK.Optimizer)
optimize!(model)
println(“Optimal Solution:”, objective_value(model))
for x in all_variables(model)
    println("$(name(x)) = $(value(x))")
end

Thank you very much.

1 Like