-
Notifications
You must be signed in to change notification settings - Fork 0
/
inline-anchoring-applier.go
89 lines (80 loc) · 2.91 KB
/
inline-anchoring-applier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package anchoring
import (
"github.com/Azbesciak/RealDecisionMaker/lib/model"
)
//go:generate easytags $GOFILE json:camel
type InlineAnchoringApplier struct {
}
const InlineAnchoringApplierName = "inline"
func (i *InlineAnchoringApplier) Identifier() string {
return InlineAnchoringApplierName
}
type InlineAnchoringApplierParams struct {
ApplyOnNotConsidered bool `json:"applyOnNotConsidered"`
}
func (i *InlineAnchoringApplier) BlankParams() FunctionParams {
return &InlineAnchoringApplierParams{}
}
type InlineAnchoringApplierResult struct {
AppliedDifferences []model.AlternativeWithCriteria `json:"appliedDifferences"`
}
func (i *InlineAnchoringApplier) ApplyAnchoring(
dmp *model.DecisionMakingParams,
perReferencePointDiffs *[]ReferencePointsDifference,
boundingsWithScales BoundingsWithScales,
params FunctionParams,
_ *model.BiasListener,
) (*model.DecisionMakingParams, AnchoringApplierResult) {
parsedParams := params.(*InlineAnchoringApplierParams)
newAlternatives := make([]model.AlternativeWithCriteria, len(*perReferencePointDiffs))
appliedDifferences := make([]model.AlternativeWithCriteria, len(*perReferencePointDiffs))
for i, p := range *perReferencePointDiffs {
newWeights := *arithmeticAverage(p.ReferencePointsDifference)
differences := make(model.Weights, len(boundingsWithScales))
for c, scaling := range boundingsWithScales {
difference := newWeights.Fetch(c)
value := p.Alternative.Criteria.Fetch(c)
newValue := value + scaling.scaling.ValuesRange.Diff()*difference
newValue = scaling.bounding.BoundValue(newValue)
differences[c] = newValue - value
newWeights[c] = newValue
}
newAlternatives[i] = *p.Alternative.WithCriteriaValues(&newWeights)
appliedDifferences[i] = *p.Alternative.WithCriteriaValues(&differences)
}
notConsidered := dmp.NotConsideredAlternatives
result := InlineAnchoringApplierResult{
AppliedDifferences: appliedDifferences,
}
if parsedParams.ApplyOnNotConsidered {
notConsidered = *model.UpdateAlternatives(¬Considered, &newAlternatives)
} else {
result.AppliedDifferences = *model.UpdateAlternatives(&dmp.ConsideredAlternatives, &appliedDifferences)
}
return &model.DecisionMakingParams{
ConsideredAlternatives: *model.UpdateAlternatives(&dmp.ConsideredAlternatives, &newAlternatives),
NotConsideredAlternatives: notConsidered,
Criteria: dmp.Criteria,
MethodParameters: dmp.MethodParameters,
}, result
}
func arithmeticAverage(points []ReferencePointDifference) *model.Weights {
newWeights := make(model.Weights, len(points[0].Coefficients))
for i, a := range points {
for c, v := range a.Coefficients {
if i == 0 {
newWeights[c] = v
} else {
oldWeight := newWeights.Fetch(c)
newWeights[c] = oldWeight + v
}
}
}
referencePointsCount := float64(len(points))
if referencePointsCount > 1 {
for c, v := range newWeights {
newWeights[c] = v / referencePointsCount
}
}
return &newWeights
}