1 year ago
#72532
kris
Linear programming. How can I add a constraint that is not constant value ? (like the allocation for each ad <= budgets, both two sides can change)
I am building an optimal model using linear programming like below:
m = gp.Model("allocation_budget")
#keywords are given tuple about keyword in ad.{UK retail, free shipping UK etc.}
allocation = m.addVars(keywords, lb = 0, name="allocation")
# budgets = np.arange(0,1100000,100000)
budget = m.addVars(budgets, lb = 0, name="budget")
#the right side of equation is wrong, it's where I am confused about
budget_constraint = m.addConstr(allocation.sum() <= budget,"budget_constraint")
#the objective is to maximize the net revenue which is equal to 4/CPC[k]*allocation[k] - budget, but I don't know to fit the variable "budget" into this equation
m.setObjective(quicksum(4/CPC[k]*allocation[k] - budget), GRB.MAXIMIZE)
#the following code contain error as well due to the previous problem
def printSolution():
if m.status == GRB.OPTIMAL:
print('\nClicks: %g' % m.objVal)
print('\nAllocation:')
allocationx = m.getAttr('x', allocation)
for k in keywords:
print('%s %g' % (k, allocationx[k]))
print("\nBudget: ",budget.x)
else:
print('No solution:', m.status)
m.optimize()
printSolution()
The objective is to find the right budget values so that the net revenue can be maximized.
Any help will be great! Please help
python
linear-programming
0 Answers
Your Answer