Skip to content

Instantly share code, notes, and snippets.

@favre49
Created May 28, 2019 04:42
Show Gist options
  • Save favre49/b2062ae84d84be245ea3e638cbc81a9f to your computer and use it in GitHub Desktop.
Save favre49/b2062ae84d84be245ea3e638cbc81a9f to your computer and use it in GitHub Desktop.
/**
* A Task class that wraps a continuous RL environment.
*/
template<class EnvironmentType>
class ContinuousRLTask
{
ContinuousRLTask(EnvironmentType& environment) : environment(environment)
{ /* Nothing to do here */ }
double Evaluate(Genome& genome)
{
// Set the initial state.
EnvironmentType::State state = environment.InitialSample();
genome.Input(state.Data());
double loss = 0;
while (!environment.IsTerminal())
{
EnvironmentType::Action action;
action.action[0] = genome.Output()[0];
// Use the current action to get the next state.
loss += environment.Sample(state, action, state);
// Update the state of the genome for the next step.
genome.Input(state.Data());
}
return loss;
}
private:
EnvironmentType environment;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment