-
Notifications
You must be signed in to change notification settings - Fork 21
/
env.go
120 lines (105 loc) · 3.42 KB
/
env.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
// Copyright (c) 2020, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"image"
"math/rand"
"github.com/emer/emergent/env"
"github.com/emer/etable/etensor"
)
// ExEnv is an example environment, that sets a single input point in a 2D
// input state and two output states as the X and Y coordinates of point.
// It can be used as a starting point for writing your own Env, without
// having much existing code to rewrite.
type ExEnv struct {
Nm string `desc:"name of this environment"`
Dsc string `desc:"description of this environment"`
Size int `desc:"size of each dimension in 2D input"`
Point image.Point `desc:"X,Y coordinates of point"`
Input etensor.Float32 `desc:"input state, 2D Size x Size"`
X etensor.Float32 `desc:"X as a one-hot state 1D Size"`
Y etensor.Float32 `desc:"Y as a one-hot state 1D Size"`
Run env.Ctr `view:"inline" desc:"current run of model as provided during Init"`
Epoch env.Ctr `view:"inline" desc:"number of times through Seq.Max number of sequences"`
Trial env.Ctr `view:"inline" desc:"trial increments over input states -- could add Event as a lower level"`
}
func (ev *ExEnv) Name() string { return ev.Nm }
func (ev *ExEnv) Desc() string { return ev.Dsc }
// Config sets the size, number of trials to run per epoch, and configures the states
func (ev *ExEnv) Config(sz int, ntrls int) {
ev.Size = sz
ev.Trial.Max = ntrls
ev.Input.SetShape([]int{sz, sz}, nil, []string{"Y", "X"})
ev.X.SetShape([]int{sz}, nil, []string{"X"})
ev.Y.SetShape([]int{sz}, nil, []string{"Y"})
}
func (ev *ExEnv) Validate() error {
if ev.Size == 0 {
return fmt.Errorf("ExEnv: %v has size == 0 -- need to Config", ev.Nm)
}
return nil
}
func (ev *ExEnv) State(element string) etensor.Tensor {
switch element {
case "Input":
return &ev.Input
case "X":
return &ev.X
case "Y":
return &ev.Y
}
return nil
}
// String returns the current state as a string
func (ev *ExEnv) String() string {
return fmt.Sprintf("Pt_%d_%d", ev.Point.X, ev.Point.Y)
}
// Init is called to restart environment
func (ev *ExEnv) Init(run int) {
ev.Run.Scale = env.Run
ev.Epoch.Scale = env.Epoch
ev.Trial.Scale = env.Trial
ev.Run.Init()
ev.Epoch.Init()
ev.Trial.Init()
ev.Run.Cur = run
ev.Trial.Cur = -1 // init state -- key so that first Step() = 0
}
// NewPoint generates a new point and sets state accordingly
func (ev *ExEnv) NewPoint() {
ev.Point.X = rand.Intn(ev.Size)
ev.Point.Y = rand.Intn(ev.Size)
ev.Input.SetZeros()
ev.Input.SetFloat([]int{ev.Point.Y, ev.Point.X}, 1)
ev.X.SetZeros()
ev.X.SetFloat([]int{ev.Point.X}, 1)
ev.Y.SetZeros()
ev.Y.SetFloat([]int{ev.Point.Y}, 1)
}
// Step is called to advance the environment state
func (ev *ExEnv) Step() bool {
ev.Epoch.Same() // good idea to just reset all non-inner-most counters at start
ev.NewPoint()
if ev.Trial.Incr() { // true if wraps around Max back to 0
ev.Epoch.Incr()
}
return true
}
func (ev *ExEnv) Action(element string, input etensor.Tensor) {
// nop
}
func (ev *ExEnv) Counter(scale env.TimeScales) (cur, prv int, chg bool) {
switch scale {
case env.Run:
return ev.Run.Query()
case env.Epoch:
return ev.Epoch.Query()
case env.Trial:
return ev.Trial.Query()
}
return -1, -1, false
}
// Compile-time check that implements Env interface
var _ env.Env = (*ExEnv)(nil)