请原谅我的代码,我是新手,并不是非常pythonic。我正在努力用监视器设置基本模拟。最好使用Simpy的Monitor类。
我已经为三个比萨的订单定义了概率分布,每个比萨都有各种成分。我想模拟1000天的订单,以便为每种成分的日常需求产生概率分布。我的代码产生了一个平均值,但我也需要方差,而且清洁度只是监控输出。
我确信它很简单,因为无法使Monitor()和.observ()代码工作。有帮助吗?干杯!!
P.S。下面的披萨配料不一定反映作者的偏好。
import numpy as np
import random, sys
from SimPy.Simulation import *
class g:
crust = 0
cheese = 0
pepperoni = 0
pineapple = 0
olive = 0
def pepperoni_pizza():
mean = 10 #average pepperoni pizzas ordered
std = 2 #standard deviation of pizzas ordered
norders = np.random.normal(mean, std) #draw random value
g.crust += norders * 10 #value indicates the amount of ingredient required for this pizza
g.cheese += norders * 5
g.pepperoni += norders * 5
g.pineapple += norders * 0
g.olive += norders * 5
def pineapple_pizza():
mean = 20
std = 6
norders = np.random.normal(mean, std)
g.crust += norders * 10
g.cheese += norders * 0
g.pepperoni += norders * 0
g.pineapple += norders * 5
g.olive += norders * 5
def cheese_pizza():
mean = 7
std = 3
norders = np.random.normal(mean, std)
g.crust += norders * 10
g.cheese += norders * 10
g.pepperoni += norders * 0
g.pineapple += norders * 0
g.olive += norders * 5
def main():
nreps = 1000
for rep in range(nreps):
while True:
pepperoni_pizza()
pineapple_pizza()
cheese_pizza()
break
print 'crust needed =', g.crust/float(nreps)
print 'cheese needed =', g.cheese/float(nreps)
print 'pepperoni needed =', g.pepperoni/float(nreps)
print 'pineapple needed =', g.pineapple/float(nreps)
print 'olive needed =', g.olive/float(nreps)
if __name__ == '__main__': main()