Example #1
0
    def test_power(self, problem):
        ctx = ProblemContext(problem)
        r = McCormickExpressionRelaxation()

        power = problem.constraint('power').root_expr
        ctx.set_polynomiality(power, PolynomialDegree(2))

        assert r.can_relax(problem, power, ctx)
Example #2
0
    def test_trilinear_terms(self, problem):
        ctx = ProblemContext(problem)
        r = McCormickExpressionRelaxation()

        trilinear = problem.constraint('trilinear').root_expr
        ctx.set_polynomiality(trilinear, PolynomialDegree(3))

        assert not r.can_relax(problem, trilinear, ctx)
Example #3
0
    def test_bilinear_terms(self, problem):
        r = McCormickExpressionRelaxation()
        ctx = ProblemContext(problem)
        bilinear = problem.constraint('bilinear').root_expr

        assert r.can_relax(problem, bilinear, ctx)

        result = r.relax(problem, bilinear, ctx)
        self._check_constraints(result)
        assert result.expression.expression_type == ExpressionType.Linear
        assert len(result.expression.children) == 1
        aux_var = result.expression.children[0]
        assert aux_var.is_auxiliary
Example #4
0
    def test_bilinear_with_coef_2(self, problem):
        ctx = ProblemContext(problem)
        r = McCormickExpressionRelaxation()

        bilinear = problem.constraint('bilinear_coef_2').root_expr
        ctx.set_polynomiality(bilinear, PolynomialDegree(2))

        assert r.can_relax(problem, bilinear, ctx)

        result = r.relax(problem, bilinear, ctx)
        self._check_constraints(result)
        expr = result.expression
        assert expr.expression_type == ExpressionType.Linear
        assert np.allclose(expr.coefficient(expr.children[0]), np.array([6.0]))
Example #5
0
    def test_bilinear_sum(self, problem):
        ctx = ProblemContext(problem)
        r = McCormickExpressionRelaxation()

        bilinear = problem.constraint('bilinear_sum').root_expr
        ctx.set_polynomiality(bilinear, PolynomialDegree(2))

        assert r.can_relax(problem, bilinear, ctx)

        result = r.relax(problem, bilinear, ctx)
        assert len(result.constraints) == 12
        expr = result.expression
        assert expr.expression_type == ExpressionType.Linear
        assert len(expr.children) == 3