model = pmo.ConcreteModel()
model.A = pmo.Set(initialize = range(7), ordered = True)
model.B = pmo.Set(initialize = range(3), ordered = True)
model.x = pmo.Var(model.A, model.B, domain=pmo.Binary, initialize = 0)
model.c_sos = pmo.SOSConstraint(var = model.x, sos = 1)(0, 0) : 1.0
(0, 1) : -0.0
(0, 2) : -0.0
(1, 0) : -0.0
(1, 1) : -0.0
(1, 2) : -0.0(0, 0) : 1.0
(0, 1) : -0.0
(0, 2) : -0.0
(1, 0) : 1.0
(1, 1) : -0.0
(1, 2) : -0.0--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
def add_sos_constraint_to_array_var(var, sos, weights=None, constraint_index='first', cl_name='con_sos'):
from itertools import product
model = var.parent_block()
first, second = var._index.subsets() # First and second index of the variable
weight_dict = None
if constraint_index
== 'first':
for i, idx in enumerate(first):
set_idx = set(product((idx,), set(second.keys())))
if weights:
weight_dict = {ii: weights[ii[1]] for ii in set_idx}
exec(f'model.{cl_name}_{i} = pe.SOSConstraint(index=set_idx, var=var, sos=sos,
weights=weight_dict)')
elif constraint_index == 'second':
for i, idx in enumerate(second):
set_idx = set(product(set(first.keys()), (idx,)))
if weights:
weight_dict = {ii: weights[ii[0]] for ii in set_idx}
exec(f'model.{cl_name}_{i} = pe.SOSConstraint(index=set_idx, var=var, sos=sos,
weights=weight_dict)')
else:
raise ValueError('Invalid constrain index. Select first or second.')