本文整理汇总了Python中gurobipy.Model.addGenConstrOr方法的典型用法代码示例。如果您正苦于以下问题:Python Model.addGenConstrOr方法的具体用法?Python Model.addGenConstrOr怎么用?Python Model.addGenConstrOr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gurobipy.Model
的用法示例。
在下文中一共展示了Model.addGenConstrOr方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_algorithm
# 需要导入模块: from gurobipy import Model [as 别名]
# 或者: from gurobipy.Model import addGenConstrOr [as 别名]
def run_algorithm(self):
old_M = self.M
old_items = [i.copy() for i in self.items]
map_name_to_old_item = dict()
for i in old_items:
map_name_to_old_item[i.name] = i
self.scale_items_by_cost()
from gurobipy import Model, GRB
model = Model("NP-Hard")
print("Setting Model Parameters")
# set timeout
model.setParam('TimeLimit', 1600)
model.setParam('MIPFocus', 3)
model.setParam('PrePasses', 1)
model.setParam('Heuristics', 0.01)
model.setParam('Method', 0)
map_name_to_item = dict()
map_name_to_cost = dict()
map_name_to_weight = dict()
map_name_to_profit = dict()
map_class_to_name = dict()
item_names = list()
print("Preprocessing data for model...")
for item in self.items:
item_names.append(item.name)
map_name_to_item[item.name] = item
map_name_to_cost[item.name] = item.cost
map_name_to_weight[item.name] = item.weight
map_name_to_profit[item.name] = item.profit
if item.classNumber not in map_class_to_name:
map_class_to_name[item.classNumber] = list()
map_class_to_name[item.classNumber].append(item.name)
class_numbers = list(map_class_to_name.keys())
print("Setting model variables...")
# binary variables =1, if use>0
items = model.addVars(item_names, vtype=GRB.BINARY, name="items")
classes = model.addVars(class_numbers, vtype=GRB.BINARY, name="class numbers")
print("Setting model objective...")
# maximize profit
objective = items.prod(map_name_to_profit)
model.setObjective(objective, GRB.MAXIMIZE)
# constraints
print("Setting model constraints")
model.addConstr(items.prod(map_name_to_weight) <= self.P,"weight capacity")
model.addConstr(items.prod(map_name_to_cost) <= self.M,"cost capacity")
# if any item from a class is chosen, that class variable has to be a binary of 1
for num in class_numbers:
model.addGenConstrOr(classes[num], [items[x] for x in map_class_to_name[num]] ,name="class count")
for c in self.raw_constraints:
count = model.addVar()
for n in c:
if n in classes:
count += classes[n]
model.addConstr(count <= 1, name="constraint")
print("Start optimizing...")
model.optimize()
print("Done! ")
# Status checking
status = model.Status
if status == GRB.Status.INF_OR_UNBD or \
status == GRB.Status.INFEASIBLE or \
status == GRB.Status.UNBOUNDED:
print('The model cannot be solved because it is infeasible or unbounded')
if status != GRB.Status.OPTIMAL:
print('Optimization was stopped with status ' + str(status))
Problem = True
try:
model.write("mps_model/" + self.filename + ".sol")
except Exception as e:
pass
print("Generating solution file...")
# Display solution
solution_names = list()
for i, v in enumerate(items):
try:
if items[v].X > 0.9:
solution_names.append(item_names[i])
except Exception as e:
pass
self.M = old_M
self.items = old_items
#.........这里部分代码省略.........