我正在尋找一種方法在某些情況下儘早終止應用功能。使用for循環,是這樣的:提前終止基於應用的功能(類似於中斷?)
FDP_HCFA = function(FaultMatrix, TestCosts, GenerateNeighbors, RandomSeed) {
set.seed(RandomSeed)
## number of tests, mind the summary column
nT = ncol(FaultMatrix) - 1
StartingSequence = sample(1:nT)
BestAPFD = APFD_C(StartingSequence, FaultMatrix, TestCosts)
BestPrioritization = StartingSequence
MakingProgress = TRUE
NumberOfIterations = 0
while(MakingProgress) {
BestPrioritizationBefore = BestPrioritization
AllCurrentNeighbors = GenerateNeighbors(BestPrioritization)
for(CurrentNeighbor in AllCurrentNeighbors) {
CurrentAPFD = APFD_C(CurrentNeighbor, FaultMatrix, TestCosts)
if(CurrentAPFD > BestAPFD) {
BestAPFD = CurrentAPFD
BestPrioritization = CurrentNeighbor
break
}
}
if(length(union(list(BestPrioritizationBefore),
list(BestPrioritization))) == 1)
MakingProgress = FALSE
NumberOfIterations = NumberOfIterations + 1
}
}
我想重寫使用apply
一些推導此功能。特別是,終止對具有增加的適應性的第一個人的評估,從而避免考慮其餘人羣的成本。
您可能可以使用signalCondition/tryCatch /等的一些組合,但看起來很複雜。我會第二個JoshUlrich的問題。 – 2011-02-06 22:41:16
-1用於發佈不可重現且具有語法錯誤的代碼。 – 2011-02-06 22:53:10