获取一个实例以继承另一个实例在pyomo中的约束
Get an instance to inherit another instance's constraints in pyomo
我正在尝试创建一个继承另一个实例约束的实例,同时添加一些新约束。
有没有办法 "copy" 以前实例的约束进入新实例?
这是代码片段
...
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] 包含 "original" 实例,我希望将其约束继承给 bnb_instance
干杯!
一种方法是克隆整个模型,然后向克隆中添加额外的约束
m = ConcreteModel()
# Add stuff to m
m2 = m.clone()
m2.c2 = Constraint(expr=m2.x**2<=10)
我正在尝试创建一个继承另一个实例约束的实例,同时添加一些新约束。
有没有办法 "copy" 以前实例的约束进入新实例?
这是代码片段
...
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] 包含 "original" 实例,我希望将其约束继承给 bnb_instance
干杯!
一种方法是克隆整个模型,然后向克隆中添加额外的约束
m = ConcreteModel()
# Add stuff to m
m2 = m.clone()
m2.c2 = Constraint(expr=m2.x**2<=10)