Parvfect / HelixWorks

Code for the Channels and Decoding Methods
0 stars 0 forks source link

Implementation of BP Decoding and Verification with BEC simulation #13

Closed Parvfect closed 8 months ago

Parvfect commented 8 months ago

Implemented BP Decoding - can be easily generalised to coupon collector case. Seems to be slower but results are the same as compared to bec decoding. Going to make it a little faster if I can. image

Parvfect commented 8 months ago

yeah dk what it is - will need to run a cProfile test - but can be done later

Code is

def belief_propagation(self, max_iterations=100):
        """ Belief Propagation decoding for the general case (currently only works for BEC) )"""

        filled_vns = sum(list([1 for i in self.vns if not np.isnan(i.value)]))
        resolved_vns = 0

        for iteration in range(max_iterations):

            # Iterating through all the check nodes
            for i in self.cns:

                # Iterating through all the connected variable nodes for the check node
                for j in i.links:

                    sum_vns = 0
                    erasure_check = False

                    for k in i.links:
                        if k != j:
                            if np.isnan(self.vns[k].value):
                                erasure_check = True
                                break
                            sum_vns += self.vns[k].value

                    if erasure_check:
                        continue

                    # Need a better Resolved VNs check for the general case, will have to be adapted for the Coupon collector
                    if np.isnan(self.vns[j].value):
                        resolved_vns += 1    

                    self.vns[j].value = sum_vns % 2  # in coupon collector, this is going to be intersection with the set of symbols

                if filled_vns + resolved_vns == self.n:
                    return np.array([i.value for i in self.vns])

        return np.array([i.value for i in self.vns])
Parvfect commented 8 months ago

better commented

def belief_propagation(self, max_iterations=100):
        """ Belief Propagation decoding for the general case (currently only works for BEC) )"""

        filled_vns = sum(list([1 for i in self.vns if not np.isnan(i.value)]))
        resolved_vns = 0

        for iteration in range(max_iterations):

            # Iterating through all the check nodes
            for i in self.cns:

                # Iterating through all the connected variable nodes for the check node
                for j in i.links:

                    sum_vns = 0
                    erasure_check = False

                    # Iterating through the other variable nodes for the check node to obtain the possible value for the selected variable node
                    for k in i.links:
                        if k != j:

                            # For BEC - if any of the connected variable nodes are erased, then the value of the selected variable node is erased
                            if np.isnan(self.vns[k].value):
                                erasure_check = True
                                break

                            # Update the sum of the variable nodes
                            sum_vns += self.vns[k].value

                    # If any of the connected variable nodes are erased, then the value of the selected variable node is erased
                    if erasure_check:
                        continue

                    # Need a better Resolved VNs check for the general case, will have to be adapted for the Coupon collector
                    if np.isnan(self.vns[j].value):
                        resolved_vns += 1    

                    self.vns[j].value = sum_vns % 2  # in coupon collector, this is going to be intersection with the set of symbols

                if filled_vns + resolved_vns == self.n:
                    return np.array([i.value for i in self.vns])

        return np.array([i.value for i in self.vns])