def opt_error(self,work): summary = [] workloads = [] """ Parsing workloads """ for wd in postRequest: blockinfo = {"columnNames":[],'buildingBlock':[],'p':[]} for bb in wd['data']: blockinfo['columnNames'].append(bb['name']) size = int((float(bb['maximum']) - float(bb['minimum']))/float(bb['bucketSize'])) pv = math.ceil(size/16.0) if math.ceil(size/16.0) != 2 else math.ceil(size/16.0) - 1 blockinfo['p'].append(pv) if bb['buildingBlock'] == 'identity': blockinfo['buildingBlock'].append(Identity(size)) elif bb['buildingBlock'] == 'allrange': blockinfo['buildingBlock'].append(AllRange(size)) elif bb['buildingBlock'] == 'prefix': blockinfo['buildingBlock'].append(Prefix(size)) else: blockinfo['buildingBlock'].append(Total(size)) wk = float(wd['weight']) * Kron(blockinfo['buildingBlock']) workloads.append(wk) self.kron = templates.KronPIdentity(wk.domain, blockinfo['p']) self.kron.optimize(wk) A = [sub.A for sub in self.kron.strategies] num_query = int(wk.queries) expected_error = wk.expected_error(A) summary.append({'wid':wd['wid'], 'expected_error':'{:.4f}'.format(expected_error), 'num_query':num_query, 'workloadString':wd['workloadString']}) print(summary) return summary
def findPIdentityStrategy(self) -> templates.TemplateStrategy: """find a Kronecker product strategy by optimizing the workload""" ps_parameters = self.gettuple_of_ints(CC.PS_PARAMETERS, section=CC.HDMM, sep=CC.REGEX_CONFIG_DELIM) print("pidentity parameters: ", ps_parameters) return templates.KronPIdentity(ps_parameters, self.hist_shape)
def opt_p_identity(workload=None): ps = [1, 1, 8, 4] # hard-coded parameters ns = [2, 2, 63, 8] template = templates.KronPIdentity(ps, ns) A = template.restart_optimize(workload, 25)[0] return A
def example4(): """ End-to-End algorithm on census workload """ print('Example 4') sf1 = SF1_Persons() domain = [2,2,64,17,115] kron = templates.KronPIdentity([1,1,6,1,10], domain) res = kron.optimize(sf1) print(sf1.shape, len(sf1.matrices)) x = np.zeros(sf1.shape[1]) mech = mechanism.HDMM(sf1, x, 1.0) mech.optimize() #xest = mech.run() print('Done')
def example3(): """ Optimize Union-of-Kronecker product workload using kronecker parameterization and marginals parameterization """ print('Example 3') sub_workloads1 = [workload.Prefix(64) for _ in range(4)] sub_workloads2 = [workload.AllRange(64) for _ in range(4)] W1 = workload.Kronecker(sub_workloads1) W2 = workload.Kronecker(sub_workloads2) W = workload.VStack([W1, W2]) K = templates.KronPIdentity([4]*4, [64]*4) K.optimize(W) print(error.expected_error(W, K.strategy())) M = templates.Marginals([64]*4) M.optimize(W) print(error.expected_error(W, M.strategy())) identity = workload.Kronecker([workload.Identity(64) for _ in range(4)]) print(error.expected_error(W, identity))
def optimize(self, restarts = 25): W = self.W if type(self.domain) is tuple: # kron or union kron workload ns = self.domain ps = [max(1, n//16) for n in ns] kron = templates.KronPIdentity(ps, ns) optk, lossk = kron.restart_optimize(W, restarts) marg = templates.Marginals(ns) optm, lossm = marg.restart_optimize(W, restarts) # multiplicative factor puts losses on same scale if lossk <= lossm: self.strategy = optk else: self.strategy = optm else: n = self.domain pid = templates.PIdentity(max(1, n//16), n) optp, loss = pid.restart_optimize(W, restarts) self.strategy = optp
def opt_strategy(workload=None): ns = [2, 2, 64, 17, 115] ps = [1, 1, 8, 1, 10] # hard-coded parameters template = templates.KronPIdentity(ps, ns) template.optimize(workload) return template.strategy()
# densely represented sub-workloads in each of the dimensions identity1 = workload.EkteloMatrix(np.eye(10)) identity2 = workload.EkteloMatrix(np.eye(25)) total = workload.EkteloMatrix(np.ones((1, 10))) prefix = workload.EkteloMatrix(np.tril(np.ones((25, 25)))) # form the kron products in each dimension W1 = workload.Kronecker([identity1, identity2]) W2 = workload.Kronecker([total, prefix]) # form the union of krons W = workload.VStack([W1, W2]) # find a Kronecker product strategy by optimizing the workload ps = [2, 2] # parameter for P-Identity strategies template = templates.KronPIdentity(ps, domain) # run optimization template.optimize(W) # get the sparse, explicit representation of the optimized strategy A = template.strategy().sparse_matrix().tocsr() # Round for Geometric Mechanism (skip this if using Laplace Mechanism) A = np.round(A * 1000) / 1000.0 # Extract diagonal and non-diagonal portion of strategy idx = np.array((A != 0).sum(axis=1) == 1).flatten() diag, extra = A[idx].diagonal(), A[~idx] print(diag.shape, extra.shape)
def optimize_workload(wk, blockinfo): kron = templates.KronPIdentity(blockinfo['p'], domain(wk)) kron.optimize(wk) gc.collect() return kron.strategy()