mip2.py


mip2.py


#!这个例子从一个文件中读取一个MIP模型,求解它,并在求解MIP时从所有可行的解决方狗万app足彩案中打印出客观值。然后,它创建相关的固定模型,#解决该模型。import sys import gurobipy as gp from gurobipy import GRB if len(sys.argv) < 2: print('Usage: mip2.py filename') sys.exit(0) #读取并解决模型model = gp. Read (sys.argv[1]) if model。print('Model is not a MIP') sys.exit(0) Model .optimize() if Model .exit(0) is . print('Model is not a MIP');状态= =伽马线暴。print('最优目标:%g') elif模型。状态= =伽马线暴。print('Model is infeasible or unbounded') sys.exit(0) elif Model。状态= =伽马线暴。sys.exit(0) elif模型。状态= =伽马线暴。sys.exit(0) #遍历解决方案并计算目标模型。params . outputflag = 0 print(") for k in range(Model . solcount):print('Solution %d has objective %g' % (k, model.poolObjVal)) print(") model.Params.outputFlag = 1 fixed = model.fixed() fixed. params .presolve = 0 fixed.optimize()如果固定。地位! =伽马线暴。sys.exit(1) diff = model. print("Error: fixed model is not OPTIMAL ")objVal——固定。objVal if abs(diff) > 1e-6 * (1.0 + abs(model.objVal)): print('Error: objective values are different') sys.exit(1) # Print values of nonzero variables for v in fixed.getVars(): if v.x != 0: print('%s %g' % (v.varName, v.x))