env

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2023 License: Apache-2.0 Imports: 21 Imported by: 0

README

Env

Env provides the environment interfaces for the agents. It contains a means of spinning up local Sphere servers and an extra wrapper around the sphere gRPC interface.

The package also includes normalization tools for transorming incoming state in norm.go. For instance one could expand the dimensions of a tensor or discretize continuous state.

Usage

Create a local Gym server in a docker container and connect to it.

server, _ := NewLocalServer(GymServerConfig)
defer server.Close()

Find a local Gym server or create one.

server, _ := FindOrCreate(GymServerConfig)
defer server.Close()

Create an environment

env, _ := server.Make("CartPole-v0")

Create an environment with a normalizer which will expand the dimensions along the 0 axis.

env, _ := server.Make("CartPole-v0", WithNormalizer(envv1.NewExpandDimsNormalizer(0)))

Create an environment and start recording video and statistics.

env, _ := server.Make("CartPole-v0", WithRecorder())

Take a step in the env.

outcome, _ := env.Step(action)

Reset the env.

init, _ := env.Reset()

End the env, printing results if present and then deleting any videos and statistics.

env.End()

Documentation

Overview

Package env provides a wrapper for interacting with Sphere as well as normalization tools.

Index

Constants

This section is empty.

Variables

View Source
var DefaultAtariWrapper = &spherev1alpha.EnvWrapper_DeepmindAtariWrapper{
	DeepmindAtariWrapper: &spherev1alpha.DeepmindAtariWrapper{EpisodeLife: true, ClipRewards: true},
}

DefaultAtariWrapper is the default deepmind atari wrapper.

View Source
var GymServerConfig = &LocalServerConfig{Image: "sphereproject/gym", Version: "latest", Port: "50051/tcp"}

GymServerConfig is a configuration for a OpenAI Gym server environment.

Functions

func PotentialsShape

func PotentialsShape(space *spherev1alpha.Space) []int

PotentialsShape is an overloaded method that will return a dense tensor of potentials for a given space.

func SpaceMinMax

func SpaceMinMax(space *sphere.Space) (min, max *tensor.Dense, err error)

SpaceMinMax returns the min/max for a space as tensors. Sphere already normalizes infinite spaces to floats.

func SpaceShape

func SpaceShape(space *spherev1alpha.Space) []int

SpaceShape return the shape of the given space.

func WithDialOpts

func WithDialOpts(dialOpts ...grpc.DialOption) func(*Server)

WithDialOpts adds dial options to the server connection.

func WithGoalNormalizer

func WithGoalNormalizer(normalizer Normalizer) func(*Env)

WithGoalNormalizer adds a normalizer for goal data.

func WithLogger

func WithLogger(logger *log.Logger) func(*Env)

WithLogger adds a logger to the env.

func WithNormalizer

func WithNormalizer(normalizer Normalizer) func(*Env)

WithNormalizer adds a normalizer for observation data.

func WithRecorder

func WithRecorder() func(*Env)

WithRecorder adds a recorder to the environment

func WithServerLogger

func WithServerLogger(logger *log.Logger) func(*Server)

WithServerLogger adds a logger to the server.

func WithWrapper

func WithWrapper(wrapper spherev1alpha.IsEnvWrapper) func(*Env)

WithWrapper adds an environment wrapper.

Types

type BoxSpace

type BoxSpace struct {
	// High values for this space.
	High *tensor.Dense

	// Low values for this space.
	Low *tensor.Dense

	// Shape of the space.
	Shape []int
}

BoxSpace is a helper for box spaces that converts the values to dense tensors. TODO: make proto plugin to do this automagically (protoc-gen-tensor)

type Env

type Env struct {
	*spherev1alpha.Environment

	// Client to connect to the Sphere server.
	Client spherev1alpha.EnvironmentAPIClient

	// VideoPaths of result videos downloadloaded from the server.
	VideoPaths []string

	// Normalizer normalizes observation data.
	Normalizer Normalizer

	// GoalNormalizer normalizes goal data.
	GoalNormalizer Normalizer
	// contains filtered or unexported fields
}

Env is a convienience environment wrapper.

func (*Env) ActionSpaceShape

func (e *Env) ActionSpaceShape() []int

ActionSpaceShape is the shape of the action space. TODO: should this be in the API of off the generated code?

func (*Env) BoxSpace

func (e *Env) BoxSpace() (*BoxSpace, error)

BoxSpace returns the box space as dense tensors.

func (*Env) Clean

func (e *Env) Clean()

Clean any results/videos saved locally.

func (*Env) Close

func (e *Env) Close() error

Close the environment.

func (*Env) End

func (e *Env) End()

End is a helper function that will close an environment and return the results and play any videos.

func (*Env) MaxSteps

func (e *Env) MaxSteps() int

MaxSteps that can be taken per episode.

func (*Env) ObservationSpaceShape

func (e *Env) ObservationSpaceShape() []int

ObservationSpaceShape is the shape of the observation space.

func (*Env) PlayAll

func (e *Env) PlayAll()

PlayAll videos stored locally.

func (*Env) Print

func (e *Env) Print()

Print a YAML representation of the environment.

func (*Env) PrintResults

func (e *Env) PrintResults() error

PrintResults results for the environment.

func (*Env) Render

func (e *Env) Render() (*spherev1alpha.Image, error)

Render the environment. TODO: should maybe be a stream.

func (*Env) Reset

func (e *Env) Reset() (init *InitialState, err error)

Reset the environment.

func (*Env) Results

func (e *Env) Results() (*Results, error)

Results results for the environment.

func (*Env) SampleAction

func (e *Env) SampleAction() (int, error)

SampleAction returns a sample action for the environment.

func (*Env) Step

func (e *Env) Step(value int) (*Outcome, error)

Step through the environment.

func (*Env) Videos

func (e *Env) Videos(path string) ([]string, error)

Videos saves all the videos for the environment episodes to the given path. Defaults to current directory. Returns an array of video paths.

type EqWidthBinNormalizer

type EqWidthBinNormalizer struct {
	// contains filtered or unexported fields
}

EqWidthBinNormalizer is an EqWidthBinner applied using tensors.

func NewEqWidthBinNormalizer

func NewEqWidthBinNormalizer(intervals *tensor.Dense) *EqWidthBinNormalizer

NewEqWidthBinNormalizer is a new dense binner.

func (*EqWidthBinNormalizer) Init

func (d *EqWidthBinNormalizer) Init(e *Env) error

Init the normalizer.

func (*EqWidthBinNormalizer) Norm

func (d *EqWidthBinNormalizer) Norm(input *tensor.Dense) (*tensor.Dense, error)

Norm normalizes the values placing them in their bins.

type ExpandDimsNormalizer

type ExpandDimsNormalizer struct {
	// contains filtered or unexported fields
}

ExpandDimsNormalizer will expand the dims of the state.

func NewExpandDimsNormalizer

func NewExpandDimsNormalizer(axis int) *ExpandDimsNormalizer

NewExpandDimsNormalizer returns a new expand dims normalizer.

func (*ExpandDimsNormalizer) Init

func (r *ExpandDimsNormalizer) Init(e *Env) error

Init the normalizer.

func (*ExpandDimsNormalizer) Norm

func (r *ExpandDimsNormalizer) Norm(input *tensor.Dense) (*tensor.Dense, error)

Norm normalizes the values by expanding their dims along an axis.

type InitialState

type InitialState struct {
	// Observation of the environment.
	Observation *tensor.Dense

	// Goal if present.
	Goal *tensor.Dense
}

InitialState of the environment.

type LocalServerConfig

type LocalServerConfig struct {
	// Docker image of environment.
	Image string

	// Version of the docker image.
	Version string

	// Port the environment is exposed on.
	Port string

	// Logger for the server.
	Logger *log.Logger
}

LocalServerConfig is the environment server config.

type MinMaxNormalizer

type MinMaxNormalizer struct {
	// contains filtered or unexported fields
}

MinMaxNormalizer is a min/max normalizer that makes all values between 0>x<1.

func NewMinMaxNormalizer

func NewMinMaxNormalizer() *MinMaxNormalizer

NewMinMaxNormalizer returns a new min/max

func (*MinMaxNormalizer) Init

func (m *MinMaxNormalizer) Init(e *Env) (err error)

Init the normalizer.

func (*MinMaxNormalizer) Norm

func (m *MinMaxNormalizer) Norm(input *tensor.Dense) (*tensor.Dense, error)

Norm normalizes the input.

type Normalizer

type Normalizer interface {
	// Init the normalizer.
	Init(env *Env) error

	// Norm normalizes the input data.
	Norm(input *tensor.Dense) (*tensor.Dense, error)
}

Normalizer will normalize the data coming from an environment.

type Opt

type Opt func(*Env)

Opt is an environment option.

type Outcome

type Outcome struct {
	// Observation of the current state.
	Observation *tensor.Dense

	// Action that was taken
	Action int

	// Reward from action.
	Reward float32

	// Whether the environment is done.
	Done bool
}

Outcome of taking an action.

type ReshapeNormalizer

type ReshapeNormalizer struct {
	// contains filtered or unexported fields
}

ReshapeNormalizer will reshape the state.

func NewReshapeNormalizer

func NewReshapeNormalizer(shape tensor.Shape) *ReshapeNormalizer

NewReshapeNormalizer returns a new reshape normalizer.

func (*ReshapeNormalizer) Init

func (r *ReshapeNormalizer) Init(e *Env) error

Init the normalizer.

func (*ReshapeNormalizer) Norm

func (r *ReshapeNormalizer) Norm(input *tensor.Dense) (*tensor.Dense, error)

Norm normalizes the values by reshaping them.

type Results

type Results struct {
	// Episodes is a map of episode id to result.
	Episodes map[int32]*spherev1alpha.EpisodeResult

	// Videos is a map of episode id to result.
	Videos map[int32]*spherev1alpha.Video

	// AverageReward is the average reward of the episodes.
	AverageReward float32
}

Results from an environment run.

type Server

type Server struct {
	// Client to connect to the Sphere server.
	Client spherev1alpha.EnvironmentAPIClient
	// contains filtered or unexported fields
}

Server of environments.

func Connect

func Connect(addr string, opts ...ServerOpts) (*Server, error)

Connect to a server.

func FindOrCreate

func FindOrCreate(config *LocalServerConfig) (*Server, error)

FindOrCreate will find a local server for the given config or create one.

func NewLocalServer

func NewLocalServer(config *LocalServerConfig) (*Server, error)

NewLocalServer creates a new environment server by launching a docker container and connecting to it.

func (*Server) Close

func (s *Server) Close() error

Close the server, removing any local resources.

func (*Server) Make

func (s *Server) Make(model string, opts ...Opt) (*Env, error)

Make an environment.

type ServerOpts

type ServerOpts func(*Server)

ServerOpts are the connection opts.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL