Media Selection Problem

33 views
Skip to first unread message

Samuel

unread,
Jun 14, 2016, 1:00:07 AM6/14/16
to Gurobi Optimization
I want to write code of Media selection problem in Python Gurobi which gives error. can anyone guide me about that code. code is given below

from gurobipy import *

# Warehouse demand in thousands of gallons
demand = [[1, 0, 1],
          [0, 1, 1],
          [1, 1, 0]]

# Plant capacity in thousands of gallons
capacity = [1, 1, 1]

# Transportation costs per thousand of gallons
Cost = [10000,20000,30000]

# Range of plants and warehouses
resource = range(len(capacity))

target_group = range(len(demand))

# Model
m = Model("media_selection")

# Transportation decision variables: how much to transport from a plant p to a warehouse w
media_selection = []
for r in resource:
    media_selection.append([])
    for t in target_group:
        media_selection[r].append(m.addVar(vtype=GRB.BINARY, name="Trans_%d,%d" % (r, t)))

# The objective is to minimize the total fixed and variable costs
m.modelSense = GRB.MINIMIZE

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

# Set optimization objective - minimize sum of fixed costs
m.setObjective(quicksum(media_selection[r][t]*Cost[r][t] for r in resource for t in target_group))

# Production/Supply constraints
for r in resource:
    m.addConstr(quicksum(media_selection[r][t] for t in target_group) <= 1, "Capacity_%d" % r)

# Demand constraints
for t in target_group:
    m.addConstr(quicksum(media_selection[r][t]*target_group for r in resource for t in target_group) >= 1, "Demand_%d" % t)

# Solve
m.optimize()

# Print solution
print('\nTOTAL COSTS: %g' % m.objVal)
print('SOLUTION:')

Sonja Mars

unread,
Jun 14, 2016, 4:11:31 AM6/14/16
to gur...@googlegroups.com
Hi,

When setting the objective you are trying to access this here

Cost[r][t]

but
> Cost = [10000,20000,30000]

This Cost is a list and not a list of lists. So what you need to to would be something like Cost[r]

Additionally, this constraint:

m.addConstr(quicksum(media_selection[r][t]*target_group for r in resource for t in target_group) >= 1, "Demand_%d" % t)

seems to be wrong, as for each term of your sum you are multiplying a variable with a list. I guess, you want something like this target_group[r]. Please also see here for how to use quicksum(): http://www.gurobi.com/documentation/6.5/refman/py_quicksum.html


Best regards,
Sonja

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






Samuel

unread,
Jun 16, 2016, 5:23:00 AM6/16/16
to Gurobi Optimization
Thank you for your reply  

Samuel

unread,
Jun 17, 2016, 2:20:46 AM6/17/16
to Gurobi Optimization
I am trying to write following formulation but i am stuck can you guide me about this 

minimize: c_m*x_m

subject to: N_tmx_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

Which i have code is following kindly guide me 
##Media Selection Problem by set covering 
from gurobipy import *

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

##Cost Function
cost_vector=[100,195,45]

# 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 c  in cost for t in target) >= 1, "Demand_%d" % t)


# Solve
m.optimize()

On Tuesday, 14 June 2016 10:00:07 UTC+5, Samuel wrote:
Reply all
Reply to author
Forward
0 new messages