Media Selection

22 views
Skip to first unread message

Samuel

unread,
Jun 20, 2016, 2:44:25 AM6/20/16
to Gurobi Optimization
I am trying to write following formulation in Python Gurobi  but i am stuck. This is giving error in Constraint that invalid argument. Can anyone help what is the problem in constraint. Code file is attached 

####Problem

minimize: c_m*x_m

subject to: N_tm*x_m>=1 for all

                x_m belong to {0,1} for all m
Indices:
           t=target audiences
           m=advertising media
Parameters:
           N_tm incidence: audience t is covered by medium m
          c_m cost of selecting advertising medium m

#####Code

from gurobipy import *

##Target Audience vs resource
target_audience_vs_resource=[[1,0,0,1,0,0,0],
                            [0,1,1,1,0,1,1],
                            [0,0,0,0,1,0,1],
                            [1,1,0,0,0,1,1],
                            [0,0,1,1,0,1,0]]

##Cost Function
cost_vector=[8,6,10,12,7,6,15]

# Model
m = Model("media_selection")

# Range of cost_vector and target audience_vs_resource
cost = range(len(cost_vector))

target = range(len(target_audience_vs_resource))

media_selection = []
for c in cost :
    media_selection.append([])
    for t in target:
        media_selection[c].append(m.addVar(vtype=GRB.BINARY, name="media_%d,%d" % (c, t)))
        
# The objective is to minimize the cost
m.modelSense = GRB.MINIMIZE

# Update model to integrate new variables
m.update()


# Set optimization objective minimize cost
m.setObjective(quicksum(media_selection[c][t]*cost_vector[c] for c in cost for t in target))
##
#####constraint 1
for t in target:
    m.addConstr(quicksum(media_selection[c][t]*target_audience_vs_resource [t]  for t in target) >= 1, "Demand_%d" % t)


# Solve
m.optimize()

Media_Selection-v1.py

Sonja Mars

unread,
Jun 22, 2016, 6:04:50 AM6/22/16
to gur...@googlegroups.com
Hi,

You need to be careful with the dimensions of your arrays.

In this constraint:
> for t in target:
> m.addConstr(quicksum(media_selection[c][t]*target_audience_vs_resource [t] for t in target) >= 1, "Demand_%d" % t)


You have these two things in the quicksum:

media_selection[c][t]: <gurobi.Var media_6,0>
target_audience_vs_resource [t]: [1, 0, 0, 1, 0, 0, 0]

The first one is a Gurobi variable, the second one is an array. These don't fit together.

Additionally, you have
> for t in target:

as loop around the constraint and
> for t in target
inside the quicksum. This will not work, you cannot have the same index for two loops.

Best regards,
Sonja

-----------------------------------------------------------------
Dr. Sonja Mars
Gurobi Optimization



Reply all
Reply to author
Forward
0 new messages