不能让资源在某个时间段内不可用
Can't make resources unavailable in a certain time period
代码如下。实际上,我有两个节点 0 和 1,其中服务器容量分别为 5 和 7。服务器每天上午 9 点前(午夜起 540 分钟)和下午 6 点后不可用。我正在尝试使用超时创建不可用但不工作。我仍然看到项目在第二天下午 6 点之后(即 2520 分钟后)进入节点 0 和 1。无法找出错误。非常感谢!
import numpy as np
import simpy
def interarrival():
return(np.random.exponential(20))
def servicetime():
return(np.random.exponential(60))
def servicing(env, servers_1):
i = 0
while(True):
i = i+1
yield env.timeout(interarrival())
print("Customer "+str(i)+ " arrived in the process at "+str(env.now))
state = 0
env.process(items(env, i, servers_array, state))
def items(env, customer_id, servers_array, state):
with servers_array[state].request() as request:
yield request
t_arrival = env.now
print("Customer "+str(customer_id)+ " arrived in "+str(state)+ " at "+str(t_arrival))
yield env.timeout(servicetime())
t_depart = env.now
print("Customer "+str(customer_id)+ " departed from "+str(state)+ " at "+str(t_depart))
if (state == 1):
print("Customer exits")
else:
state = 1
env.process(items(env, customer_id, servers_array, state))
def delay(env, servers_array):
while(True):
#print(servers_array[0].request())
if (env.now%1440 >= 540 and env.now%1440 < 1080):
yield env.timeout(1080 - env.now%1440)
else:
print(str(env.now), "at resources will be blocked")
resource_unavailability_dict = dict()
resource_unavailability_dict[0] = []
resource_unavailability_dict[1] = []
for nodes in resource_unavailability_dict:
for _ in range(servers_array[nodes].capacity):
resource_unavailability_dict[nodes].append(servers_array[nodes].request())
print(resource_unavailability_dict)
for nodes in resource_unavailability_dict:
yield env.all_of(resource_unavailability_dict[nodes])
if (env.now < 540):
yield env.timeout(540)
else:
yield env.timeout((int(env.now/1440)+1)*1440+540 - env.now)
for nodes in resource_unavailability_dict:
for request in resource_unavailability_dict[nodes]:
servers_array[nodes].release(request)
print(str(env.now), "resources are released")
env = simpy.Environment()
servers_array = []
servers_array.append(simpy.Resource(env, capacity = 5))
servers_array.append(simpy.Resource(env, capacity = 7))
env.process(servicing(env, servers_array))
env.process(delay(env,servers_array))
env.run(until=2880)
我想我找到了。你屈服于 int 表达,我认为你需要用超时事件包装你的 int 表达式
尝试改变
yield(1080 - env.now%1440)
至
yield env.timeout(1080 - env.now%1440)
在延迟中,
我不知道如果您的 int 表达式将负值传递给超时会发生什么。您可能需要为此添加一个支票
代码如下。实际上,我有两个节点 0 和 1,其中服务器容量分别为 5 和 7。服务器每天上午 9 点前(午夜起 540 分钟)和下午 6 点后不可用。我正在尝试使用超时创建不可用但不工作。我仍然看到项目在第二天下午 6 点之后(即 2520 分钟后)进入节点 0 和 1。无法找出错误。非常感谢!
import numpy as np
import simpy
def interarrival():
return(np.random.exponential(20))
def servicetime():
return(np.random.exponential(60))
def servicing(env, servers_1):
i = 0
while(True):
i = i+1
yield env.timeout(interarrival())
print("Customer "+str(i)+ " arrived in the process at "+str(env.now))
state = 0
env.process(items(env, i, servers_array, state))
def items(env, customer_id, servers_array, state):
with servers_array[state].request() as request:
yield request
t_arrival = env.now
print("Customer "+str(customer_id)+ " arrived in "+str(state)+ " at "+str(t_arrival))
yield env.timeout(servicetime())
t_depart = env.now
print("Customer "+str(customer_id)+ " departed from "+str(state)+ " at "+str(t_depart))
if (state == 1):
print("Customer exits")
else:
state = 1
env.process(items(env, customer_id, servers_array, state))
def delay(env, servers_array):
while(True):
#print(servers_array[0].request())
if (env.now%1440 >= 540 and env.now%1440 < 1080):
yield env.timeout(1080 - env.now%1440)
else:
print(str(env.now), "at resources will be blocked")
resource_unavailability_dict = dict()
resource_unavailability_dict[0] = []
resource_unavailability_dict[1] = []
for nodes in resource_unavailability_dict:
for _ in range(servers_array[nodes].capacity):
resource_unavailability_dict[nodes].append(servers_array[nodes].request())
print(resource_unavailability_dict)
for nodes in resource_unavailability_dict:
yield env.all_of(resource_unavailability_dict[nodes])
if (env.now < 540):
yield env.timeout(540)
else:
yield env.timeout((int(env.now/1440)+1)*1440+540 - env.now)
for nodes in resource_unavailability_dict:
for request in resource_unavailability_dict[nodes]:
servers_array[nodes].release(request)
print(str(env.now), "resources are released")
env = simpy.Environment()
servers_array = []
servers_array.append(simpy.Resource(env, capacity = 5))
servers_array.append(simpy.Resource(env, capacity = 7))
env.process(servicing(env, servers_array))
env.process(delay(env,servers_array))
env.run(until=2880)
我想我找到了。你屈服于 int 表达,我认为你需要用超时事件包装你的 int 表达式
尝试改变
yield(1080 - env.now%1440)
至
yield env.timeout(1080 - env.now%1440)
在延迟中, 我不知道如果您的 int 表达式将负值传递给超时会发生什么。您可能需要为此添加一个支票