Closed wenkesj closed 7 years ago
This looks alright, although a lot of crazy indentation. Perhaps this could be implemented with recursion, allowing for infinite nesting?
This is a small enough change that I highly doubt anything is broken. However, eventually I (or someone else) should create more rigorous test cases. The demo was kind of my only (informal) test. Testing might have to shell out to Python, but could be better than nothing.
To elaborate on recursion: after we see that it's a []interface{}, we call normalizeSpaceElem() on the elements, then use reflection to create a slice of those objects. That way, we'd end up with []float64 or [][]float64 or [][][]float64, etc. This could remove a lot of redundancy, but we'd have to decide if it's worth using reflection for.
EDIT: here's a draft of what it might look like. I haven't benchmarked or tested this thoroughly:
func normalizeSpaceElem(obs interface{}) (interface{}, error) {
if obs == nil {
return nil, errors.New("unsupported observation: nil")
}
switch obs := obs.(type) {
case float64:
return int(obs), nil
case []interface{}:
if len(obs) == 0 {
return nil, errors.New("unsupported observation: empty array")
} else if _, isFloat := obs[0].(float64); isFloat {
return normalizeOneDimSpace(obs)
} else {
return normalizeMultiDimSpace(obs)
}
default:
return nil, fmt.Errorf("unsupported observation: %v", obs)
}
}
func normalizeOneDimSpace(obs []interface{}) ([]float64, error) {
res := make([]float64, len(obs))
for i, x := range obs {
var isFloat bool
res[i], isFloat = x.(float64)
if !isFloat {
return nil, errors.New("unsupported observation: heterogeneous array")
}
}
return res, nil
}
func normalizeMultiDimSpace(obs []interface{}) (interface{}, error) {
firstElem, err := normalizeSpaceElem(obs[0])
if err != nil {
return nil, err
}
elemType := reflect.TypeOf(firstElem)
sliceType := reflect.SliceOf(elemType)
slice := reflect.MakeSlice(sliceType, len(obs), len(obs))
for i, x := range obs {
obj, err := normalizeSpaceElem(x)
if err != nil {
return nil, err
}
val := reflect.ValueOf(obj)
if val.Type() != elemType {
return nil, errors.New("unsupported observation: heterogeneous array")
}
slice.Index(i).Set(val)
}
return slice.Interface(), nil
}
I agree. This looks better, this should be moved to an actual issue. I just avoided using reflect for the time being but that looks more reasonable.
@wenkesj should probably close this now in favor of #48.
This pull request adds support for observations with 1-3D slices
binding-go
, i.e.Pong-v0
, test case failed previously, but now is supported.CC @unixpickle I made this as readable as I could, let me know what you think.