0

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

kris
  • 27
  • 6
  • 1
    The left hand side of your constraint is a single number because you are taking a sum, but the right hand side is a collection of variables. You can use a for loop to add constraints. – Kraigolas Jan 19 '22 at 23:58
  • I try for loop like this. budget_constraint = m.addConstr(allocation.sum() <= budget [i] for i in range (11) still incorrect – kris Jan 19 '22 at 23:59

0 Answers0