Created
June 24, 2023 20:45
-
-
Save abhijeet-talaulikar/e166cfcf9bdcc39dca4e5b8cfb0f4ad4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# initialize model | |
opt_model = grb.Model(name="Media Budget Optimization") | |
x_vars = opt_model.addVars(media_roi['media'], vtype=grb.GRB.CONTINUOUS, | |
lb=0, name="media") | |
# keep total spend less than new available budget | |
opt_model.addConstr(sum(x_vars[i] for i in media_roi['media']) <= | |
new_budget, name="New Budget") | |
# keep new media spend within +-25% | |
for i in media_roi['media']: | |
# minimum | |
opt_model.addConstr( | |
x_vars[i] >= 0.75 * media_roi.query("media == @i")['spend'].iloc[0], | |
name=f"{i}_min_spend" | |
) | |
# maximum | |
opt_model.addConstr( | |
x_vars[i] <= 1.25 * media_roi.query("media == @i")['spend'].iloc[0], | |
name=f"{i}_max_spend" | |
) | |
# objective function to maximize return on spend | |
opt_model.setObjective(sum(x_vars[i] * media_roi.query("media == @i")['ROI'].iloc[0] | |
for i in media_roi['media'])) | |
# run | |
opt_model.ModelSense = grb.GRB.MAXIMIZE | |
opt_model.optimize() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment