Try our new documentation site (beta).
Filter Content By
Version
Text Search
${sidebar_list_label} - Back
Filter by Language
matrix1.py
#!/usr/bin/env python3.7 # Copyright 2023, Gurobi Optimization, LLC # This example formulates and solves the following simple MIP model # using the matrix API: # maximize # x + y + 2 z # subject to # x + 2 y + 3 z <= 4 # x + y >= 1 # x, y, z binary import gurobipy as gp from gurobipy import GRB import numpy as np import scipy.sparse as sp try: # Create a new model m = gp.Model("matrix1") # Create variables x = m.addMVar(shape=3, vtype=GRB.BINARY, name="x") # Set objective obj = np.array([1.0, 1.0, 2.0]) m.setObjective(obj @ x, GRB.MAXIMIZE) # Build (sparse) constraint matrix val = np.array([1.0, 2.0, 3.0, -1.0, -1.0]) row = np.array([0, 0, 0, 1, 1]) col = np.array([0, 1, 2, 0, 1]) A = sp.csr_matrix((val, (row, col)), shape=(2, 3)) # Build rhs vector rhs = np.array([4.0, -1.0]) # Add constraints m.addConstr(A @ x <= rhs, name="c") # Optimize model m.optimize() print(x.X) print('Obj: %g' % m.ObjVal) except gp.GurobiError as e: print('Error code ' + str(e.errno) + ": " + str(e)) except AttributeError: print('Encountered an attribute error')
![](/wp-content/plugins/hd_documentations/content/images/documentation-ampl-guide.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-cloud.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-example-tour.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-quick-start.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-reference-manuals.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/documentation-remote-services.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/quickstart-os-linux.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/quickstart-os-mac-osx.webp?x70725)
![](/wp-content/plugins/hd_documentations/content/images/quickstart-os-windows.webp?x70725)