# Defining Objective Function in PuLP

I have a binary variable $$X_{ijk}$$ which I have defined as the following :

X_ijk = [LpVariable("X{0}{1}{2}".format(i+1, j+1, k+1), cat="Binary")
for i in range(N1) for j in range(N2) for k in range (N3)]


I am struggling to code the following objective function

$$\min \quad \left[\sum_{k=1}^{k=N_3} C^k \left(\sum_{i=1}^{i=N_1}\sum_{j=1}^{j=N_2}P_{ij}^k \cdot X_{ijk}\right)\right]$$

where we know $$C^k$$ and $$P_{ij}^k$$. I have defined them as follows :

C_k = numpy.random.uniform(0,100,size=(N3))
P_ijk = numpy.random.uniform(0,100,size=(N1,N2,N3))


Assuming the following has been defined :

prob = LpProblem('Optimization Problem', LpMinimize)


How should I go about modeling the objective function?

• Hi @Alphanerd and welcome to or.stackexchange. What have you tried so far that doesn't work?
– Sune
Mar 26, 2022 at 21:38

I think it would be more convenient storing all the binary decision variables $$X$$ in a python dict such that x[i,j,k] directly corresponds to $$X_{i,j,k}$$. Then, because

$$\sum_{k=1}^{k=N_3} C^k \left(\sum_{i=1}^{i=N_1}\sum_{j=1}^{j=N_2}P_{ij}^k \cdot X_{ijk}\right) = \sum_{k=1}^{k=N_3}\sum_{i=1}^{i=N_1}\sum_{j=1}^{j=N_2} C^k \cdot P_{ij}^k \cdot X_{ijk},$$

import numpy as np
from pulp import *

# your definitions of N1, N2, N3 here
C = np.random.uniform(0,100,size=(N3))
P = np.random.uniform(0,100,size=(N1,N2,N3))

prob = LpProblem('Optimization Problem', LpMinimize)