-
Notifications
You must be signed in to change notification settings - Fork 0
/
criterion.go
171 lines (146 loc) · 3.77 KB
/
criterion.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
package model
import (
"fmt"
"github.com/Azbesciak/RealDecisionMaker/lib/utils"
"sort"
"strconv"
"strings"
)
//go:generate easytags $GOFILE json:camel
type CriterionType string
const (
Gain CriterionType = "gain"
Cost CriterionType = "cost"
)
type Criterion struct {
Id string `json:"id"`
Type CriterionType `json:"type"`
ValuesRange *utils.ValueRange `json:"valuesRange,omitempty"`
}
func (c Criterion) Identifier() string {
return c.Id
}
type Criteria []Criterion
func (c *Criteria) Len() int {
return len(*c)
}
func (c *Criteria) Get(index int) utils.Identifiable {
return (*c)[index]
}
func (c *Criteria) ShallowCopy() *Criteria {
criteriaCopy := make(Criteria, len(*c))
copy(criteriaCopy, *c)
return &criteriaCopy
}
func (c *Criteria) Validate() {
criteriaSet := make(map[string]bool)
for i, criterion := range *c {
if _, ok := criteriaSet[criterion.Id]; ok {
panic(fmt.Errorf("criterion '%s' [index %d] is not unique", criterion.Id, i))
}
if criterion.ValuesRange != nil && criterion.ValuesRange.Max <= criterion.ValuesRange.Min {
panic(fmt.Errorf("criterion '%s' [index %d] has invalid value range %v: min must be lower than max",
criterion.Id, i, *criterion.ValuesRange,
))
}
criteriaSet[criterion.Id] = true
}
}
func (c *Criteria) NotUsedName(name string) string {
count := c.countWithPrefix(name)
return firstFreeName(name, count)
}
func firstFreeName(name string, count int) string {
if count == 0 {
return name
} else {
return name + strconv.Itoa(count)
}
}
func (c *Criteria) countWithPrefix(prefix string) int {
concealedCriteriaCount := 0
for _, cr := range *c {
if strings.HasPrefix(cr.Id, prefix) {
concealedCriteriaCount += 1
}
}
return concealedCriteriaCount
}
func (c *Criteria) SortByWeights(weights Weights) *WeightedCriteria {
result := make(WeightedCriteria, len(weights))
for i, criterion := range *c {
result[i] = WeightedCriterion{
Criterion: criterion,
Weight: c.Weight(weights, i),
}
}
sort.SliceStable(result, func(i, j int) bool {
return result[i].Weight < result[j].Weight
})
return &result
}
func (c *Criteria) Weight(weights Weights, criterionIndex int) Weight {
return c.FindWeight(&weights, &(*c)[criterionIndex])
}
func (c *Criteria) FindWeight(weights *Weights, criterion *Criterion) Weight {
if v, ok := (*weights)[criterion.Id]; !ok {
criteria := weights.AsKeyValue()
panic(fmt.Errorf("weight for criterion '%s' not found in criteria %v", criterion.Id, criteria))
} else {
return v
}
}
func (c *Criteria) First() Criterion {
return (*c)[0]
}
func (c *Criterion) Multiplier() int8 {
if c.Type == Cost {
return -1
} else {
return 1
}
}
func (c *Criterion) IsGain() bool {
return c.Multiplier() == 1
}
func (c *Criteria) Names() *[]string {
result := make([]string, len(*c))
for i, crit := range *c {
result[i] = crit.Id
}
return &result
}
func (c *Criteria) Add(criterion *Criterion) Criteria {
for _, crit := range *c {
if crit.Id == criterion.Id {
panic(fmt.Errorf("cannot add criterion '%v' - already exists in criteria: %v", *criterion, *c))
}
}
return append(*c, *criterion)
}
type WeightedCriterion struct {
Criterion
Weight Weight `json:"weight"`
}
type WeightedCriteria []WeightedCriterion
func (w *WeightedCriteria) Criteria() *Criteria {
result := make(Criteria, len(*w))
for i, c := range *w {
result[i] = c.Criterion
}
return &result
}
func (c *Criteria) ZipWithWeights(weights *Weights) *WeightedCriteria {
weightedCriteria := make(WeightedCriteria, len(*c))
for i, crit := range *c {
value := c.FindWeight(weights, &crit)
weightedCriteria[i] = WeightedCriterion{
Criterion: crit,
Weight: value,
}
}
return &weightedCriteria
}
func (c *WeightedCriterion) AsWeights() *Weights {
return &Weights{c.Id: c.Weight}
}