Using data from .sol file to fix variable values
回答済みHello,
Is it possible to fix the values for the variables to match the values saved in .sol .mst or .hnt file? This way, only the part of the values is subject to optimization.
Pseudo code would look something like this:
model.addConstrs(variable[a, b, c] == variable_from_sol_file[a, b, c] for a in a_list for b in b_list for c in c_list)
0
-
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 -
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
サインインしてコメントを残してください。
コメント
2件のコメント