Commit 0f6d9e9d authored by Ashkan's avatar Ashkan
Browse files

Update systemParameters

Add Full search method
Add ric method
parent 2ced4aea
Loading
Loading
Loading
Loading
Loading

prrt/hec_search.py

0 → 100644
+102 −0
Original line number Diff line number Diff line
from abc import ABC, abstractmethod
import prrt
import math
import ric

class AbstractSearch(ABC):
    @abstractmethod
    def search(self):
        pass


class HECFullSearch(AbstractSearch):
    def __init__(self, n_p_max, prrtApplicationParameters, prrtChannelParameters, prrtSystemParameters):
        self.n_max = 255
        self.n_p_max = n_p_max
        self.prrtApplicationParameters = prrtApplicationParameters
        self.prrtChannelParameters = prrtChannelParameters
        self.prrtSystemParameters = prrtSystemParameters
        self.p_t = self.prrtApplicationParameters.max_residual_loss_rate
        pass

    def search(self):
        ri_min = math.inf
        k_opt = 0
        n_c_opt = 0
        n_p_opt = []
        # Eq.5.9, page 125
        fec_delay_min = self.prrtSystemParameters.source_packet_interval + \
                        self.n_p_max * self.prrtSystemParameters.redundancy_packet_transmission_delay + \
                        (self.prrtChannelParameters.rtt_prop_fwd + self.prrtSystemParameters.processing_delay) / 2 + \
                        self.prrtSystemParameters.packet_loss_detection_delay
        # Eq.5.9 page 125 assumung D_sup = 0
        req_delay = self.prrtChannelParameters.rtt_prop_fwd + \
                    self.n_p_max * self.prrtSystemParameters.redundancy_packet_transmission_delay + \
                    self.prrtSystemParameters.processing_delay
        # Eq.5.10, page 125
        n_c_max = math.ceil((self.prrtApplicationParameters.max_latency - fec_delay_min) / req_delay)
        # self.k_lim = somewhat(prrtApplicationParameters.loss_tolerance, self.n_max)

        for n_c in range(n_c_max + 1):
            # Eq.5.11, k(Nc, D_T), page 125
            k_max = min(self.get_k(n_c, req_delay), self.get_k_lim(1, self.n_max))
            for k in range(1, k_max + 1):
                n_p = ric.get_ric(self.n_max - k, n_c, 1, self.n_p_max)
                for i in n_p:
                    coding_conf = prrt.PrrtCodingConfiguration(self.n_max, k, i)
                    ri = coding_conf.get_redundant_information(self.prrtChannelParameters)
                    if ri < ri_min:
                        k_opt = k
                        n_c_opt = n_c
                        n_p_opt = i

        return prrt.PrrtCodingConfiguration(k_opt, n_c_opt, n_p_opt)

    def get_k(self, n_c, req_delay):
        math.ceil((self.prrtApplicationParameters.max_latency -
                   self.n_p_max * self.prrtSystemParameters.redundancy_packet_transmission_delay -
                   (self.prrtChannelParameters.rtt_prop_fwd + self.prrtSystemParameters.processing_delay) / 2 -
                   self.prrtSystemParameters.packet_loss_detection_delay -
                   n_c * req_delay) / self.prrtSystemParameters.source_packet_interval)


    def get_k_lim(self, start, end):
        mid_point = math.ceil((end - start) / 2)
        p_r = self.get_max_coding_block_length(mid_point)
        if (p_r == self.p_t):
            return mid_point
        elif (p_r  > self.p_t):
            self.get_k_lim(start, mid_point - 1)
        else:
            self.get_k_lim(mid_point + 1, end)

    # Pr(k, n_max)
    def get_max_coding_block_length(self, k):
        total_packet_erasure = 0
        for i in range(1, k):
            for j in range(max(self.n_max - k + 1, i), self.n_max - k + i):
                packet_erasure_at_i = i * self.hypergeometric_distribution(self.n_max, k, i, j) * self.get_error_prob(
                    j, self.n_max, self.prrtChannelParameters.loss_rate_fwd)
                total_packet_erasure += packet_erasure_at_i
        return (1 / k) * total_packet_erasure


# Restricted Integer combinations
"""
def partitionfunc(n, k, smallest=1):
    '''n is the integer to partition, k is the length of partitions, smallest is the min partition element size'''
    if k < 1:
        raise StopIteration
    if k == 1:
        if n >= smallest:
            yield (n,)
        raise StopIteration
    for i in range(smallest, n + 1):
        for result in partitionfunc(n - i, k - 1, i):
            yield (i,) + result


generator = partitionfunc(3, 1, )
for i in generator:
    print(i)
"""
 No newline at end of file
+37 −13
Original line number Diff line number Diff line
@@ -175,26 +175,31 @@ class PrrtSystemParameters:
                block_coding_delay,
                redundancy_packet_transmission_delay,
                processing_delay,
                packet_loss_detection_delay):
                packet_loss_detection_delay,
                source_packet_interval):
       self.block_coding_delay = block_coding_delay
       self.redundancy_packet_transmission_delay = redundancy_packet_transmission_delay
       self.processing_delay = processing_delay
       self.packet_loss_detection_delay = packet_loss_detection_delay
       self.source_packet_interval = source_packet_interval

   def __repr__(self):
       return "PrrtSystemParameters(block_coding_delay={}," \
              "redundancy_packet_transmission_delay={}," \
              "processing_delay={}," \
              "packet_loss_detection_delay={})".format(self.block_coding_delay,
              "packet_loss_detection_delay={}," \
              "source_packet_interval={})".format(self.block_coding_delay,
                                                  self.redundancy_packet_transmission_delay,
                                                  self.processing_delay,
                                                       self.packet_loss_detection_delay)
                                                  self.packet_loss_detection_delay,
                                                  self.source_packet_interval)

   def __str__(self):
       return "({},{},{},{})".format(self.block_coding_delay,
       return "({},{},{},{},{})".format(self.block_coding_delay,
                                        self.redundancy_packet_transmission_delay,
                                        self.processing_delay,
                                     self.packet_loss_detection_delay)
                                        self.packet_loss_detection_delay,
                                        self.source_packet_interval)

class PrrtCodingConfiguration:
    def __init__(self, n, k, n_p=None):
@@ -240,6 +245,19 @@ class PrrtCodingConfiguration:
        ri = 1 / self.k * self.n_p[0] + 1 / self.k * np.dot(error_prob_up_to_every_cycle, self.n_p[1:])
        return ri

    def get_ri_min(self, prrtChannelParameters):
        p_e = prrtChannelParameters.loss_rate_fwd
        return p_e / (1 - p_e)

    def get_ri_residual(self, prrtChannelParameters, prrtApplicationParameters):
        p_e = prrtChannelParameters.loss_rate_fwd
        p_res = prrtApplicationParameters.max_residual_loss_rate
        if (p_e >= p_res):
            return (p_e - p_res) / (1 - p_e)
        else:
            return 0


    # TODO
    # Check if it is able to fulfill application parameters given channel parameters.
    def hypergeometric_distribution(self, n, k, i, j):
@@ -299,24 +317,30 @@ cdef class PrrtSocket:
            return PrrtSystemParameters(cprrt.PrrtSocket_get_block_coding_delay,
                                        cprrt.PrrtSocket_get_redundancy_packet_transmission_delay,
                                        cprrt.PrrtSocket_get_processing_delay, # processing_delay
                                        cprrt.PrrtSocket_get_loss_detection_delay) # packet_loss_detection_delay
                                        cprrt.PrrtSocket_get_loss_detection_delay, # packet_loss_detection_delay
                                        cprrt.PrrtSocket_get_source_packet_interval)

    property block_coding_delay:
        def __get__(self):
            return 0

    property redundancy_packet_transmission_delay:
        def __get__(self, data):
            return len(data) / cprrt.PrrtSocket_get_btlbw_fwd(self._c_socket)
        def __get__(self):
            # len(data) / cprrt.PrrtSocket_get_btlbw_fwd(self._c_socket)
            return 0

    property processing_delay:
        def __get__(self):
            return 0

    property packet_loss_detection_delay:
        def __get__(self)
        def __get__(self):
            return 0

    property source_packet_interval:
        def __get__(self):
            return 0.01 # in seconds

    # property data_rate_btl_fwd:
    #     def __get__(self):
    #         return cprrt.PrrtSocket_get_btlbw_fwd(self._c_socket)

prrt/ric.pyx

0 → 100644
+33 −0
Original line number Diff line number Diff line
import pyximport; pyximport.install()
import itertools

def gen_ric(redundancy, positions, min, max):
    cdef c_redundancy = redundancy
    cdef c_positions = positions
    cdef c_min = min
    cdef c_max = max

    if c_positions < 1:
        raise StopIteration
    if c_positions == 1:
        if c_redundancy >= c_min and c_redundancy <= c_max:
            yield (c_redundancy,)
        raise StopIteration

    for i in range(c_min, c_redundancy + 1):
        for result in gen_ric(c_redundancy - i, c_positions - 1, i, c_max):
            if (i <= c_max):
                yield (i,) + result

def get_ric(redundancy, positions, min, max):
    rawList = []
    permuted = []

    f = gen_ric(redundancy, positions, min, max)
    for i in f:
        rawList.append(i)

    for i in rawList:
        permuted.append(itertools.permutations(i))

    return permuted
 No newline at end of file

tests/__init__.py

deleted100644 → 0
+0 −0

Empty file deleted.