0

I am using Gurobi with Python to solve the following problem:

max. (1-Var(3^{-VC})) * (mean(VC)) s. t. sum(C) <= 3 and C in {0,1}

Whereas, C is a binary decision vector of length n. V is a coefficient (information) matrix of size mxn (m<=n). Here is my python code:

from gurobipy import *
import numpy as np

# EXAMPLE DATA SET
# ----------------------------------------------
V = np.matrix((
            (1,1,1,1,0,0),
            (0,1,1,1,1,0),
            (1,1,0,0,0,1),
            (0,0,0,1,1,1)))
V = V.tolist()

# LOCAL FUNCTIONS 
# ----------------------------------------------

# V times C
def V_x_C(V,C):
    VC = {}
    for i in range(np.shape(V)[0]):
        ij_sum = 0;
        for j in range(len(C)):
            ij_sum +=  V[i][j]*C[j]
        VC[i] = ij_sum
    return VC

# Square
def sqr(vec):
    sqr_vec = {}
    for i in range(len(vec)):
        sqr_vec[i] = vec[i]*vec[i]    
    return sqr_vec

# Variance
def variance(vec):
    mean_vec = sum(vec)/len(vec)
    diff_vec = vec-mean_vec
    sqdf_vec = sqr(diff_vec)
    var_vec  = (sum(sqdf_vec))/len(vec)
    return var_vec

# Power
def pwr(scalr,vec):
    reslt = {}
    for i in range(len(vec)):
        reslt[i] = scalr**vec[i]
    return reslt

# ObjVar
def objVar(V,C):
    VC = V_x_C(V,C)
    negVC = np.dot(-1,VC.values())
    pwrVC = pwr(3,negVC) 
    varVC = variance(1-pwrVC)
    return varVC

# ObjMean
def objMean(V,C):
    VC = V_x_C(V,C)
    sumVC = 0
    for i in range(len(VC)):
        sumVC += VC[i]
    meanVC = float(sumVC)/len(VC)
    return meanVC

# OPTIMIZATION
# ----------------------------------------------

# Create a new model
m = Model("My Model")

# Optimization Variables
C = {}
for i in range(np.shape(V)[1]):
    C[i] = m.addVar(vtype=GRB.BINARY)

# The objective is to maximize the costs
m.modelSense = GRB.MAXIMIZE

# Integrate new variables
m.update()

# Objective Function
m.setObjective( (1-objVar(V,C))*(objMean(V,C)) )

# Constraint
m.addConstr( quicksum ([C[i] for i in range(np.shape(V)[1])]) <= 3)

m.optimize()

# Publish results
for v in m.getVars(): 
    print('%s %g' % (v.varName, v.x))

I am getting following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/asif/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 682, in runfile
    execfile(filename, namespace)
  File "/home/asif/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
    builtins.execfile(filename, *where)
  File "/media/Win7/DATA/MPS-GSMR/PLANNER/TestPython/question_gurobi_form.py", line 83, in <module>
    m.setObjective( (1-objVar(V,C))*(objMean(V,C)) )
  File "/media/Win7/DATA/MPS-GSMR/PLANNER/TestPython/question_gurobi_form.py", line 52, in objVar
    pwrVC = pwr(3,negVC) 
  File "/media/Win7/DATA/MPS-GSMR/PLANNER/TestPython/question_gurobi_form.py", line 45, in pwr
    reslt[i] = scalr**vec[i]
TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'LinExpr'

So apparently, the main issue is how to use gurobi variable as power on a constant. Please suggest me to address the bug in particular and the overall implementation in general.

Asif Arain
  • 11
  • 3

0 Answers0