def test_univariate_concave(self, problem, constraint_name): ctx = ProblemContext(problem) r = UnivariateConcaveExpressionRelaxation() constraint_expr = problem.constraint(constraint_name).root_expr ctx.set_convexity(constraint_expr, Convexity.Concave) assert r.can_relax(problem, constraint_expr, ctx) result = r.relax(problem, constraint_expr, ctx) assert result.constraints == []
def test_quadratic_expression(self, problem): ctx = ProblemContext(problem) r = ConvexExpressionRelaxation() constraint_expr = problem.constraint('c0').root_expr ctx.set_convexity(constraint_expr, Convexity.Unknown) assert r.can_relax(problem, constraint_expr, ctx) result = r.relax(problem, constraint_expr, ctx) expr = result.expression assert expr.expression_type == ExpressionType.Quadratic assert len(expr.terms) == 2 for term in expr.terms: assert term.var1 == term.var2 assert term.coefficient == 2.0 or term.coefficient == 4.0 assert result.constraints == []
def test_sum_of_convex_expressions(self, problem): ctx = ProblemContext(problem) r = ConvexExpressionRelaxation() constraint_expr = problem.objective.root_expr ctx.set_convexity(constraint_expr, Convexity.Convex) assert r.can_relax(problem, constraint_expr, ctx) result = r.relax(problem, constraint_expr, ctx) assert len(result.expression.children) == 2 assert result.expression.expression_type == ExpressionType.Sum a, b = result.expression.children assert a.expression_type == ExpressionType.UnaryFunction assert b.expression_type == ExpressionType.UnaryFunction assert result.constraints == []