Exemplo n.º 1
0
 def slack_variable(self) -> LpVariable:
     """ Getter for the slack variable of the problem. Sets if does not exist. """
     self._slack_variable = (self._slack_variable or LpVariable(
         name=self.name + '_slack_variable',
         var_type=VarType.Continuous,
         low_bound=0)) if self.slack else None
     return self._slack_variable
Exemplo n.º 2
0
    def add_variable(var: LpVariable, obj_coef: Numeric,
                     model: gurobipy.Model) -> None:
        """ Add a variable to the LP

        Parameters
        ----------
        var:
            The linear variable to add
        obj_coef:
            The coefficient of the linear variable in the objective
        model:
            The gurobi model to add the variable to
        """
        low_bound = var.low_bound
        if low_bound is None:
            low_bound = -gurobipy.GRB.INFINITY
        up_bound = var.up_bound
        if up_bound is None:
            up_bound = gurobipy.GRB.INFINITY
        if var.var_type == VarType.Continuous:
            var_type = gurobipy.GRB.CONTINUOUS
        else:
            var_type = gurobipy.GRB.INTEGER
        var.solver_var = model.addVar(low_bound,
                                      up_bound,
                                      vtype=var_type,
                                      obj=obj_coef,
                                      name=var.name)
Exemplo n.º 3
0
    def test_add_variable(self, problem, x):
        problem.add_variable(x)
        assert problem.lp_variables == {'x': x}

        with pytest.raises(Exception) as e:
            problem.add_variable('123')
        assert e.value.args == ('123 is not an LpVariable', )

        x2 = LpVariable('x')
        with pytest.raises(Exception) as e:
            problem.add_variable(x2)
        assert e.value.args == (
            'LP variable name x conflicts with an existing LP variable', )
Exemplo n.º 4
0
 def test_bound_error(self):
     z = LpVariable('z')
     z.low_bound = 2
     with pytest.raises(ValueError) as e:
         z.up_bound = 1
     assert 'cannot be below lower bound' in str(e.value)
     z.up_bound = 4
     with pytest.raises(ValueError) as e:
         z.low_bound = 5
     assert 'cannot be above upper bound' in str(e.value)
     z.low_bound = 1
     assert z.low_bound == 1
     assert z.up_bound == 4
Exemplo n.º 5
0
    def test_write_long(self, problem, x):
        a = LpVariable('a', low_bound=0, up_bound=10, var_type=VarType.Integer)
        b = LpVariable('b', low_bound=0, up_bound=10, var_type=VarType.Integer)
        c = LpVariable('c', low_bound=0, up_bound=10, var_type=VarType.Integer)
        d = LpVariable('d', low_bound=0, up_bound=10, var_type=VarType.Integer)
        e = LpVariable('e', var_type=VarType.Binary)
        f = LpVariable('f', var_type=VarType.Binary)
        g = LpVariable('g', var_type=VarType.Binary)
        h = LpVariable('h', var_type=VarType.Binary)
        vars = [a, b, c, d, e, f, g, h]

        # make sure objective is long enough to test the line break
        objective = LpObjective(name='minimize_cpm',
                                expression={v: 3.1415926535
                                            for v in vars},
                                constant=8)

        rhs = LpExpression('rhs', {a: 1000, b: 1000, c: 1000, d: 1000})
        lhs = LpExpression('lhs', {}, -2)
        constraint = LpConstraint(rhs, 'geq', lhs, 'constraint')

        problem.add_constraint(constraint)
        problem.set_objective(objective)
        buffer = StringIO()
        problem.write_lp(buffer)
        lp_str = buffer.getvalue()
        assert lp_str.split('\n') == [
            '\\* test_problem *\\', 'Minimize',
            'minimize_cpm: 3.1415926535 a + 3.1415926535 b + 3.1415926535 c + 3.1415926535 d',
            '+ 3.1415926535 e + 3.1415926535 f + 3.1415926535 g + 3.1415926535 h + 8',
            'Subject To',
            'constraint: 1000 a + 1000 b + 1000 c + 1000 d >= -2', 'Bounds',
            '0 <= a <= 10', '0 <= b <= 10', '0 <= c <= 10', '0 <= d <= 10',
            '0 <= e <= 1', '0 <= f <= 1', '0 <= g <= 1', '0 <= h <= 1',
            'Generals', 'a', 'b', 'c', 'd', 'Binaries', 'e', 'f', 'g', 'h',
            'End'
        ]
Exemplo n.º 6
0
    def _find_variable(cls, variables: Mapping[str, LpVariable],
                       var_name: str) -> LpVariable:
        """ Find a variable from the variable dictionary, create a new one if not found

        Parameters
        ----------
        variables: dict(str -> LpVariable)
            Mapping from variable names to LpVariable

        var_name: str
            Name of the variable

        Returns
        -------
        LpVariable
            LpVariable found or created
        """

        try:
            return variables[var_name]
        except KeyError:
            var = LpVariable(var_name)
            variables[var_name] = var
            return var
Exemplo n.º 7
0
def z():
    return LpVariable(name='z')
Exemplo n.º 8
0
def x():
    return LpVariable('x', low_bound=0, up_bound=10)
Exemplo n.º 9
0
def y():
    return LpVariable('y', low_bound=0, up_bound=5)