Beispiel #1
0
    def compute_shapley_value(self,t,idxs,**kwargs):
        V_S_t=kwargs['V_func']

        util={}
        powerset=list(powersettool(idxs))

        # TMR round truncation below
        S_0=()
        util[S_0]=V_S_t(t=t,S=S_0)

        S_all=powerset[-1]
        util[S_all]=V_S_t(t=t,S=S_all)

        if abs(util[S_all]-util[S_0])<=self.round_trunc_threshold:
            sv_dict={id:0 for id in idxs}
            return sv_dict
        # TMR round truncation above


        for S in powerset[1:-1]:
            util[S]=V_S_t(t=t,S=S)

        self.SV_t[t]=self.shapley_value(util,idxs)

        self.Ut[t]=copy.deepcopy(util)

        return self.SV_t[t]
Beispiel #2
0
    def compute_shapley_value(self, idxs, **kwargs):
        V_S_0 = kwargs['V_func']
        N = len(idxs)
        self.Contribution_records = []

        powerset = list(powersettool(idxs))

        util = {}
        S_0 = ()
        util[S_0] = V_S_0(S=S_0)

        S_all = powerset[-1]
        util[S_all] = V_S_0(S=S_all)

        k = 0
        while self.isnotconverge(k):
            k += 1
            v = [0 for i in range(N + 1)]
            v[0] = util[S_0]
            marginal_contribution_k = [0 for i in range(N)]

            idxs_k = np.random.permutation(idxs)

            for j in range(1, N + 1):
                # key = C subset
                C = idxs_k[:j]
                C = tuple(np.sort(C, kind='mergesort'))

                #truncation
                if abs(util[S_all] - v[j - 1]) >= self.eps:
                    if util.get(C) != None:
                        v[j] = util[C]
                    else:
                        v[j] = V_S_0(S=C)
                else:
                    v[j] = v[j - 1]

                # record calculated V(C)
                util[C] = v[j]

                # update SV
                marginal_contribution_k[idxs_k[j - 1] - 1] = v[j] - v[j - 1]

            self.Contribution_records.append(marginal_contribution_k)

        # shapley value calculation
        shapley_value = (
            np.cumsum(self.Contribution_records, 0) /
            np.reshape(np.arange(1,
                                 len(self.Contribution_records) + 1),
                       (-1, 1)))[-1:].tolist()[0]

        # store round t results
        self.SV = {key + 1: sv for key, sv in enumerate(shapley_value)}

        self.Ut = copy.deepcopy(util)

        return self.SV
Beispiel #3
0
    def compute_shapley_value(self, idxs, **kwargs):
        V_S_D = kwargs['V_func']

        util = {}
        powerset = list(powersettool(idxs))
        for S in powerset:
            util[S] = V_S_D(S=S)

        self.SV = self.shapley_value(util, idxs)

        self.Ut = copy.deepcopy(util)

        return self.SV
Beispiel #4
0
    def compute_shapley_value(self, t, idxs, **kwargs):
        #timer only
        self.st_t = time.time()

        V_S_t = kwargs['V_func']

        util = {}
        powerset = list(powersettool(idxs))
        for S in powerset:
            util[S] = V_S_t(t=t, S=S)

        #for print only
        self.full_set = powerset[-1]

        self.SV_t[t] = self.shapley_value(util, idxs)

        self.Ut[t] = copy.deepcopy(util)

        self.print_results(t)

        return self.SV_t[t]
Beispiel #5
0
    def compute_shapley_value(self,t,idxs,**kwargs):
        V_S_t=kwargs['V_func']
        N=len(idxs)
        powerset=list(powersettool(idxs))


        util={}
        S_0=()
        util[S_0]=V_S_t(t=t,S=S_0)

        S_all=powerset[-1]
        util[S_all]=V_S_t(t=t,S=S_all)


        # group test relate
        last_uds=[]
        Z=0
        for n in range(1,N):
            Z+=1/n
        Z*=2
        UD=np.zeros([N,N],dtype=np.float32)
        p=np.array([N/(i*(N-i)*Z) for i in range(1,N)])

        k=0
        while self.isnotconverge_Group(last_uds,UD) or k<self.CONVERGE_MIN_K:
            k+=1
            len_k=0
            # 1. draw len_K ~ q(len_k)
            len_k=np.random.choice(np.arange(1,N),p=p)

            # 2. sample S with len_k
            S=np.random.choice(idxs,size=len_k,replace=False)

            # 3. M(S) + V(S)
            S=tuple(np.sort(S,kind='mergesort'))
            if util.get(S)!=None:
                u_S=util[S]
            else:
                u_S=V_S_t(t=t,S=S)

            # 4. Group Testing update UD
            UD=(k-1)/k*UD

            for i in range(0,N):
                for j in range(0,N):
                    delta_beta=S.count(i+1)-S.count(j+1)
                    if delta_beta!=0:
                        value=delta_beta*u_S*Z/k
                        UD[i,j]+=value

            last_uds.append(UD)

        u_N=util[S_all]

        #timer
        st=time.time()
        #timer

        shapley_value=self.solveFeasible(N,u_N,UD)

        #timer
        dura=time.time()-st
        print('Solve Feasible using %.3f seconds'%dura)
        #timer

        self.Ut[t]=copy.deepcopy(util)
        self.SV_t[t]={key+1:sv for key,sv in enumerate(shapley_value)}


        return self.SV_t[t]