Try our new documentation site.

Filter Content By
Version

### dense.py

#!/usr/bin/python

# Copyright 2019, Gurobi Optimization, LLC

# This example formulates and solves the following simple QP model:
#
#    minimize    x + y + x^2 + x*y + y^2 + y*z + z^2
#    subject to  x + 2 y + 3 z >= 4
#                x +   y       >= 1
#                x, y, z non-negative
#
# The example illustrates the use of dense matrices to store A and Q
# (and dense vectors for the other relevant data).  We don't recommend
# that you use dense matrices, but this example may be helpful if you
# already have your data in this format.

import sys
from gurobipy import *

def dense_optimize(rows, cols, c, Q, A, sense, rhs, lb, ub, vtype,
solution):

model = Model()

# Add variables to model
vars = []
for j in range(cols):

# Populate A matrix
for i in range(rows):
expr = LinExpr()
for j in range(cols):
if A[i][j] != 0:
expr += A[i][j]*vars[j]

# Populate objective
for i in range(cols):
for j in range(cols):
if Q[i][j] != 0:
obj += Q[i][j]*vars[i]*vars[j]
for j in range(cols):
if c[j] != 0:
obj += c[j]*vars[j]
model.setObjective(obj)

# Solve
model.optimize()

# Write model to a file
model.write('dense.lp')

if model.status == GRB.Status.OPTIMAL:
x = model.getAttr('x', vars)
for i in range(cols):
solution[i] = x[i]
return True
else:
return False

# Put model data into dense matrices

c = [1, 1, 0]
Q = [[1, 1, 0], [0, 1, 1], [0, 0, 1]]
A = [[1, 2, 3], [1, 1, 0]]
sense = [GRB.GREATER_EQUAL, GRB.GREATER_EQUAL]
rhs = [4, 1]
lb = [0, 0, 0]
ub = [GRB.INFINITY, GRB.INFINITY, GRB.INFINITY]
vtype = [GRB.CONTINUOUS, GRB.CONTINUOUS, GRB.CONTINUOUS]
sol = [0]*3

# Optimize

success = dense_optimize(2, 3, c, Q, A, sense, rhs, lb, ub, vtype, sol)

if success:
print('x: %g, y: %g, z: %g' % (sol[0], sol[1], sol[2]))


Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
##### Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.