What are the constraints for this linear programming problem?
An answer to this question on Stack Overflow.
Question
A writer creates math problems for a website. She gets paid $5 per word problem and $2 per algebraic problem. On average, it takes her 4 minutes to create a word problem and 2 minutes to create an algebraic problem. Her boss wants her to make at least 50 problems total and have more algebraic problems than word problems. If the writer has three hours, what is the greatest profit she can make?
Please correct my constraints if they are wrong.
4 minutes to create a word problem and 2 minutes to create an algebraic problem. If the writer has 180 minutes. Constraint 1 is 4x+2y<=180
Her boss wants her to make at least 50 problems total. Constraint 2 is x<=50
and have more algebraic problems than word problems. Constraint 3 is y>+x
Answer
You probably want these constraints:
#!/usr/bin/env python3
import cvxpy as cp
import numpy as np
word_count = cp.Variable()
algebraic_count = cp.Variable()
constraints = [
4*word_count + 2*algebraic_count <= 180, # Time constraint
word_count + algebraic_count >= 50, # Need at least 50 problems
algebraic_count >= word_count, # More algebraic problems then word problems
]
objective = cp.Maximize(5*word_count + 2*algebraic_count)
prob = cp.Problem(objective, constraints)
optval = prob.solve()
print("Optimum profit", optval)
print("Word problem count", word_count.value)
print("Algebraic problem count", algebraic_count.value)
Giving
Optimum profit 209.99999999414464
Word problem count 29.999999990727126
Algebraic problem count 30.000000020254507
Note that the numbers here are close enough to integers that we can believe they're correct. However, this isn't true in general. If you got a value of, say, 29.5 for the number of word problems, that would imply that you need to either force word count to be <=29 or >=30 to try to get an integer value.
Alternatively, you could use an integer programming mode by setting:
word_count = cp.Variable(integer=True)
algebraic_count = cp.Variable(integer=True)
which gives
Optimum profit 210.0
Word problem count 30.0
Algebraic problem count 30.0