-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.go
More file actions
158 lines (134 loc) · 3.77 KB
/
resources.go
File metadata and controls
158 lines (134 loc) · 3.77 KB
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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
rprop "github.com/bestbug456/gorpropplus"
gorest "github.com/fredmaggiowski/gorest"
)
// NNResource is responsable of contain all the method for
// neural network prediction
type NNResource struct {
nn *rprop.NeuralNetwork
}
// PredictionRequest contain a slice of hero id
type PredictionRequest struct {
Heros []string
}
// PredictionResponse contain the team ID who
// will win and the probability of winning.
type PredictionResponse struct {
Win int
Prob float64
}
// Post is the method called for request a new predition. The request
// body contain a PredictionRequest struct.
func (p *NNResource) Post(r *http.Request) (int, gorest.Response) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Can't read the body: %s.\n", err.Error())
return http.StatusInternalServerError, nil
}
var request PredictionRequest
err = json.Unmarshal(b, &request)
if err != nil {
log.Printf("Can't unmarshal heros: %s.\n", err.Error())
return http.StatusInternalServerError, nil
}
picks := make([]int, 10)
for i := 0; i < len(request.Heros); i++ {
picks[i] = heroID[request.Heros[i]]
}
input := orderPickByTeamAndCreateBitmask(picks)
ris, err := p.nn.Predict(input)
if err != nil {
log.Printf("Can't predict: %s.\n", err.Error())
return http.StatusInternalServerError, nil
}
var prob float64
var win int
if int(ris[0]) == 0 {
prob = 1 - ris[0]
} else {
win = 1
prob = ris[0]
}
out, err := json.Marshal(PredictionResponse{
Prob: prob,
Win: win,
})
if err != nil {
log.Printf("Can't marshal heros: %s.\n", err.Error())
return http.StatusInternalServerError, nil
}
response := gorest.NewStandardResponse()
response.SetBody(out)
return http.StatusOK, response
}
func orderPickByTeamAndCreateBitmask(picks []int) []float64 {
team1Pick := []int{
picks[0],
picks[3],
picks[5],
picks[7],
picks[8],
}
team2Pick := []int{
picks[1],
picks[2],
picks[4],
picks[6],
picks[9],
}
bitmasks := createBitmasksForTeam(team1Pick)
supp := createBitmasksForTeam(team2Pick)
bitmasks = append(bitmasks, supp...)
return bitmasks
}
func createBitmasksForTeam(team []int) []float64 {
bitmasks := make([]float64, 115)
for i := 0; i < len(team); i++ {
bitmasks[team[i]] = 1
}
return bitmasks
}
// HeroResource is the handy way to exspose a list of hero
// to a web app
type HeroResource struct{}
// Get method of HeroResource is call for request an update list of
// suppoerted hero.
func (p *HeroResource) Get(r *http.Request) (int, gorest.Response) {
// Return the array of avaiable dota heros
out, err := json.Marshal(heros)
if err != nil {
log.Printf("Can't marshal heros: %s.\n", err.Error())
return http.StatusInternalServerError, nil
}
response := gorest.NewStandardResponse()
response.SetBody(out)
return http.StatusOK, response
}
// StatisticsResource is the classic resouce "for nerd". It
// will contain all the statistic the web app will expose.
type StatisticsResource struct {
Stats *rprop.ValidationResult
}
type stats struct {
Accuracy float64
TotalDataTested int
}
// Get is call for request all the statistics of the system
func (p *StatisticsResource) Get(r *http.Request) (int, gorest.Response) {
var statistics stats
statistics.TotalDataTested = p.Stats.ConfusionMatrix[0][0] + p.Stats.ConfusionMatrix[0][1] + p.Stats.ConfusionMatrix[1][0] + p.Stats.ConfusionMatrix[1][1]
statistics.Accuracy = float64(p.Stats.ConfusionMatrix[0][0]+p.Stats.ConfusionMatrix[1][1]) / float64(statistics.TotalDataTested)
out, err := json.Marshal(statistics)
if err != nil {
log.Printf("Can't marshal stats: %s.\n", err.Error())
return http.StatusInternalServerError, nil
}
response := gorest.NewStandardResponse()
response.SetBody(out)
return http.StatusOK, response
}