Parameter Sweeps
Sweep Score Table
Best Candidate
A sweep can score each candidate and keep the best result seen so far.
Program
Play the program to change the target and watch the best candidate shift.
sweep_score_table.f90
Replay: real traced execution (multi-file project)
program sweep_score_table_demo
implicit none
integer :: candidates(3)
integer :: target
integer :: i
integer :: score
integer :: best_value
integer :: best_score
candidates = [1, 2, 3]
target = 2
best_value = candidates(1)
best_score = -999
do i = 1, 3
score = 10 - abs(candidates(i) - target)
if (score > best_score) then
best_score = score
best_value = candidates(i)
end if
end do
print '(I0, 1X, I0)', best_value, best_score
end program sweep_score_table_demo
program sweep_score_table_demo
implicit none
integer :: candidates(3)
integer :: target
integer :: i
integer :: score
integer :: best_value
integer :: best_score
candidates = [1, 2, 3]
target = 1
best_value = candidates(1)
best_score = -999
do i = 1, 3
score = 10 - abs(candidates(i) - target)
if (score > best_score) then
best_score = score
best_value = candidates(i)
end if
end do
print '(I0, 1X, I0)', best_value, best_score
end program sweep_score_table_demo
program sweep_score_table_demo
implicit none
integer :: candidates(3)
integer :: target
integer :: i
integer :: score
integer :: best_value
integer :: best_score
candidates = [1, 2, 3]
target = 3
best_value = candidates(1)
best_score = -999
do i = 1, 3
score = 10 - abs(candidates(i) - target)
if (score > best_score) then
best_score = score
best_value = candidates(i)
end if
end do
print '(I0, 1X, I0)', best_value, best_score
end program sweep_score_table_demo
candidates ← [1, 2, 3]
10candidates = [1, 2, 3]11target = 2values this step[1, 2, 3]candidatestarget ← 2
10candidates = [1, 2, 3]11target = 212best_value = candidates(1)values this step2targetbest_value ← 1
11target = 212best_value = candidates(1)13best_score = -999values this step1best_valuebest_score ← -999
12best_value = candidates(1)13best_score = -99914do i = 1, 3values this step-999best_scorei ← 1
13best_score = -99914do i = 1, 315 score = 10 - abs(candidates(i) - target)values this step1iscore ← 9
14do i = 1, 315 score = 10 - abs(candidates(i) - target)16 if (score > best_score) thenvalues this step9score1candidates(1)2targetif (score > best_score) then
15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17 best_score = scorevalues this step.true.score > best_scorebest_score ← 9
16if (score > best_score) then17 best_score = score18 best_value = candidates(i)values this step9best_scorebest_value ← 1
17 best_score = score18 best_value = candidates(i)19end ifvalues this step1best_valuei ← 2
13best_score = -99914do i = 1, 315 score = 10 - abs(candidates(i) - target)values this step2iscore ← 10
14do i = 1, 315 score = 10 - abs(candidates(i) - target)16 if (score > best_score) thenvalues this step10score2candidates(2)2targetif (score > best_score) then
15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17 best_score = scorevalues this step.true.score > best_scorebest_score ← 10
16if (score > best_score) then17 best_score = score18 best_value = candidates(i)values this step10best_scorebest_value ← 2
17 best_score = score18 best_value = candidates(i)19end ifvalues this step2best_valuei ← 3
13best_score = -99914do i = 1, 315 score = 10 - abs(candidates(i) - target)values this step3iscore ← 9
14do i = 1, 315 score = 10 - abs(candidates(i) - target)16 if (score > best_score) thenvalues this step9score3candidates(3)2targetif (score > best_score) then
15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17 best_score = scorevalues this step.false.score > best_scoreprint '(I0, 1X, I0)', best_value, best_score
20 end do21 print '(I0, 1X, I0)', best_value, best_score22end program sweep_score_table_demooutput2 10values this step2best_value10best_score
candidates ← [1, 2, 3]
10candidates = [1, 2, 3]11target = 1values this step[1, 2, 3]candidatestarget ← 1
10candidates = [1, 2, 3]11target = 112best_value = candidates(1)values this step1targetbest_value ← 1
11target = 112best_value = candidates(1)13best_score = -999values this step1best_valuebest_score ← -999
12best_value = candidates(1)13best_score = -99914do i = 1, 3values this step-999best_scorei ← 1
13best_score = -99914do i = 1, 315 score = 10 - abs(candidates(i) - target)values this step1iscore ← 10
14do i = 1, 315 score = 10 - abs(candidates(i) - target)16 if (score > best_score) thenvalues this step10score1candidates(1)1targetif (score > best_score) then
15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17 best_score = scorevalues this step.true.score > best_scorebest_score ← 10
16if (score > best_score) then17 best_score = score18 best_value = candidates(i)values this step10best_scorebest_value ← 1
17 best_score = score18 best_value = candidates(i)19end ifvalues this step1best_valuei ← 2
13best_score = -99914do i = 1, 315 score = 10 - abs(candidates(i) - target)values this step2iscore ← 9
14do i = 1, 315 score = 10 - abs(candidates(i) - target)16 if (score > best_score) thenvalues this step9score2candidates(2)1targetif (score > best_score) then
15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17 best_score = scorevalues this step.false.score > best_scorei ← 3
13best_score = -99914do i = 1, 315 score = 10 - abs(candidates(i) - target)values this step3iscore ← 8
14do i = 1, 315 score = 10 - abs(candidates(i) - target)16 if (score > best_score) thenvalues this step8score3candidates(3)1targetif (score > best_score) then
15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17 best_score = scorevalues this step.false.score > best_scoreprint '(I0, 1X, I0)', best_value, best_score
20 end do21 print '(I0, 1X, I0)', best_value, best_score22end program sweep_score_table_demooutput1 10values this step1best_value10best_score
candidates ← [1, 2, 3]
10candidates = [1, 2, 3]11target = 3values this step[1, 2, 3]candidatestarget ← 3
10candidates = [1, 2, 3]11target = 312best_value = candidates(1)values this step3targetbest_value ← 1
11target = 312best_value = candidates(1)13best_score = -999values this step1best_valuebest_score ← -999
12best_value = candidates(1)13best_score = -99914do i = 1, 3values this step-999best_scorei ← 1
13best_score = -99914do i = 1, 315 score = 10 - abs(candidates(i) - target)values this step1iscore ← 8
14do i = 1, 315 score = 10 - abs(candidates(i) - target)16 if (score > best_score) thenvalues this step8score1candidates(1)3targetif (score > best_score) then
15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17 best_score = scorevalues this step.true.score > best_scorebest_score ← 8
16if (score > best_score) then17 best_score = score18 best_value = candidates(i)values this step8best_scorebest_value ← 1
17 best_score = score18 best_value = candidates(i)19end ifvalues this step1best_valuei ← 2
13best_score = -99914do i = 1, 315 score = 10 - abs(candidates(i) - target)values this step2iscore ← 9
14do i = 1, 315 score = 10 - abs(candidates(i) - target)16 if (score > best_score) thenvalues this step9score2candidates(2)3targetif (score > best_score) then
15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17 best_score = scorevalues this step.true.score > best_scorebest_score ← 9
16if (score > best_score) then17 best_score = score18 best_value = candidates(i)values this step9best_scorebest_value ← 2
17 best_score = score18 best_value = candidates(i)19end ifvalues this step2best_valuei ← 3
13best_score = -99914do i = 1, 315 score = 10 - abs(candidates(i) - target)values this step3iscore ← 10
14do i = 1, 315 score = 10 - abs(candidates(i) - target)16 if (score > best_score) thenvalues this step10score3candidates(3)3targetif (score > best_score) then
15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17 best_score = scorevalues this step.true.score > best_scorebest_score ← 10
16if (score > best_score) then17 best_score = score18 best_value = candidates(i)values this step10best_scorebest_value ← 3
17 best_score = score18 best_value = candidates(i)19end ifvalues this step3best_valueprint '(I0, 1X, I0)', best_value, best_score
20 end do21 print '(I0, 1X, I0)', best_value, best_score22end program sweep_score_table_demooutput3 10values this step3best_value10best_score
candidate score
A score formula converts each candidate into a comparable value.
running best
`best_score` and `best_value` carry the best result across loop iterations.
tie shape
This example keeps the first candidate when a later score is not greater.