def main(): """Solve 'tsp_gurobi.mod' using a cut generator.""" from pympl import PyMPL, Tools, glpkutils from gurobipy import GRB, LinExpr, read os.chdir(os.path.dirname(__file__) or os.curdir) mod_in = "tsp_gurobi.mod" mod_out = "tmp/tsp_gurobi.out.mod" graph_size = "large" parser = PyMPL(locals_=locals(), globals_=globals()) parser.parse(mod_in, mod_out) lp_out = "tmp/tsp_gurobi.lp" glpkutils.mod2lp(mod_out, lp_out, verbose=True) m = read(lp_out) m.params.LazyConstraints = 1 m.params.MIPGap = 0 m.params.MIPGapAbs = 1-1e-5 def sep_callback(model, where): """Gurobi callback function.""" if where == GRB.callback.MIPNODE: model._cnt += 1 if model._cnt - model._lastrun < 10: return model._lastrun = model._cnt # check if the submodel was used assert "ATSP_MTZ" in parser.submodels() # calls the separate method to compute valid inequalities cuts = parser["ATSP_MTZ"].separate( lambda name: model.cbGetNodeRel(model.getVarByName(name)) ) # add the cuts to the model if len(cuts) > 0: print("add {0} {1}".format( len(cuts), "cuts" if len(cuts) > 1 else "cut" )) for cut in cuts: lincomb, sign, rhs = cut expr = LinExpr([ (coef, model.getVarByName(var)) for var, coef in lincomb ]) if sign[0] == ">": model.cbLazy(expr >= rhs) elif sign[0] == "<": model.cbLazy(expr <= rhs) else: model.cbLazy(expr == rhs) m._cnt = 0 m._lastrun = float("-inf") m.optimize(sep_callback) print("Objective:", m.ObjVal)
def read_model(filename): """ Read a model using gurobipy. """ m = gp.read(filename) return m
def read_netlib_sif_gurobi(fhandle): tmp_file = tempfile.mktemp(suffix='.mps') with open(tmp_file, 'w') as tmp_handle: content = ''.join([str(s) for s in fhandle if str(s).strip()]) tmp_handle.write(content) fhandle.close() log.debug(tmp_file) problem = gurobipy.read(tmp_file) return problem
def __setstate__(self, repr_dict): with TemporaryFilename(suffix=".lp", content=repr_dict["lp"]) as tmp_file_name: problem = gurobipy.read(tmp_file_name) # if repr_dict['status'] == 'optimal': # TODO: uncomment this # # turn off logging completely, get's configured later # problem.set_error_stream(None) # problem.set_warning_stream(None) # problem.set_log_stream(None) # problem.set_results_stream(None) # problem.solve() # since the start is an optimal solution, nothing will happen here self.__init__(problem=problem) self.configuration = Configuration.clone(repr_dict['config'], problem=self) # TODO: make configuration work
def load_problem(mps_file): prob_tmp_file = tempfile.mktemp(suffix='.mps') with open(prob_tmp_file, 'wb') as tmp_handle: f = gzip.open(mps_file, 'rb') tmp_handle.write(f.read()) f.close() problem = gurobipy.read(prob_tmp_file) model = Model(problem=problem) model.configuration.presolve = True model.configuration.verbosity = 3 model.configuration.timeout = 60 * 9 return problem, model
def read_helper(mm_model, fname, actions, feature_fn, margin_fn): mm_model.actions = actions[:] grb_model = grb.read(fname) mm_model.model = grb_model N = 0 w = [] while grb_model.getVarByName(str(N)) is not None: w.append(grb_model.getVarByName(str(N))) N += 1 mm_model.N = N mm_model.w = np.asarray(w) mm_model.populate_xi() mm_model.weights = np.zeros(N) mm_model.feature_fn = feature_fn mm_model.margin_fn = margin_fn mm_model.constraints_cache = set()
def test_saving_model(mm_model): # Use Gurobi to save the model in MPS format weights = mm_model.optimize_model() mm_model.save_model('data/rope_model_saved_test.mps') mm_model_saved = grb.read('data/rope_model_saved_test.mps') mm_model_saved.optimize() weights_from_saved = [x.X for x in mm_model.w] saved_correctly = True for i in range(len(weights)): if weights[i] != weights_from_saved[i]: print "ERROR in saving model: Orig weight: ", weights[i], \ ", Weight from optimizing saved model: ", weights_from_saved[i] saved_correctly = False if saved_correctly: print "PASSED: Model saved and reloaded correctly"
def read_helper(mm_model, fname, actions, num_features): mm_model.actions = actions[:] grb_model = grb.read(fname) mm_model.model = grb_model w = [] for var in mm_model.model.getVars(): try: if var.VarName == 'w0': mm_model.w0 = var continue int(var.VarName) w.append(var) except ValueError: pass mm_model.N = len(w) assert mm_model.N == num_features, "Number of features in the model mismatches the number of the feature vector" mm_model.w = np.asarray(w) mm_model.populate_slacks() mm_model.weights = np.zeros(len(w))
def solve_ilp(filename, timelimit=60): # import here, so only if actually solving we will need gurobi. from gurobipy import read, GRB # pycharm complains of gurobi commands, cannot see them from the import model = read(filename) # set some options: # time limit in seconds: model.params.timeLimit = timelimit # not verbose: # model.setParam('OutputFlag', False) # MIP focus, from 0 to 3: model.params.MIPFocus = 1 # best solutions, less focus on bounds. model.optimize() if model.status != GRB.Status.INFEASIBLE: print('FINISHED: Best objective: %g' % model.objVal) print('Optimization ended with status %d' % model.status) model.write(filename + '.sol') if model.status == GRB.INFEASIBLE: model.computeIIS() model.write("unfeasible.lp") print('\nThe following constraint(s) cannot be satisfied:') for c in model.getConstrs(): if c.IISConstr: print('%s' % c.constrName) else: z = 0 n = 0 for v in model.getVars(): if v.varName == "n": n = v.x if v.varName.startswith("z") and v.x == 1: z += 1 print "N: %d cycles:%d" % (n, z) return model
def solve_ilp(timelimit=60): # import here, so only if actually solving we will need gurobi. from gurobipy import read, GRB # pycharm complains of gurobi commands, cannot see them from the import model = read(filename) # set some options: # time limit in seconds: model.params.timeLimit = timelimit # not verbose: # model.setParam('OutputFlag', False) # MIP focus, from 0 to 3: model.params.MIPFocus = 1 # best solutions, less focus on bounds. model.optimize() if model.status != GRB.Status.INFEASIBLE: print('FINISHED: Best objective: %g' % model.objVal) print('Optimization ended with status %d' % model.status) model.write(filename + '.sol') if model.status == GRB.INFEASIBLE: model.computeIIS() model.write("unfeasible.lp") print('\nThe following constraint(s) cannot be satisfied:') for c in model.getConstrs(): if c.IISConstr: print('%s' % c.constrName) else: z = n = c = 0 solution_matching = collections.defaultdict(list) matching_regexp = re.compile("x_A(\d+)_(\d+)h,B(\d+)_(\d+)h") # get basic vars and matching: for v in model.getVars(): if v.varName == "n": n = v.x elif v.varName == "c": c = v.x elif v.varName.startswith("z") and v.x >= 0.9: z += 1 else: m = matching_regexp.match(v.varName) if m is not None and v.x == 1: g_a, c_a, g_b, c_b = map(int, m.groups()) solution_matching[g_a].append((c_a, c_b)) from parse_orthology import build_correct_matching, parse_orthology_quality correct_matching = build_correct_matching(genome_a, genome_b) tp, fp, fn = parse_orthology_quality(solution_matching, correct_matching) print "N: %d cycles:%d (%d fixed, %d from opt)" % (n, z + c, c, z) print "Orthology. TP:%d FP:%d FN:%d" % (len(tp), len(fp), len(fn)) # print match_edges # Now, analyse the BP graph, for the incomplete matching model, to find AA-, BB- and AB- components: master_graph = nx.Graph() # fixed vars: # add matching edges of genes with single copy: # for (gene, copy_a), copy_j in match_edges.iteritems(): for gene, pair_list in solution_matching.iteritems(): for copy_a, copy_b in pair_list: for ext in [Ext.HEAD, Ext.TAIL]: master_graph.add_edge(("A", gene, copy_a, ext), ("B", gene, copy_b, ext)) # add adjacency edges: for genome, genome_name in [(genome_a, "A"), (genome_b, "B")]: for (g_i, copy_a, e_i), (g_j, copy_j, e_j) in genome.adjacency_iter_with_copies(): master_graph.add_edge((genome_name, g_i, copy_a, e_i), (genome_name, g_j, copy_j, e_j)) count = {"A": 0, "B": 0, "AB": 0} c = 0 # print "C:", len([x for x in connected_components(master_graph)]) for comp in connected_components(master_graph): degree_one = [v for v in comp if master_graph.degree(v) == 1] if len(degree_one) == 0: c += 1 else: if len(degree_one) != 2: import ipdb; ipdb.set_trace() if degree_one[0][0] == degree_one[1][0]: count[degree_one[0][0]] += 1 else: count["AB"] += 1 print count if skip_balancing: print "Corrected distance: %d" % (model.objVal + count["AB"] / 2) return model
import gurobipy model = gurobipy.read('/Library/gurobi651/mac64/examples/data//afiro.mps') model.optimize() model.printAttr('X')
ub[i_var] = var.getAttr("UB") lb[i_var] = var.getAttr("LB") name[i_var] = var.getAttr("VarName") return H,f,A,b,C,d,lb, ub, name, eqcntname, ineqcntname def test_solve(): n_vars = 3 H = np.eye(n_vars) f = np.ones(n_vars) C = np.ones((1,n_vars)) d = np.ones(1) * 100 lb = np.ones(n_vars)*0 ub = np.ones(n_vars)*3 #A, B, ub, lb, start xsol = solve_iqp(H, f, C, d, lb, ub,np.ones(n_vars)) print xsol if __name__ == "__main__": #test_gurobi() #def test_convert(): import gurobipy model = gurobipy.read("arm_table.mps") #model.optimize() H,f,A,b,C,d, lb, ub, varname, eqcntname, ineqcntname = convert_gurobi_model(model) xopt = solve_qp(H,f,A,b,C,d,lb, ub) model.optimize() vals = np.array([var.getAttr(gurobipy.GRB.attr.X) for var in model.getVars()]) print vals[0*7:(4*7)] - xopt[1*7:5*7]
# Copyright 2020, Gurobi Optimization, LLC # Solve a model with different values of the Method parameter; # show which value gives the shortest solve time. import sys import gurobipy as gp from gurobipy import GRB if len(sys.argv) < 2: print('Usage: lpmethod.py filename') sys.exit(0) # Read model m = gp.read(sys.argv[1]) # Solve the model with different values of Method bestTime = m.Params.timeLimit bestMethod = -1 for i in range(3): m.reset() m.Params.method = i m.optimize() if m.status == GRB.OPTIMAL: bestTime = m.Runtime bestMethod = i # Reduce the TimeLimit parameter to save time with other methods m.Params.timeLimit = bestTime # Report which method was fastest
def run_gurobi( Model, io_api=None, problem_fn=None, solution_fn=None, log_fn=None, warmstart_fn=None, basis_fn=None, keep_files=False, **solver_options, ): """ Solve a linear problem using the gurobi solver. This function communicates with gurobi using the gurubipy package. """ # disable logging for this part, as gurobi output is doubled otherwise logging.disable(50) log_fn = maybe_convert_path(log_fn) warmstart_fn = maybe_convert_path(warmstart_fn) basis_fn = maybe_convert_path(basis_fn) if io_api is None or (io_api == "lp"): problem_fn = Model.to_file(problem_fn) problem_fn = maybe_convert_path(problem_fn) m = gurobipy.read(problem_fn) else: problem_fn = None m = gurobipy.Model() lower = Model.variables.ravel("lower", filter_missings=True) upper = Model.variables.ravel("upper", filter_missings=True) xlabels = Model.variables.ravel("labels", filter_missings=True) names = "v" + xlabels.astype(str).astype(object) kwargs = {} if len(Model.binaries.labels): specs = { name: "B" if name in Model.binaries else "C" for name in Model.variables } specs = Dataset({k: DataArray(v) for k, v in specs.items()}) kwargs["vtype"] = Model.variables.ravel(specs, filter_missings=True) x = m.addMVar(xlabels.shape, lower, upper, name=list(names), **kwargs) coeffs = np.zeros(Model._xCounter) coeffs[np.asarray(Model.objective.vars)] = np.asarray( Model.objective.coeffs) m.setObjective(coeffs[xlabels] @ x) A = Model.constraints.to_matrix(filter_missings=True) sense = Model.constraints.ravel("sign", filter_missings=True).astype( np.dtype("<U1")) b = Model.constraints.ravel("rhs", filter_missings=True) clabels = Model.constraints.ravel("labels", filter_missings=True) names = "c" + clabels.astype(str).astype(object) c = m.addMConstr(A, x, sense, b) c.setAttr("ConstrName", list(names)) m.update() if solver_options is not None: for key, value in solver_options.items(): m.setParam(key, value) if log_fn is not None: m.setParam("logfile", log_fn) if warmstart_fn: m.read(warmstart_fn) m.optimize() logging.disable(1) if basis_fn: try: m.write(basis_fn) except gurobipy.GurobiError as err: logger.info("No model basis stored. Raised error: ", err) Status = gurobipy.GRB.Status statusmap = { getattr(Status, s): s.lower() for s in Status.__dir__() if not s.startswith("_") } termination_condition = statusmap[m.status] if termination_condition == "optimal": status = "ok" elif termination_condition == "suboptimal": status = "warning" elif termination_condition == "inf_or_unbd": status = "warning" termination_condition = "infeasible or unbounded" else: status = "warning" if termination_condition not in ["optimal", "suboptimal"]: return dict( status=status, termination_condition=termination_condition, model=m, ) objective = m.ObjVal solution = pd.Series({v.VarName: v.x for v in m.getVars()}) solution = set_int_index(solution) try: dual = pd.Series({c.ConstrName: c.Pi for c in m.getConstrs()}) dual = set_int_index(dual) except AttributeError: logger.warning("Shadow prices of MILP couldn't be parsed") dual = None return dict( status=status, termination_condition=termination_condition, solution=solution, dual=dual, objective=objective, model=m, )
def run_and_read_gurobi(n, problem_fn, solution_fn, solver_logfile, solver_options, warmstart=None, store_basis=True): """ Solving function. Reads the linear problem file and passes it to the gurobi solver. If the solution is sucessful it returns variable solutions and constraint dual values. Gurobipy must be installed for using this function For more information on solver options: https://www.gurobi.com/documentation/{gurobi_verion}/refman/parameter_descriptions.html """ if find_spec('gurobipy') is None: raise ModuleNotFoundError( "Optional dependency 'gurobipy' not found. " "Install via 'conda install -c gurobi gurobi' or follow the " "instructions on the documentation page " "https://www.gurobi.com/documentation/") import gurobipy # disable logging for this part, as gurobi output is doubled otherwise logging.disable(50) m = gurobipy.read(problem_fn) if solver_options is not None: for key, value in solver_options.items(): m.setParam(key, value) if solver_logfile is not None: m.setParam("logfile", solver_logfile) if warmstart: m.read(warmstart) m.optimize() logging.disable(1) if store_basis: n.basis_fn = solution_fn.replace('.sol', '.bas') try: m.write(n.basis_fn) except gurobipy.GurobiError: logger.info('No model basis stored') del n.basis_fn Status = gurobipy.GRB.Status statusmap = { getattr(Status, s): s.lower() for s in Status.__dir__() if not s.startswith('_') } termination_condition = statusmap[m.status] if termination_condition == "optimal": status = 'ok' elif termination_condition == 'suboptimal': status = 'warning' elif termination_condition == "infeasible": status = 'warning' elif termination_condition == "inf_or_unbd": status = 'warning' termination_condition = "infeasible or unbounded" else: status = "warning" if termination_condition not in ["optimal", "suboptimal"]: return status, termination_condition, None, None, None variables_sol = pd.Series({v.VarName: v.x for v in m.getVars()}).pipe(set_int_index) try: constraints_dual = pd.Series( {c.ConstrName: c.Pi for c in m.getConstrs()}).pipe(set_int_index) except AttributeError: logger.warning("Shadow prices of MILP couldn't be parsed") constraints_dual = pd.Series( index=[c.ConstrName for c in m.getConstrs()]) objective = m.ObjVal del m return (status, termination_condition, variables_sol, constraints_dual, objective)
# -*- coding: utf-8 -*- """ Created on Thu Sep 24 13:09:16 2020 @author: Pramesh Kumar """ import gurobipy as gp from gurobipy import GRB model = gp.read('model.lp') model.Params.outputFlag = 0 model.Params.DualReductions = 0 model.optimize() if model.status == GRB.OPTIMAL: print('model is optimal') elif model.status == GRB.INFEASIBLE: print('model is infeasible') elif model.status == GRB.UNBOUNDED: print('model is unbounded') else: print('Not sure') #model.Params.InfUnbdInfo = 1 #model.computeIIS() #model.write("infeasible_model.lp") print('The otimal value of the model is', model.objVal)
def from_lp(cls, lp_form): with TemporaryFilename(suffix=".lp", content=lp_form) as tmp_file_name: problem = gurobipy.read(tmp_file_name) model = cls(problem=problem) return model
import itertools import pickle # Read in dictionaries and recreate arrays ------------------------ family_choices = pickle.load( open("attempt_01/artifacts/family_choices.pkl", "rb")) preference_cost = pickle.load( open("attempt_01/artifacts/preference_cost.pkl", "rb")) family_members = pickle.load( open("attempt_01/artifacts/family_members.pkl", "rb")) family = list(range(0, 5000)) days = list(range(1, 101)) # Initiate model -------------------------------------------------- tour_model = grb.read('attempt_01/artifacts/tour_model.lp') tour_model.update() # Read in optimal solution ---------------------------------------- tour_model.read('attempt_01/outputs/tour_initial_solution.sol') tour_model.update() # Rebuild visit dictionary ---------------------------------------- all_vars = tour_model.getVars() visit = {} for v in all_vars: if 'x_' in v.varname: var_string = v.varname.replace('x_', '') var_split = var_string.split('_') f = int(var_split[0])
results = opt.solve(linear, keepfiles=False) DIR = 'products/' os.makedirs(DIR, exist_ok=True) linear.display(filename=DIR + 'linear_solution.txt') ############################################### # 4. Dual variables & 5. Sensitivity analysis # ############################################### linear.write(filename=DIR + 'linear_model.lp', format=ProblemFormat.cpxlp, io_options={'symbolic_solver_labels': True}) gmodel = read('products/linear_model.lp') gmodel.optimize() constrs = {'Name': [], 'Dual': [], 'Slack': [], 'Lower': [], 'Upper': []} for c in gmodel.getConstrs(): constrs['Name'].append(c.ConstrName) constrs['Dual'].append(c.Pi) constrs['Slack'].append(c.Slack) constrs['Lower'].append(c.SARHSLow) constrs['Upper'].append(c.SARHSUp) constrs = pd.DataFrame(constrs) with open(DIR + 'linear_constraints.txt', 'w') as f: pd.set_option('display.max_rows', None)
lhs = lhs - aux_var rhs = constraint.ub self.problem.addConstr(lhs, sense, rhs, name=constraint.name) else: raise ValueError( "GUROBI currently only supports linear constraints. %s is not linear." % self) # self.problem.addQConstr(lhs, sense, rhs) constraint.problem = self def _remove_constraints(self, constraints): self.problem.update() internal_constraints = [constraint._internal_constraint for constraint in constraints] super(Model, self)._remove_constraints(constraints) for internal_constraint in internal_constraints: self.problem.remove(internal_constraint) if __name__ == '__main__': x = Variable('x', lb=0, ub=10) y = Variable('y', lb=0, ub=10) z = Variable('z', lb=-100, ub=99.) constr = Constraint(0.3 * x + 0.4 * y + 66. * z, lb=-100, ub=0., name='test') solver = Model(problem=gurobipy.read("tests/data/model.lp")) solver.add(z) solver.add(constr) print(solver) print(solver.optimize()) print(solver.status)
def __setstate__(self, repr_dict): tmp_file = tempfile.mktemp(suffix=".lp") open(tmp_file, 'w').write(repr_dict['cplex_form']) self.problem = read(tmp_file) self.var_ids = repr_dict['var_ids'] self.constr_ids = repr_dict['constr_ids']
### TODO: set assigned to current timestamp if not args.quiet: print " Problem: \033[1;33m", row[0], "\033[0m" cur.close() ### ### Computation ### # TODO: write log to appropriate file (not console) start_time = time.time() * 1000.0 model = gurobipy.read(directory + row[8]) model.params.threads = 1 model.params.MIPfocus = 3 model.optimize() elapsed_time = time.time() * 1000.0 - start_time if model.Status == gurobipy.GRB.OPTIMAL: solution_type = 'FEASIBLE' elif model.Status == gurobipy.GRB.INFEASIBLE: print "\033[1mError:\033[0m\033[1;31m model is INFEASIBLE.\033[0m" exit(3) elif model.Status == gurobipy.GRB.INF_OR_UNBD: print "\033[1mError:\033[0m\033[1;31m model is UNBOUNDED.\033[0m" exit(3) else: print "\n\033[1;33mComputation interrupted, terminating.\033[0m\n"
def setUp(self): problem = gurobipy.read(TESTMODELPATH) self.model = Model(problem=problem) self.obj = self.model.objective
self.problem.update() internal_constraints = [ constraint._internal_constraint for constraint in constraints ] super(Model, self)._remove_constraints(constraints) for internal_constraint in internal_constraints: self.problem.remove(internal_constraint) @property def is_integer(self): self.problem.update() return self.problem.NumIntVars > 0 if __name__ == '__main__': x = Variable('x', lb=0, ub=10) y = Variable('y', lb=0, ub=10) z = Variable('z', lb=-100, ub=99.) constr = Constraint(0.3 * x + 0.4 * y + 66. * z, lb=-100, ub=0., name='test') solver = Model(problem=gurobipy.read("tests/data/model.lp")) solver.add(z) solver.add(constr) print(solver) print(solver.optimize()) print(solver.status)
hydro_path = os.path.dirname(os.path.realpath(__file__)) parent_path = os.path.abspath(os.path.join(hydro_path, os.pardir)) sys.path.append(parent_path) os.chdir(hydro_path) def gv(m, n): var = m.getVarByName(n) print(var) return var ''' Read model at iteration 3 and its basis. ''' m3 = read('./sp0_iter3.mps') m3.read('./sp0_iter3.bas') m3.optimize() #Print solutions for v in m3.getVars(): print(v.VarName, v.X, v.RC, v.LB, v.UB) #Manually add the cut #Add (benders) cut cut_exp = 80 * gv(m3, 'reservoir_level[0]') + 25 * gv( m3, 'reservoir_level[1]') + 52.88232369018938 * gv( m3, 'inflow[0,1]') + 8.310453412619006 * gv( m3, 'inflow[1,1]') + 1.0 * gv(m3, 'oracle[0][0]') print(cut_exp) m3.addConstr(cut_exp >= 23918.67191367184, 'cut4') m3.optimize()
import gurobipy as grb # Load a model print("Loading a model") model = grb.read("/apps/software/vendor/gurobi/8.0.1/examples/data/afiro.mps") model.optimize() model.printAttr('X') print("Creating a new model") m = grb.Model() # Variables x = m.addVar(vtype=grb.GRB.BINARY, name = "x") y = m.addVar(vtype=grb.GRB.BINARY, name = "y") z = m.addVar(vtype=grb.GRB.BINARY, name = "z") # Objectives m.setObjective(x + y + 2 * z, grb.GRB.MAXIMIZE) # Constraints c1 = m.addConstr(2 * x + y + 3 * z <= 4) c2 = m.addConstr(x + y >= 1) # Solve
def test_gurobi(): m = gurobipy.read("C:\gurobi652\win64\examples\data\coins.lp"); m.optimize(); m.printAttr('X');
Created on Thu Feb 6 16:34:28 2020 @author: yj """ import sys import gurobipy as gp from gurobipy import GRB #%% if len(sys.argv) < 2: print('Usage: lp.py filename') # Read and solve the model model = gp.read(sys.argv[1]) model.optimize() if model.status == GRB.INF_OR_UNBD: # Turn presolve off to determine whether model is infeasible # or unbounded model.setParam(GRB.Param.Presolve, 0) model.optimize() if model.status == GRB.OPTIMAL: print('Optimal objective: %g' % model.objVal) model.write('model.sol') sys.exit(0) elif model.status != GRB.INFEASIBLE: print('Optimization was stopped with status %d' % model.status)