I have the following code: import simpy import random import matplotlib.pyplot as plt…
- User
I have the following code: import simpy import random import matplotlib.pyplot as plt import numpy as np
Parameters
num_edge_nodes = 2 num_cloud_nodes = 4 edge_buffer_size = 10 cloud_buffer_size = 15
service times
cloud_server_time = 10 A_edge_service_time = 4 A_cloud_service_time = 2 B_edge_sevice_time = 2 B_cloud_service_partialprocess = 4 B_cloud_service_fullprocess = 5 propagation_delay = 1
cloud_server_times = [8, 10, 12] # Add a list of service times for each cloud server type edge_server_times = [A_edge_service_time, B_edge_sevice_time] # Add edge service times for easy indexing
arrival_rate = 0.8
edge_costs = [1, 2, 3] cloud_costs = [2, 4, 6]
Modify cloud_costs and edge_costs to support more configurations
cloud_costs = [cloud_cost * server_time for cloud_cost, server_time in zip(cloud_costs, cloud_server_times)] edge_costs = [edge_cost * server_time for edge_cost, server_time in zip(edge_costs, edge_server_times)]
Measurements
class Measure: def init(self, N_arr_a, N_arr_b, drop, total_queuing_delay, edge_cost, cloud_cost): self.N_arr_A = N_arr_a self.N_arr_B = N_arr_b self.drop = drop self.total_queuing_delay = total_queuing_delay self.edge_cost = edge_cost self.cloud_cost = cloud_cost
measurements = Measure(0, 0, 0, 0, 0, 0)
def packet_arrivals(env, micro_data_center, cloud_data_center, data, edge_buffer, cloud_buffer, f_value): packet_type_options = ['A', 'B'] packet_id = 1
while True: packet_type = random.choices(packet_type_options, weights=(1 - f_value, f_value))[0] if packet_type == 'A': data.N_arr_A += 1 else: data.N_arr_B += 1 arrival_time = env.now # Adding current time stamp for calculating delay if len(micro_data_center.items) < edge_buffer: if packet_type == 'A': micro_data_center.put((packet_id, packet_type, A_edge_service_time, arrival_time)) elif packet_type == 'B': micro_data_center.put((packet_id, packet_type, B_edge_sevice_time, arrival_time)) else: if len(cloud_data_center.items) < cloud_buffer: if packet_type == 'A': cloud_data_center.put((packet_id, packet_type, A_cloud_service_time + propagation_delay)) elif packet_type == 'B': cloud_data_center.put((packet_id, packet_type, B_cloud_service_fullprocess + propagation_delay)) else: data.drop += 1 yield env.timeout(random.expovariate(arrival_rate)) packet_id += 1def edge_node(env, micro_data_center, cloud_data_center, node_id, data, edge_cost, service_time): while True: packet_id, packet_type, packet_processing_time, arrival_time = yield micro_data_center.get()
queuing_delay = env.now - arrival_time data.total_queuing_delay += queuing_delay data.edge_cost += edge_cost * packet_processing_time print(f"Edge Node {node_id} processed packet {packet_id} of type {packet_type} at time {env.now}") yield env.timeout(service_time) # Use parametrized service time for delay if packet_type == 'B': yield cloud_data_center.put((packet_id, packet_type, B_cloud_service_partialprocess + propagation_delay)) - Assistant
You can use matplotlib to plot the trade-offs between the overall cost, queuing delays, and packet drop probability. First, separate the results in the combinations list into different arrays to plot them easily. Then, plot them using scatter plots. I've added comments throughout the code to explain each step.
import matplotlib.pyplot as plt # Separate the results into arrays edge_costs = [comb["edge_cost"] for comb in combinations] cloud_costs = [comb["cloud_cost"] for comb in combinations] f_values = [comb["f"] for comb in combinations] total_costs = [comb["total_cost"] for comb in combinations] queuing_delays = [comb["queuing_delay"] for comb in combinations] packet_loss_rates = [comb["packet_loss_rate"] for comb in combinations] # Plot total cost vs queuing delay plt.figure() plt.scatter(total_costs, queuing_delays, c=f_values, cmap="viridis", marker="o", alpha=0.7) plt.xlabel("Total Cost") plt.ylabel("Queuing Delay") plt.title("Total Cost vs Queuing Delay") plt.colorbar(label="f Value") plt.savefig("total_cost_vs_queuing_delay.png") # Plot total cost vs packet drop rate plt.figure() plt.scatter(total_costs, packet_loss_rates, c=f_values, cmap="viridis", marker="o", alpha=0.7) plt.xlabel("Total Cost") plt.ylabel("Packet Drop Rate") plt.title("Total Cost vs Packet Drop Rate") plt.colorbar(label="f Value") plt.savefig("total_cost_vs_packet_drop_rate.png") # Plot queuing delay vs packet drop rate plt.figure() plt.scatter(queuing_delays, packet_loss_rates, c=f_values, cmap="viridis", marker="o", alpha=0.7) plt.xlabel("Queuing Delay") plt.ylabel("Packet Drop Rate") plt.title("Queuing Delay vs Packet Drop Rate") plt.colorbar(label="f Value") plt.savefig("queuing_delay_vs_packet_drop_rate.png") # Show the plot plt.show() - User
moreover create a meaningfull and beautifull table out of this data. (You should maintain a beautifull and readable table)
- Assistant
A popular library for creating tables in Python is
prettytable. First, you will need to install it usingpip:pip install prettytableNext, you can use it to create a nicely formatted table with the results from the combinations.
from prettytable import PrettyTable # Create the table with headers table = PrettyTable() table.field_names = ["f Value", "Edge Cost", "Cloud Cost", "Total Cost", "Queuing Delay", "Packet Drop Rate"] # Iterate through the combinations and add a row in the table for each one for comb in combinations: table.add_row([comb["f"], comb["edge_cost"], comb["cloud_cost"], comb["total_cost"], comb["queuing_delay"], comb["packet_loss_rate"]]) # Format the table table.align = 'r' # Sort the table by Total Cost table.sortby = "Total Cost" # Pretty print the table print(table)This code will print a nicely formatted table with the headers: f Value, Edge Cost, Cloud Cost, Total Cost, Queuing Delay, Packet Drop Rate. The rows in the table will represent each combination, sorted by the total cost.