Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Multi-dimension observation support for Golang binding #47

Closed
wants to merge 1 commit into from
Closed

FIX: Multi-dimension observation support for Golang binding #47

wants to merge 1 commit into from

Conversation

wenkesj
Copy link

@wenkesj wenkesj commented Apr 9, 2017

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.

@unixpickle
Copy link
Contributor

unixpickle commented Apr 9, 2017

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.

@unixpickle
Copy link
Contributor

unixpickle commented Apr 9, 2017

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
}

@wenkesj
Copy link
Author

wenkesj commented Apr 9, 2017

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.

@unixpickle
Copy link
Contributor

@wenkesj should probably close this now in favor of #48.

@wenkesj wenkesj closed this Apr 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants