srgam
srgam

Reputation: 366

How to automatize the variables definition in PuLP

I´m trying to automatize the model definition in PuLP. Right now, I have the following model:

import pulp as pl

" Cost parameters"
p1 = 200  # Cost per unit 1
p2 = 300  # Cost per unit 2

" VARIABLES"
k0101 = pl.LpVariable("k0101", 0, 1, pl.LpInteger) 
k0102 = pl.LpVariable("k0102", 0, 1, pl.LpInteger) 

k0201 = pl.LpVariable("k0201", 0, 1, pl.LpInteger) 
k0202 = pl.LpVariable("k0202", 0, 1, pl.LpInteger) 

###### DEMAND
x010101 = pl.LpVariable("x010101", lowBound = 0) 
x010102 = pl.LpVariable("x010102", lowBound = 0) 
x010103 = pl.LpVariable("x010103", lowBound = 0) 
x010104 = pl.LpVariable("x010104", lowBound = 0) 

x010201 = pl.LpVariable("x010201", lowBound = 0)
x010202 = pl.LpVariable("x010202", lowBound = 0)
x010203 = pl.LpVariable("x010203", lowBound = 0)
x010204 = pl.LpVariable("x010204", lowBound = 0)

x020101 = pl.LpVariable("x020101", lowBound = 0) 
x020102 = pl.LpVariable("x020102", lowBound = 0) 
x020103 = pl.LpVariable("x020103", lowBound = 0) 
x020104 = pl.LpVariable("x020104", lowBound = 0)

x020201 = pl.LpVariable("x020201", lowBound = 0) 
x020202 = pl.LpVariable("x020202", lowBound = 0) 
x020203 = pl.LpVariable("x020203", lowBound = 0) 
x020204 = pl.LpVariable("x020204", lowBound = 0) 

# Problem
z = pl.LpProblem("optimizator", pl.LpMinimize)

"OBJECTIVE FUNCTION"
z += ((p1) * (x010101 + x010102 + x010103 + x010104) + (p1) * (x010201 + x010202 + x010203 + x010204) + (p2) * (x020101 + x020102 + x020103 + x020104) + (p2) * (x020201 + x020202 + x020203 + x020204) + (p1) * (x010101 + x010102 + x010103 + x010104) + (p1) * (x010201 + x010202 + x010203 + x010204) + (p2) * (x020101 + x020102 + x020103 + x020104) + (p2) * (x020201 + x020202 + x020203 + x020204))

" CONSTRAINTS "
z += x010101 + x020101 >= 15 * k0101

" SOLUTION "
print(z)
estado = z.solve()
print(pl.LpStatus[estado]) 

"TOTAL COST:"
print(pl.value(z.objective))

I would like to simplify this variable definitions, in order to be able to define more variable in an easier description.

Does anyone now how can I define my variables and parameters as a dictionary, and consider that in the objective function and the constraints?

Upvotes: 0

Views: 522

Answers (1)

Eric Truett
Eric Truett

Reputation: 3010

It would help to explain the problem more. The objective function as written right now has duplicate terms and it is hard to understand conceptually what you are trying to minimize.

That being said, you can use lpSum to express the sum of the variable * cost.

# create the variables
k_variable_names = ('k0101', 'k0102', 'k0201', 'k0202')
k_variables = {var: pl.LpVariable(var, cat=pl.LpBinary)
               for var in k_variable_names}

x_variables_names = ('x010101' ...)
x_variables = {var: pl.LpVariable(var, lowBound=0)
               for var in x_variable_names}

# objective function
z += (
  lpSum([var * 2 * p1 for var_name, var in x_variables.items() if 'x010' in var_name]) +
  lpSum([var * 2 * p2 for var_name, var in x_variables.items() if 'x020' in var_name])

)

Upvotes: 1

Related Questions