Python gurobipy.LinExpr() Examples
The following are 6
code examples of gurobipy.LinExpr().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
gurobipy
, or try the search function
.
Example #1
Source File: statistics.py From msppy with BSD 3-Clause "New" or "Revised" License | 8 votes |
def fit(array, convex=1): """Fit a smooth line to the given time-series data""" N = len(array) m = gurobipy.Model() fv = m.addVars(N) if convex == 1: m.addConstrs(fv[i] <= fv[i-1] for i in range(1,N)) m.addConstrs(fv[i] + fv[i-2] >= 2*fv[i-1] for i in range(2,N)) else: m.addConstrs(fv[i] >= fv[i-1] for i in range(1,N)) m.addConstrs(fv[i] + fv[i-2] <= 2*fv[i-1] for i in range(2,N)) m.setObjective( gurobipy.quicksum([fv[i] * fv[i] for i in range(N)]) - 2 * gurobipy.LinExpr(array,fv.values()) ) m.Params.outputFlag = 0 m.optimize() return [fv[i].X for i in range(N)]
Example #2
Source File: solvers.py From MatchingMarkets.py with BSD 3-Clause "New" or "Revised" License | 5 votes |
def addConstraint(self, constraint, name = None): if not isinstance(constraint, LpConstraint): raise TypeError("Can only add LpConstraint objects") if name: constraint.name = name try: if constraint.name: name = constraint.name else: name = self.unusedConstraintName() except AttributeError: raise TypeError("Can only add LpConstraint objects") #if self._addVariables(constraint.keys()): #self.gurobi_model.update() expr = gurobipy.LinExpr(constraint.values(), [v.solver_var for v in constraint.keys()]) # Solver_var is added inside addVariable if constraint.sense == LpConstraintLE: relation = gurobipy.GRB.LESS_EQUAL elif constraint.sense == LpConstraintGE: relation = gurobipy.GRB.GREATER_EQUAL elif constraint.sense == LpConstraintEQ: relation = gurobipy.GRB.EQUAL else: raise PulpSolverError('Detected an invalid constraint type') self.gurobi_model.addConstr(expr, relation, -constraint.constant, name)
Example #3
Source File: sp.py From msppy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _add_cut(self, rhs, gradient): temp = gurobipy.LinExpr(gradient, self.states) self.cuts.append( self._model.addConstr( self.modelSense * (self.alpha - temp - rhs) >= 0 ) ) self._model.update()
Example #4
Source File: solvers.py From MatchingMarkets.py with BSD 3-Clause "New" or "Revised" License | 4 votes |
def buildSolverModel(self, lp): """ Takes the pulp lp model and translates it into a gurobi model """ log.debug("create the gurobi model") lp.solverModel = gurobipy.Model(lp.name) log.debug("set the sense of the problem") if lp.sense == LpMaximize: lp.solverModel.setAttr("ModelSense", -1) if self.timeLimit: lp.solverModel.setParam("TimeLimit", self.timeLimit) if self.epgap: lp.solverModel.setParam("MIPGap", self.epgap) log.debug("add the variables to the problem") for var in lp.variables(): lowBound = var.lowBound if lowBound is None: lowBound = -gurobipy.GRB.INFINITY upBound = var.upBound if upBound is None: upBound = gurobipy.GRB.INFINITY obj = lp.objective.get(var, 0.0) varType = gurobipy.GRB.CONTINUOUS if var.cat == LpInteger and self.mip: varType = gurobipy.GRB.INTEGER var.solverVar = lp.solverModel.addVar(lowBound, upBound, vtype = varType, obj = obj, name = var.name) lp.solverModel.update() log.debug("add the Constraints to the problem") for name,constraint in lp.constraints.items(): #build the expression expr = gurobipy.LinExpr(list(constraint.values()), [v.solverVar for v in constraint.keys()]) if constraint.sense == LpConstraintLE: relation = gurobipy.GRB.LESS_EQUAL elif constraint.sense == LpConstraintGE: relation = gurobipy.GRB.GREATER_EQUAL elif constraint.sense == LpConstraintEQ: relation = gurobipy.GRB.EQUAL else: raise PulpSolverError('Detected an invalid constraint type') constraint.solverConstraint = lp.solverModel.addConstr(expr, relation, -constraint.constant, name) lp.solverModel.update()
Example #5
Source File: msp.py From msppy with BSD 3-Clause "New" or "Revised" License | 4 votes |
def read_cuts(self, path): """Read all cuts from csv files. csv files takes the form of: x.varName | y.varName | rhs a | b | c which specifies cut: alpha + ax + by >= c in minimization problem alpha + ax + by <= c in maximization problem Parameters ---------- path: string The location to read csv files """ self._update() for t in range(self.T - 1): m = self.models[t] if type(m) != list: coeffs = pandas.read_csv( path + "{}.csv".format(t), index_col=0 ).values for coeff in coeffs: m.addConstr( ( m.alpha + gurobipy.LinExpr(coeff[:-1], m.states) - coeff[-1] ) * m.modelsense >= 0 ) m.update() else: for k, m in enumerate(m): coeffs = pandas.read_csv( path + "{}_{}.csv".format(t, k), index_col=0 ).values for coeff in coeffs: m.addConstr( ( m.alpha + gurobipy.LinExpr(coeff[:-1], m.states) - coeff[-1] ) * m.modelsense >= 0 ) m.update()
Example #6
Source File: gurobi.py From GridCal with GNU General Public License v3.0 | 4 votes |
def buildSolverModel(self, lp): """ Takes the pulp lp model and translates it into a gurobi model """ log.debug("create the gurobi model") lp.solverModel = gurobipy.Model(lp.name) log.debug("set the sense of the problem") if lp.sense == LpMaximize: lp.solverModel.setAttr("ModelSense", -1) if self.timeLimit: lp.solverModel.setParam("TimeLimit", self.timeLimit) if self.epgap: lp.solverModel.setParam("MIPGap", self.epgap) log.debug("add the variables to the problem") for var in lp.variables(): lowBound = var.lowBound if lowBound is None: lowBound = -gurobipy.GRB.INFINITY upBound = var.upBound if upBound is None: upBound = gurobipy.GRB.INFINITY obj = lp.objective.get(var, 0.0) varType = gurobipy.GRB.CONTINUOUS if var.cat == LpInteger and self.mip: varType = gurobipy.GRB.INTEGER var.solverVar = lp.solverModel.addVar(lowBound, upBound, vtype=varType, obj=obj, name=var.name) lp.solverModel.update() log.debug("add the Constraints to the problem") for name, constraint in lp.constraints.items(): # build the expression expr = gurobipy.LinExpr(list(constraint.values()), [v.solverVar for v in constraint.keys()]) if constraint.sense == LpConstraintLE: relation = gurobipy.GRB.LESS_EQUAL elif constraint.sense == LpConstraintGE: relation = gurobipy.GRB.GREATER_EQUAL elif constraint.sense == LpConstraintEQ: relation = gurobipy.GRB.EQUAL else: raise PulpSolverError('Detected an invalid constraint type') constraint.solverConstraint = lp.solverModel.addConstr(expr, relation, -constraint.constant, name) lp.solverModel.update()