matrix1.py
# !/usr/bin/env python3.7 # 2021年版权,Gurobi优化狗万app足彩,LLC #这个例子中制定和解决以下简单的MIP模型使用矩阵API: # # # 2 x + y + z最大化#话题# x + 2 y + 3 z < = 4 # x + y > = 1 # x, y, z二进制进口gurobipy从gurobipy gp进口伽马线暴进口numpy np scipy导入。#创建变量x = m. addmvar (shape=3, vtype=GRB)。# Set objective obj = np.array([1.0, 1.0, 2.0]) m. setobobjective (obj @ x, GRB.MAXIMIZE) # Set (sparse) constraint matrix val = np.array([1.0, 2.0, 3.0, -1.0, -1.0]) row = np。数组([0,0,0,1,1])col = np。array([0,1,2,0,1]) A = sp.csr_matrix((val, (row, col)), shape=(2,3)) #创建rhs vector rhs = np.array([4.0, -1.0]) #添加约束m.a dconstr (A @ x <= rhs, name="c") #优化模型m.a dconstr (A @ x <= rhs, name="c") #优化模型m.a dconstr () print(x.X) print('Obj: %g' % m.ObjVal)除gp。GurobiError as e: print('Error code ' + str(e.r errno) + ": " + str(e)) except AttributeError: print('遇到属性错误')