How to linearize a weighted average using Pyomo?

114 Views Asked by At

I'm learning about linear optimization problems using Pyomo. At this time I'm looking to practice and ended up getting stuck in a constraint that contains the weighted average formula. Using this weighted average the solver shows an error that it cannot solve the problem because of the non-linear terms. Below I show how I defined my constraint, which is basically an average that needs to be between a fixed interval.

So, is it possible to rewrite this constraint in a linear format or do I need to use a non-linear solver for this problem? As I'm new to this, please, show me an example of what I should create of variables or constraints. I found this answer here but I do not know if it can solve my problem and how to implement it.

Thanks in advance for your help.

Constraint description
The RSP (quality measure) of each Product Unity (UP) is weighted by the volumes transported daily must be within the limits stipulated by the factory.

Symbols:
f: factory
u: product unity
t: carrier company
d: day

Sets:
days: days of planning (from 1 to 31)
route: a carrier t, product unity u, factory f and day d

Parameters:
up_rsp: vector with float a positive value for each product unity u

Variables:
delivered_volume: Binary

# Constraint definition
def rsp_constraint(model):
  numerator = sum(model.delivered_volume[f,u,t,d]*model.up_rsp[u] for (f,u,t,d) in model.route)
  denominator = sum(model.delivered_volume[f,u,t,d] for (f,u,t,d) in model.route)
  rsp_medio_ponderado = numerator/denominator
  return (factory['rsp_min'], rsp_medio_ponderado, factory['rsp_max'])
model.rsp_constraint = pe.Constraint(model.days, rule = rsp_constraint)

Erro
enter image description here

1

There are 1 best solutions below

4
On

Given constants $\ell, u, a_j, b_j$, a constraint of the form $$\ell \le \frac{\sum_j a_j x_j}{\sum_j b_j x_j} \le u,$$ where the denominator is positive, can be linearized by clearing the denominator: $$\ell \sum_j b_j x_j \le \sum_j a_j x_j \le u \sum_j b_j x_j$$