rosa/bench/graspels/common.h

87 lines
1.9 KiB
C++

#ifndef ROSA_BENCH_GRASPELS_COMMON_H
#define ROSA_BENCH_GRASPELS_COMMON_H
#include <algorithm>
#include <iostream>
#include <random>
#include <thread>
#include <sys/resource.h>
#include <sys/time.h>
#include <alsk/alsk.h>
#include <muscles/descent.h>
#include <muscles/move2opt.h>
#include <muscles/rgreedy.h>
#include <rosa/els.h>
#include <rosa/grasp.h>
#include <tsp/solution.h>
#include <tsp/problem.h>
#include <tsp/tsp.h>
#ifndef DATA_FILE
#define DATA_FILE "../data/qa194"
#endif
#ifndef GRASP_N
#define GRASP_N 2
#endif
#ifndef ELS_ITER_MAX
#define ELS_ITER_MAX 20
#endif
#ifndef ELS_GEN
#define ELS_GEN 10
#endif
#ifndef FUNC
#define FUNC none
#endif
#ifndef NTHREADS
#define NTHREADS 1
#endif
#ifndef SEED
#define SEED std::mt19937::default_seed
#endif
#define STR_(A) #A
#define STR(A) STR_(A)
/* repeatable* */
#define REPRODUCIBLE
using RNG = std::mt19937;
struct Arguments {
std::mt19937::result_type seed;
};
inline tsp::Solution selectMin(tsp::Solution const& a, tsp::Solution const& b) { return a<b? a:b; }
inline auto rgreedy() { return RGreedy<tsp::Solution>{2}; }
inline double tvdiff(struct timeval& b, struct timeval& e) {
return (e.tv_sec - b.tv_sec) + (e.tv_usec - b.tv_usec) / 1e6;
}
template<typename F, typename... Args>
void timeit(int who, std::string const& prefix, F&& f, Args&&... args) {
using Clock = std::chrono::high_resolution_clock;
struct rusage b, e;
auto tp0 = Clock::now();
getrusage(who, &b);
std::forward<F>(f)(std::forward<Args>(args)...);
getrusage(who, &e);
auto tp1 = Clock::now();
std::cout << prefix;
std::cout << "[" << std::this_thread::get_id() << "] ";
std::cout << "time: ";
std::cout << "real " << std::chrono::duration<double>(tp1 - tp0).count() << " ";
std::cout << "user " << tvdiff(b.ru_utime, e.ru_utime) << " ";
std::cout << "sys " << tvdiff(b.ru_stime, e.ru_stime);
std::cout << std::endl;
}
#endif