模拟:T时间内的N个任务

时间:2014-07-31 07:57:42

标签: python simulation

我正在建立一个模拟器,用于在线任务调度系统中的任务到达。 我必须运行模拟一段时间T(以滴答数而非时钟时间测量) 我将获得N到达T的任务数量mean以及到达时间import numpy.random as R t = 0 T = 2700 # i want to run the simulation for 2700 ticks (assume 1 tick = 1 minute) N = 8000 # total number of tasks in the system is 8000 (Say) mean = 5 arrivals = [] nextArrival = 0 arrivals.append(nextArrival) while t < T: nextArrival = arrivals[-1] + R.poisson(5) # a task comes in here and i do some processing # which is not relevant to the question here arrivals.append(nextArrival) t = nextArrival print len(arrivals) print "\n", arrivals 。{/ p>

我不知道每个时间点有多少任务到达。

这是我的代码。

$ python sim.py
563

[0, 9, 12, 17, 21, 27, 30, 35, 40, 44, 52, 59, 66, 67, 71, 75, 78, 85, 87, 95, 97, 102, 108, 113, 116, 120, 125, 130, 136, 141, 147, 152, 157, 162, 163, 167, 169, 173, 180, 183, 187, 188, 200, 200, 203, 206, 212, 215, 219, 225, 229, 233, 241, 245, 249, 253, 258, 263, 266, 270, 274, 278, 284, 288, 298, 301, 306, 308, 310, 316, 322, 327, 331, 334, 345, 352, 360, 366, 370, 373, 379, 382, 390, 393, 396, 400, 408, 415, 417, 425, 431, 432, 434, 440, 444, 450, 454, 462, 467, 470, 476, 477, 483, 486, 490, 498, 509, 513, 515, 521, 527, 530, 539, 541, 545, 550, 553, 558, 562, 568, 575, 577, 583, 586, 596, 603, 608, 613, 615, 619, 621, 626, 633, 637, 640, 645, 650, 653, 654, 661, 667, 672, 677, 683, 687, 695, 703, 707, 709, 712, 722, 726, 731, 738, 742, 744, 746, 748, 751, 755, 758, 765, 769, 772, 777, 784, 790, 794, 795, 797, 800, 803, 807, 817, 823, 827, 834, 841, 845, 855, 858, 864, 869, 875, 880, 882, 890, 894, 902, 909, 916, 921, 926, 932, 939, 948, 949, 954, 959, 961, 962, 966, 972, 974, 979, 983, 990, 994, 998, 1002, 1006, 1013, 1020, 1025, 1026, 1028, 1035, 1038, 1044, 1046, 1052, 1058, 1063, 1067, 1071, 1076, 1079, 1084, 1093, 1099, 1107, 1110, 1112, 1118, 1125, 1135, 1138, 1141, 1145, 1147, 1152, 1155, 1159, 1164, 1169, 1174, 1182, 1184, 1190, 1193, 1199, 1205, 1209, 1213, 1220, 1222, 1229, 1230, 1234, 1240, 1245, 1252, 1256, 1258, 1259, 1262, 1268, 1276, 1278, 1282, 1283, 1287, 1292, 1299, 1307, 1311, 1315, 1321, 1328, 1331, 1339, 1343, 1347, 1351, 1357, 1357, 1361, 1364, 1369, 1371, 1373, 1377, 1379, 1390, 1393, 1400, 1405, 1412, 1419, 1424, 1427, 1429, 1439, 1447, 1452, 1456, 1460, 1463, 1469, 1474, 1479, 1486, 1490, 1493, 1501, 1506, 1509, 1515, 1518, 1519, 1522, 1525, 1528, 1532, 1538, 1544, 1546, 1550, 1554, 1561, 1565, 1567, 1575, 1577, 1584, 1587, 1592, 1594, 1596, 1601, 1605, 1609, 1611, 1612, 1617, 1622, 1625, 1633, 1636, 1641, 1645, 1650, 1653, 1661, 1667, 1672, 1676, 1682, 1694, 1703, 1709, 1718, 1726, 1732, 1737, 1745, 1751, 1756, 1757, 1760, 1767, 1778, 1781, 1781, 1785, 1791, 1794, 1798, 1799, 1804, 1810, 1812, 1816, 1822, 1829, 1834, 1836, 1841, 1853, 1856, 1861, 1866, 1870, 1872, 1877, 1881, 1881, 1888, 1896, 1899, 1902, 1911, 1914, 1916, 1919, 1925, 1928, 1929, 1934, 1937, 1941, 1947, 1951, 1955, 1960, 1964, 1967, 1973, 1975, 1981, 1985, 1990, 1993, 1999, 2002, 2005, 2007, 2012, 2017, 2023, 2026, 2035, 2043, 2044, 2051, 2055, 2058, 2059, 2063, 2071, 2076, 2080, 2084, 2087, 2092, 2094, 2100, 2107, 2110, 2118, 2130, 2141, 2144, 2150, 2157, 2165, 2168, 2175, 2184, 2191, 2195, 2200, 2203, 2207, 2211, 2217, 2223, 2225, 2228, 2231, 2236, 2242, 2248, 2253, 2256, 2262, 2269, 2275, 2281, 2285, 2288, 2293, 2297, 2300, 2300, 2304, 2313, 2317, 2320, 2323, 2333, 2336, 2341, 2346, 2353, 2356, 2359, 2363, 2364, 2370, 2372, 2375, 2379, 2387, 2392, 2396, 2403, 2415, 2421, 2427, 2431, 2435, 2437, 2439, 2446, 2447, 2452, 2458, 2473, 2477, 2483, 2489, 2495, 2500, 2503, 2509, 2513, 2519, 2526, 2528, 2532, 2538, 2545, 2552, 2557, 2565, 2572, 2576, 2578, 2588, 2590, 2594, 2602, 2603, 2610, 2615, 2618, 2628, 2632, 2638, 2645, 2645, 2650, 2653, 2662, 2665, 2671, 2680, 2688, 2690, 2695, 2698, 2701]

输出:(不确定它是否有帮助!)

563

上面列出的列表是任务的到达时间。 现在考虑每次到达时的1个任务,我可以在2700分钟内生成arrivals = [] nextArrival = 0 taskCount = 0 t = 0 arrivals.append(nextArrival) while taskCount < N: nextArrival = arrivals[-1] + R.poisson(5) arrivals.append(nextArrival) t = nextArrival taskCount += 1 print t $ python main.py 40283 个任务,而有必要生成8000个任务。

另一方面,如果我运行模拟直到生成如下所需的任务数,则时间会变为40283。

n < N

正如我所看到的那样(普遍化)问题在于: 如果在每次到达时我考虑1个任务,则模拟会导致某些N任务不正确。如果我为T任务运行模拟,则时间超出{{1}}。

  • 据我所知,两者都不正确,我们需要知道在每个时间实例生成的平均任务。我们如何确定? (有些相关问题:Problems Simulating Interarrival Times
  • 我在这里错过了什么吗?我是模拟和领域的新手。建模并且对此不太了解。
  • 我如何实现目标?

其他问题:

  • 我被要求为上述过程绘制直方图,这是否意味着我记录到达时间并绘制直方图?

1 个答案:

答案 0 :(得分:1)

如果这应该是以特定数量的到达为条件的泊松过程,最简单的方法是生成80000个值,每个值从0到27000均匀分布,然后对值进行排序。排序集是事件时间,或者,如果您愿意,它们之间的差异是事件间时间。