Search code examples
juliajulia-jump

Is it possible to convert a Tsp model in Julia to a *.lp file?


Is there any way for Julia to change the format of the following Tsp model to *.lp file?

using JuMP,CPLEX
Tsp=Model(solver=CplexSolver());
#Sets-------------------------------------------------------------------------
totalu=4;
U=1:4;
totalV=5;
V=1:totalV;
#Parameters-------------------------------------------------------------------
d=[100  10  8   9   7;10    100 10  5   6;8 10  100 8   9;9 5   8   100 6;7 6   9   6   100];
#variables---------------------------------------------------------------------
@variable(Tsp,x[V,V],Bin);

@variable(Tsp,u[V]>=0);
#constrains---------------------------------------------------------------------
@constraint(Tsp,c1[i in V ], sum(x[i,j] for j in V )==1);

@constraint(Tsp,c2[j in V], sum(x[i,j] for i in V )==1);

@constraint(Tsp,c3[i in U,j in V; i!=j],u[i]-u[j]+totalV*x[i,j]<=totalV-1);
# objective function------------------------------------------------------------

ff=sum(d[i,j]*x[i,j] for i in V,j in V);

@objective(Tsp, Min, ff);

solve(Tsp);

I tired this:

open("Tsp.lp", "w") do obj1
    println(obj1, Tsp)
end

It doesn't give any error but I can't see in the console the code as a *.lp file. Moreover, is it possible to save the model as a *.lp file?

I am thankful for your help.


Solution

  • Use write_to_file: https://jump.dev/JuMP.jl/stable/manual/models/#Write-a-model-to-file

    write_to_file(Tsp, "Tsp.lp")
    

    However, it looks like you're using a (very) old version of JuMP? Please update to JuMP 1.0 for this to work.

    The only differences after updating are:

    # Tsp=Model(solver=CplexSolver())
    Tsp = Model(CPLEX.Optimizer)
    
    
    # solve(Tsp)
    optimize!(Tsp)