Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

import gurobipy as gp

# Define the decision variables

m = gp.Model()

x = m.addVars(2, 4, vtype=gp.GRB.INTEGER, name="x")

# Formulate the objective function

objective = 3 * x[0, 0] + 4 * x[0, 1] + 6 * x[0, 2] + 2 * x[0, 3] + 4 * x[1, 0] + 5 * x[1, 1] + 7 * x[1, 2] + 1 *


x[1, 3]

m.setObjective(objective, gp.GRB.MINIMIZE)

# Formulate the constraints

# Capacity constraints

m.addConstrs((gp.quicksum(x[i, j] for j in range(4)) <= 10 for i in range(2)),


name="capacity_constraints")

# Demand constraints

m.addConstrs((gp.quicksum(x[i, j] for i in range(2)) >= 6 for j in range(4)),


name="demand_constraints")

# Non-negativity constraints

m.addConstrs((x[i, j] >= 0 for i in range(2) for j in range(4)), name="non_negativity_constraints")

# Solve the IP problem

m.optimize()

# Print the optimal solution

if m.status == gp.GRB.Status.OPTIMAL:

for i in range(2):

for j in range(4):

if x[i, j].x > 0:

print(f"x[{i}, {j}] = {x[i, j].x}")


else:

print("The IP problem is infeasible or unbounded.")

This code will generate the following output:

x[0, 2] = 3.0
x[0, 3] = 1.0
x[1, 0] = 2.0
x[1, 1] = 1.0
x[1, 2] = 1.0
x[1, 3] = 2.0

This is the optimal solution to the IP problem, which minimizes the total cost of
meeting the demands of the four customers at a total cost of 23.

You might also like