-
Notifications
You must be signed in to change notification settings - Fork 0
/
decision-maker.go
159 lines (141 loc) · 5.01 KB
/
decision-maker.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
package model
import (
"fmt"
"github.com/Azbesciak/RealDecisionMaker/lib/utils"
"strings"
)
//go:generate easytags $GOFILE json:camel
type DecisionMaker struct {
PreferenceFunction string `json:"preferenceFunction"`
Biases BiasesParams `json:"biases"`
BiasApplyRandomSeed int64 `json:"biasApplyRandomSeed"`
KnownAlternatives []AlternativeWithCriteria `json:"knownAlternatives"`
ChoseToMake []Alternative `json:"choseToMake"`
Criteria Criteria `json:"criteria"`
MethodParameters RawMethodParameters `json:"methodParameters"`
}
type DecisionMakingParams struct {
NotConsideredAlternatives []AlternativeWithCriteria
ConsideredAlternatives []AlternativeWithCriteria
Criteria Criteria
MethodParameters interface{}
}
func (p *DecisionMakingParams) AllAlternatives() []AlternativeWithCriteria {
notConsider := p.NotConsideredAlternatives
if notConsider == nil {
notConsider = make([]AlternativeWithCriteria, 0)
}
toConsider := p.ConsideredAlternatives
if toConsider == nil {
toConsider = make([]AlternativeWithCriteria, 0)
}
return append(toConsider, notConsider...)
}
type RawMethodParameters = map[string]interface{}
type MethodParameters = interface{}
func (dm *DecisionMaker) Alternative(id Alternative) AlternativeWithCriteria {
return FetchAlternative(&dm.KnownAlternatives, id)
}
func UpdateAlternatives(old *[]AlternativeWithCriteria, newOnes *[]AlternativeWithCriteria) *[]AlternativeWithCriteria {
res := make([]AlternativeWithCriteria, len(*old))
for i, a := range *old {
res[i] = FetchAlternative(newOnes, a.Id)
}
return &res
}
func FetchAlternative(a *[]AlternativeWithCriteria, id Alternative) AlternativeWithCriteria {
for _, a := range *a {
if a.Id == id {
return a
}
}
panic(fmt.Errorf("alternative '%s' is unknown", id))
}
func (dm *DecisionMaker) AlternativesToConsider() *[]AlternativeWithCriteria {
return FetchAlternatives(&dm.KnownAlternatives, &dm.ChoseToMake)
}
func FetchAlternatives(a *[]AlternativeWithCriteria, ids *[]Alternative) *[]AlternativeWithCriteria {
results := make([]AlternativeWithCriteria, len(*ids))
for i, id := range *ids {
results[i] = FetchAlternative(a, id)
}
return &results
}
type DecisionMakerChoice struct {
Result AlternativesRanking `json:"result"`
Biases BiasesParams `json:"biases"`
}
func (dm *DecisionMaker) MakeDecision(
preferenceFunctions PreferenceFunctions,
biasListeners BiasListeners,
availableBiases *BiasMap,
biasApplyProbGenerator utils.SeededValueGenerator,
) *DecisionMakerChoice {
if IsStringBlank(&dm.PreferenceFunction) {
panic(fmt.Errorf("preference function must not be empty"))
}
dm.Criteria.Validate()
dm.validateAlternatives()
preferenceFunction := preferenceFunctions.Fetch(dm.PreferenceFunction)
params := dm.prepareParams(preferenceFunction)
chosenBiases := ChooseBiases(availableBiases, &dm.Biases)
processedParams, biasesProps := dm.processBiases(chosenBiases, params, &biasListeners, biasApplyProbGenerator)
res := (*preferenceFunction).Evaluate(processedParams)
return &DecisionMakerChoice{*res, *biasesProps}
}
func (dm *DecisionMaker) validateAlternatives() {
for i, a := range dm.KnownAlternatives {
for _, c := range dm.Criteria {
_, ok := a.Criteria[c.Id]
if !ok {
panic(fmt.Errorf("value of criterion '%s' not found for alternative %d '%s'", c.Id, i, a.Id))
}
}
}
}
func (dm *DecisionMaker) prepareParams(preferenceFunction *PreferenceFunction) *DecisionMakingParams {
return &DecisionMakingParams{
NotConsideredAlternatives: *dm.NotConsideredAlternatives(),
ConsideredAlternatives: *dm.AlternativesToConsider(),
Criteria: dm.Criteria,
MethodParameters: (*preferenceFunction).ParseParams(dm),
}
}
func (dm *DecisionMaker) NotConsideredAlternatives() *[]AlternativeWithCriteria {
var result []AlternativeWithCriteria
for _, a := range dm.KnownAlternatives {
if !utils.ContainsString(&dm.ChoseToMake, &a.Id) {
result = append(result, a)
}
}
return &result
}
func (dm *DecisionMaker) processBiases(
biases *BiasesWithProps,
params *DecisionMakingParams,
listeners *BiasListeners,
biasApplyProbGenerator utils.SeededValueGenerator,
) (*DecisionMakingParams, *BiasesParams) {
biasesToProcessCount := len(*biases)
result := make(BiasesParams, biasesToProcessCount)
current := params
if biasesToProcessCount == 0 {
return current, &result
}
listener := listeners.Fetch(dm.PreferenceFunction)
generator := biasApplyProbGenerator(dm.BiasApplyRandomSeed)
for i, h := range *biases {
// check for >=1 omitted to keep results independence when other changes
if h.Props.ApplyProbability > generator() {
res := (*h.Bias).Apply(params, current, &h.Props.Props, listener)
current = res.DMP
result[i] = *UpdateBiasesProps(h.Props, res.Props)
} else {
result[i] = *UpdateBiasesProps(h.Props, nil)
}
}
return current, &result
}
func IsStringBlank(str *string) bool {
return len(strings.TrimSpace(*str)) == 0
}