GithubHelp home page GithubHelp logo

asieradzk / rl_matrix Goto Github PK

View Code? Open in Web Editor NEW
56.0 9.0 2.0 23.37 MB

Deep Reinforcement Learning in C#

License: Other

C# 100.00%
cartpole deep-learning deep-reinforcement-learning dotnet dqn machine-learning multi-agent multi-agent-reinforcement-learning multi-environment ppo

rl_matrix's People

Contributors

asieradzk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

lulzzz rob011235

rl_matrix's Issues

Reinforcement Learning The Identity Function

Context

I was having difficulty using reinforcement learning for more complex problems, so I made a simple project to test the learning potential. In the code below, I am trying to teach the Boolean identity function to the agent, but the agent gives random answers before and after training.

Code

using OneOf;
using RLMatrix; //NuGet v0.2.0

namespace ReinforcementLearning.Trivial;

internal class Program
{
    static void Main(string[] args)
    {
        var options = new PPOAgentOptions(lr: .005f);
        List<IEnvironment<float[]>> environments = [new TrivialEnvironment(), new TrivialEnvironment(), new TrivialEnvironment()];
        var agent = new PPOAgent<float[]>(options, environments, new PPONetProviderBase<float[]>(16, 2));

        Console.WriteLine($"Pre-training: {Evaluate(agent)}");

        for (int i = 0; i < 10000; i++)
        {
            Console.Write(i);
            agent.Step();
            Console.Write("\b\b\b\b\b\b\b\b\b\b\b\b");
        }

        Console.WriteLine($"Post-training: {Evaluate(agent)}");
    }

    private static float Evaluate(PPOAgent<float[]> agent, int count = 1000)
    {
        float[] state = new float[1];
        int correct = 0;
        for (int i = 0; i < count; i++)
        {
            state[0] = TrivialEnvironment.RandomValue();
            (int[] greedyDiscreteActions, float[] meanContinuousActions) = agent.SelectAction(state, false);
            if (state[0] == greedyDiscreteActions[0])
            {
                correct++;
            }
        }
        return (float)correct / count;
    }

    private sealed class TrivialEnvironment : IEnvironment<float[]>
    {
        public const float CorrectAnswerReward = 1;
        public const float WrongAnswerPenalty = -1;

        private readonly float[] state = [RandomValue()];

        public static int RandomValue()
        {
            return Random.Shared.Next(2);
        }

        public int stepCounter { get; set; }
        public int maxSteps { get; set; } = int.MinValue;
        public bool isDone { get; set; }
        public OneOf<int, (int, int)> stateSize { get; set; } = 1;
        public int[] actionSize { get; set; } = [2];

        public float[] GetCurrentState() => state;

        public TrivialEnvironment() => Initialise();

        public void Initialise() => Reset();

        public void Reset()
        {
            stepCounter = 0;
            isDone = false;
        }

        public float Step(int[] actionsIds)
        {
            float input = state[0];
            float output = actionsIds[0];

            state[0] = RandomValue();

            return input == output ? CorrectAnswerReward : WrongAnswerPenalty;
        }
    }
}

Am I using the library incorrectly?

Documentation question

Hello, I'm currently in the process of porting a (failed) TorchSharp agent over to RL Matrix. Honestly thanks so much for this library.
I'm just wondering if you're planning on using the TorchSharp-cuda-windows or TorchSharp-cuda-linux packages eventually? I was using cuda in my previous environment and was hoping it was in the plans

EDIT: I stepped through the debugger and saw that the tensors were already using cuda. I just assumed it didn't since I didn't see it in the README.

I guess while I'm here writing this, I was also wondering if there was an example for a IContinuousEnvironment<>? This is the environment I need since I don't need discrete actions, but the examples were all regular IEnvironment<> from what I saw. Maybe I missed it as well lol

I'm asking since I'm running into this error:
image

My env is extremely basic, I am trying to set up 3 inputs and 3 (continuous) outputs with 0 discrete outputs.

private sealed class Env : IContinuousEnvironment<float[]>
{
	public int stepCounter { get; set; }
	public int maxSteps { get; set; }
	public bool isDone { get; set; }
	public OneOf<int, (int, int)> stateSize { get; set; }
	public int[] actionSize { get; set; }
	public (float min, float max)[] continuousActionBounds { get; set; }

	private readonly float[] _inputs;

	public Env()
	{
		_inputs = new float[3];
		Initialise();
	}

	public void Initialise()
	{
		// new physics
		stepCounter = 0;
		maxSteps = 100_000;
		stateSize = 3;
		actionSize = [3];
		continuousActionBounds = [(-1f, 1f), (-1f, 1f), (-1f, 1f)];
		// physics reset
		isDone = false;
	}

	public float[] GetCurrentState()
	{
		_inputs[0] = 1f;
		_inputs[1] = 0.5f;
		_inputs[2] = -1f;
		return _inputs;
	}

	public void Reset()
	{
		// physics reset
		isDone = false;
		stepCounter = 0;
	}

	public float Step(int[] actionsIds)
	{
		throw new NotImplementedException();
	}
	public float Step(int[] discreteActions, float[] continuousActions)
	{
		return continuousActions[0];
	}
}

I also noticed stepCounter never increases from 0

Introduce Test Driven Development!

It is now possible to do Test Driven Development using VS2022 to create Gym 2D and 3D RL environments for testing

using the latest gdUnit4API TestAdaptor on VS2022

Z0nhScGr3J

Train one observation at a time

Thank you for the RLMatrix.

I have a complex environment that cannot be simulated, in which interdependent observations are generated one at a time, so running training on an entire episode at once is not an option. I had to add a TrainObservation() based on your TrainEpisode() code. Please add this minor fix.

Thanks

How to save and load models trained using Godot

Hey there, I'm curious about how to save and load models trained using Godot. I've been experimenting with reinforcement learning algorithms, and now I'm at a point where I want to persist my trained models for future use. Can anyone guide me through the process? Any tips or examples would be greatly appreciated! Thanks in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.