Search code examples
pythonpython-3.xpyomo

Constraint with both or neither in pyomo


I am trying to design an optimizer that chooses which products to sell based on some pre-defined parameters. The only restrictions would be the maximal amount of products to sell and some dependencies between products (If you sell product B, you have to sell product D f.e.). I am having problems defining the latter constraint.

What follows is a simplified version of the problem:

import numpy as np 
from pyomo import environ as pe    

## define articles
article_list=('A','B','C','D')

## and their values ("sales")
alphas=(0,1,2,3)
alphas_input=dict(zip(article_list,alphas))


## generate compatibility matrix, 1 means article pair is dependant
compatibilities=dict(
    ((article_a,article_b),0)
    for article_a in article_list
    for article_b in article_list
)

## manually assign compatibilities so that 
## every product is dependant on itself and D and B are dependant on each other
comp_arr=[1,0,0,0,0,1,0,1,0,0,1,0,0,1,0,1]
compatibilities=dict(zip(compatibilities.keys(),comp_arr))

## indices: articles
model_exp.article_list = pe.Set(
    initialize=article_list)

Defining model

## create model
model_exp = pe.ConcreteModel()

## parameters: fixed values
model_exp.alphas=pe.Param(
    model_exp.article_list,
    initialize=alphas_input,
    within=pe.Reals)

model_exp.compatibilities=pe.Param(
    model_exp.article_list*model_exp.article_list,
    initialize=compatibilities,
    within=pe.Binary
)


## variables: selected articles -> 0/1 values
model_exp.assignments=pe.Var(
    model_exp.article_list,
    domain=pe.Binary
)


## objective function
model_exp.objective=pe.Objective(
    expr=pe.summation(model_exp.alphas,model_exp.assignments),
    sense=pe.maximize
)

Defining constraints

def limit_number_articles(model):
    n_products_assigned=sum(
        model_exp.assignments[article]
        for article in model.article_list
        )
    return n_products_assigned<=2

model_exp.limit_number_articles=pe.Constraint(
    rule=limit_number_articles
)

Now to the problematic constraint. Without this constraint the optimizer would choose C and D as the two articles since they have the higher alphas. But since I have defined D and B as dependant on each other, I need the optimizer to either choose both of them or none of them (since they have higher alphas than A and C, the optimal solution would be to choose them).

This is the closest I've got to defining the constraint I need:

def control_compatibilities(model,article_A):
    sum_list=[]
    #loopo over article pairs
    for article_loop in model_exp.article_list:
        # check whether the article pair is dependant
        if model_exp.compatibilities[article_A,article_loop]==1:
            # sum the amount of articles among the pair that are included
            # if none are (0) or both are (2) return True
            sum_list.append(sum([model_exp.assignments[article_A]==1,
                model_exp.assignments[article_loop]==1]) in [0,2])
        else:
            #if they are not dependant, no restruction applies
            sum_list.append(True)

    sum_assignments=sum(sum_list)

    return sum_assignments==4


model_exp.control_compatibilities=pe.Constraint(
    model_exp.article_list,
    rule=control_compatibilities
)

The above contraint returns the following error:

Invalid constraint expression. The constraint expression resolved to a 
trivial Boolean (True) instead of a Pyomo object. Please modify your rule to 
return Constraint.Feasible instead of True.

Any ideas on to how to define the constraint would be very helpful.


Solution

  • I solved it substracting the selection of one item from the other (0-0=0 and 1-1=0) and iterating over all dependant product pairs.

    def control_compatibilities(model,article_A):
        compatible_pairs=[k for k,v in compatibilities.items() if v==1]
        compatible_pairs_filt=[a for a in compatible_pairs if a[0]==article_A]
        sum_assignments=sum(model_exp.assignments[a[0]]-model_exp.assignments[a[1]]
        for a in compatible_pairs_filt)
        return sum_assignments==0
    
    model_exp.control_compatibilities=pe.Constraint(
        model_exp.article_list,
        rule=control_compatibilities
    )