manbet体育手机客户端


poolsearch.py


#!/usr/bin/python版权所有2018,Gurobi Opt狗万app足彩imization,LLC#我们通过使用来自uu future uuu导入打印函数的PoolSearchMode从gurobipy import*尝试:#示例数据背景集=范围(10)objCoef=[32,32,15,6,6,1,1,1]背包系数=[16,16,8,8,4,4,2,2,1,1]预算=33#创建初始模型模型=模型(“池搜索”)#为tupledict.prod()创建dict()函数objCoefDict=dict(zip(Groundset,objCoef))背包coefdict=dict(zip(Groundset,knapsackCoef))#初始化地面集的决策变量:#x[e]=1如果选择元素e=Elem=model.addVars(Groundset,vtype=GRB.BINARY,name='El')#Set objective function model.ModelSense=GRB.MAXIMIZE model.setObjective(Elem.prod(objCoefDict))#约束:限制最多拾取的元素总数#预算模型.addConstr(Elem.prod(背包系数)<=Budget,name='Budget')#限制收集model.setParam的解决方案数(GRB.Param.PoolSolutions,1024)#通过为模型接受的最差可能解决方案设置间隔来限制搜索空间。setParam(GRB.Param.PoolGap,0.10)#系统搜索k-最佳解决方案模型。setParam(GRB.Param.PoolSearchMode,2)#保存问题模型。write('poolsearch.lp')#优化模型。Optimize()模型。setParam(GRB.Param.OutputFlag,0)#状态检查状态=模型。如果状态==GRB.Status.INF_或_UNBD或\Status==GRB.Status.invasible或\Status==GRB.Status.UNBOUNDED:print('由于模型不可行或无边界而无法求解')sys.exit(1)如果状态!=GRB.Status.OPTIMAL:print('以状态停止优化'+str(状态))sys.exit(1)#打印最佳选择集打印('best solution中的选定元素:')打印('\t',end='')地面集中的e:if Elem[e].X>.9:Print('El%d'%e,end='')打印('')#打印存储的解决方案数nSolutions=model.SolCount打印('找到的解决方案数:'+str(nSolutions))#打印范围内e的解决方案的目标值(解决方案):model.setParam(GRB.Param.SolutionNumber,e)Print(“%g”%model.PoolObjVal,end=”)如果e%15==14:Print(“”)Print(“”)Print(“”)Print(“”)#如果(解决方案>=4):model.setParam(GRB.Param.SolutionNumber,3)Print(“‘第四个最佳解决方案中的选定元素:’)Print('\t',end='')用于地面集合中的e:if Elem[e].Xn>.9:print('El%d'%e,end='')打印('')除了GurobiError作为e:print('Gurobi error'+str(e.errno)+':“+str(e.message)),除了AttributeError作为e:print('遇到属性错误:'+str(e))