Skip to content

Instantly share code, notes, and snippets.

@cderekw4224
Created May 5, 2022 20:41
Show Gist options
  • Select an option

  • Save cderekw4224/b2b12cfbac1e17fad4c4a4df9a63ebd4 to your computer and use it in GitHub Desktop.

Select an option

Save cderekw4224/b2b12cfbac1e17fad4c4a4df9a63ebd4 to your computer and use it in GitHub Desktop.
# https://towardsdatascience.com/linear-programming-using-python-priyansh-22b5ee888fe0
# Toy Example:
# If we had Decision Varibales that will use i and j for example
# with 2 warehouses & 4 customers
# Data Definition
# Let’s define the data and assign it to variables which can be then used to feed into the model, objective function and constraints.
n_warehouses = 2
n_customers = 4
# Cost Matrix
cost_matrix = np.array([[1, 3, 0.5, 4],
[2.5, 5, 1.5, 2.5]])
# Demand Matrix
cust_demands = np.array([35000, 22000, 18000, 30000])
# Supply Matrix
warehouse_supply = np.array([60000, 80000])
# Model Initialization
# We can initialize the model by calling LpProblem() function. The first argument in the function represents the name we want to give to our model. The second argument tells our model whether we want to minimize or maximize our objective function.
model = LpProblem("Supply-Demand-Problem", LpMinimize)
# You can use LpMaximize instead incase you want to maximize your objective function.
# Defining Decision Variables
# You can define variable names in your model to make your model look more intuitive to the person who will be reading it later. Hence, we create indices for our decision variables which will be defined later.
variable_names = [str(i)+str(j) for j in range(1, n_customers+1) for i in range(1, n_warehouses+1)]
variable_names.sort()
print("Variable Indices:", variable_names)
# Output looks like this:-
# Variable Indices: ['11', '12', '13', '14', '21', '22', '23', '24']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment