Try our new documentation site (beta).


Source code for the experiment with column scalings


import sys
import random
import argparse
from gurobipy import *

# Use parameters for greater flexibility
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-f','--infile', help='Problem File',
                    default=None, required=True)
parser.add_argument('-s','--scale', help='Scaling Factor',
                    type=float, default=10000.0)
parser.add_argument('-w','--outfile', help='Save scaled model',
                    default=None)
parser.add_argument('-o','--optimize', help='Optimize scaled problem',
                    type=int, default=1)
args = parser.parse_args()

# Load input problem
m = read(args.infile)
 
# Scale domain of all columns randomly in the given domain
for var in m.getVars():
  if var.vtype == GRB.CONTINUOUS:
    scale = random.uniform(args.scale/2.0, args.scale*2.0)
    flip = random.randint(0,3)
    if flip == 0:
      scale = 1.0
    elif flip == 1:
      scale = 1.0/scale
    col = m.getCol(var)
    for i in range(col.size()):
      coeff = col.getCoeff(i)
      row   = col.getConstr(i)
      m.chgCoeff(row, var, coeff*scale)
    var.obj = var.obj*scale
    if var.lb > -GRB.INFINITY:
      var.lb  = var.lb/scale
    if var.ub < GRB.INFINITY:
      var.ub  = var.ub/scale

if args.outfile != None:
  m.write(args.outfile)

# Optimize
if args.optimize:
  m.optimize()
  if m.Status == GRB.OPTIMAL:
    print('Kappa: %e\n' % m.KappaExact)

Try Gurobi for Free

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

Evaluation License
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.
Academic License
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.

Search

Gurobi Optimization