Skip to main content

write solution which can be read back by rlp format

Answered

Comments

5 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    This can be done by writing the model to an rlp file it and then reading the rlp file before the model is optimized.

    import gurobipy as gp

    m = gp.Model()
    # construct model
    #...
    # write model to rlp file
    m.write("model.rlp")
    # read model from rlp file to overwrite variable and constraint names
    m = gp.read("model.rlp")
    # optimize model
    m.optimize()
    # write solution file
    # the solution file holds variable names from the rlp file and thus,
    # can be read in for the rlp file
    if m.SolCount > 0:
    m.write("solution_rlp.sol")

    # ...
    # read rlp file
    m = gp.read("model.rlp")
    # read solution for rlp file generated above
    m.read("solution_rlp.sol")

    Best regards, 
    Jaromił

    0
  • ce jekl
    Collaborator
    Investigator

    Thanks, but in my situation, I don't want to call optimize, which will cost a lot of time without any feasible solution. However, I have a mps and a sol to it, I want to convert the sol of mps into a sol of rlp. Is there a easy way to do that?

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    If you have an MPS file and a corresponding SOL file then this can be achieved with quite low effort

    import gurobipy as gp

    # read mps file and corresponding solution file
    m = gp.read("model.mps")
    m.read("solution.sol")
    m.update()
    # save the initial solution values
    start = []
    for v in m.getVars():
      start.append(v.Start)
    # write the model as rlp file to convert variable names
    m.write("model.rlp")
    # read the rlp model to use new variable names
    m = gp.read("model.rlp")
    # set initial point values
    vars = m.getVars()
    for i in range(len(vars)):
      vars[i].Start = start[i]
    # set SolutionLimit parameter to 1 to terminate
    # immediately after using MIP start values
    m.setParam("SolutionLimit",1)
    m.optimize()
    # write the MIP start values out with rlp variable names
    m.write("solution_rlp.sol")

    The key points in the above is that the variable ordering stays the same the reading an MPS file and the corresponding RLP file and that setting the parameter SolutionLimit to 1 avoids unnecessary optimization.

    Best regards, 
    Jaromił

    0
  • ce jekl
    Collaborator
    Investigator

    Thanks, that solves my problem.

    From your code, I finally realize that reading a sol file and a mst file has the same effect, which set the start attribute of each variable in the file, right?

    As the documentation says, lp format won't keep column order. Is that different for lp and rlp file?

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    From your code, I finally realize that reading a sol file and a mst file has the same effect, which set the start attribute of each variable in the file, right?

    This is correct with the subtle difference that an MST file holds integer information only. A SOL file holds also solution values of continuous variables.

    As the documentation says, lp format won't keep column order. Is that different for lp and rlp file?

    Good point. While usually the order should be prevailed when reading an LP file, it is definitely not guaranteed. This also holds for RLP files. In this case, I would recommend to change the above code to write and read a REW file, which is the RLP equivalent of MPS files.

    # ...
    # write the model as rew file to convert variable names
    m.write("model.rew")
    # read the rew model to use new variable names
    m = gp.read("model.rew")
    # ...

    Best regards, 
    Jaromił

    0

Please sign in to leave a comment.