-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* contractor: fix logging * contractor: fix nil map and copy * contractor: add comment * contractor: have candidateHosts adjust the lowest score if there are no candidates * contractor: refactor it more thoroughly * contractor: pass used hosts * autopilot: implement Error for refillError * autopilot: implement MR remarks * testing: cleanup PR * contractor: avoid parsing --------- Co-authored-by: Chris Schinnerl <[email protected]>
- Loading branch information
1 parent
20c3d81
commit c92e343
Showing
6 changed files
with
214 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package autopilot | ||
|
||
import "lukechampine.com/frand" | ||
|
||
type ( | ||
scoredHosts []scoredHost | ||
) | ||
|
||
func (hosts scoredHosts) randSelectByScore(n int) (selected []scoredHost) { | ||
if len(hosts) < n { | ||
n = len(hosts) | ||
} else if n < 0 { | ||
return nil | ||
} | ||
|
||
used := make(map[int]struct{}) | ||
for len(selected) < n { | ||
// map indices properly | ||
imap := make(map[int]int) | ||
|
||
// deep copy | ||
var candidates []scoredHost | ||
for i, h := range hosts { | ||
if _, used := used[i]; !used { | ||
candidates = append(candidates, h) | ||
imap[len(candidates)-1] = i | ||
} | ||
} | ||
|
||
// normalize | ||
var total float64 | ||
for _, h := range candidates { | ||
total += h.score | ||
} | ||
for i := range candidates { | ||
candidates[i].score = candidates[i].score / total | ||
} | ||
|
||
// select | ||
sI := len(candidates) - 1 | ||
r := frand.Float64() | ||
var sum float64 | ||
for i, host := range candidates { | ||
sum += host.score | ||
if r < sum { | ||
sI = i | ||
break | ||
} | ||
} | ||
|
||
// update | ||
used[imap[sI]] = struct{}{} | ||
selected = append(selected, hosts[imap[sI]]) | ||
} | ||
|
||
return | ||
} |
Oops, something went wrong.