def test_slack(self): b = np.abs(np.random.randn(*self.x.shape)) obj = sum_squares(self.x - b) constr = [ChanceConstraint(self.x >= 0, 0.8)] p = ccprob.Problem(Minimize(obj), constr) p.solve(slack=False) val_noslack = p.value slacks = [cc.slack_value for cc in p.chance_constraints] self.assertItemsAlmostEqual(slacks, len(p.chance_constraints) * [0]) p.solve(slack=True) self.assertTrue(p.value <= val_noslack) slacks = [cc.slack_value for cc in p.chance_constraints] for slack in slacks: self.assertAlmostGeq(slack, 0) # Conflicting constraints: Prob(x <= 0) >= 0.2 and x >= 1. constr += [self.x >= 1] p = ccprob.Problem(Minimize(obj), constr) with self.assertRaises(SolverError) as cm: p.solve(slack=False) # Solvable with slackened chance constraint. p.solve(slack=True) slacks = [cc.slack_value for cc in p.chance_constraints] for slack in slacks: self.assertAlmostGeq(slack, 0)
def test_dcp(self): # Prob(g(x) >= 0) <= p is DCP iff g is convex. constr = [ChanceConstraint(log(self.x) >= 0, 0.5)] p = ccprob.Problem(Minimize(norm(self.x)), constr) with self.assertRaises(DCPError) as cm: p.solve() # Prob(h(x) <= 0) <= p is DCP iff h is concave. constr = [ChanceConstraint(exp(self.x) <= 5, 0.8)] p = ccprob.Problem(Minimize(norm(self.x)), constr) with self.assertRaises(DCPError) as cm: p.solve()
def test_constraints(self): obj = norm(self.x) constr = [quantile(self.x, 0.6) <= -1] p = ccprob.Problem(Minimize(obj), constr) p.solve() self.assertTrue( np.sum(self.x.value <= -1 + self.tolerance) >= 0.6 * self.x.size) b = np.abs(np.random.randn(self.x.size)) obj = norm(self.x - b) constr = [quantile(self.x, 0.7) >= 1] p = ccprob.Problem(Minimize(obj), constr) p.solve() self.assertTrue( np.sum(self.x.value >= 1 - self.tolerance) >= np.round(1 - 0.7) * self.x.size)
def test_solve(self): b = np.abs(np.random.randn(*self.x.shape)) obj = sum_squares(self.x - b) constr = [ChanceConstraint(self.x <= 0, 0.8)] p = ccprob.Problem(Minimize(obj), constr) p.solve() self.assertTrue( np.sum(self.x.value <= self.tolerance) <= 0.8 * self.x.size) b = np.random.randn(self.A.shape[0]) obj = sum_squares(self.A * self.x - b) constr = [ChanceConstraint(self.x >= 0, 0.8)] p = ccprob.Problem(Minimize(obj), constr) p.solve() self.assertTrue( np.sum(self.x.value >= -self.tolerance) <= 0.8 * self.x.size)
def test_2step(self): b = np.abs(np.random.randn(*self.x.shape)) obj = sum_squares(self.x - b) constr = [ChanceConstraint(self.x <= 0, 0.8)] p = ccprob.Problem(Minimize(obj), constr) p.solve(two_step=False) val_1step = p.value p.solve(two_step=True) self.assertTrue(p.value <= val_1step)
def test_probability(self): b = np.random.randn(self.A.shape[0]) obj = sum_squares(self.A * self.x - b) cc = ChanceConstraint([self.x <= 0], 0.8) pc = prob(self.x <= 0) <= 0.8 self.assertEqual(cc.size, pc.size) self.assertEqual(cc.fraction, pc.fraction) self.assertEqual(cc.max_violations, pc.max_violations) self.assertAlmostEqual( ccprob.Problem(Minimize(obj), [cc]).solve(), ccprob.Problem(Minimize(obj), [pc]).solve()) cc = ChanceConstraint([self.x >= 0], 0.8) pc = prob(self.x <= 0) >= 0.2 self.assertEqual(cc.size, pc.size) self.assertEqual(cc.fraction, pc.fraction) self.assertEqual(cc.max_violations, pc.max_violations) self.assertAlmostEqual( ccprob.Problem(Minimize(obj), [cc]).solve(), ccprob.Problem(Minimize(obj), [pc]).solve())
def test_reduction_nested(self): # Minimize 2*quantile(y, 0.5)) + 1 # subject to y >= 0. t = Variable() obj = 2 * t + 1 constr = [quantile(self.y, 0.5) <= t, self.y >= 0] p0 = ccprob.Problem(Minimize(obj), constr) p0.solve() y_epi = self.y.value obj = 2 * quantile(self.y, 0.5) + 1 constr = [self.y >= 0] p1 = ccprob.Problem(Minimize(obj), constr) p1.solve() self.assertAlmostEqual(p1.value, p0.value) self.assertItemsAlmostEqual(self.y.value, y_epi) # Minimize -2*quantile(y, 0.75) + 3 # subject to y <= 5. obj = -2 * t + 3 constr = [quantile(self.y, 0.75) >= t, self.y <= 5] p0 = ccprob.Problem(Minimize(obj), constr) p0.solve() y_epi = self.y.value obj = -2 * quantile(self.y, 0.75) + 3 constr = [self.y <= 5] p1 = ccprob.Problem(Minimize(obj), constr) p1.solve() self.assertAlmostEqual(p1.value, p0.value) self.assertItemsAlmostEqual(self.y.value, y_epi) # Minimize quantile(y, 0.75) - quantile(y, 0.5) u = Variable() obj = t - u constr = [ quantile(self.y, 0.75) <= t, quantile(self.y, 0.5) >= u, self.y >= 0, self.y <= 1 ] p0 = ccprob.Problem(Minimize(obj), constr) p0.solve() y_epi = self.y.value obj = quantile(self.y, 0.75) - quantile(self.y, 0.5) constr = [self.y >= 0, self.y <= 1] p1 = ccprob.Problem(Minimize(obj), constr) p1.solve() self.assertAlmostEqual(p1.value, p0.value) self.assertItemsAlmostEqual(self.y.value, y_epi)
def test_reduction_basic(self): # Minimize quantile(y, 0.5) subject to y >= 0. t = Variable() constr = [quantile(self.y, 0.5) <= t, self.y >= 0] p0 = ccprob.Problem(Minimize(t), constr) p0.solve() y_epi = self.y.value obj = quantile(self.y, 0.5) constr = [self.y >= 0] p1 = ccprob.Problem(Minimize(obj), constr) p1.solve() self.assertAlmostEqual(p1.value, p0.value) self.assertItemsAlmostEqual(self.y.value, y_epi) # Maximize quantile(y, 0.75) subject to y <= 5. constr = [quantile(self.y, 0.75) >= t, self.y <= 5] p0 = ccprob.Problem(Maximize(t), constr) p0.solve() y_epi = self.y.value obj = quantile(self.y, 0.75) constr = [self.y <= 5] p1 = ccprob.Problem(Maximize(obj), constr) p1.solve() self.assertAlmostEqual(p1.value, p0.value) self.assertItemsAlmostEqual(self.y.value, y_epi) # Minimize quantile(abs(A*x - b), 0.5) # subject to 0 <= x <= 1, # quantile(x, 0.25) >= 0.1 b = np.random.randn(self.A.shape[0]) constr = [ quantile(abs(self.A * self.x - b), 0.5) <= t, self.x >= 0, self.x <= 1, quantile(self.x, 0.25) >= 0.1 ] p0 = ccprob.Problem(Minimize(t), constr) p0.solve() x_epi = self.x.value obj = quantile(abs(self.A * self.x - b), 0.5) constr = [self.x >= 0, self.x <= 1, quantile(self.x, 0.25) >= 0.1] p1 = ccprob.Problem(Minimize(obj), constr) p1.solve() self.assertAlmostEqual(p1.value, p0.value) self.assertItemsAlmostEqual(self.x.value, x_epi)