
在Python中實(shí)現(xiàn)貪婪排名算法的教程
這篇文章主要介紹了在Python中實(shí)現(xiàn)貪婪排名算法的教程,也是對學(xué)習(xí)算法的一個很好的演示,需要的朋友可以參考下
在較早的一遍文章中,我曾經(jīng)提到過我已經(jīng)寫了一個屬于自己的排序算法,并且認(rèn)為需要通過一些代碼來重新回顧一下這個排序算法。
對于我所完成的工作,我核實(shí)并且保證微處理器的安全。對非常復(fù)雜的CPU進(jìn)行測試的一個方法就是創(chuàng)建該芯片的另一個模型,其可以用來產(chǎn)生在CPU上運(yùn)行的偽隨機(jī)指令流。這所謂的ISG(指令流產(chǎn)生器)能夠在很短的時間內(nèi)創(chuàng)建幾千(甚至幾百萬)個這樣的測試,通過某種方式,使其可以巧妙地給出一些對將在CPU上執(zhí)行的指令流的控制或操縱。
現(xiàn)在對這些指令流進(jìn)行模擬,可以通過每一個測試實(shí)例花費(fèi)的時間獲取到CPU的那一部分被使用了(這叫做被覆蓋)的信息,并且ISG所產(chǎn)生的的過個測試可能會覆蓋CPU的同一個區(qū)域。為了增加CPU的整體覆蓋范圍,我們啟動一個被稱作復(fù)原的行為——所有的測試都運(yùn)行,并且它們的覆蓋范圍和花費(fèi)的時間將被存儲起來。在這次復(fù)原的最后,您可能會有幾千個測試實(shí)例只覆蓋了CPU的某一部分。
如果你拿著這個復(fù)原測試的記過,并且對其進(jìn)行排序,你會發(fā)現(xiàn)這個測試結(jié)果的一個子集會給出它們覆蓋了CPU的所有部分。通常,上千的偽隨機(jī)測試可能會被排序,進(jìn)而產(chǎn)生一個只有幾百個測試的子列表,它們在運(yùn)行時將會給出同樣的覆蓋范圍。接下來我們經(jīng)常會做的是,查看CPU的哪個部分沒有被覆蓋,然后通過ISG或其它方法在產(chǎn)生更多的測試,來試圖填補(bǔ)這一空白。再然后會運(yùn)行一次新的復(fù)原,并且循環(huán)得再一次進(jìn)行排序來充分使用該CPU,以達(dá)到某個覆蓋范圍目標(biāo)。
對測試進(jìn)行排名是復(fù)原流程的一個重要部分,當(dāng)其進(jìn)行地很好時你可能就會忘記它。不幸的是,有時,當(dāng)我想要對其它數(shù)據(jù)進(jìn)行排名時,CAD工具廠商所提供的常用排名算法并不適合。因此,能夠擴(kuò)展到處理成百上千個測試和覆蓋點(diǎn)才是一個排名算法的本質(zhì)。
輸入
通常情況下,我不得不從其他CAD程序產(chǎn)生的文本或HTML文件來解析我的輸入 - 這是個是單調(diào)乏味的工作,我會跳過這個乏味的工作,而通過以Python字典的形式提供理想的輸入。 (有時用于解析輸入文件的代碼可以跟排名算法一樣大或著更大)。
讓我們假設(shè)每個ISG測試都有一個名稱,在確定的“時間”內(nèi)運(yùn)行,當(dāng)模擬顯示'覆蓋'設(shè)計中的 一組編號的特性時。解析之后,所收集的輸入數(shù)據(jù)由程序中的結(jié)果字典來表示。
results = {
# 'TEST': ( TIME, set([COVERED_POINT ...])),
'test_00': ( 2.08, set([2, 3, 5, 11, 12, 16, 19, 23, 25, 26, 29, 36, 38, 40])),
'test_01': ( 58.04, set([0, 10, 13, 15, 17, 19, 20, 22, 27, 30, 31, 33, 34])),
'test_02': ( 34.82, set([3, 4, 6, 12, 15, 21, 23, 25, 26, 33, 34, 40])),
'test_03': ( 32.74, set([4, 5, 10, 16, 21, 22, 26, 39])),
'test_04': (100.00, set([0, 1, 4, 6, 7, 8, 9, 11, 12, 18, 26, 27, 31, 36])),
'test_05': ( 4.46, set([1, 2, 6, 11, 14, 16, 17, 21, 22, 23, 30, 31])),
'test_06': ( 69.57, set([10, 11, 15, 17, 19, 22, 26, 27, 30, 32, 38])),
'test_07': ( 85.71, set([0, 2, 4, 5, 9, 10, 14, 17, 24, 34, 36, 39])),
'test_08': ( 5.73, set([0, 3, 8, 9, 13, 19, 23, 25, 28, 36, 38])),
'test_09': ( 15.55, set([7, 15, 17, 25, 26, 30, 31, 33, 36, 38, 39])),
'test_10': ( 12.05, set([0, 4, 13, 14, 15, 24, 31, 35, 39])),
'test_11': ( 52.23, set([0, 3, 6, 10, 11, 13, 23, 34, 40])),
'test_12': ( 26.79, set([0, 1, 4, 5, 7, 8, 10, 12, 13, 31, 32, 40])),
'test_13': ( 16.07, set([2, 6, 9, 11, 13, 15, 17, 18, 34])),
'test_14': ( 40.62, set([1, 2, 8, 15, 16, 19, 22, 26, 29, 31, 33, 34, 38])),
}<span style="font-size:10pt;line-height:1.5;font-family:'sans serif', tahoma, verdana, helvetica;"></span>
貪婪排名算法的核心是對當(dāng)前選擇測試的子集進(jìn)行排序:
至少用一個測試集覆蓋盡可能大的范圍。
經(jīng)過第一個步驟,逐步減少測試集,同時覆蓋盡可能大的范圍。
給選擇的測試做出一個排序,這樣小數(shù)據(jù)集的測試也可以選擇使用
完成上述排序后,接下來就可以優(yōu)化算法的執(zhí)行時間了
當(dāng)然,他需要能在很大的測試集下工作。
貪婪排名算法的工作原理就是先選擇當(dāng)前測試集的某一項(xiàng)的最優(yōu)解,然后尋找下一項(xiàng)的最優(yōu)解,依次進(jìn)行...
如果有兩個以上的算法得出相同的執(zhí)行結(jié)果,那么將以執(zhí)行”時間“來比較兩種算法優(yōu)劣。
用下面的函數(shù)完成的算法:
def greedyranker(results):
results = results.copy()
ranked, coveredsofar, costsofar, round = [], set(), 0, 0
noncontributing = []
while results:
round += 1
# What each test can contribute to the pool of what is covered so far
contributions = [(len(cover - coveredsofar), -cost, test)
for test, (cost, cover) in sorted(results.items()) ]
# Greedy ranking by taking the next greatest contributor
delta_cover, benefit, test = max( contributions )
if delta_cover > 0:
ranked.append((test, delta_cover))
cost, cover = results.pop(test)
coveredsofar.update(cover)
costsofar += cost
for delta_cover, benefit, test in contributions:
if delta_cover == 0:
# this test cannot contribute anything
noncontributing.append( (test, round) )
results.pop(test)
return coveredsofar, ranked, costsofar, noncontributing
每次while循環(huán)(第5行),下一個最好的測試會被追加到排名和測試,不會 丟棄貢獻(xiàn)的任何額外覆蓋(37-41行)
上面的函數(shù)是略顯簡單,所以我花了一點(diǎn)時間用tutor來標(biāo)注,當(dāng)運(yùn)行時打印出它做的。
函數(shù)(有指導(dǎo)):
它完成同樣的事情,但代碼量更大,太繁冗:
def greedyranker(results, tutor=True):
results = results.copy()
ranked, coveredsofar, costsofar, round = [], set(), 0, 0
noncontributing = []
while results:
round += 1
# What each test can contribute to the pool of what is covered so far
contributions = [(len(cover - coveredsofar), -cost, test)
for test, (cost, cover) in sorted(results.items()) ]
if tutor:
print('\n## Round %i' % round)
print(' Covered so far: %2i points: ' % len(coveredsofar))
print(' Ranked so far: ' + repr([t for t, d in ranked]))
print(' What the remaining tests can contribute, largest contributors first:')
print(' # DELTA, BENEFIT, TEST')
deltas = sorted(contributions, reverse=True)
for delta_cover, benefit, test in deltas:
print(' %2i, %7.2f, %s' % (delta_cover, benefit, test))
if len(deltas)>=2 and deltas[0][0] == deltas[1][0]:
print(' Note: This time around, more than one test gives the same')
print(' maximum delta contribution of %i to the coverage so far'
% deltas[0][0])
if deltas[0][1] != deltas[1][1]:
print(' we order based on the next field of minimum cost')
print(' (equivalent to maximum negative cost).')
else:
print(' the next field of minimum cost is the same so')
print(' we arbitrarily order by test name.')
zeroes = [test for delta_cover, benefit, test in deltas
if delta_cover == 0]
if zeroes:
print(' The following test(s) cannot contribute more to coverage')
print(' and will be dropped:')
print(' ' + ', '.join(zeroes))
# Greedy ranking by taking the next greatest contributor
delta_cover, benefit, test = max( contributions )
if delta_cover > 0:
ranked.append((test, delta_cover))
cost, cover = results.pop(test)
if tutor:
print(' Ranking %s in round %2i giving extra coverage of: %r'
% (test, round, sorted(cover - coveredsofar)))
coveredsofar.update(cover)
costsofar += cost
for delta_cover, benefit, test in contributions:
if delta_cover == 0:
# this test cannot contribute anything
noncontributing.append( (test, round) )
results.pop(test)
if tutor:
print('\n## ALL TESTS NOW RANKED OR DISCARDED\n')
return coveredsofar, ranked, costsofar, noncontributing
每一塊以 if tutor開始: 添加以上代碼
樣值輸出
調(diào)用排序并打印結(jié)果的代碼是:
totalcoverage, ranking, totalcost, nonranked = greedyranker(results)
print('''
A total of %i points were covered,
using only %i of the initial %i tests,
and should take %g time units to run.
The tests in order of coverage added:
TEST DELTA-COVERAGE'''
% (len(totalcoverage), len(ranking), len(results), totalcost))
print('\n'.join(' %6s %i' % r for r in ranking))
結(jié)果包含大量東西,來自tutor并且最后跟著結(jié)果。
對這個偽隨機(jī)生成15條測試數(shù)據(jù)的測試案例,看起來只需要七條去產(chǎn)生最大的總覆蓋率。(而且如果你愿意放棄三條測試,其中每個只覆蓋了一個額外的點(diǎn),那么15條測試中的4條就將給出92.5%的最大可能覆蓋率)。
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
SQL Server 中 CONVERT 函數(shù)的日期轉(zhuǎn)換:從基礎(chǔ)用法到實(shí)戰(zhàn)優(yōu)化 在 SQL Server 的數(shù)據(jù)處理中,日期格式轉(zhuǎn)換是高頻需求 —— 無論 ...
2025-09-18MySQL 大表拆分與關(guān)聯(lián)查詢效率:打破 “拆分必慢” 的認(rèn)知誤區(qū) 在 MySQL 數(shù)據(jù)庫管理中,“大表” 始終是性能優(yōu)化繞不開的話題。 ...
2025-09-18CDA 數(shù)據(jù)分析師:表結(jié)構(gòu)數(shù)據(jù) “獲取 - 加工 - 使用” 全流程的賦能者 表結(jié)構(gòu)數(shù)據(jù)(如數(shù)據(jù)庫表、Excel 表、CSV 文件)是企業(yè)數(shù)字 ...
2025-09-18DSGE 模型中的 Et:理性預(yù)期算子的內(nèi)涵、作用與應(yīng)用解析 動態(tài)隨機(jī)一般均衡(Dynamic Stochastic General Equilibrium, DSGE)模 ...
2025-09-17Python 提取 TIF 中地名的完整指南 一、先明確:TIF 中的地名有哪兩種存在形式? 在開始提取前,需先判斷 TIF 文件的類型 —— ...
2025-09-17CDA 數(shù)據(jù)分析師:解鎖表結(jié)構(gòu)數(shù)據(jù)特征價值的專業(yè)核心 表結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 規(guī)范存儲的結(jié)構(gòu)化數(shù)據(jù),如數(shù)據(jù)庫表、Excel 表、 ...
2025-09-17Excel 導(dǎo)入數(shù)據(jù)含缺失值?詳解 dropna 函數(shù)的功能與實(shí)戰(zhàn)應(yīng)用 在用 Python(如 pandas 庫)處理 Excel 數(shù)據(jù)時,“缺失值” 是高頻 ...
2025-09-16深入解析卡方檢驗(yàn)與 t 檢驗(yàn):差異、適用場景與實(shí)踐應(yīng)用 在數(shù)據(jù)分析與統(tǒng)計學(xué)領(lǐng)域,假設(shè)檢驗(yàn)是驗(yàn)證研究假設(shè)、判斷數(shù)據(jù)差異是否 “ ...
2025-09-16CDA 數(shù)據(jù)分析師:掌控表格結(jié)構(gòu)數(shù)據(jù)全功能周期的專業(yè)操盤手 表格結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 存儲的結(jié)構(gòu)化數(shù)據(jù),如 Excel 表、數(shù)據(jù) ...
2025-09-16MySQL 執(zhí)行計劃中 rows 數(shù)量的準(zhǔn)確性解析:原理、影響因素與優(yōu)化 在 MySQL SQL 調(diào)優(yōu)中,EXPLAIN執(zhí)行計劃是核心工具,而其中的row ...
2025-09-15解析 Python 中 Response 對象的 text 與 content:區(qū)別、場景與實(shí)踐指南 在 Python 進(jìn)行 HTTP 網(wǎng)絡(luò)請求開發(fā)時(如使用requests ...
2025-09-15CDA 數(shù)據(jù)分析師:激活表格結(jié)構(gòu)數(shù)據(jù)價值的核心操盤手 表格結(jié)構(gòu)數(shù)據(jù)(如 Excel 表格、數(shù)據(jù)庫表)是企業(yè)最基礎(chǔ)、最核心的數(shù)據(jù)形態(tài) ...
2025-09-15Python HTTP 請求工具對比:urllib.request 與 requests 的核心差異與選擇指南 在 Python 處理 HTTP 請求(如接口調(diào)用、數(shù)據(jù)爬取 ...
2025-09-12解決 pd.read_csv 讀取長浮點(diǎn)數(shù)據(jù)的科學(xué)計數(shù)法問題 為幫助 Python 數(shù)據(jù)從業(yè)者解決pd.read_csv讀取長浮點(diǎn)數(shù)據(jù)時的科學(xué)計數(shù)法問題 ...
2025-09-12CDA 數(shù)據(jù)分析師:業(yè)務(wù)數(shù)據(jù)分析步驟的落地者與價值優(yōu)化者 業(yè)務(wù)數(shù)據(jù)分析是企業(yè)解決日常運(yùn)營問題、提升執(zhí)行效率的核心手段,其價值 ...
2025-09-12用 SQL 驗(yàn)證業(yè)務(wù)邏輯:從規(guī)則拆解到數(shù)據(jù)把關(guān)的實(shí)戰(zhàn)指南 在業(yè)務(wù)系統(tǒng)落地過程中,“業(yè)務(wù)邏輯” 是連接 “需求設(shè)計” 與 “用戶體驗(yàn) ...
2025-09-11塔吉特百貨孕婦營銷案例:數(shù)據(jù)驅(qū)動下的精準(zhǔn)零售革命與啟示 在零售行業(yè) “流量紅利見頂” 的當(dāng)下,精準(zhǔn)營銷成為企業(yè)突圍的核心方 ...
2025-09-11CDA 數(shù)據(jù)分析師與戰(zhàn)略 / 業(yè)務(wù)數(shù)據(jù)分析:概念辨析與協(xié)同價值 在數(shù)據(jù)驅(qū)動決策的體系中,“戰(zhàn)略數(shù)據(jù)分析”“業(yè)務(wù)數(shù)據(jù)分析” 是企業(yè) ...
2025-09-11Excel 數(shù)據(jù)聚類分析:從操作實(shí)踐到業(yè)務(wù)價值挖掘 在數(shù)據(jù)分析場景中,聚類分析作為 “無監(jiān)督分組” 的核心工具,能從雜亂數(shù)據(jù)中挖 ...
2025-09-10統(tǒng)計模型的核心目的:從數(shù)據(jù)解讀到?jīng)Q策支撐的價值導(dǎo)向 統(tǒng)計模型作為數(shù)據(jù)分析的核心工具,并非簡單的 “公式堆砌”,而是圍繞特定 ...
2025-09-10