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()
策略myself
最新推荐文章于 2024-09-17 23:15:58 发布