i using gurobi python solve following problem:
max. (1-var(3^{-vc})) * (mean(vc)) s. t. sum(c) <= 3 , c in {0,1}
whereas, c binary decision vector of length n. v coefficient (information) matrix of size mxn (m<=n). here python code:
from gurobipy import * import numpy 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 = {} in range(np.shape(v)[0]): ij_sum = 0; j in range(len(c)): ij_sum += v[i][j]*c[j] vc[i] = ij_sum return vc # square def sqr(vec): sqr_vec = {} 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 = {} 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 in range(len(vc)): sumvc += vc[i] meanvc = float(sumvc)/len(vc) return meanvc # optimization # ---------------------------------------------- # create new model m = model("my model") # optimization variables c = {} in range(np.shape(v)[1]): c[i] = m.addvar(vtype=grb.binary) # objective maximize 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] in range(np.shape(v)[1])]) <= 3) m.optimize() # publish results v in m.getvars(): print('%s %g' % (v.varname, v.x)) i 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) ** or pow(): 'int' , 'linexpr' so apparently, main issue how use gurobi variable power on constant. please suggest me address bug in particular , overall implementation in general.
Comments
Post a Comment