Skip to content

Explaining merge_sparse_regs and "combinatorial explosion" #60

@kaghi

Description

@kaghi
def _merge_sparse_regs(
        self, s, regs, err_rtol=0, max_len=9, constraint_sum: bool = True
    ):
        """Merge sparse regions to minimize error."""
        max_combos = 10_000
        s_ret = s.copy()
        for r in range(regs.max() + 1):
            ridx = np.where(regs == r)[0]
            ridx = sorted(list(set(ridx).intersection(set(self.nzidx_s))))
            rlen = len(ridx)
            rsum = s[ridx].sum()
            ns_min = max(int(np.around(rsum)), 1)
            if rlen > max_len or ns_min > rlen or rlen <= 1:
                continue
            s_new = s_ret.copy()
            s_new[ridx] = 0
            err_before = self._compute_err(s=s_ret[self.nzidx_s])
            err_ls = []
            idx_ls = []
            if constraint_sum:
                ns_vals = [ns_min]
            else:
                ns_vals = list(range(ns_min, rlen + 1))
            for ns in ns_vals:
                # Defensive guard: avoid combinatorial explosion.
                if math.comb(rlen, ns) > max_combos:
                    continue
                for idxs in itt.combinations(ridx, ns):
                    idxs = np.array(idxs)
                    s_test = s_new.copy()
                    s_test[idxs] = rsum / ns
                    err_after = self._compute_err(s=s_test[self.nzidx_s])
                    err_ls.append(err_after)
                    idx_ls.append(idxs)
            if len(err_ls) > 0:
                err_min_idx = np.argmin(err_ls)
                err_min = err_ls[err_min_idx]
                if err_min - err_before < err_rtol * err_before:
                    idx_min = idx_ls[err_min_idx]
                    s_new[idx_min] = rsum / len(idx_min)
                    s_ret = s_new
        return s_ret

What is a combinatorial explosion in this context? I am also a bit confused by what this function does.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions