我正在尝试创建一个继承另一个实例约束的实例,同时添加一些新约束。
有没有办法将前一个实例的约束“复制”到新的约束中?
这是代码中的一个片段
...
model.bnb_bound_constraints = ConstraintList()
...
non_solved_instances = []
bnb_end = False
incumbent_value = 0
solve_counter = 0
integer_variables_list = []
bnb_instance = model.create_instance(data)
variables_list = [bnb_instance.x]
non_solved_instances.append(bnb_instance)
while bnb_end != True:
solve_counter += 1
print len(non_solved_instances), "Non solved instance(s)"
bnb_solve = optsolver.solve(non_solved_instances[0], tee = False)
print "Objective value for instance", solve_counter,":",value(non_solved_instances[0].obj)
if value(non_solved_instances[0].obj.sense)*value(non_solved_instances[0].obj) <= value(non_solved_instances[0].obj.sense)*incumbent_value:
incumbent_value = 2000
for new_instance in range(2):
bnb_instance = model.create_instance(data)
variables_list = [bnb_instance.x]
for con in non_solved_instances[0].bnb_bound_constraints:
bnb_instance.bnb_bound_constraints.add(non_solved_instances[0].bnb_bound_constraints[con].expr)
#This is what ive been trying to do to copy the constraints, doesnt work though.
if new_instance == 0:
bnb_instance.bnb_bound_constraints.add(expr = bnb_instance.x[solve_counter] <= floor(value(non_solved_instances[0].x[solve_counter])))
#adds a new constraint to bnb_instance
elif new_instance == 1:
bnb_instance.bnb_bound_constraints.add(expr = bnb_instance.x[solve_counter] >= ceil(value(non_solved_instances[0].x[solve_counter])))
#adds a new constraint to bnb_instance
non_solved_instances.append(bnb_instance)
del non_solved_instances[0]
if len(non_solved_instances) == 0 or solve_counter >= 20:
bnb_end = True
non_solved_instances[0]包含“原始”实例,我希望将其约束继承给bnb_instance
干杯!