def test_pownd_constraint(self) -> None: n = 4 W, z = Variable(n), Variable() np.random.seed(0) alpha = 0.5 + np.random.rand(n) alpha /= np.sum(alpha) with self.assertRaises(ValueError): # entries don't sum to one con = PowConeND(W, z, alpha + 0.01) with self.assertRaises(ValueError): # shapes don't match exactly con = PowConeND(W, z, alpha.reshape((n, 1))) with self.assertRaises(ValueError): # wrong axis con = PowConeND(reshape_atom(W, (n, 1)), z, alpha.reshape((n, 1)), axis=1) # Compute a violation con = PowConeND(W, z, alpha) W0 = 0.1 + np.random.rand(n) z0 = np.prod(np.power(W0, alpha)) + 0.05 W.value, z.value = W0, z0 viol = con.violation() self.assertGreaterEqual(viol, 0.01) self.assertLessEqual(viol, 0.06)
def pcp_4(ceei: bool = True): """ A power cone formulation of a Fisher market equilibrium pricing model. ceei = Competitive Equilibrium from Equal Incomes """ # Generate test data np.random.seed(0) n_buyer = 4 n_items = 6 V = np.random.rand(n_buyer, n_items) X = cp.Variable(shape=(n_buyer, n_items), nonneg=True) u = cp.sum(cp.multiply(V, X), axis=1) if ceei: b = np.ones(n_buyer) / n_buyer else: b = np.array([0.3, 0.15, 0.2, 0.35]) log_objective = cp.Maximize(cp.sum(cp.multiply(b, cp.log(u)))) log_cons = [cp.sum(X, axis=0) <= 1] log_prob = cp.Problem(log_objective, log_cons) log_prob.solve(solver='SCS', eps=1e-8) expect_X = X.value z = cp.Variable() pow_objective = (cp.Maximize(z), np.exp(log_prob.value)) pow_cons = [(cp.sum(X, axis=0) <= 1, None), (PowConeND(W=u, z=z, alpha=b), None)] pow_vars = [(X, expect_X)] sth = STH.SolverTestHelper(pow_objective, pow_vars, pow_cons) return sth