策略myself

import random
import heapq
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_0
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ipv4
class ThreeAndSTF():
    def __init__(self,tasks):
        net = Mininet(topo=None, switch=OVSKernelSwitch, controller=RemoteController)
        path=self.stf_scheduling(tasks)
    #贪婪算法
    def greedy_routing(self,graph, source, destination, energy_cost_func):
        current = source
        path = [current]
        while current != destination:
            neighbors = graph.neighbors(current)
            best_neighbor = min(neighbors, key=lambda n: energy_cost_func(n, current))
            path.append(best_neighbor)
            current = best_neighbor
        return path
    #随机路由算法
    def random_routing(self,graph, source, destination):
        current = source
        path = [current]
        while current != destination:
            neighbors = graph.neighbors(current)
            if neighbors:
                next_node = random.choice(neighbors)
                path.append(next_node)
                current = next_node
            else:
                raise ValueError("No path to destination")
        return path
    #最短路径算法(Dijkstra)
    def dijkstra(self,graph):
        start=0
        distances = {node: float('infinity') for node in graph}
        distances[start] = 0
        queue = [(0, start)]
        while queue:
            current_distance, current_node = heapq.heappop(queue)
            if current_distance > distances[current_node]:
                continue
            for neighbor, weight in graph[current_node].items():
                distance = current_distance + weight
                if distance < distances[neighbor]:
                    distances[neighbor] = distance
                    heapq.heappush(queue, (distance, neighbor))
        return distances
    #STF(Shortest Task First)调度策略
    def stf_scheduling(self,tasks):
        graph=tasks.graph
        source=tasks.source
        destination=tasks.destination
        energy_cost_func=self.energy_cost_func(graph)
        path_reedy=self.reedy_routing(graph, source, destination, energy_cost_func)
        path_random=self.random_routing( graph, source, destination)
        distances=self.dijkstra(graph)
        if path_random>path_reedy and distances<=path_reedy.length:
           return path_reedy
        else:
            return None
ThreeAndSTF()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值