| 95 | } |
| 96 | |
| 97 | std::vector<std::vector<int>> TrainingGround::TrainAgents(uint16_t nGenerations, uint32_t nTicks) { |
| 98 | std::vector<std::vector<int>> agentFitnesses; |
| 99 | // TODO: Remove this and |
| 100 | std::vector<int> dummyAgentData = {0, 0}; |
| 101 | agentFitnesses.emplace_back(dummyAgentData); |
| 102 | |
| 103 | for (uint16_t gen_Idx = 0; gen_Idx < nGenerations; ++gen_Idx) { |
| 104 | LOG(INFO) << "Beginning Generation " << gen_Idx; |
| 105 | |
| 106 | // Simulate the population |
| 107 | for (uint32_t tick_Idx = 0; tick_Idx < nTicks; ++tick_Idx) { |
| 108 | for (auto &car_agent : car_agents) { |
| 109 | car_agent->simulate(); |
| 110 | } |
| 111 | physicsEngine.stepSimulation(stepTime); |
| 112 | raceNetRenderer.Render(tick_Idx, car_agents, training_track); |
| 113 | if (glfwWindowShouldClose(window)) return agentFitnesses; |
| 114 | } |
| 115 | |
| 116 | // Clear fitness data for next generation |
| 117 | agentFitnesses.clear(); |
| 118 | |
| 119 | // Evaluate the fitnesses and sort them |
| 120 | for (auto &car_agent : car_agents) { |
| 121 | LOG(INFO) << "Agent " << car_agent->populationID << " made it to trkblock " << EvaluateFitness(car_agent); |
| 122 | std::vector<int> agentData = {car_agent->populationID, (int) EvaluateFitness(car_agent)}; |
| 123 | agentFitnesses.emplace_back(agentData); |
| 124 | } |
| 125 | |
| 126 | std::sort(agentFitnesses.begin(), agentFitnesses.end(), |
| 127 | [](const std::vector<int> &a, const std::vector<int> &b) { |
| 128 | return a[1] > b[1]; |
| 129 | }); |
| 130 | |
| 131 | |
| 132 | LOG(DEBUG) << "Agent " << agentFitnesses[0][0] << " was fittest"; |
| 133 | |
| 134 | // Mutate the fittest network |
| 135 | // Mutate(car_agents[agentFitnesses[0][0]]->carNet); |
| 136 | |
| 137 | // Perform selection |
| 138 | SelectAgents(car_agents, agentFitnesses); |
| 139 | |
| 140 | // Mutate all networks |
| 141 | for (auto &car_agent : car_agents) { |
| 142 | Mutate(car_agent->carNet); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | // Reset the cars for the next generation |
| 147 | for (auto &car_agent : car_agents) { |
| 148 | Renderer::ResetToVroad(1, training_track, car_agent); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return agentFitnesses; |
| 153 | } |
nothing calls this directly
no test coverage detected