t_battery *get_bat(char *name) { t_battery *result; char *path; int fd; result = init_bat(); path = my_printf_str("%s/state", name); fd = open(path, O_RDONLY); xfree(path); init_buffer(); if (fd == -1) return (get_batfail(result)); if (init_batstate(fd, result) == -1) return (get_batfail(result)); xclose(fd); path = my_printf_str("%s/info", name); fd = open(path, O_RDONLY); xfree(path); init_buffer(); if (fd == -1) return (get_batfail(result)); if (init_batinfo(fd, result) == -1) return (get_batfail(result)); xclose(fd); return (result); }
void setup() { glClearColor(0.0, 0.0, 0.0, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-RADIUS, RADIUS, -RADIUS, RADIUS, -RADIUS, RADIUS); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); init_light(GL_LIGHT0, 0, 1, 1, 1, 1, 1); init_light(GL_LIGHT1, 1, 0, 1, 1, 1, 1); init_light(GL_LIGHT2, 1, 1, 0, 1, 1, 1); init_balls(); init_bat(); }
bat_t algorithm(float A, float r, uint pop_size, uint max_it, fit_t (*fit_fnc)(bat_t &), float q_min=0, float q_max=MAX/10, uint debug=1, std::vector<float> *avg_fit_at_it=NULL, std::vector<float> *best_at_iter=NULL) { srand(time(NULL)); /*inits randomness*/ bats_t population; bats_fit_t fit; std::vector<float> Q(pop_size); /*frequency*/ std::vector<bat_t> v(pop_size); /*bats velocity*/ /*inits bats velocity to zero*/ std::for_each(v.begin(), v.end(), [](bat_t &velocity) { velocity = init_bat(BAT_LENGTH, 0.0, 0.0); }); init_population(population, pop_size, BAT_LENGTH); init_fit(fit, pop_size); uint it = 0; do { /*one iteration of bat alg.*/ evaluate_population(fit, population, fit_fnc); bat_t best_bat = utils::return_best_bat(population, fit); if (debug >=2) { best_at_iter->push_back(fit_fnc(best_bat)); avg_fit_at_it->push_back(std::accumulate(fit.begin(), fit.end(), 0.0) / fit.size()); } uint index = 0; std::for_each(population.begin(), population.end(), [&](bat_t &bat) { //Q[index] = utils::rand_in<float>(Q_MIN, Q_MAX); Q[index] = utils::rand_in<float>(0.0, 0.1); bat_t candidate_v = v[index] + (bat - best_bat)*Q[index]; bat_t candidate_bat = bat + candidate_v; if (utils::rand_in<float>(0.0,1.0) > r) { /*random walk in direction of candidate_v scalled down by q_max/100 factor*/ for (int i=0;i<2;i++) { candidate_v = init_bat(BAT_LENGTH)*(q_max/100); candidate_bat = best_bat+candidate_v; } } /*evaluate candidate solution, it might be either solution found by appling movement equotions to current solution or by using random walk around best solution*/ float candidate_bat_fit = fit_fnc(candidate_bat); /*accept candidate solution as current solution if better or if not better accept solution with some small probability*/ if (candidate_bat_fit < fit[index] || utils::rand_in<float>(0.0,1.0) < A) { bat = candidate_bat; v[index] = candidate_v; fit[index] = candidate_bat_fit; } index++; }); if (debug>=1 && it%100==1) { evaluate_population(fit, population, fit_fnc); bat_t best = utils::return_best_bat(population, fit); std::cout << "at iteration: " << it << " pop avg fit: " << std::accumulate(fit.begin(), fit.end(), 0.0) / fit.size() << " best bat fit: " << fit_fnc(best) << std::endl; } }while(++it < max_it); /*find and return best solution*/ evaluate_population(fit, population, fit_fnc); return utils::return_best_bat(population, fit); }
void init_population(bats_t &pop, uint population_length, uint bat_length) { for (uint i=0; i< population_length; i++) { pop.push_back( init_bat(bat_length) ); } }