Exemplo n.º 1
0
        public void TestSetDebugLevel()
        {
            var env = new UnityEnvironment();

            env.SetDebugLevel(DebugLevels.Prod);

            Assert.AreEqual(DebugLevels.Prod, env.DebugLevel);
        }
Exemplo n.º 2
0
 public UnityEnvironmentProxy(UnityEnvironment unityEnvironment)
 {
     this.unityEnvironment = unityEnvironment ?? throw new ArgumentNullException(nameof(unityEnvironment));
 }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            GradientEngine.UseEnvironmentFromVariable();
            TensorFlowSetup.Instance.EnsureInitialized();
            Console.Title = "ML Agents";

            if (args.Length == 1)
            {
                SetCpuAffinity(limitCoresTo: int.Parse(args[0]));
            }

            // debug environment, that does not require Unity: goal is to simply repeat observation
            // RunRepeat();

            var engineConfigChannel = new EngineConfigurationChannel();
            // connect to running Unity, you'll have to press Play there
            const string?envName = null;

            Console.WriteLine("RELEASE THE KRAKEN!");
            var env = new UnityEnvironment(base_port: 5004, file_name: envName,
                                           side_channels: new[] { engineConfigChannel });

            try {
                engineConfigChannel.set_configuration_parameters(time_scale: 3.3);
                env.reset();

                // TODO: fix behaviors_specs to be real Dictionary
                const string agentGroup = "3DBall?team=0";
                BehaviorSpec spec       = env.behavior_specs_dyn[agentGroup];

                (DecisionSteps, TerminalSteps)stepResult = env.get_steps(agentGroup);
                Debug.Assert(stepResult.Item1.obs.Count == 1);
                (int agentCount, int observationSize) = ((int, int))((ndarray)stepResult.Item1.obs[0]).shape;

                if (!spec.is_action_continuous())
                {
                    throw new NotImplementedException("discrete");
                }

                var random = new Random();
                ndarray RandomActionSampler()
                // a list of random values between -1.0 and +1.0
                => (ndarray)ndarray.FromList(Range(0, spec.action_size * agentCount)
                                             .Select(_ => (float)random.NextDouble() * 2 - 1)
                                             .ToList())
                .reshape(new int[] { agentCount, spec.action_size })
                .astype(PythonClassContainer <float32> .Instance);

                SoftActorCritic.SoftActorCritic.Run(new UnityEnvironmentProxy(env),
                                                    agentGroup: agentGroup,
                                                    actorCriticFactory: ActorCriticFactory,
                                                    observationDimensions: observationSize,
                                                    actionDimensions: spec.action_size,
                                                    actionLimit: 1,
                                                    feedFrames: 1,
                                                    maxEpisodeLength: 1024,
                                                    startSteps: 2048,
                                                    replaySize: 1024 * 1024 / 8,
                                                    actionSampler: RandomActionSampler);
            } finally {
                env.close();
            }
        }
Exemplo n.º 4
0
        public static async Task <int> Main(string[] args)
        {
            // monitoring when the ipc host shuts down
            var exiting = new ManualResetEventSlim();

            var configuration = GetConfiguration(args);

            if (configuration.Debug)
            {
                if (!Debugger.IsAttached)
                {
                    Debugger.Launch();
                }
                else
                {
                    Debugger.Break();
                }
            }

            var taskManager = new TaskManager();
            var syncContext = new ThreadSynchronizationContext(taskManager.Token);

            taskManager.Initialize(syncContext);

            var environment = new UnityEnvironment("Process Manager")
                              .Initialize(configuration.ProjectPath, configuration.UnityVersion,
                                          configuration.UnityApplicationPath, configuration.UnityContentsPath);


            var host = new RpcHostedServer(configuration)
                       .AddRemoteProxy <IServerNotifications>()
                       .AddRemoteProxy <IProcessNotifications>()
                       .AddLocalTarget <ProcessServer.Implementation>()
                       .AddLocalScoped <ProcessRunner.Implementation>()
            ;

            host.Stopping(s => {
                s.GetService <ProcessRunner>().Shutdown();
                s.GetService <ProcessServer>().Shutdown();
                exiting.Set();
            })
            .ClientConnecting(s => {
                // keep track of clients so we can broadcast notifications to them
                s.GetService <ProcessServer>().ClientConnecting(s.GetRequestContext());
            })
            .ClientDisconnecting((s, disconnected) => {
                s.GetService <ProcessRunner>().ClientDisconnecting(s.GetRequestContext());
                s.GetService <ProcessServer>().ClientDisconnecting(s.GetRequestContext());
            });

            // set up a logger
            var logLevelSwitch = new LoggingLevelSwitch {
                MinimumLevel = LogEventLevel.Debug
            };

            host.UseSerilog((context, config) =>
                            config.MinimumLevel.ControlledBy(logLevelSwitch)
                            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                            .Enrich.FromLogContext()
                            .WriteTo.Console());

            host.ConfigureServices(services => {
                // register the log switch so it can be retrieved and changed by any code
                services.AddSingleton(logLevelSwitch);

                // make the configuration available to everyone
                services.AddSingleton(configuration);
                services.AddSingleton <ITaskManager>(taskManager);
                services.AddSingleton <IEnvironment>(environment);
                services.AddSingleton <IProcessEnvironment>(s => s.GetService <IProcessManager>().DefaultProcessEnvironment);
                services.AddSingleton <IProcessManager, ProcessManager>();
                services.AddSingleton <ProcessRunner>();
                services.AddSingleton <ProcessServer>();
            });

            host.UseConsoleLifetime();

            await host.Start();

            Console.WriteLine($"Port:{host.Rpc.Configuration.Port}");

            try
            {
                await host.Run();
            } catch {}

            // wait until all stop events have completed
            exiting.Wait();

            syncContext.Dispose();

            return(0);
        }