当前位置: 首页>>代码示例>>Python>>正文


Python gurobipy.LinExpr方法代码示例

本文整理汇总了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)] 
开发者ID:lingquant,项目名称:msppy,代码行数:20,代码来源:statistics.py

示例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() 
开发者ID:lingquant,项目名称:msppy,代码行数:10,代码来源:sp.py

示例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) 
开发者ID:QuantEcon,项目名称:MatchingMarkets.py,代码行数:28,代码来源:solvers.py

示例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() 
开发者ID:QuantEcon,项目名称:MatchingMarkets.py,代码行数:47,代码来源:solvers.py

示例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() 
开发者ID:lingquant,项目名称:msppy,代码行数:50,代码来源:msp.py

示例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() 
开发者ID:SanPen,项目名称:GridCal,代码行数:54,代码来源:gurobi.py


注:本文中的gurobipy.LinExpr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。