/
sysadmin.go
107 lines (101 loc) · 2.5 KB
/
sysadmin.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
package sysadmin
import (
"fmt"
"strings"
"strconv"
"go-glue.googlecode.com/hg/rlglue"
"gostat.googlecode.com/hg/stat"
)
var count int
type Config struct {
FailBase, FailIncr float64
FailStay, StartBoot float64
NumSystems int
DiscountFactor float64
}
func ConfigDefault() (cfg Config) {
cfg.FailBase = 0.05
cfg.FailIncr = 0.3
cfg.FailStay = 0.95
cfg.StartBoot = 0.9
cfg.NumSystems = 8
cfg.DiscountFactor = 0.9
return
}
type Environment struct {
task *rlglue.TaskSpec
cfg Config
status []bool
hash uint64
}
func New(cfg Config) (this *Environment) {
this = new(Environment)
this.cfg = cfg
return
}
func (this *Environment) ConstructObs() (obs rlglue.Observation) {
ints := make([]int32, this.cfg.NumSystems)
for i := range ints {
if this.status[i] {
ints[i] = 1
}
}
obs = rlglue.NewObservation(ints, []float64{}, []byte{})
this.hash = this.task.Obs.Ints.Index(ints)
count++
return
}
func (this *Environment) EnvInit() (taskString string) {
fstr := "VERSION RL-Glue-3.0 PROBLEMTYPE episodic DISCOUNTFACTOR %f OBSERVATIONS INTS (%d 0 1) ACTIONS INTS (0 %d) REWARDS (-1.0 1.0)"
taskString = fmt.Sprintf(fstr, this.cfg.DiscountFactor, this.cfg.NumSystems, this.cfg.NumSystems)
this.task, _ = rlglue.ParseTaskSpec(taskString)
this.status = make([]bool, this.cfg.NumSystems)
for i := range this.status {
this.status[i] = stat.NextBernoulli(this.cfg.StartBoot) == 1
}
return
}
func (this *Environment) EnvStart() (obs rlglue.Observation) {
return this.ConstructObs()
}
func (this *Environment) EnvStep(action rlglue.Action) (obs rlglue.Observation, r float64, t bool) {
fps := make([]float64, len(this.status))
reboot := int(action.Ints()[0])
for i := range this.status {
if reboot == i {
fps[i] = 0
} else {
fps[i] = this.cfg.FailBase
li := (i + this.cfg.NumSystems - 1) % this.cfg.NumSystems
ri := (i + 1) % this.cfg.NumSystems
if !this.status[li] {
fps[i] += this.cfg.FailIncr
}
if !this.status[ri] {
fps[i] += this.cfg.FailIncr
}
}
if this.status[i] || reboot == i {
this.status[i] = stat.NextUniform() < (1 - fps[i])
} else {
this.status[i] = stat.NextUniform() < (1 - this.cfg.FailStay)
}
if this.status[i] {
r++
}
}
if reboot < this.cfg.NumSystems {
r--
}
obs = this.ConstructObs()
return
}
func (this *Environment) EnvCleanup() {
}
func (this *Environment) EnvMessage(message string) (reply string) {
if strings.HasPrefix(message, "seed ") {
seed, _ := strconv.Atoi(message[5:])
stat.Seed(int64(seed))
}
return ""
}