Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

gurobi python example

#!/usr/bin/env python3.7

# Copyright 2021, 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')
Comment

PREVIOUS NEXT
Code Example
Python :: Format UTC to local timezone using PYTZ for Django 
Python :: how to run shell command ctrl + c in python script 
Python :: to str python 
Python :: Python Difference between two dates and times 
Python :: binary to string python 
Python :: information of environment variables in python 
Python :: how to find the path of a python module 
Python :: get a column of a csv python 
Python :: python tkinter label widget 
Python :: hashmap python 
Python :: for loop to convert a list to lowercase 
Python :: pandas insert row 
Python :: Invalid password format or unknown hashing algorithm. 
Python :: global in python 
Python :: python find index of an item in an array 
Python :: lasso regression 
Python :: Scrapping tables in an HTML file with BeautifulSoup 
Python :: pandas convert string to datetime 
Python :: formula of factorial 
Python :: np.eye 
Python :: I have string index in pandas DataFrame how can I select by startswith? 
Python :: fakultät python 
Python :: docker flask 
Python :: set python 3 as default mac 
Python :: raw query in django 
Python :: extract all capital words dataframe 
Python :: python password generation 
Python :: flask sending post request 
Python :: A Python Class Constructor 
Python :: How to get the date from week number in Python? 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =