98 lines
3.1 KiB
Python
98 lines
3.1 KiB
Python
|
|
# import simpy
|
|||
|
|
#
|
|||
|
|
# def task(env, name, resource, start_time, duration):
|
|||
|
|
# """任务进程"""
|
|||
|
|
# yield env.timeout(start_time) # 等待任务开始时间
|
|||
|
|
# print(f"{name} tries to use the resource at {env.now}")
|
|||
|
|
# with resource.request() as req:
|
|||
|
|
# yield req
|
|||
|
|
# print(f"{name} starts using the resource at {env.now}")
|
|||
|
|
# yield env.timeout(duration)
|
|||
|
|
# print(f"{name} finishes using the resource at {env.now}")
|
|||
|
|
#
|
|||
|
|
# # 创建仿真环境
|
|||
|
|
# env = simpy.Environment()
|
|||
|
|
#
|
|||
|
|
# # 创建共享资源
|
|||
|
|
# resource = simpy.Resource(env, capacity=1)
|
|||
|
|
#
|
|||
|
|
# # 启动任务进程
|
|||
|
|
# env.process(task(env, "Task A", resource, 0, 5)) # 任务A,从时间0开始,持续5个时间单位
|
|||
|
|
# env.process(task(env, "Task B", resource, 2, 3)) # 任务B,从时间2开始,持续3个时间单位
|
|||
|
|
#
|
|||
|
|
# # 运行仿真
|
|||
|
|
# env.run()
|
|||
|
|
|
|||
|
|
import simpy
|
|||
|
|
import numpy as np
|
|||
|
|
import matplotlib.pyplot as plt
|
|||
|
|
|
|||
|
|
def task(env, name, resource, start_time, duration, conflicts,taskID):
|
|||
|
|
"""任务进程"""
|
|||
|
|
yield env.timeout(start_time) # 等待任务开始时间
|
|||
|
|
print(f"{name} tries to use the resource at {env.now}")
|
|||
|
|
with resource.request() as req:
|
|||
|
|
yield req
|
|||
|
|
print(f"{name} starts using the resource at {env.now}")
|
|||
|
|
if resource.count == 1: # 检查是否发生冲突
|
|||
|
|
conflicts[0] = 1 # 记录冲突事件
|
|||
|
|
yield env.timeout(duration)
|
|||
|
|
print(f"{name} finishes using the resource at {env.now}")
|
|||
|
|
|
|||
|
|
def run_simulation(num_tasks, num_rounds):
|
|||
|
|
"""运行多轮仿真"""
|
|||
|
|
# 存储冲突概率的列表
|
|||
|
|
conflict_probabilities = []
|
|||
|
|
task_start_times = np.random.randint(0, 10, num_tasks) # 任务开始时间均匀分布
|
|||
|
|
count = 0
|
|||
|
|
for round_num in range(num_rounds):
|
|||
|
|
count = count + 1
|
|||
|
|
print("=============第" + str(count) + "轮=============")
|
|||
|
|
# 创建仿真环境
|
|||
|
|
env = simpy.Environment()
|
|||
|
|
|
|||
|
|
# 创建共享资源
|
|||
|
|
resource = simpy.Resource(env, capacity=1)
|
|||
|
|
|
|||
|
|
# 记录冲突事件的列表
|
|||
|
|
conflicts = [0]
|
|||
|
|
|
|||
|
|
# 仿真参数
|
|||
|
|
task_durations = np.random.normal(5, 1, num_tasks) # 任务持续时间正态分布
|
|||
|
|
|
|||
|
|
|
|||
|
|
a = np.zeros([num_rounds,num_tasks])
|
|||
|
|
print(task_start_times[0],task_start_times[1])
|
|||
|
|
# 启动任务进程
|
|||
|
|
for i in range(num_tasks):
|
|||
|
|
##conflicts[i] = 0
|
|||
|
|
env.process(task(env, f"Task {i}", resource, task_start_times[i], 10, conflicts, i))
|
|||
|
|
# 运行仿真
|
|||
|
|
env.run()
|
|||
|
|
print("=============end=============")
|
|||
|
|
for j in range(1, num_tasks):
|
|||
|
|
task_start_times[j] = task_start_times[j] + 10
|
|||
|
|
# 计算并记录冲突概率
|
|||
|
|
conflict_probabilities.append(conflicts[0] / num_tasks)
|
|||
|
|
|
|||
|
|
return conflict_probabilities
|
|||
|
|
|
|||
|
|
# 参数设置
|
|||
|
|
num_tasks = 2
|
|||
|
|
num_rounds = 2
|
|||
|
|
|
|||
|
|
# 运行多轮仿真
|
|||
|
|
conflict_probabilities = run_simulation(num_tasks, num_rounds)
|
|||
|
|
|
|||
|
|
# 绘制多个冲突概率曲线
|
|||
|
|
plt.plot(conflict_probabilities)
|
|||
|
|
plt.xlabel('Simulation Round')
|
|||
|
|
plt.ylabel('Conflict Probability')
|
|||
|
|
plt.title('Conflict Probability Across Simulation Rounds')
|
|||
|
|
plt.grid(True)
|
|||
|
|
plt.show()
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|