void bot_mtdf::do_move_search(const board* b, board* res) { stats.start_timer(); last_search_exact = exact; board children[32]; int child_count = b->get_children(children) - children; output() << "bot_" << get_name() << " searching "; if(exact){ output() << "perfectly at depth " << b->count_empty_fields() << '\n'; } else{ output() << "at depth " << get_search_depth() << '\n'; } moves_left = exact ? get_perfect_depth() : get_search_depth(); do_sorting(children,child_count); int best_heur = exact ? MIN_PERFECT_HEURISTIC : MIN_HEURISTIC; int first_guess; { int tmp = moves_left - 6; if(tmp < 0){ first_guess = heuristic(); } else{ std::swap(tmp,moves_left); first_guess = mtdf<false,false>(0,best_heur); std::swap(tmp,moves_left); } } for(int id=0;id<child_count;++id){ inspected = children[id]; moves_left--; int cur_heur = mtdf<true,exact>(id==0 ? first_guess : best_heur,best_heur); moves_left++; if(cur_heur > best_heur){ best_heur = cur_heur; *res = children[id]; } output() << "move " << (id+1) << "/" << (child_count); output() << " (" << board::index_to_position(b->get_move_index(children+id)) << ')'; output() << ": " << best_heur << '\n'; } stats.stop_timer(); output() << big_number(stats.get_nodes()) << " nodes in "; output() << stats.get_seconds() << " seconds: "; output() << big_number(stats.get_nodes_per_second()) << " nodes / sec\n"; }
int bot_mtdf::null_window(int alpha) { if(sort){ return null_window<false,exact>(alpha); } stats.inc_nodes(); if((!exact) && moves_left == 0){ return heuristic(); } if(sort && ((!exact && moves_left<NORMAL_MOVE_SORT_DEPTH) || (exact && moves_left<PERFECT_MOVE_SORT_DEPTH))){ return null_window<false,exact>(alpha); } int move = -1; if(suitable_hashtable_entry<exact>()){ hash_table_t::iterator ht_iter = hash_table.find(inspected); if(ht_iter != hash_table.end()){ ++ht_iter->second.uses; if(ht_iter->second.lower_bound > alpha){ return alpha+1; } if(ht_iter->second.upper_bound <= alpha){ return alpha; } move = ht_iter->second.best_move; if(move != -1){ bits64 undo_data = inspected.do_move(move); if(!exact){ moves_left--; } int score = -null_window<sort,exact>(-alpha-1); if(!exact){ moves_left++; } inspected.undo_move(bits64::mask_set[move],undo_data); if(score > alpha){ move = ht_iter->second.best_move; return update_hash_table<exact>(alpha,alpha+1,move); } } } } bits64 valid_moves = inspected.get_valid_moves(); if(move != -1){ valid_moves &= bits64::mask_reset[move]; if(valid_moves.none()){ return update_hash_table<exact>(alpha,alpha,move); } } if(valid_moves.none()){ int heur; inspected.switch_turn(); if(inspected.get_valid_moves().none()){ inspected.switch_turn(); heur = inspected.get_disc_diff(); if(!exact){ heur *= EXACT_SCORE_FACTOR; } } else{ heur = -null_window<sort,exact>(-alpha-1); assert(heur==alpha || heur==alpha+1); inspected.switch_turn(); } return update_hash_table<exact>(alpha,alpha+(heur>alpha),move); } if(sort){ board children[32]; int child_count = inspected.get_children(children,valid_moves) - children; do_sorting(children,child_count); for(int i=0;i<child_count;++i){ move = children[i].get_move_index(&inspected); std::swap(inspected,children[i]); if(!exact){ moves_left--; } int score = -null_window<sort,exact>(-alpha-1); if(!exact){ moves_left++; } std::swap(inspected,children[i]); if(score > alpha){ return update_hash_table<exact>(alpha,alpha+1,move); } } } else{ for(int i=0;i<9;i++){ bits64 location_moves = valid_moves & board::ordered_locations[i]; while(location_moves.any()){ bits64 bit = location_moves.first_bit(); move = bit.only_bit_index(); bits64 undo_data = inspected.do_move(move); if(!exact){ moves_left--; } int score = -null_window<sort,exact>(-alpha-1); if(!exact){ moves_left++; } inspected.undo_move(bit,undo_data); location_moves ^= bit; if(score > alpha){ return update_hash_table<exact>(alpha,alpha+1,move); } } } } return update_hash_table<exact>(alpha,alpha,move); }
int main_i(const std::vector<std::string> &argv) { int argc = argv.size(); if (argc == 1 || (argc == 2 && argv[1] == "-h")) { std:: cout << "Picosel ver. 1.5 (C) 2009-2010 AIST" "\n" "Usage 1: picosel OPTION from inputfile select column where EXPRESSION" "\n" "Usage 2: picosel OPTION from inputfile select column order by column" "\n" "Option" "\n" " -o outputfile: specify output file." "\n"; return 0; } if (argc == 3 && argv[1] == "--text") { return convert_binary_file_to_text(argv[2]); } int r = analyze_commandline(argv); if (r != 0) { return r; } std:: ifstream input; input.open(fromFile.c_str(), std:: ios::in | std:: ios::binary); if (! input.is_open()) { std:: cerr << "error: can not open file '" << fromFile << "'" << std:: endl; return 1; } std:: istream *pInput = &input; r = get_column_names(pInput); if (r != 0) { return r; } int selectColIndex = findNamedColumn(selectCol); if (selectColIndex == -1) { std:: cerr << "error: unknown column name '" << selectCol << "'" << std:: endl; return 1; } r = check_expression(); if (r != 0) { return r; } std:: ostream *pOutput = &std:: cout; std:: ofstream output; if (! outputFile.empty()) { output.open(outputFile.c_str(), binaryOutputFactor != 0 ? (std:: ios::out | std:: ios::binary) : std:: ios::out); if (! output.is_open()) { std:: cerr << "error: can not create file '" << outputFile << "'" << std:: endl; return 1; } pOutput = &output; } if (! orderByAsc.empty()) { int orderColIndex = findNamedColumn(orderByAsc); if (orderColIndex == -1) { std:: cerr << "error: unknown column name '" << orderByAsc << "'" << std:: endl; return 1; } r = do_sorting(pInput, pOutput, selectColIndex, orderColIndex, 1/* asc */, outputFile); } else if (! orderByDesc.empty()) { int orderColIndex = findNamedColumn(orderByDesc); if (orderColIndex == -1) { std:: cerr << "error: unknown column name '" << orderByDesc << "'" << std:: endl; return 1; } r = do_sorting(pInput, pOutput, selectColIndex, orderColIndex, -1/* desc */, outputFile); } else { r = do_selecting(pInput, pOutput, selectColIndex); } output.close(); input.close(); return r; }