Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <mlpack/methods/neat/neat.hpp>
- #include "environment.hpp"
- using namespace gym;
- using namespace mlpack::neat;
- class GymTask
- {
- public:
- GymTask()
- { /* Nothing to do here */ }
- double Evaluate(Genome<> genome)
- {
- const std::string environment = "CartPole-v0";
- const std::string host = "127.0.0.1";
- const std::string port = "4040";
- double totalReward = 0;
- size_t totalSteps = 0;
- Environment env(host, port, environment);
- arma::mat observation = env.reset();
- while (1)
- {
- arma::vec input = {observation(0, 0), observation(1, 0), observation(2, 0), observation(3, 0)};
- arma::vec output = genome.Evaluate(input);
- output = arma::clamp(output, 0, 1);
- output[0] = std::round(output[0]);
- arma::mat action(output);
- env.step(action);
- observation = env.observation;
- if (env.done)
- break;
- totalReward += env.reward;
- totalSteps += 1;
- }
- std::cout << "Instance: " << env.instance << " total steps: " << totalSteps
- << " reward: " << totalReward << std::endl;
- return totalReward;
- }
- double EvaluateWithVid(Genome<> genome)
- {
- const std::string environment = "CartPole-v0";
- const std::string host = "127.0.0.1";
- const std::string port = "4040";
- double totalReward = 0;
- size_t totalSteps = 0;
- Environment env(host, port, environment);
- env.compression(0);
- env.monitor.start("./dummy/", true, true);
- arma::mat observation = env.reset();
- env.render();
- while (1)
- {
- arma::vec input = {observation(0, 0), observation(1, 0), observation(2, 0), observation(3, 0)};
- arma::vec output = genome.Evaluate(input);
- output = arma::clamp(output, 0, 1);
- output[0] = std::round(output[0]);
- arma::mat action(output);
- env.step(action);
- if (env.done)
- break;
- observation = env.observation;
- totalReward += env.reward;
- totalSteps += 1;
- std::cout << "Current step: " << totalSteps << " current reward: "
- << totalReward << std::endl;
- }
- std::cout << "Instance: " << env.instance << " total steps: " << totalSteps
- << " reward: " << totalReward << std::endl;
- std::cout << "Video: https://kurg.org/media/gym/" << env.instance
- << " (it might take some minutes before the video is accessible)."
- << std::endl;
- return totalReward;
- }
- };
- int main(int argc, char* argv[])
- {
- GymTask task;
- NEAT<GymTask> model(task, 4, 1, 100, 20, 10);
- Genome<> bestGenome = model.Train();
- double finalFitness = task.EvaluateWithVid(bestGenome);
- std::cout << finalFitness << std::endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment