PHP code example of rindow / rindow-rl-agents

1. Go to this page and download the library: Download rindow/rindow-rl-agents library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rindow / rindow-rl-agents example snippets





Rindow\Math\Matrix\MatrixOperator;
use Rindow\NeuralNetworks\Builder\NeuralNetworks;
use Rindow\RL\Gym\ClassicControl\CartPole\CartPoleV1;
use Rindow\RL\Agents\Agent\DQN\DQNAgent;
use Rindow\RL\Agents\Agent\DQN\Runner;
use Rindow\RL\Agents\Env\CartPole\DeviceWrapper;

$mo = new MatrixOperator();
$nn = new NeuralNetworks($mo);
$la = $nn->la();
$hostLA = $mo->laRawMode();
$la->setSeed(42);

$env = new CartPoleV1($hostLA);
$evalEnv = new CartPoleV1($hostLA);

if ($la->accelerated()) {
    $env = new DeviceWrapper($nn, $env);
    $evalEnv = new DeviceWrapper($nn, $evalEnv);
}

$obsDim = $env->observationSpace()->shape()[0];
$numActions = $env->actionSpace()->n();

$agent = new DQNAgent(
    $nn,
    obsDim: $obsDim,
    numActions: $numActions,
    hiddenLayers: [128, 128],
    learningRate: 1.0e-3,
    gamma: 0.99,
    batchSize: 64,
    targetUpdateInterval: 250,
    ddqn: true,
);

$runner = new Runner(
    $la,
    $env,
    $evalEnv,
    $agent,
    obsDim: $obsDim,
    bufferSize: 100_000,
    solvedReward: 475.0,
    solvedEvaluations: 3,
);

$history = $runner->train(
    totalSteps: 200_000,
    learningStarts: 1_000,
    trainEvery: 4,
    evalEvery: 2_000,
    evalEpisodes: 30,
    epsilonStart: 1.0,
    epsilonEnd: 0.05,
    epsilonDecaySteps: 50_000,
    bestModelFile: __DIR__.'/cartpole-ddqn.weights',
);

$agent = new DQNAgent(
    $nn,
    obsDim: 2,
    numActions: 4,
    stateField: 'location',
    actionMaskField: 'actionMask',
);

$agent->saveWeightsToFile(__DIR__.'/agent.weights');
$agent->loadWeightsFromFile(__DIR__.'/agent.weights');
powershell
$env:RINDOW_NEURALNETWORKS_BACKEND = "rindowclblast::GPU"
php samples/cartpole-dqn.php
bash
php samples/cartpole-ppo.php