autopilot/interface+externalscoreattach: define ScoreSettable

ScoreSettable is an interface that let caller set external scores for
the heuristic. The ExternalScoreAttachment and WeightedCombAttachment
heuristics implement this interface.
This commit is contained in:
Johan T. Halseth 2019-02-14 11:37:47 +01:00
parent 83edcb7153
commit b23e53ea33
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
3 changed files with 24 additions and 3 deletions

@ -25,7 +25,7 @@ type WeightedCombAttachment struct {
// NewWeightedCombAttachment creates a new instance of a WeightedCombAttachment.
func NewWeightedCombAttachment(h ...*WeightedHeuristic) (
AttachmentHeuristic, error) {
*WeightedCombAttachment, error) {
// The sum of weights given to the sub-heuristics must sum to exactly
// 1.0.
@ -44,8 +44,9 @@ func NewWeightedCombAttachment(h ...*WeightedHeuristic) (
}
// A compile time assertion to ensure WeightedCombAttachment meets the
// AttachmentHeuristic interface.
// AttachmentHeuristic and ScoreSettable interfaces.
var _ AttachmentHeuristic = (*WeightedCombAttachment)(nil)
var _ ScoreSettable = (*WeightedCombAttachment)(nil)
// Name returns the name of this heuristic.
//
@ -135,6 +136,8 @@ func (c *WeightedCombAttachment) NodeScores(g ChannelGraph, chans []Channel,
//
// Since this heuristic doesn't keep any internal scores, it will recursively
// apply the scores to its sub-heuristics.
//
// NOTE: This is a part of the ScoreSettable interface.
func (c *WeightedCombAttachment) SetNodeScores(targetHeuristic string,
newScores map[NodeID]float64) (bool, error) {

@ -23,8 +23,9 @@ func NewExternalScoreAttachment() *ExternalScoreAttachment {
}
// A compile time assertion to ensure ExternalScoreAttachment meets the
// AttachmentHeuristic interface.
// AttachmentHeuristic and ScoreSettable interfaces.
var _ AttachmentHeuristic = (*ExternalScoreAttachment)(nil)
var _ ScoreSettable = (*ExternalScoreAttachment)(nil)
// Name returns the name of this heuristic.
//
@ -38,6 +39,8 @@ func (s *ExternalScoreAttachment) Name() string {
// of the targeted heuristic, to allow recursively target specific
// sub-heuristics. The returned boolean indicates whether the targeted
// heuristic was found.
//
// NOTE: This is a part of the ScoreSettable interface.
func (s *ExternalScoreAttachment) SetNodeScores(targetHeuristic string,
newScores map[NodeID]float64) (bool, error) {

@ -142,6 +142,21 @@ type AttachmentHeuristic interface {
map[NodeID]*NodeScore, error)
}
// ScoreSettable is an interface that indicates that the scores returned by the
// heuristic can be mutated by an external caller. The ExternalScoreAttachment
// currently implements this interface, and so should any heuristic that is
// using the ExternalScoreAttachment as a sub-heuristic, or keeps their own
// internal list of mutable scores, to allow access to setting the internal
// scores.
type ScoreSettable interface {
// SetNodeScores is used to set the internal map from NodeIDs to
// scores. The passed scores must be in the range [0, 1.0]. The fist
// parameter is the name of the targeted heuristic, to allow
// recursively target specific sub-heuristics. The returned boolean
// indicates whether the targeted heuristic was found.
SetNodeScores(string, map[NodeID]float64) (bool, error)
}
var (
// availableHeuristics holds all heuristics possible to combine for use
// with the autopilot agent.