def test_analytic_param_in_exponent(self) -> None: # construct a problem with solution # x^\star(\alpha) = 1 - 2^alpha, and derivative # x^\star'(\alpha) = -log(2) * 2^\alpha base = 2.0 alpha = cp.Parameter() x = cp.Variable(pos=True) objective = cp.Maximize(x) constr = [cp.one_minus_pos(x) >= cp.Constant(base)**alpha] problem = cp.Problem(objective, constr) alpha.value = -1.0 alpha.delta = 1e-5 problem.solve(solver=cp.DIFFCP, gp=True, requires_grad=True, eps=1e-6) self.assertAlmostEqual(x.value, 1 - base**(-1.0)) problem.backward() problem.derivative() self.assertAlmostEqual(alpha.gradient, -np.log(base) * base**(-1.0)) self.assertAlmostEqual(x.delta, alpha.gradient * 1e-5, places=3) gradcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3) perturbcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3) alpha.value = -1.2 alpha.delta = 1e-5 problem.solve(solver=cp.DIFFCP, gp=True, requires_grad=True, eps=1e-6) self.assertAlmostEqual(x.value, 1 - base**(-1.2)) problem.backward() problem.derivative() self.assertAlmostEqual(alpha.gradient, -np.log(base) * base**(-1.2)) self.assertAlmostEqual(x.delta, alpha.gradient * 1e-5, places=3) gradcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3) perturbcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3)
def test_one_minus_analytic(self) -> None: # construct a problem with solution # x^\star(\alpha) = 1 - \alpha^2, and derivative # x^\star'(\alpha) = -2\alpha alpha = cp.Parameter(pos=True) x = cp.Variable(pos=True) objective = cp.Maximize(x) constr = [cp.one_minus_pos(x) >= alpha**2] problem = cp.Problem(objective, constr) alpha.value = 0.4 alpha.delta = 1e-5 problem.solve(solver=cp.DIFFCP, gp=True, requires_grad=True, eps=1e-5) self.assertAlmostEqual(x.value, 1 - 0.4**2, places=3) problem.backward() problem.derivative() self.assertAlmostEqual(alpha.gradient, -2 * 0.4, places=3) self.assertAlmostEqual(x.delta, -2 * 0.4 * 1e-5, places=3) gradcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3) perturbcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3) alpha.value = 0.5 alpha.delta = 1e-5 problem.solve(solver=cp.DIFFCP, gp=True, requires_grad=True, eps=1e-5) problem.backward() problem.derivative() self.assertAlmostEqual(x.value, 1 - 0.5**2, places=3) self.assertAlmostEqual(alpha.gradient, -2 * 0.5, places=3) self.assertAlmostEqual(x.delta, -2 * 0.5 * 1e-5, places=3) gradcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3) perturbcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3)
def test_paper_example_one_minus_pos(self): x = cvxpy.Variable(pos=True) y = cvxpy.Variable(pos=True) obj = cvxpy.Minimize(x * y) constr = [(y * cvxpy.one_minus_pos(x / y))**2 >= 1, x >= y / 3] problem = cvxpy.Problem(obj, constr) # smoke test. problem.solve(SOLVER, gp=True)
def test_one_minus_pos(self): x = cvxpy.Variable(pos=True) obj = cvxpy.Maximize(x) constr = [cvxpy.one_minus_pos(x) >= 0.4] problem = cvxpy.Problem(obj, constr) problem.solve(SOLVER, gp=True) self.assertAlmostEqual(problem.value, 0.6) self.assertAlmostEqual(x.value, 0.6)
def test_one_minus_pos(self) -> None: x = cp.Variable(pos=True) a = cp.Parameter(pos=True, value=3) b = cp.Parameter(pos=True, value=0.1) obj = cp.Maximize(x) constr = [cp.one_minus_pos(a * x) >= a * b] problem = cp.Problem(obj, constr) gradcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3) perturbcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3)
def test_paper_example_one_minus_pos(self) -> None: x = cp.Variable(pos=True) y = cp.Variable(pos=True) a = cp.Parameter(pos=True, value=2) b = cp.Parameter(pos=True, value=1) c = cp.Parameter(pos=True, value=3) obj = cp.Minimize(x * y) constr = [(y * cp.one_minus_pos(x / y))**a >= b, x >= y / c] problem = cp.Problem(obj, constr) gradcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3) perturbcheck(problem, solve_methods=[s.SCS], gp=True, atol=1e-3)
def test_one_minus_pos(self): x = cp.Variable(pos=True) obj = cp.Maximize(x) alpha = cp.Parameter(pos=True, value=0.1) constr = [cp.one_minus_pos(alpha + x) >= 0.4] problem = cp.Problem(obj, constr) problem.solve(SOLVER, gp=True, enforce_dpp=True) self.assertAlmostEqual(problem.value, 0.5) self.assertAlmostEqual(x.value, 0.5) alpha.value = 0.4 problem.solve(SOLVER, gp=True, enforce_dpp=True) self.assertAlmostEqual(problem.value, 0.2) self.assertAlmostEqual(x.value, 0.2)
def test_param_used_in_exponent_and_elsewhere(self) -> None: # construct a problem with solution # x^\star(\alpha) = 1 - 0.3^alpha - alpha^2, and derivative # x^\star'(\alpha) = -log(0.3) * 0.2^\alpha - 2*alpha base = 0.3 alpha = cp.Parameter(pos=True, value=0.5) x = cp.Variable(pos=True) objective = cp.Maximize(x) constr = [cp.one_minus_pos(x) >= cp.Constant(base)**alpha + alpha**2] problem = cp.Problem(objective, constr) alpha.delta = 1e-5 problem.solve(solver=cp.DIFFCP, gp=True, requires_grad=True, eps=1e-5) self.assertAlmostEqual(x.value, 1 - base**(0.5) - 0.5**2) problem.backward() problem.derivative() self.assertAlmostEqual(alpha.gradient, -np.log(base) * base**(0.5) - 2 * 0.5) self.assertAlmostEqual(x.delta, alpha.gradient * 1e-5, places=3)
def test_param_used_twice(self) -> None: # construct a problem with solution # x^\star(\alpha) = 1 - \alpha^2 - alpha^3, and derivative # x^\star'(\alpha) = -2\alpha - 3\alpha^2 alpha = cp.Parameter(pos=True) x = cp.Variable(pos=True) objective = cp.Maximize(x) constr = [cp.one_minus_pos(x) >= alpha**2 + alpha**3] problem = cp.Problem(objective, constr) alpha.value = 0.4 alpha.delta = 1e-5 problem.solve(solver=cp.DIFFCP, gp=True, requires_grad=True, eps=1e-6) self.assertAlmostEqual(x.value, 1 - 0.4**2 - 0.4**3) problem.backward() problem.derivative() self.assertAlmostEqual(alpha.gradient, -2 * 0.4 - 3 * 0.4**2) self.assertAlmostEqual(x.delta, alpha.gradient * 1e-5) gradcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3) perturbcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3)