Try our new documentation site (beta).
Filter Content By
Version
Text Search
${sidebar_list_label} - Back
Filter by Language
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)