Python Pulp package: how to create constraints using the max function

Asked

Viewed 77 times

1

I’m writing linear programming using the python Pulp package. I’m having doubts about building a constraint.

To illustrate the problem, imagine a square measuring 100 x 100 cm. I have several circles with varying dimensions. I need to include these circles (not necessarily all of them) within the square in order to maximize the sum of the area of the diameters. When the first row is finished the second row will be inserted at the top, forming the second row, and so on, always considering the following restrictions:

  1. Each circle can be selected only 1 time and for only 1 row (not all circles need to be selected)
  2. The sum of circle diameters shall not exceed the length of the square.
  3. The sum of the maximum diameters of the circles in each row shall not exceed the height of the square
  4. The sum of the area of the circles must not exceed the area of the square.

My question is in restriction 3. My idea is to identify the maximum diameter selected for each row (may be the average as well). The sum of these maxima cannot exceed the value of 100 (which is the square height). Is it possible to create this type of constraint? If so, how do I write it. Below follows the code I already developed. It does not include the restriction 3.

Anyone who can help me thank me immensely.

'Variables'

    alocado = pulp.LpVariable.dicts("alocado",((n, t) for n, t in df.index),cat='Binary')

    model = pulp.LpProblem("Alocacao_na_pilha", pulp.LpMaximize)

'Objective Function'

    model += pulp.lpSum(alocado[n, t] * df.loc[(n, t), 'area'] for n, t in df.index),'z'

'Set'

    circle= df.index.get_level_values(0).unique()
    filled= df.index.get_level_values(1).unique()

'Restrictions'

    for n in circle:
        model += pulp.lpSum(alocado[(n, t)] for t in filled) <= 1

    for t in filled:
        model += pulp.lpSum(alocado[(n, t)] * df.loc[(n, t),'d1'] for n in circle) <= 100

    model += pulp.lpSum(pulp.lpSum(alocado[n, t] for t in filled) * df.loc[(n, t), 'area'] for n in         circle) <= 1000

'Solving the problem'

    model.solve(pulp.PULP_CBC_CMD(maxSeconds=1000, msg=1, fracGap=0))
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.