2
$\begingroup$

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?

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

1 Answer 1

3
$\begingroup$

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},$$

writing your objective is straightforward:

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)

# add the decision variables
X = {}
for i in range(N1):
    for j in range(N2):
        for k in range(N3):
            X[i,j,k] = LpVariable(f"X[{i},{j},{k}]", cat="Binary")

# set the objective
prob.setObjective(sum(C[k]*P[i,j,k]*X[i,j,k] for i in range(N1) 
                                             for j in range(N2) 
                                             for k in range(N3)))
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.