Pyomo / PyomoGallery

A collection of Pyomo examples
Other
284 stars 158 forks source link

unable to write complex constraint in pyomo #21

Closed WaqasSwati1 closed 2 years ago

WaqasSwati1 commented 2 years ago

I am trying to write the following constraint in pyomo:

Screenshot 2022-06-28 at 16 10 08

The description is as follows:

ū ∈ Nf=Nodes from a set Gf

u ∈ Ns=Nodes from a set Gs

Φf=binary variable indicating 1 if the node ū from Nf can be deployed on u from Ns, otherwise 0.

Df cpu ū = demand (CPU parameter) of node ū.

a cpu u = available ratio of node u (ratio = capacity-demand).

C cpu u = capacity (CPU parameter) of node u.

model = ConcreteModel() 
model.nf = (['VNF1', 'VNF2', 'VNF3'])
model.ns = (['S1', 'S2', 'S3'])

model.fi = Var(model.nf, model.ns, within=Binary)

model.Dcpu = Param(model.nf, within=NonNegativeReals, initialize=1)

model.Ccpu = Param(model.ns, within=NonNegativeReals, initialize=5)

def acpu_rule(model, ns, nf): 
      return (model.Ccpu[ns]/model.Dcpu[nf])
model.acp=Param(model.ns, model.nf, initialize=acpu_rule)

model.c1 = Constraint(sum(model.fi[ub, u] for ub in model.nf for u in model.ns * model.Dcpu[ub] for ub in model.nf) <= model.Ccpu[u] for u in model.ns) * model.acp[ub] for ub in model.nf)

For a long, I am receiving different errors and I am unable to locate the problem.

Currently, I am getting the 'TypeError: unhashable type: 'InequalityExpression' ' error.

Please help me to get out of this.

Regards

jsiirola commented 2 years ago

You are passing a generator that returns expressions to the Constraint() definition. Pyomo interprets generators as indexing sets. The correct way to declare an indexed constraint is with a rule:

def c1_rule(m, ub):
    return sum(m.fi[ub, u] for ub in m.nf for u in m.ns * m.Dcpu[ub] for ub in m.nf) <= m.Ccpu[u] for u in m.ns) * m.acp[ub]
model.c1 = Constraint(model.nf, rule=c1_rule)

or more concisely:

@model.Constraint(model.nf)
def c1(m, ub):
    return sum(m.fi[ub, u] for ub in m.nf for u in m.ns * m.Dcpu[ub] for ub in m.nf) <= m.Ccpu[u] for u in m.ns) * m.acp[ub]

For future reference, GitHub issues are for reporting bugs in Pyomo and requesting new features. Please direct support questions to the Pyomo forum or StackOverflow (see Getting Help).