示例#1
0
文件: oml.py 项目: shawnadelic/adhoc
 def __init__(self, output_path = False, **kwargs):
     self.net = RandomAdHocNetwork(**kwargs)
     self.output_path = output_path
示例#2
0
class MECBE(object):
    def __init__(self, output_path = False, **kwargs):
        self.net = RandomAdHocNetwork(**kwargs)
        self.output_path = output_path
    def run(self):
        if self.net.connected:
            self.all_requests = list(self.net.requests)
            self.remaining_requests = list(self.net.requests)
            self.satisfied_requests = []
            self.request_solutions = []
            total_energy = 0
            net_prime = self.net.copy()
            while self.remaining_requests:
                net_prime.prune_edges()
                request = self.remaining_requests.pop()
                src, dest = request 

                try:
                    minimum_path = self.minimum_metric_path(net_prime, src, dest)
                except NetworkXNoPath:
                    print "Stopping, cannot satisfy request: {} -> {}".format(src, dest)
                    break

                request_energy = net_prime.update_along_path(minimum_path)

                self.satisfied_requests.append((src, dest))
                self.request_solutions.append(((src, dest), minimum_path, request_energy))

                total_energy += request_energy

            if self.request_solutions:
                print "Satisfied requests:"
            for request, path, energy in self.request_solutions:
                print
                if self.output_path is True:
                    print "Path: {}".format(" -> ".join(path))
                print "Request {} -> {}".format(request[0], request[1])
                print "Energy Consumed: {}".format(energy)
     
            #self.net.draw(requests=self.satisfied_requests)
            print
            print "**************************************************************************************"
            print "Total Requests Satisfied: {}".format(len(self.satisfied_requests))
            print "Total Energy Consumed: {}".format(total_energy)

    def minimum_metric_path(self, net, src, dest):
        for edge in net.edges():
            temp_src, temp_dest = edge
            if net[temp_src][temp_dest]["type"] == "external":
                net[temp_src][temp_dest]["metric"] = 1/net.node[temp_src]["energy"]
            else:
                net[temp_src][temp_dest]["metric"] = 0

        shortest_path = net.shortest_path(src, dest, weight="metric")

        return shortest_path

    def draw(self, output_file=None, requests=None):
        if self.net.connected:
            if requests is None:
                requests = self.satisfied_requests
            self.net.draw(output_file=output_file, requests=requests)
示例#3
0
文件: oml.py 项目: shawnadelic/adhoc
class OML(object):
    def __init__(self, output_path = False, **kwargs):
        self.net = RandomAdHocNetwork(**kwargs)
        self.output_path = output_path
    def run(self):
        if self.net.connected:
            self.all_requests = list(self.net.requests)
            self.remaining_requests = list(self.net.requests)
            self.satisfied_requests = []
            self.request_solutions = []
            self.c = 1000
            self.lmbda = 10000
            net_prime = None
            net_double_prime = None
            total_energy = 0
            while self.remaining_requests:
                request = self.remaining_requests.pop()
                src, dest = request 

                # Step 1

                if net_prime is None:
                    net_prime = self.net.copy()
                net_prime.prune_edges()
                try:
                    p_prime = net_prime.shortest_path(src, dest, weight="cost")
                except NetworkXNoPath:
                    print "Cannot satisfy request: {} -> {}".format(src, dest)
                    break

                min_edge, min_re = net_prime.min_residual_energy(p_prime)
                min_src, min_dest = min_edge
        
                net_double_prime = net_prime.copy()
                net_double_prime.prune_edges(min_re)

                # Step 2

                for edge_src, edge_dest in net_double_prime.edges():
                    w_double_prime = self._w_double_prime(net_double_prime, edge_src, edge_dest, min_re)
                    net_double_prime.set_weight(edge_src, edge_dest, w_double_prime)
                try:
                    p_double_prime = net_double_prime.shortest_path(src, dest, weight="weight")
                except NetworkXNoPath:
                    print "Cannot satisfy request on net_double_prime: {} -> {}".format(src, dest)
                    break

                net_double_prime.update_along_path(p_double_prime)
                path_total_energy = net_prime.update_along_path(p_double_prime)
                total_energy += path_total_energy

                self.satisfied_requests.append((src, dest))
                self.request_solutions.append(((src, dest), p_double_prime, path_total_energy))

            if self.request_solutions:
                print "Satisfied requests:"
            for request, path, energy in self.request_solutions:
                print
                print "Request: {} -> {}".format(request[0], request[1])
                if self.output_path is True:
                    print "Path: {}".format(" -> ".join(path))
                print "Energy Consumed: {}".format(energy)
            
            #self.net.draw(requests=self.satisfied_requests)
            print
            print "**************************************************************************************"
            print "Total Requests Satisfied: {}".format(len(self.satisfied_requests))
            print "Total Energy Consumed: {}".format(total_energy)

    def _e_min(self, net, node):
        min_node = min(net[node], key=lambda dest: net.weight(node, dest))
        return net.weight(node, min_node)

    def _alpha(self, net, node, min_re):
        return float(min_re) / net.get_energy(node)

    def _rho(self, net, src, dest):
        if net.get_energy(src) - net.weight(src, dest) > self._e_min(net, src):
            return 0
        else:
            return self.c

    def _w_double_prime(self, net, src, dest, min_re):
        if net[src][dest]["type"] == "external":
            value = net.weight(src, dest) + self._rho(net, src, dest)
            alpha = self._alpha(net, src, min_re)
            weight = (self.lmbda**alpha - 1)
            return value * weight
        else:
            return net.weight(src, dest)

    def draw(self, output_file=None, requests=None):
        if self.net.connected:
            if requests is None:
                requests = self.satisfied_requests
            self.net.draw(output_file=output_file, requests=requests)
示例#4
0
文件: gdp.py 项目: shawnadelic/adhoc
class GDP(object):
    def __init__(self, output_path = False, **kwargs):
        self.net = RandomAdHocNetwork(**kwargs)
        self.output_path = output_path
    def run(self):
        if self.net.connected:
            self.all_requests = list(self.net.requests)
            self.remaining_requests = list(self.net.requests)
            self.satisfied_requests = []
            self.request_solutions = []
            total_energy = 0
            net = self.net
            self.beta = self.calculate_beta(net)
            while self.remaining_requests:
                net.prune_edges()
                #request = self.remaining_requests.pop()
                #src, dest = request

                try:
                    min_request, min_path, min_path_value = self.minimum_weighted_path(net, self.remaining_requests)
                except NetworkXNoPath:
                    print "Stopping, cannot satisfy any more requests"
                    break

                min_energy = net.update_along_path(min_path)

                self.remaining_requests.remove(min_request)
                self.satisfied_requests.append(min_request)
                self.request_solutions.append((min_request, min_path, min_energy))

                self.multiply_weight_along_path(net, min_path)

                total_energy += min_energy

            if self.request_solutions:
                print "Satisfied requests:"
            for request, path, energy in self.request_solutions:
                print
                print "Request: {} -> {}".format(request[0], request[1])
                if self.output_path is True:
                    print "Path: {}".format(" -> ".join(path))
                print "Energy Consumed: {}".format(energy)
     
            #self.net.raw(requests=self.satisfied_requests)
            print
            print "**************************************************************************************"
            print "Total Requests Satisfied: {}".format(len(self.satisfied_requests))
            print "Total Energy Consumed: {}".format(total_energy)

    def minimum_weighted_path(self, net, requests):
        path = None
        path_value = None
        min_path = None
        min_request = None
        min_path_value = None
        for src, dest in requests:
            try:
                path = net.shortest_path(src, dest, weight="weight")
                path_value = net.shortest_path_length(src, dest, weight="weight")
            except NetworkXNoPath:
                pass
            if min_path is None or path_value < min_path_value:
                min_path = path
                min_path_value = path_value
                min_request = (src, dest)

        if min_path is None:
            raise NetworkXNoPath

        return (min_request, min_path, min_path_value)

    def multiply_weight_along_path(self, net, path):
        for index, value in enumerate(path[:-1]):
            net[path[index]][path[index+1]]["weight"] *= self.beta

    def calculate_beta(self, net):
        epsilon = (net.min_energy + net.max_energy) / (float(2) * 1000000000)
        m = len(net.edges())
        beta = m ** (float(1)/ (epsilon + 1) )
        return beta

    def draw(self, output_file=None, requests=None):
        if self.net.connected:
            if requests is None:
                requests = self.satisfied_requests
            self.net.draw(output_file=output_file, requests=requests)