/// Perturbs a random plan void PlanningSystem::mutate() { double d = rand.uniform(); GMatrix& p = *plans[rand.next(plans.size())]; if(d < 0.1) { // lengthen the plan if(p.rows() < maxPlanLength) { GVec* newActions = new GVec(actionDims); newActions->fillUniform(rand); p.takeRow(newActions, rand.next(p.rows() + 1)); } } else if(d < 0.2) { // shorten the plan if(p.rows() > 1) { p.deleteRow(rand.next(p.rows())); } } else if(d < 0.7) { // perturb a single element of an action vector GVec& actions = p[rand.next(p.rows())]; size_t i = rand.next(actions.size()); actions[i] = std::max(0.0, std::min(1.0, actions[i] + 0.03 * rand.normal())); } else if(d < 0.9) { // perturb a whole action vector GVec& actions = p[rand.next(p.rows())]; for(size_t i = 0; i < actions.size(); i++) { actions[i] = std::max(0.0, std::min(1.0, actions[i] + 0.02 * rand.normal())); } } else { // perturb the whole plan for(size_t j = 0; j < p.rows(); j++) { GVec& actions = p[j]; for(size_t i = 0; i < actions.size(); i++) { actions[i] = std::max(0.0, std::min(1.0, actions[i] + 0.01 * rand.normal())); } } } }