-
Notifications
You must be signed in to change notification settings - Fork 0
/
new-criterion-anchoring-applier.go
189 lines (173 loc) · 6.56 KB
/
new-criterion-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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package anchoring
import (
"fmt"
"github.com/Azbesciak/RealDecisionMaker/lib/model"
"github.com/Azbesciak/RealDecisionMaker/lib/model/reference-criterion"
"github.com/Azbesciak/RealDecisionMaker/lib/utils"
)
//go:generate easytags $GOFILE json:camel
type NewCriterionAnchoringApplier struct {
generator utils.SeededValueGenerator
referenceCriterionManager reference_criterion.ReferenceCriteriaManager
}
func NewNewCriterionAnchoringApplier(
generator utils.SeededValueGenerator,
referenceCriterionManager reference_criterion.ReferenceCriteriaManager,
) *NewCriterionAnchoringApplier {
return &NewCriterionAnchoringApplier{
generator: generator,
referenceCriterionManager: referenceCriterionManager,
}
}
const NewCriterionAnchoringApplierName = "newCriterion"
func (n *NewCriterionAnchoringApplier) Identifier() string {
return NewCriterionAnchoringApplierName
}
// +RefrenceCriterion
type NewCriterionAnchoringApplierParams struct {
RandomSeed int64 `json:"randomSeed"`
}
func (n *NewCriterionAnchoringApplier) BlankParams() FunctionParams {
return &utils.Map{
"randomSeed": 0,
}
}
func addedCriterionName(criteria *model.Criteria, refPointDif model.Alternative) string {
return criteria.NotUsedName("__anchoring_criterion_" + refPointDif)
}
type additionalCriterionAnchoringState struct {
listener *model.BiasListener
referenceCriterion *model.Criterion
generator utils.SeededValueGenerator
params NewCriterionAnchoringApplierParams
currentCriteria model.Criteria
methodParams model.MethodParameters
addedCriteria []AddedCriterion
}
func (a *additionalCriterionAnchoringState) newCriterion(ri int, r ReferencePointDifference) *AddedCriterion {
if len(a.addedCriteria) == ri {
generator := a.generator(a.params.RandomSeed + int64(ri))
newCriterionName := addedCriterionName(&a.currentCriteria, r.ReferencePoint)
criterion := model.Criterion{
Id: newCriterionName,
Type: a.referenceCriterion.Type,
ValuesRange: a.referenceCriterion.ValuesRange,
}
a.currentCriteria = a.currentCriteria.Add(&criterion)
newCriterionParams := (*a.listener).OnCriterionAdded(&criterion, a.referenceCriterion, a.methodParams, generator)
addedCriterion := AddedCriterion{
Id: criterion.Id,
Type: criterion.Type,
ValuesRange: utils.ValueRange{},
MethodParameters: newCriterionParams,
AlternativesValues: model.Weights{},
}
a.addedCriteria = append(a.addedCriteria, addedCriterion)
a.methodParams = (*a.listener).Merge(a.methodParams, newCriterionParams)
}
// cannot return addedCriterion in if because append makes a copy of it.
return &a.addedCriteria[ri]
}
type AddedCriterion struct {
Id string `json:"id"`
Type model.CriterionType `json:"type"`
ValuesRange utils.ValueRange `json:"valuesRange"`
MethodParameters model.MethodParameters `json:"methodParameters"`
AlternativesValues model.Weights `json:"alternativesValues"`
}
type NewCriterionAnchoringApplierResult struct {
ReferenceCriterion model.Criterion `json:"referenceCriterion"`
AddedCriteria []AddedCriterion `json:"addedCriteria"`
}
func (n *NewCriterionAnchoringApplier) ApplyAnchoring(
dmp *model.DecisionMakingParams,
perReferencePointDiffs *[]ReferencePointsDifference,
boundingsWithScales BoundingsWithScales,
params FunctionParams,
listener *model.BiasListener,
) (*model.DecisionMakingParams, AnchoringApplierResult) {
parsedParams := NewCriterionAnchoringApplierParams{}
utils.DecodeToStruct(params, &parsedParams)
referenceCriterionProvider := n.referenceCriterionManager.ForParams(¶ms)
criteria := *(*listener).RankCriteriaAscending(dmp)
state := additionalCriterionAnchoringState{
listener: listener,
referenceCriterion: referenceCriterionProvider.Provide(&criteria),
generator: n.generator,
params: parsedParams,
addedCriteria: []AddedCriterion{},
currentCriteria: *dmp.Criteria.ShallowCopy(),
methodParams: dmp.MethodParameters,
}
normalizeCriteriaByTotalValue(criteria)
if scaling, ok := boundingsWithScales[state.referenceCriterion.Id]; !ok {
panic(fmt.Errorf("scaling for criterion '%s' not found", state.referenceCriterion.Id))
} else {
newAlternatives := addAnchoringCriteriaToAlternatives(perReferencePointDiffs, &state, &criteria, &scaling)
result := NewCriterionAnchoringApplierResult{
ReferenceCriterion: *state.referenceCriterion,
AddedCriteria: state.addedCriteria,
}
return &model.DecisionMakingParams{
ConsideredAlternatives: *model.UpdateAlternatives(&dmp.ConsideredAlternatives, &newAlternatives),
NotConsideredAlternatives: *model.UpdateAlternatives(&dmp.NotConsideredAlternatives, &newAlternatives),
Criteria: state.currentCriteria,
MethodParameters: state.methodParams,
}, result
}
}
func addAnchoringCriteriaToAlternatives(
perReferencePointDiffs *[]ReferencePointsDifference,
state *additionalCriterionAnchoringState,
criteria *model.WeightedCriteria,
bounding *BoundingWithScale,
) []model.AlternativeWithCriteria {
diff := bounding.scaling.ValuesRange.Diff() / 2
newAlternatives := make([]model.AlternativeWithCriteria, len(*perReferencePointDiffs))
for i, p := range *perReferencePointDiffs {
alt := p.Alternative
for ri, r := range p.ReferencePointsDifference {
anchoringCriterion := state.newCriterion(ri, r)
criterionValue := 0.0
for _, c := range *criteria {
value := r.Coefficients.Fetch(c.Id)
criterionValue += value * c.Weight
}
newValue := bounding.scaling.ValuesRange.Min + diff + diff*criterionValue
newValue = bounding.bounding.BoundValue(newValue)
alt = *alt.WithCriterion(anchoringCriterion.Id, newValue)
anchoringCriterion.AlternativesValues[alt.Id] = newValue
valuesRange := &anchoringCriterion.ValuesRange
if i == 0 {
valuesRange.Min = newValue
valuesRange.Max = newValue
} else {
if valuesRange.Min >= newValue {
valuesRange.Min = newValue
}
if valuesRange.Max <= newValue {
valuesRange.Max = newValue
}
}
}
newAlternatives[i] = alt
}
return newAlternatives
}
const _minAllowedWeight = 0.01
func normalizeCriteriaByTotalValue(criteria model.WeightedCriteria) {
minWeight := criteria[0].Weight
dif := 0.0
if minWeight < _minAllowedWeight {
dif = _minAllowedWeight - minWeight
}
total := 0.0
for i, c := range criteria {
weight := c.Weight + dif
total += weight
criteria[i].Weight = weight
}
for i, c := range criteria {
criteria[i].Weight = c.Weight / total
}
}