本文整理汇总了Python中gurobipy.Model.getAttr方法的典型用法代码示例。如果您正苦于以下问题:Python Model.getAttr方法的具体用法?Python Model.getAttr怎么用?Python Model.getAttr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gurobipy.Model
的用法示例。
在下文中一共展示了Model.getAttr方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BFPBackupNetwork_Continuous
# 需要导入模块: from gurobipy import Model [as 别名]
# 或者: from gurobipy.Model import getAttr [as 别名]
#.........这里部分代码省略.........
def Optimize(self, MipGap=None, TimeLimit=None, LogLevel=None):
""" Optimize the defined model.
Parameters
----------
MipGap : desired gap
TimeLimit : time limit
LogLevel: log level 1 for printing all optimal variables and None otherwise
Returns
-------
BackupCapacity: The total capacity assigned per backup link
BackupRoutes: The set of selected backup links
A tuple list with all paths for edge (s,d) that uses (i,j).
"""
self.model.write("bpbackup.lp")
if MipGap != None:
self.model.params.MIPGap = MipGap
if TimeLimit != None:
self.model.params.timeLimit = TimeLimit
# Compute optimal solution
self.model.optimize()
# Print solution
if self.model.status == GRB.Status.OPTIMAL:
if LogLevel == 1:
for v in self.model.getVars():
print("%s %g" % (v.varName, v.x))
self.BackupCapacitySolution = self.model.getAttr("x", self.BackupCapacity)
self.BackupRoutesSolution = self.model.getAttr("x", self.bBackupLink)
self.BackupLinksSolution = {}
self.HatBackupCapacity = {}
for link in self.BackupCapacitySolution:
if self.BackupCapacitySolution[link] < 1 and self.BackupCapacitySolution[link] > 0.001:
self.HatBackupCapacity[link] = math.ceil(self.BackupCapacitySolution[link])
else:
self.HatBackupCapacity[link] = math.floor(self.BackupCapacitySolution[link])
if self.HatBackupCapacity[link] > 0:
if len(self.BackupLinksSolution) == 0:
self.BackupLinksSolution = [link]
else:
self.BackupLinksSolution = self.BackupLinksSolution + [link]
else:
print("Optimal value not found!\n")
self.BackupCapacitySolution = []
self.BackupRoutesSolution = {}
self.BackupLinksSolution = {}
return self.BackupCapacitySolution, self.BackupRoutesSolution, self.BackupLinksSolution, self.HatBackupCapacity
def SaveBakupNetwork(self, file_name):
"""Save the optimal backup network to the file ``file_name``."""
data = {
"links": [i for i in self.BackupCapacitySolution],
"capacities": [self.BackupCapacitySolution[i] for i in self.BackupCapacitySolution],
"routes": [i for i in self.BackupRoutesSolution],
"status": [self.BackupRoutesSolution[i] for i in self.BackupRoutesSolution],
}
f = open(file_name, "w")
示例2: get_mapping
# 需要导入模块: from gurobipy import Model [as 别名]
# 或者: from gurobipy.Model import getAttr [as 别名]
def get_mapping(PG, VG):
vnodes, vhosts = multidict({n: len(VG.node[n]['host_ports']) for n in VG.nodes()})
pnodes, phosts = multidict({n: len(PG.node[n]['host_ports']) for n in PG.nodes()})
parcs, pcapacity = multidict({(m, n): PG.edge[m][n]['weight'] for (m, n) in PG.edges()})
parcs = tuplelist(parcs)
varcs, vcapacity = multidict({(m, n): VG.edge[m][n]['weight'] for (m, n) in VG.edges()})
varcs = tuplelist(varcs)
m = Model('mapping')
# Create variables
node_mapping = {}
for i in vnodes:
for j in pnodes:
node_mapping[i, j] = m.addVar(vtype=GRB.BINARY, name="x_%s_%s" % (i, j))
edge_mapping = {}
for i in vnodes:
for p in VG.neighbors(i):
for (j, q) in parcs:
edge_mapping[i, p, j, q] = m.addVar(vtype=GRB.BINARY, name="y_%s_%s_%s_%s" % (i, p, j, q))
m.update()
# Arc capacity constraints
for i in vnodes:
m.addConstr(quicksum(node_mapping[i, j] for j in pnodes) == 1, 'node_%s' % i)
for i in vnodes:
for p in VG.neighbors(i):
for (j, q) in parcs:
m.addConstr(edge_mapping[i, p, j, q] <= ( node_mapping[i, j] + node_mapping[p, q] )/2, 'edge_%s_%s_%s_%s' % (i, p, j, q))
for (j, q) in parcs:
m.addConstr(quicksum(edge_mapping[i, p, j, q] + edge_mapping[p, i, j, q]for (i, p) in varcs) <= pcapacity[j, q], 'pcap_%s_%s' % (j, q))
for (i, p) in varcs:
m.addConstr(quicksum(edge_mapping[i, p, j, q] + edge_mapping[p, i, j, q] for (j, q) in parcs) >= vcapacity[i, p], 'vcap_%s_%s' % (i, p))
for j in pnodes:
m.addConstr(quicksum(node_mapping[i, j] * vhosts[i] for i in vnodes) <= phosts[j], 'phosts_%s' % j)
for i in vnodes:
m.addConstr(quicksum(node_mapping[i, j] * phosts[j] for j in pnodes) >= vhosts[i], 'vhosts_%s' % i)
# Compute optimal solution
m.optimize()
# Print solution
if m.status == GRB.status.OPTIMAL:
mapping = {}
mapping2 = {}
portlist = {}
for n in PG:
if 'host_ports' in PG.node[n]:
portlist[n] = PG.node[n]['host_ports']
solution = m.getAttr('x', edge_mapping)
for h in solution:
if solution[h] == 1.0:
vid1, vid2, pid1, pid2 = h
vport1, vport2 = list(VG.node[vid1]['links'][vid2])[0]
if (vid1, pid1) not in mapping:
mapping[(vid1, pid1)] =[vid1, pid1]
if (pid1, vid1) not in mapping2:
mapping2[(pid1, vid1)] =[pid1, vid1]
(pport1, pport2) = PG.node[pid1]['links'][pid2].pop()
# print vid1, vid2, pid1, pid2, pport1, pport2
if pid1 != pid2:
PG.node[pid2]['links'][pid1].remove((pport2, pport1))
mapping[(vid1, pid1)].append(vport1)
mapping[(vid1, pid1)].append(pport1)
mapping2[(pid1, vid1)].append(pport1)
mapping2[(pid1, vid1)].append(vport1)
if (vid2, pid2) not in mapping:
mapping[(vid2, pid2)] =[vid2, pid2]
if (pid2, vid2) not in mapping2:
mapping2[(pid2, vid2)] =[pid2, vid2]
mapping[(vid2, pid2)].append(vport2)
mapping[(vid2, pid2)].append(pport2)
mapping2[(pid2, vid2)].append(pport2)
mapping2[(pid2, vid2)].append(vport2)
solution2 = m.getAttr('x', node_mapping)
print [h for h in solution2 if solution2[h] == 1.0]
for h in solution2:
if solution2[h] == 1.0:
vid, pid = h
if len(VG.node[vid]['host_ports']) == 0:
continue
for vport in VG.node[vid]['host_ports']:
pport = portlist[pid].pop()
mapping[(vid, pid)].append(vport)
mapping[(vid, pid)].append(pport)
mapping2[(pid, vid)].append(pport)
mapping2[(pid, vid)].append(vport)
#.........这里部分代码省略.........
示例3: Backup
# 需要导入模块: from gurobipy import Model [as 别名]
# 或者: from gurobipy.Model import getAttr [as 别名]
#.........这里部分代码省略.........
self.__model = Model('Backup')
# Auxiliary variables for SOCP reformulation
U = {}
R = {}
# Create variables
for i,j in self.__links:
self.__BackupCapacity[i,j] = self.__model.addVar(lb=0, obj=1, name='Backup_Capacity[%s,%s]' % (i, j))
self.__model.update()
for i,j in self.__links:
for s,d in self.__links:
self.__bBackupLink[i,j,s,d] = self.__model.addVar(vtype=GRB.BINARY,obj=1,name='Backup_Link[%s,%s,%s,%s]' % (i, j, s, d))
self.__model.update()
for i,j in self.__links:
U[i,j] = self.__model.addVar(obj=1,name='U[%s,%s]' % (i, j))
self.__model.update()
for i,j in self.__links:
for s,d in self.__links:
R[i,j,s,d] = self.__model.addVar(obj=1,name='R[%s,%s,%s,%s]' % (i,j,s,d))
self.__model.update()
self.__model.modelSense = GRB.MINIMIZE
#m.setObjective(quicksum([fixedCosts[p]*open[p] for p in plants]))
self.__model.setObjective(quicksum(self.__BackupCapacity[i,j] for i,j in self.__links))
self.__model.update()
#------------------------------------------------------------------------#
# Constraints definition #
# #
# #
#------------------------------------------------------------------------#
# Link capacity constraints
for i,j in self.__links:
self.__model.addConstr(self.__BackupCapacity[i,j] >= quicksum(self.__mean[s,d]*self.__bBackupLink[i,j,s,d] for (s,d) in self.__links) + U[i,j]*self.__invstd,'[CONST]Link_Cap_%s_%s' % (i, j))
self.__model.update()
# SCOP Reformulation Constraints
for i,j in self.__links:
self.__model.addConstr(quicksum(R[i,j,s,d]*R[i,j,s,d] for (s,d) in self.__links) <= U[i,j]*U[i,j],'[CONST]SCOP1[%s][%s]' % (i, j))
self.__model.update()
# SCOP Reformulation Constraints
for i,j in self.__links:
for s,d in self.__links:
self.__model.addConstr(self.__std[s,d]*self.__bBackupLink[i,j,s,d] == R[i,j,s,d],'[CONST]SCOP2[%s][%s][%s][%s]' % (i, j,s,d))
self.__model.update()
for i in self.__nodes:
for s,d in self.__links:
# Flow conservation constraints
if i == s:
self.__model.addConstr(quicksum(self.__bBackupLink[i,j,s,d] for i,j in self.__links.select(i,'*')) -
quicksum(self.__bBackupLink[j,i,s,d] for j,i in self.__links.select('*',i)) == 1,'Flow1[%s,%s,%s,%s]' % (i,j,s, d))
# Flow conservation constraints
elif i == d:
self.__model.addConstr(quicksum(self.__bBackupLink[i,j,s,d] for i,j in self.__links.select(i,'*')) -
quicksum(self.__bBackupLink[j,i,s,d] for j,i in self.__links.select('*',i)) == -1,'Flow2[%s,%s,%s,%s]' % (i,j,s, d))
# Flow conservation constraints
else:
self.__model.addConstr(quicksum(self.__bBackupLink[i,j,s,d] for i,j in self.__links.select(i,'*')) -
quicksum(self.__bBackupLink[j,i,s,d] for j,i in self.__links.select('*',i)) == 0,'Flow3[%s,%s,%s,%s]' % (i,j,s, d))
self.__model.update()
def optimize(self,MipGap, TimeLimit):
self.__model.write('backup.lp')
if MipGap != None:
self.__model.params.timeLimit = TimeLimit
if TimeLimit != None:
self.__model.params.MIPGap = MipGap
# Compute optimal solution
self.__model.optimize()
# Print solution
if self.__model.status == GRB.Status.OPTIMAL:
solution = self.__model.getAttr('x', self.__BackupCapacity)
for i,j in self.__links:
if solution[i,j] > 0:
print('%s -> %s: %g' % (i, j, solution[i,j]))
else:
print('Optimal value not found!\n')
solution = []
return solution;
def reset(self):
'''
Reset model solution
'''
self.__model.reset()
示例4: PathBackup
# 需要导入模块: from gurobipy import Model [as 别名]
# 或者: from gurobipy.Model import getAttr [as 别名]
#.........这里部分代码省略.........
def __loadModel(self):
# Create optimization model
self.__model = Model('PathBackup')
# Auxiliary variables for SOCP reformulation
U = {}
R = {}
# Create variables
for i,j in self.__links:
self.__BackupCapacity[i,j] = self.__model.addVar(lb=0, obj=1, name='Backup_Capacity[%s,%s]' % (i, j))
self.__model.update()
for p in self.__paths:
#LP Relaxation
#self.__bPath[self.__paths.index(p)] = self.__model.addVar(lb=0,obj=1,name='Backup_Path[%s]' % (self.__paths.index(p)))
self.__bPath[self.__paths.index(p)] = self.__model.addVar(vtype=GRB.BINARY,obj=1,name='Backup_Path[%s]' % (self.__paths.index(p)))
self.__model.update()
for i,j in self.__links:
U[i,j] = self.__model.addVar(obj=1,name='U[%s,%s]' % (i, j))
self.__model.update()
for i,j in self.__links:
for s,d in self.__links:
R[i,j,s,d] = self.__model.addVar(obj=1,name='R[%s,%s,%s,%s]' % (i,j,s,d))
self.__model.update()
self.__model.modelSense = GRB.MINIMIZE
self.__model.setObjective(quicksum(self.__BackupCapacity[i,j] for i,j in self.__links))
self.__model.update()
#------------------------------------------------------------------------#
# Constraints definition #
# #
# #
#------------------------------------------------------------------------#
# Link capacity constraints
for i,j in self.__links:
self.__model.addConstr(self.__BackupCapacity[i,j] >= quicksum(self.__mean[s,d]*quicksum(self.__bPath[self.__paths.index(p)] for p in self.__Pij[i,j,s,d]) for s,d in self.__links) + U[i,j]*self.__invstd,'[CONST]Link_Cap[%s][%s]' % (i, j))
self.__model.update()
# SCOP Reformulation Constraints
for i,j in self.__links:
self.__model.addConstr(quicksum(R[i,j,s,d]*R[i,j,s,d] for s,d in self.__links) <= U[i,j]*U[i,j],'[CONST]SCOP1[%s][%s]' % (i, j))
self.__model.update()
# SCOP Reformulation Constraints
for i,j in self.__links:
for s,d in self.__links:
self.__model.addConstr(self.__std[s,d]*quicksum(self.__bPath[self.__paths.index(p)] for p in self.__Pij[i,j,s,d]) == R[i,j,s,d],'[CONST]SCOP2[%s][%s][%s][%s]' % (i,j,s,d))
self.__model.update()
# Unique path
for s,d in self.__links:
self.__model.addConstr(quicksum(self.__bPath[self.__paths.index(p)] for p in self.__Psd[s,d]) == 1,'UniquePath[%s,%s]' % (s, d))
self.__model.update()
def optimize(self,MipGap,TimeLimit):
self.__model.write('pathbackup.lp')
if TimeLimit != None:
self.__model.params.timeLimit = TimeLimit
if MipGap != None:
self.__model.params.MIPGap=MipGap
# Compute optimal solution
self.__model.optimize()
# Print solution
if self.__model.status == GRB.Status.OPTIMAL:
solution = self.__model.getAttr('x', self.__bPath)
for p in self.__paths:
if solution[self.__paths.index(p)] > 0.001:
print('Path[%s] = %s = %s' % (self.__paths.index(p),p,solution[self.__paths.index(p)]))
solution = self.__model.getAttr('x', self.__BackupCapacity)
for i,j in self.__links:
if solution[i,j] > 0:
print('%s -> %s: %g' % (i, j, solution[i,j]))
else:
print('Optimal value not found!\n')
solution = self.__model.getAttr('x', self.__BackupCapacity)
for i,j in self.__links:
if solution[i,j] > 0:
print('%s -> %s: %g' % (i, j, solution[i,j]))
#solution = []
return solution;
def reset(self):
'''
Reset model solution
'''
self.__model.reset()