Skip to content

Instantly share code, notes, and snippets.

@samuelcaldas
Last active May 7, 2023 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelcaldas/5401d949b800be53b811ac815774cd3e to your computer and use it in GitHub Desktop.
Save samuelcaldas/5401d949b800be53b811ac815774cd3e to your computer and use it in GitHub Desktop.
This class represents a gym environment that can be interacted with in C# using Python.NET. It uses the Python.Runtime library to create an instance of a gym environment and exposes methods to perform actions, get observations, reset the environment, render it, and close it. The ObservationDimensions and NumActions properties return the dimensio…
using Python.Runtime;
using System;
using System.Collections.Generic;
namespace GymSharp
{
public class Env : IDisposable
{
private dynamic env;
private PyObject pyEnv;
public int ObservationDimensions
{
get
{
using (Py.GIL())
{
return env.observation_space.shape[0];
}
}
}
public int NumActions
{
get
{
using (Py.GIL())
{
return env.action_space.n;
}
}
}
public Env(string environment)
{
using (Py.GIL())
{
dynamic gym = Py.Import("gym");
pyEnv = gym.make(environment);
env = pyEnv.AsManagedObject(typeof(object));
}
}
public Tuple<double[], float, bool, Dictionary<string, object>> Step(object action)
{
using (Py.GIL())
{
var result = env.step(action);
var observation = result[0].AsManagedObject(typeof(double[]));
var reward = (float)result[1].AsManagedObject(typeof(float));
var terminated = (bool)result[2].AsManagedObject(typeof(bool));
//var truncated = (bool)result[3].AsManagedObject(typeof(bool));
var info = new Dictionary<string, object>();
foreach (PyObject key in result[3].keys())
{
string keyString = key.AsManagedObject(typeof(string)) as string;
info[keyString] = result[3][key].AsManagedObject(typeof(object));
}
return new Tuple<double[], float, bool, Dictionary<string, object>>(observation, reward, terminated, info);
}
}
public Tuple<double[], Dictionary<string, object>> Reset()
{
using (Py.GIL())
{
var result = env.reset();
var observation = result[0].AsManagedObject(typeof(double[]));
var info = new Dictionary<string, object>();
foreach (PyObject key in result[1].keys())
{
string keyString = key.AsManagedObject(typeof(string)) as string;
info[keyString] = result[1][key].AsManagedObject(typeof(object));
}
return new Tuple<double[], Dictionary<string, object>>(observation, info);
}
}
public void Render()
{
using (Py.GIL())
{
env.render();
}
}
public void Close()
{
using (Py.GIL())
{
env.close();
}
}
public void Dispose()
{
pyEnv.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment