Created
March 9, 2016 02:55
-
-
Save ajfriend/3b7a4213979950d424d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cvxpy.utilities as u | |
import cvxpy.lin_ops.lin_utils as lu | |
from cvxpy.atoms.atom import Atom | |
from cvxpy.constraints import SOC | |
import math | |
class gm_constr(Atom): | |
""" Geometric mean of two scalars; :math:`(x_1, \cdots, x_n)^{1/n}`. """ | |
def __init__(self, t, x, y): | |
super(gm_constr, self).__init__(t, x, y) | |
# Returns the geometric mean of x and y. | |
def numeric(self, values): | |
return 0 | |
# The shape is the common width and the sum of the heights. | |
def shape_from_args(self): | |
return u.Shape(1, 1) | |
# Always unknown. | |
def sign_from_args(self): | |
return u.Sign.ZERO | |
# Default curvature. | |
def func_curvature(self): | |
return u.Curvature.CONSTANT | |
def monotonicity(self): | |
return len(self.args)*[u.monotonicity.INCREASING] | |
# Only scalar arguments are valid. | |
def validate_arguments(self): | |
if not self.args[0].is_scalar() or not self.args[1].is_scalar() or not self.args[2].is_scalar(): | |
raise TypeError("The arguments to gm_constr must resolve to scalars." ) | |
@staticmethod | |
def graph_implementation(arg_objs, size, data): | |
"""Reduces the atom to an affine expression and list of constraints. | |
Parameters | |
---------- | |
arg_objs : list | |
LinExpr for each argument. | |
size : tuple | |
The size of the resulting expression. | |
data : | |
Additional data required by the atom. | |
Returns | |
------- | |
tuple | |
(LinOp for objective, list of constraints) | |
""" | |
t = arg_objs[0] | |
x = arg_objs[1] | |
y = arg_objs[2] | |
two = lu.create_const(2, (1, 1)) | |
# SOC(x + y, [y - x, 2*v]) | |
constraints = [ | |
SOC(lu.sum_expr([x, y]), | |
[lu.sub_expr(y, x), | |
lu.mul_expr(two, t, (1, 1))]) | |
] | |
return (0, constraints) | |
x = cvx.Variable() | |
y = cvx.Variable() | |
t = cvx.Variable() | |
prob = cvx.Problem(cvx.Minimize(gm_constr(t,x,y))) | |
prob.get_problem_data('SCS')['dims'] | |
# gives: SolverError: The solver SCS cannot solve the problem. | |
prob.get_problem_data('ECOS')['dims'] | |
# gives: {'ep': 0, 'f': 0, 'l': 0, 'q': [], 's': []} | |
# so it looks like the constraints aren't registering |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment