Exemplo n.º 1
0
# Problem #2: resource allocation (this time with PuLP)

# max 20x1 + 12x2 + 40x3 + 25x4
# st  x1 + x2 + x3 + x4 <= 50
#     3x1 + 2x2 + x3 <= 100 
#     x2 + 2x3 + 3x4 <= 90 
#     x1, x2, x3, x4 >= 0

# Define the model
model = LpProblem(name="resource-allocation", sense=LpMaximize)

# Define the decision variables
x = {i: LpVariable(name=f"x{i}", lowBound=0) for i in range(1, 5)}

# Add constraints
model += (lpSum(x.values()) <= 50, "manpower")
model += (3 * x[1] + 2 * x[2] + x[3] <= 100, "material_a")
model += (x[2] + 2 * x[3] + 3 * x[4] <= 90, "material_b")

# Set the objective
model += 20 * x[1] + 12 * x[2] + 40 * x[3] + 25 * x[4]

# Solve the optimization problem
status = model.solve()

# Get the results
print(f"status: {model.status}, {LpStatus[model.status]}")
print(f"objective: {model.objective.value()}")

for var in x.values():
    print(f"{var.name}: {var.value()}")