- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试模拟两条生产线(A 线和 B 线)的运行。它们的故障时间遵循 Weibull 分布(形状 = 0.7,比例 = 12(分钟))。 A 线和 B 线均以 100 件产品/分钟的速度生产。从现实世界的场景来看,如果任何一条线出现故障,我想提高其他线的速率(比如 - 120 产品/分钟)直到故障线没有修复的时间。
挑战:随着速率的增加,失败的机会增加,因此,尺度参数发生变化(例如,对于 120 产品/分钟的速率,尺度参数从 12 分钟变为 10 分钟)。我想在仿真中对这种分布参数的变化进行建模。
示例:
目前我正在使用 Python Simpy 编写逻辑代码,但找不到对此建模的方法。任何帮助或引用都会非常有帮助。这是我的尝试,但我肯定遗漏了一些东西。
import simpy
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import weibull_min
class Machine(object):
def __init__(self, env, name,scale_parameter,shape_parameter, mean_repair_time,std_repair_time,increased_rate):
self.env = env
self.name = name
self.scale_parameter = scale_parameter
self.shape_parameter = shape_parameter
self.mean_repair_time = mean_repair_time
self.std_repair_time = std_repair_time
self.increased_rate = increased_rate
self.broken = False
self.processing_list = [0,]
self.machine_status = 1
self.process = env.process(self.run())
# Start the failure process
env.process(self.check_machines_status())
def run(self):
"""
Run as long as the simulation runs.
"""
while True:
try:
yield self.env.timeout(self.mean_time_to_failure())
self.processing_list.append(self.env.now)
print(f'{self.env.now:.2f} {self.name} is in failure.')
trial_resource.get(1)
yield self.env.timeout(self.mean_time_to_repair())
print(f'{self.env.now:.2f} {self.name} is repaired.')
self.processing_list.append(env.now)
trial_resource.put(1)
except simpy.Interrupt:
self.machine_status = 0
yield self.env.timeout(self.updated_mean_time_to_failure())
print(f'{self.env.now:.2f} {self.name} is in updated failure.')
#trial_resource.get(1)
self.broken = True
yield self.env.timeout(self.mean_time_to_repair())
print(f'{self.env.now:.2f} {self.name} is in updated repaired.')
trial_resource.put(1)
self.machine_status =1
def check_machines_status(self):
"""Periodically check the status of running machines. If any machine fails
interrupt the process"""
while True:
print(self.env.now,trial_resource.level)
print(self.name)
if trial_resource.level < trial_resource.capacity and self.broken == False and self.machine_status == 1:
# Only break the machine if it is currently working.
self.process.interrupt()
print('Machine running process interrupted %d' % env.now)
yield env.timeout(1)
def mean_time_to_failure(self):
x = int(weibull_min.rvs(self.shape_parameter, loc=0, scale= self.scale_parameter, size=1).tolist()[0])
if x == 0:
x = 1
return x
def updated_mean_time_to_failure(self):
correction_factor = (1-self.increased_rate)/100
x = int(weibull_min.rvs(self.shape_parameter*correction_factor, loc=0, scale= self.scale_parameter, size=1).tolist()[0])
if x == 0:
x = 1
return x
def mean_time_to_repair(self):
x = int(np.random.lognormal(self.mean_repair_time,self.std_repair_time))
if x ==0:
x =1
return x
env = simpy.Environment()
trial_resource = simpy.Container(env,init=3,capacity=3)
machine_1 = Machine(env, 'M1', 12, 0.65, 0.51,1,10)
machine_2 = Machine(env, 'M2', 14, 0.65, 0.51,1,10)
machine_3 = Machine(env, 'M3', 8, 0.65, 0.51,1,10)
env.run(until = 12)
print(machine_1.processing_list)
print(machine_2.processing_list)
print(machine_3.processing_list)
最佳答案
此解决方案可以处理超过 2 台机器。当一台机器发生故障时,它会向所有其他机器发送一条消息,通知它们发生故障。机器在修复时也会发送通知。每台机器都会跟踪有多少台机器坏了,如果有一台或多台机器坏了,就会加快生产速度。失败前时间 (ttf) 也加快了。当速率发生变化时,当前正在进行的部分的进度将被纳入计算新完成时间的因素。中断既用于触发故障,也用于在正在进行的工作完成时重置。另请查看文档中的机械车间示例。我没时间做这件事,所以它可能并不完美
"""
Machine shop example where machines change their
production rate and break down rates in response
to other machines breaking down or getting fixed
If one machine breaks down, the remaining machines
will speed up until all machines are fixed
speeding up the production rate also speeds up the
time till failure ttf
rates can change in the middle of making a part
Programmmer: Michael R. Gibbs
"""
import simpy
import random
class Machine():
"""
Machine that makes parts.
Machine breaks down at ramdom time
The machine has a normal mode, and a fast mode
Machine speeds up when it receives a message that
another machine has broken down and slows back down
when it receives messages that all broken machines are fixed
env: simulation environment
id: name of the machine
rate: number of parts per time unit (normal mode)
fast_rate: number of parts per time unit (fast mode)
ttf_gen: zero param func to generate ttf (normal mode)
fast_ttf_gen: zero param func to generate ttf (fast mode)
"""
def __init__(self, env, id, rate, fast_rate, ttf_gen, fast_ttf_gen):
self.id = id
self.env = env
self.rate = rate
self.fast_rate = fast_rate
self.ttf_gen = ttf_gen
self.fast_ttf_gen = fast_ttf_gen
self.broken = False
self.fast_mode = False
self.remaining_time = 0
self.current_rate = self.rate
self.mach_list = []
self.broke_cnt = 0
# start the part making, an the count down till failure
self.make_parts_proc = self.env.process(self.make_parts())
self.breakdown_proc = self.env.process(self.gen_breakdown())
def make_parts(self):
"""
Main loop to manufacture parts
interupts are used to trigger updates
when rates change and when breakdowns occure
"""
while True:
if self.remaining_time <= 0:
# starting a new part
print(f'{self.env.now:.2f}: mach: {self.id} has started a part')
# need to store times so other methods can upate
# the procesing state
self.total_part_time = 1 / self.current_rate
self.remaining_time = self.total_part_time
while self.remaining_time > 0:
# need to loop incase get inturrupted
# while in the middle of making a part
try:
self.part_start_time = self.env.now # used to track progress
yield self.env.timeout(self.remaining_time)
# got here without being interupted, done with part
self.remaining_time = 0
except simpy.Interrupt:
# can get inturpted because processing rate changed
# or a breakdown has happend
# if rate changed, we are using the inturput to update
# the processing timeout
if self.broken:
# processing is interuped, fix machine
# update processing progress
self.remaining_time -= (self.env.now - self.part_start_time)
print(f'{self.env.now:.2f}: mach: {self.id} has broken down')
# notify other machines that this machine has broke
for m in self.mach_list:
m.someone_broke(self)
# time out for fixing
yield self.env.timeout(5)
# notify other machines that this machine is fixed
for m in self.mach_list:
m.someone_fixed(self)
print(f'{self.env.now:.2f}: mach: {self.id} has been fixed')
# start a new breakdown count down
self.breakdown_proc = self.env.process(self.gen_breakdown())
self.broken = False
print(f'{self.env.now:.2f}: mach: {self.id} has finish a part')
def gen_breakdown(self):
"""
counts down to failure and uses
a interrupt to stop processing
and start repairs
using class properties instead of local
variables so other methods can update
the countdown state of ttf
"""
if not self.broken:
# get breakdown based on current fast mode
if self.fast_mode:
self.total_ttf = self.fast_ttf_gen()
else:
self.total_ttf = self.ttf_gen()
self.remaining_ttf = self.total_ttf
while self.remaining_ttf > 0:
self.ttf_start = self.env.now
print(f'{self.env.now:.2f}: mach: {self.id} has {self.remaining_ttf} till breakdown')
try:
yield self.env.timeout(self.remaining_ttf)
# failure has orrured
self.broken = True
self.make_parts_proc.interrupt()
self.remaining_ttf = 0
except simpy.Interrupt:
# the state has been updated
# the timeout has been interupted
# so it can be restarted with new state
print(f'{self.env.now:.2f}: mach: {self.id} updating ttf {self.remaining_ttf}')
print(f'{self.env.now:.2f}: mach: {self.id} ttf gen exit')
def someone_broke(self, mach):
"""
Another machine is notifing this machine that it has broken
and this machine needs to change to fast mode, if not already
in fast mode
"""
self.broke_cnt += 1
print(f'{self.env.now:.2f}: mach: {self.id} received mess that mach: {mach.id} has broke, broke cnt: {self.broke_cnt}')
if not self.fast_mode:
self.fast_mode = True
# update the ttf based on the fast mode ttf func
# keep the same progress so if we were 90% of of the
# old ttf, then set things so we are still 90% of new ttf
# update with the last bit of progress
self.remaining_ttf -= self.env.now - self.ttf_start
per_ttf_left = (self.remaining_ttf/self.total_ttf)
# update based on fast ttf
self.total_ttf = self.fast_ttf_gen()
self.remaining_ttf = per_ttf_left * self.total_ttf
if self.remaining_ttf <= 0:
# special case when notification happens at same time as breakdown
self.remaining_ttf = 0.001
# update the part processing state
# update the last bit of processing progress
self.remaining_time -= self.env.now - self.part_start_time
# update state based on new processing fast rate
# like ttf keep the same perenct of progress
self.current_rate = self.fast_rate
old_total = self.total_part_time
self.total_part_time = 1 / self.current_rate
per_left = self.remaining_time / old_total
self.remaining_time = self.total_part_time * per_left
if not self.broken:
# if broken nothing to interrupt
# new states will used when machine
# is fixed and processing starts up again
self.breakdown_proc.interrupt()
self.make_parts_proc.interrupt()
def someone_fixed(self, mach):
"""
Another machine is notifing this machine that it has been fixed
and this machine needs to change to normal mode, if there are
no more broken machines
"""
self.broke_cnt -= 1
print(f'{self.env.now:.2f}: mach: {self.id} received mess that mach: {mach.id} is fixed, broke cnt: {self.broke_cnt}')
# only change if all machines are fixed and broke cnt is 0
if self.broke_cnt <= 0:
self.broke_cnt = 0
if self.fast_mode:
self.fast_mode = False
# update the ttf based on the normal mode ttf func
# keep the same progress so if we were 90% of of the
# old ttf, then set things so we are still 90% of new ttf
# update with the last bit of progress
self.remaining_ttf -= self.env.now - self.ttf_start
per_ttf_left = (self.remaining_ttf/self.total_ttf)
self.total_ttf = self.ttf_gen()
self.remaining_ttf = per_ttf_left * self.total_ttf
if self.remaining_ttf <= 0:
# special case when notifcation happens at breakdown time
self.remaining_ttf = 0.001
# update state based on new processing normal rate
# like ttf keep the same perenct of progress
self.remaining_time -= self.env.now - self.part_start_time
self.current_rate = self.rate
old_total = self.total_part_time
self.total_part_time = 1 / self.current_rate
per_left = self.remaining_time / old_total
self.remaining_time = self.total_part_time * per_left
if not self.broken:
# if broken nothing to interrupt
# new states will be used when machine
# is fixed and processing starts up again
self.breakdown_proc.interrupt()
self.make_parts_proc.interrupt()
def set_mach_list(self, mach_list):
"""
set the list of machines to be notified if this machine
breaks down, or is fixed
"""
self.mach_list = mach_list
# ttf generator function
#
# by wrapping a dist in a lambda I
# created create a 0 param function
# that I can pass to the Machine class
# To change the dist I just need to
# update the lambda, no hard codeing
# of dist func parameters in the Machine
# class code
ttf = lambda : random.randint(8,10)
fast_ttf = lambda: random.randint(5,7)
# create sim
env = simpy.Environment()
mach_list = []
machines_cnt = 2 # can be more then 2
for i in range(1, machines_cnt + 1):
m = Machine(env, i, 5, 8, ttf, fast_ttf)
mach_list.append(m)
# build list of machines to notify
# when a machine breaks or gets fixed
for m in mach_list:
# filter out the current machine
# don't want to send to self
other_m = mach_list.copy()
other_m.remove(m)
m.set_mach_list(other_m)
env.run(until = 50)
print("end of simulation")
关于python - 蒙特卡罗模拟 : Underlying distribution parameter changes during simulation run based on conditions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74010378/
如果我不定义自己的构造函数,Base *b = new Base; 与 Base *b = new Base(); 之间有什么区别吗? 最佳答案 初始化是标准中要遵循的一种 PITA...然而,这两个
是否有现成的函数可以在 C# 中进行基本转换?我希望将以 26 为基数和以 27 为基数的数字转换为以 10 为基数。我可以在纸上完成,但我不是一个非常有经验的程序员,如果可能的话,我宁愿不要从头开始
JNA 中'base'是什么意思 Pointer.getPointerArray(long base) Pointer.getStringArray(long base) ? JNA Document
我正在做一个将数字从 10 进制转换为 2 进制的基本程序。我得到了这段代码: #include #include #include #include using namespace std;
“假设以下代码: public class MultiplasHerancas { static GrandFather grandFather = new GrandFather();
当我分析算法的时候,我突然问自己这个问题,如果我们有三元计算机时间复杂度会更便宜吗?还是有任何基础可以让我们构建计算机,这样时间复杂度分析就无关紧要了?我在互联网上找不到太多,但是基于三元的计算机在给
一个简化的场景。三个类,GrandParent,Parent 和 Child。我想要做的是利用 GrandParent 和 Parent 构造函数来初始化一个 Child 实例。 class Gran
我编写了一个简单的函数来将基数为 10 的数字转换为二进制数。我编写的函数是我使用我所知道的简单工具的最佳尝试。我已经在这个网站上查找了如何执行此操作的其他方法,但我还不太了解它。我确定我编写的函数非
我尝试了以下代码将数字从 base-10 转换为另一个 base。如果目标基地中没有零(0),它就会工作。检查 79 和 3 并正确打印正确的 2221。现在尝试数字 19 和 3,结果将是 21 而
这个问题在这里已经有了答案: Is Big O(logn) log base e? (7 个答案) 关闭 8 年前。 Intro 练习 4.4.6 的大多数解决方案。算法第三版说,n*log3(n)
如何判断基类(B)的指针是否(多态)重写了基类的某个虚函数? class B{ public: int aField=0; virtual void f(){}; }; class C
我测试了这样的代码: class A { public A() { } public virtual void Test () { Console.WriteL
两者都采用相同的概念:定义一些行和列并将内容添加到特定位置。但是 Grid 是最常见的 WPF 布局容器,而 html 中基于表格的布局是 very controversial .那么,为什么 WPF
我试图在 JS 中“获得”继承。我刚刚发现了一种基本上可以将所有属性从一个对象复制到另一个对象的简洁方法: function Person(name){ this.name="Mr or Miss
class A { public override int GetHashCode() { return 1; } } class B : A { pu
我有一个 Base32 信息哈希。例如IXE2K3JMCPUZWTW3YQZZOIB5XD6KZIEQ ,我需要将其转换为base16。 我怎样才能用 PHP 做到这一点? 我的代码如下所示: $ha
我已经使用其实验界面对 Google Analytics 进行了一些实验,一切似乎都运行良好,但我无法找到 Google Analytics 属性如何达到变体目标的答案,即归因 session - 基
if (state is NoteInitial || state is NewNote) return ListView.builder(
MSVC、Clang 和 GCC 不同意此代码: struct Base { int x; }; struct Der1 : public Base {}; struct Der2 : public
我已经尝试构建一个 Base 10 到 Base 2 转换器... var baseTen = window.prompt("Put a number from Base 10 to conver
我是一名优秀的程序员,十分优秀!