本文整理汇总了Python中gurobipy.Model.addQConstr方法的典型用法代码示例。如果您正苦于以下问题:Python Model.addQConstr方法的具体用法?Python Model.addQConstr怎么用?Python Model.addQConstr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gurobipy.Model
的用法示例。
在下文中一共展示了Model.addQConstr方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_gurobi_model
# 需要导入模块: from gurobipy import Model [as 别名]
# 或者: from gurobipy.Model import addQConstr [as 别名]
def build_gurobi_model(case):
G, B = case.G, case.B
P = real(case.demands)
Q = imag(case.demands)
branches = case.branch_list
n = len(case.demands)
vhat = case.vhat
s2 = 2**.5
gens = {bus: gen.v for bus, gen in case.gens.items()}
del gens[0]
m = GurobiModel("jabr")
u = [m.addVar(name='u_%d'%i) for i in range(n)]
R = {(i, j): m.addVar(name='R_%d_%d' % (i, j)) for i, j in branches}
I = {(i, j): m.addVar(lb=-GRB.INFINITY, name='I_%d_%d' % (i, j)) for i, j in branches}
for i, j in branches:
R[j, i] = R[i, j]
I[j, i] = I[i, j]
m.update()
m.addConstr(u[0] == vhat*vhat/s2, 'u0')
for gen, v in gens.iteritems():
m.addConstr(u[gen] == v*v/s2, 'u%d' % gen)
for i, j in branches:
m.addQConstr(2*u[i]*u[j] >= R[i,j]*R[i,j] + I[i,j]*I[i,j], 'cone_%d_%d' % (i, j))
k = lambda i: (j for j in B[i, :].nonzero()[1])
s = lambda i, j: 1 if i < j else -1
for i in range(1, n):
m.addConstr(-s2*u[i]*G[i, :].sum() + quicksum(G[i,j]*R[i,j] + B[i,j]*s(i,j)*I[i,j] for j in k(i)) == P[i],
'real_flow_%d_%d' % (i, j))
if i in gens:
continue
m.addConstr(s2*u[i]*B[i, :].sum() + quicksum(-B[i,j]*R[i,j] + G[i,j]*s(i,j)*I[i,j] for j in k(i)) == Q[i],
'reac_flow_%d_%d' % (i, j))
m.setObjective(quicksum(R[i,j] for i, j in branches), sense=GRB.MAXIMIZE)
m.params.outputFlag = 0
#m.params.barQCPConvTol = 5e-10
m.optimize()
if m.status != 2:
raise ValueError("gurobi failed to converge: %s (check log)" % m.status)
u_opt = [x.getAttr('x') for x in u]
R_opt = {(i, j): x.getAttr('x') for (i, j), x in R.items()}
I_opt = {(i, j): x.getAttr('x') for (i, j), x in I.items()}
return u_opt, R_opt, I_opt
示例2: build_model
# 需要导入模块: from gurobipy import Model [as 别名]
# 或者: from gurobipy.Model import addQConstr [as 别名]
def build_model(data):
n = data['n']
r = data['r']
p = data['p']
s = data['s']
c = data['c']
h = data['h']
Q = data['Q']
T = data['T']
conflicts = data['conflicts']
locking_times = data['locking_times']
model = Model("ExaminationSchedulingQ_v2")
print("Building variables...")
x = {}
y = {}
for i in range(n):
for k in range(r):
x[i,k] = model.addVar(vtype=GRB.BINARY, name="x_%s_%s" % (i,k))
for l in range(p):
y[i, l] = model.addVar(vtype=GRB.BINARY, name="y_%s_%s" % (i,l))
model.update() # integrate new variables
print("Building constraints...")
# Add constraints
for i in range(n):
# Add constraint: Each i is planned in exactly one period
model.addConstr( quicksum([y[i,l] for l in range(p)]) == 1, "c0")
# Add constraint: Each i has enough seats
model.addConstr( quicksum([x[i,k] * c[k] for k in range(r)]) >= s[i], "c1")
print "Bedingungen 1 generiert"
# Add constraint: Each k has at most one i per period
for k in range(r):
for l in range(p):
model.addQConstr( quicksum([x[i,k] * y[i,l] for i in range(n)]) <= T[k][l], "c2")
# Add constraint: There are no conflicts quadratic
for l in range(p):
model.addQConstr( quicksum([ y[i,l] * y[j,l] * Q[i][j] for i, j in itertools.combinations(range(n),2) if Q[i][j] == 1]) == 0, "c3")
###### Improve speed by generating combinations of i and j outside of loop
# Set objective
#model.setObjective( quicksum([ x[i,k] * s[i] for i,k in itertools.product(range(n), range(r)) ]), GRB.MINIMIZE)
gamma = 1
model.setObjective( -gamma*quicksum([Q[i][j]*(quicksum([y[i,l]*h[l] - y[j,l]*h[l] for l in range(p)]))*(quicksum([y[i,l]*h[l] - y[j,l]*h[l] for l in range(p)])) for i, j in itertools.combinations(range(n),2) if Q[i][j] == 1 and j > i]) + quicksum(x[i,k] * s[i] for i,k in itertools.product(range(n),range(r)) ) , GRB.MINIMIZE)
print "Zielfunktion gesetzt"
# Set Parameter
#model.params.mipfocus = 3
#maximum level of linearization
model.params.preqlinearize = 1
#max presolve agressivity
model.params.presolve = 2
#Choosing root method 3= concurrent = run barrier and dual simplex in parallel
model.params.method = 1
return(model)