1 #ifndef MTTOOLBOX_ALGORITHM_BEST_BITS_HPP
2 #define MTTOOLBOX_ALGORITHM_BEST_BITS_HPP
38 #if __cplusplus >= 201103L
41 #define MTTOOLBOX_USE_TR1
56 #if defined(MTTOOLBOX_USE_TR1)
57 using std::tr1::shared_ptr;
59 using std::shared_ptr;
120 param =
new U[param_num];
121 for (
int i = 0; i < param_num; i++) {
141 ss << dec << delta <<
":";
142 for (
int i = 0; i < size; i++) {
143 ss << hex << setw(8) << setfill(
'0') << param[i] <<
",";
187 template<
typename U,
typename V = U>
232 const int shift_values[],
236 obSize = bit_size<U>();
237 bit_len = out_bit_length;
239 shifts =
new int[size];
240 num_pat = size * (size + 1) / 2;
241 for (
int i = 0; i < size; i++) {
242 shifts[i] = shift_values[i];
286 bool verbose =
false) {
289 cout <<
"searching from MSB" << endl;
291 shared_ptr<tempp> initial(
new tempp(size));
293 for (
int i = 0; i < size; i++) {
294 initial->param[i] = 0;
296 vector<shared_ptr<tempp> > params;
297 params.push_back(initial);
300 cout <<
"DEBUG: bit_len = " << dec << bit_len << endl;
302 for (
int p = 0; p < limit; p++) {
303 vector<shared_ptr<tempp> > current;
305 for (
unsigned int i = 0; i < params.size(); i++) {
306 search_best_temper(rand, p, *params[i],
309 delta = rand.
bitSize() * obSize;
310 for (
unsigned int i = 0; i < current.size(); i++) {
311 if (current[i]->delta < delta) {
312 delta = current[i]->delta;
316 cout <<
"DEBUG: delta = " << dec << delta << endl;
319 cout <<
"delta = " << dec << delta << endl;
322 for (
unsigned int i = 0; i < current.size(); i++) {
323 if (current[i]->delta == delta) {
324 params.push_back(current[i]);
330 for (
int i = 0; i < size; i++) {
335 cout <<
"delta = " << dec << delta << endl;
378 vector<shared_ptr<tempp> >& current,
380 int delta = rand.
bitSize() * obSize;
384 num_pat = size * (size + 1) / 2;
385 for (int32_t i = (1 << num_pat) -1; i >= 0; i--) {
386 if (! inRange(i, v_bit)) {
389 shared_ptr<tempp> pattern(
new tempp(size));
390 make_pattern(*pattern, i, v_bit, para);
392 cout <<
"pattern:" << pattern->toString() << endl;
394 for (
int j = 0; j < size; j++) {
397 pattern->delta = get_equidist(rand, v_bit + 1);
399 cout <<
"pattern->delta:" << dec << pattern->delta << endl;
401 if (pattern->delta <= delta) {
402 current.push_back(pattern);
403 delta = pattern->delta;
426 int get_equidist(TemperingCalculatable<U, V>& rand,
428 AlgorithmEquidistribution<U, V> sb(rand, bit_length);
430 return sb.get_all_equidist(veq);
460 void make_pattern(tempp& result,
461 int pat,
int v,
const tempp& para)
const {
463 for (
int i = 0; i < result.size; i++) {
464 result.param[i] = para.param[i];
467 para_mask = (~para_mask) >> v;
471 U mask = 1 << (num_pat - 1);
475 if ((pat & mask) && (obSize > v + sum + 1)) {
476 result.param[index] |= (one << (obSize - v - sum - 1))
478 }
else if (((pat & mask) == 0) && (obSize > v + sum + 1)) {
479 result.param[index] &= ~(one << (obSize - v - sum - 1));
492 cout <<
"make_pattern:" << dec << pat <<
","
493 << dec << v <<
"," << result.toString()
494 << hex << para_mask << endl;
522 bool inRange(
int pat,
int v)
const {
526 U mask = 1 << (num_pat - 1);
529 if ((pat & mask) && (v + shifts[index] + sum > obSize)) {
545 #if defined(MTTOOLBOX_USE_TR1)
546 #undef MTTOOLBOX_USE_TR1
548 #endif // MTTOOLBOX_ALGORITHM_BEST_BITS_HPP
テンパリングパラメータ探索アルゴリズムのための抽象クラス