本文整理汇总了Python中gurobipy.LinExpr方法的典型用法代码示例。如果您正苦于以下问题:Python gurobipy.LinExpr方法的具体用法?Python gurobipy.LinExpr怎么用?Python gurobipy.LinExpr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gurobipy
的用法示例。
在下文中一共展示了gurobipy.LinExpr方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fit
# 需要导入模块: import gurobipy [as 别名]
# 或者: from gurobipy import LinExpr [as 别名]
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)]
示例2: _add_cut
# 需要导入模块: import gurobipy [as 别名]
# 或者: from gurobipy import LinExpr [as 别名]
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()
示例3: addConstraint
# 需要导入模块: import gurobipy [as 别名]
# 或者: from gurobipy import LinExpr [as 别名]
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)
示例4: buildSolverModel
# 需要导入模块: import gurobipy [as 别名]
# 或者: from gurobipy import LinExpr [as 别名]
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()
示例5: read_cuts
# 需要导入模块: import gurobipy [as 别名]
# 或者: from gurobipy import LinExpr [as 别名]
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()
示例6: buildSolverModel
# 需要导入模块: import gurobipy [as 别名]
# 或者: from gurobipy import LinExpr [as 别名]
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()