gpt4 book ai didi

python - 类型错误 - Python Simpy

转载 作者:行者123 更新时间:2023-12-01 05:01:02 30 4
gpt4 key购买 nike

我已经在线搜索了许多与 TypeError 相关的答案,并多次扫描了我的代码,但我似乎看不到我缺少的第三个参数是什么。我正在使用 Python 2.7 和 Simpy 3

我的代码如下:

    import simpy
import random

RANDOM_SEED = 42
NUM_SERVERS = 1
MTBF = 10
MTTR = 5
TOTAL_ENGINES = 6
TOTAL_SPARES = 3
TOTAL_IN_USE = TOTAL_ENGINES - TOTAL_SPARES
SIM_TIME = 100

class Working(object):

def __init__ (self, env, num, repair_facility, spares_inventory, downtime):
self.env = env
self.repair_facility = repair_facility
self.spares_inventory = spares_inventory
self.downtime = downtime
self.name = 'Engine %d' % (num + 1)
print('%s at %.2f' % (self.name, self.env.now))
self.env.process(self.run())

def run(self):
yield self.env.timeout(random.expovariate(1.0 / MTBF))
print('%s at %.2f' % (self.name, self.env.now))

downtime_start = self.env.now
spare = yield self.spares_inventory.get()
self.downtime.append(self.env.now - downtime_start)

print('%s at %.2f' % (spare.name, self.env.now))
print('%d' % len(spares_inventory.items))

with self.repair_facility.request() as req:
yield req
print('%s begins repair at %.2f' % (self.name, self.env.now))

yield self.env.timeout(random.expovariate(1.0 / MTTR))

yield self.spares_inventory.put(self)
print('%s at %.2f' % (self.name, self.env.now))

print('%d' % len(spares_inventory.items))

def main():
env = simpy.Environment()
repair_facility = simpy.Resource(env, capacity = NUM_SERVERS)
spares_inventory = simpy.Container(env, capacity = TOTAL_ENGINES, init = TOTAL_SPARES)
downtime = []
working = [Working(env, i, repair_facility, spares_inventory, downtime) for i in range(TOTAL_IN_USE)]

env.run(SIM_TIME)

if __name__ == '__main__':
main()

这是我不断收到的错误:

回溯(最近一次调用最后一次):

      File "", line 61, in <module>
main()
File "", line 55, in main
env.run(SIM_TIME)
File "", line 120, in run
self.step()
File "", line 213, in step
raise event._value
TypeError: __init__() takes exactly 3 arguments (2 given)

非常感谢任何帮助,提前非常感谢

最佳答案

您在回溯中忘记了一些额外信息;在您引用的回溯上方,有几行内容是:

Traceback (most recent call last):
File "/data/evertr/sw/lib/python2.7/site-packages/simpy/events.py", line 312, in _resume
event = self._generator.send(event._value)
File "simptest.py", line 31, in run
spare = yield self.spares_inventory.get()
TypeError: __init__() takes exactly 3 arguments (2 given)

The above exception was the direct cause of the following exception:

后面是您的回溯。

这样,您可以看到 self.spares_inventory.get()调用才是真正的罪魁祸首。令人烦恼的是,这个方法实际上是一个隐藏的类实例化(我注意到,在幕后发生了很多棘手的事情),这就是为什么你会看到 __init__()警告。

基本上,您需要提供 amountself.spares_inventory.get() (无论好坏,没有方便的默认值 1)。

所以将其更改为

spare = yield self.spares_inventory.get(1)

可以解决你的问题。

(不过之后您会遇到其他错误;您会发现的。这些新错误遵循相同的结构:回溯,后跟行 The above exception was the direct cause of the following exception ,然后是另一个(不太相关的)回溯)。

关于python - 类型错误 - Python Simpy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26217994/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com