/
main.go
288 lines (249 loc) · 6.29 KB
/
main.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"math"
"math/bits"
"os"
"runtime"
"runtime/pprof"
"runtime/trace"
"sort"
"strconv"
"strings"
"sync"
"time"
)
const epsilon = 1e-9
var (
cpuprofile = flag.Bool("cpuprofile", false, "write cpu profile to cpu.prof")
memprofile = flag.Bool("memprofile", false, "write memory profile to mem.prof")
blockprofile = flag.Bool("blockprofile", false, "write block profile to block.prof")
tracing = flag.Bool("trace", false, "write tracing the execution of a program to trace.out")
genes = flag.Int("genes", 9, "number of genes")
)
func main() {
flag.Parse()
var r func() error
if noFlags := !(*cpuprofile || *memprofile || *blockprofile || *tracing); noFlags {
r = run
} else {
r = runWithFlags
}
if err := r(); err != nil {
log.Fatalf("[ERROR] %v", err)
}
}
func run() error {
start := time.Now()
sValues, err := calc()
if err != nil {
return err
}
elapsed := time.Since(start)
genes := make([]string, 0, len(sValues))
for gene := range sValues {
genes = append(genes, gene)
}
sort.Slice(genes, func(i, j int) bool {
return sValues[genes[i]] < sValues[genes[j]]
})
for _, gene := range genes {
fmt.Printf("Gene: %s, Shapley value: %f\n", gene, sValues[gene])
}
fmt.Printf("Measure time: %s\n", elapsed)
return nil
}
func runWithFlags() error {
if *cpuprofile {
f, err := os.Create("cpu.prof")
if err != nil {
return fmt.Errorf("could not create CPU profile: %w", err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
return fmt.Errorf("could not start CPU profile: %w", err)
}
defer pprof.StopCPUProfile()
}
if *blockprofile {
runtime.SetBlockProfileRate(1)
f, err := os.Create("block.prof")
if err != nil {
return fmt.Errorf("could not create block profile: %w", err)
}
defer f.Close()
defer func() {
if err = pprof.Lookup("block").WriteTo(f, 0); err != nil {
log.Printf("[WARN] write to file: %v", err)
}
}()
}
if *tracing {
f, err := os.Create("trace.out")
if err != nil {
return fmt.Errorf("failed to create trace output file: %w", err)
}
defer f.Close()
if err := trace.Start(f); err != nil {
return fmt.Errorf("failed to start trace: %w", err)
}
defer trace.Stop()
}
if _, err := calc(); err != nil {
return err
}
if *memprofile {
f, err := os.Create("mem.prof")
if err != nil {
return fmt.Errorf("could not create memory profile: %w", err)
}
defer f.Close()
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
return fmt.Errorf("could not write memory profile: %w", err)
}
}
return nil
}
func calc() (map[string]float64, error) {
f, err := os.Open("data/N" + strconv.Itoa(*genes))
if err != nil {
return nil, fmt.Errorf("failed to open csv file, %w", err)
}
defer func() {
if err = f.Close(); err != nil {
log.Printf("[WARN] closing file: %v", err)
}
}()
records, err := prepare(f, *genes)
if err != nil {
return nil, fmt.Errorf("failed to prepare data, %w", err)
}
players, bitset, worths, err := handle(records)
if err != nil {
return nil, fmt.Errorf("failed to handle data, %w", err)
}
sValues, checkSum := shapley(players, bitset, worths)
if notEqualsOne(checkSum) {
return nil, fmt.Errorf("sum of Shapley values isn't equal to one, %v", checkSum)
}
return sValues, nil
}
func prepare(r io.Reader, g int) ([][]string, error) {
records := make([][]string, 0, (1<<g)-1)
sc := bufio.NewScanner(r)
for sc.Scan() {
record := strings.Split(sc.Text(), ",")
if l := len(record); l < 2 {
return nil, fmt.Errorf("length of row less 2, %d", l)
}
records = append(records, record)
}
if err := sc.Err(); err != nil {
return nil, fmt.Errorf("failed to scan tokens, %w", err)
}
return records, nil
}
func handle(records [][]string) (players []string, bitset []uint16, worths map[uint16]float64, err error) {
lenRecords := len(records)
players = strings.Fields(records[lenRecords-1][0])
sort.Strings(players)
lenPlayers := len(players)
bitset = make([]uint16, lenPlayers)
mapBits := make(map[string]uint16, lenPlayers)
var bit uint16
for i, player := range players {
bit = 1 << i
bitset[i] = bit
mapBits[player] = bit
}
cValues := make([]float64, 1<<lenPlayers)
worths = make(map[uint16]float64, lenRecords)
for _, rec := range records {
vec := strings.Fields(rec[0])
coalition := mapBits[vec[0]]
for _, v := range vec[1:] {
coalition |= mapBits[v]
}
var worth float64
for bit, cValue := range cValues {
if ^coalition&uint16(bit) == 0 {
worth += cValue
}
}
cValue, err := strconv.ParseFloat(rec[1], 64)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to convert string to int, %w", err)
}
cValues[coalition] = cValue
worths[coalition] = worth + cValue
}
return players, bitset, worths, nil
}
func shapley(players []string, bitset []uint16, worths map[uint16]float64) (map[string]float64, float64) {
n := len(players)
vector := make([]float64, n)
weight := makeWeight(n)
powersets := make([]chan uint16, n)
buffer := 1 << (n / 2)
for ch := 0; ch < n; ch++ {
powersets[ch] = make(chan uint16, buffer)
}
go func() {
makeSubsetsIdxs(n, powersets)
}()
var wgg sync.WaitGroup
wgg.Add(n)
for i, bs := range bitset {
vector[i] = worths[bs] / float64(n)
go func(i int, bs uint16) {
defer wgg.Done()
var pSum float64
for S := range powersets[i] {
if S&bs != 0 {
continue
}
k := bits.OnesCount16(S)
Si := S | bs
// Weight = |S|!(n-|S|-1)!/n!
w := weight(k)
// Marginal contribution = v(S U {i})-v(S)
contrib := worths[Si] - worths[S]
pSum += w * contrib
}
vector[i] += pSum
}(i, bs)
}
wgg.Wait()
var vSum float64
sValues := make(map[string]float64, n)
for i, value := range vector {
vSum += value
sValues[players[i]] = value
}
return sValues, vSum
}
func makeSubsetsIdxs(n int, powersets []chan uint16) {
defer func() {
for _, powerset := range powersets {
close(powerset)
}
}()
max := (1 << n) - 1
for set := 1; set <= max; set++ { // if set := 1 that without a null set
for _, powerset := range powersets {
powerset <- uint16(set)
}
}
}
func notEqualsOne(f float64) bool {
return math.Abs(f-1) > epsilon
}
func makeWeight(n int) func(k int) float64 {
wsn := weights[n]
return func(k int) float64 { return wsn[k] }
}