メインコンテンツへスキップ

Using data from .sol file to fix variable values

回答済み

コメント

2件のコメント

  • Marika Karbstein
    • Gurobi Staff

    If you want to fix (some) variables to the values given in a sol file, you can fix the upper and the lower bound to the values. This is preferable to adding constraints.
    If the variable names of sol file and lp file are equivalent, a simple way to do this could be for example:

    import gurobipy as gp

    model = gp.read("model.mps")

    with open('solfile.mst') as f:
        for line in f.readlines():
            if not line.startswith('#'):
                split=line.split()
                model.getVarByName(split[0]).LB = float(split[1])
              model.getVarByName(split[0]).UB = float(split[1])

    Could you explain what you mean by

    This way, only the part of the values is subject to optimization.?

    0
  • Riley Clement
    • Gurobi Staff

    Here is another way I have been using that may be worth mentioning, for anyone that stumbles across this in the future.

    # define model m here
    # define iterable of fixed_vars (containing variables you want to fix)

    m.read('solfile.mst')
    m.params.SolutionLimit = 1
    m.optimize()  # loads the solution from solfile.mst into the variables

    for var in fixed_vars:
        var.lb = var.ub = var.X

    # reset model and SolutionLimit
    m.reset()
    m.resetParams()

    # optimize with fixed variables
    m.optimize()
    0

サインインしてコメントを残してください。