From d3dc469aa65761e1432727b72b0accb9325e9cc3 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 23 Feb 2018 12:53:35 +0000 Subject: Update prof instuctions with line number hint --- build-instructions.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/build-instructions.md b/build-instructions.md index 7a76227..ae6d386 100644 --- a/build-instructions.md +++ b/build-instructions.md @@ -50,7 +50,9 @@ Note: this doesn't have to (and probably shouldn't) be run from within the proje ## Profiling -env CPUPROFILE=solvitaire.prof CPUPROFILESIGNAL=12 CPUPROFILE_FREQUENCY=1000 cmake-build-release/bin/solvitaire --type black-hole --random 4 --classify & +To get line numbers, you will first need to change the release build from -03 to -02 + +env CPUPROFILE=solvitaire.prof CPUPROFILESIGNAL=12 CPUPROFILE_FREQUENCY=1000 cmake-build-release/bin/solvitaire --type spanish-patience --random 4 --classify & killall -12 solvitaire (start profiling) killall -12 solvitaire (stop profiling) pprof --callgrind src/ solvitaire.prof.0 > solvitaire.callgrind (ignore any warnings) -- cgit v1.2.1 From fc8d0664e2e1263c6b1de1244190aa4dd66682cd Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 23 Feb 2018 12:53:56 +0000 Subject: Change next moves from list to vector --- src/main/solver/solver.cpp | 1 + src/main/solver/solver.h | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index e55a4e3..3542383 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -77,6 +77,7 @@ bool solver::run() { } void solver::add_children(std::vector& moves) { + current_node->children.reserve(moves.size()); for (auto move : moves) { add_child(move); } diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 9488dda..518ba2b 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -6,7 +6,6 @@ #define SOLVITAIRE_SOLVER_H #include -#include #include #include "../game/global_cache.h" @@ -20,7 +19,7 @@ public: node(node*, game_state::move); node* parent; const game_state::move move; - std::list children; + std::vector children; }; explicit solver(const game_state&); -- cgit v1.2.1 From 27972117db0feafd481d1a6907ef93752cd2226c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 23 Feb 2018 21:26:31 +0000 Subject: Fix double call to gen legal moves in solver --- src/main/solver/solver.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 3542383..9f0e1f3 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -48,10 +48,11 @@ bool solver::run() { bool is_new_state = cache.insert(state); if (is_new_state) { // Gets the legal moves in the current state - vector next_moves = state.get_legal_moves(); + vector next_moves = + state.get_legal_moves(); // If there are none, reverts to the last node with children - if (state.get_legal_moves().empty()) { + if (next_moves.empty()) { states_exhausted = revert_to_last_node_with_children(); } else { add_children(next_moves); -- cgit v1.2.1 From fddc559c5ae441906c2edeaab9ec8ea1a06e62e0 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 24 Feb 2018 11:09:47 +0000 Subject: Add NO_CARD_JUST_MOVED optimisation flag to CMAKE --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index ff12767..0e2a21b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -153,6 +153,11 @@ add_executable(solvitaire-no-auto-foundations ${main} ${sources}) target_link_libraries(solvitaire-no-auto-foundations LINK_PUBLIC ${Boost_LIBRARIES} -lprofiler) set_target_properties(solvitaire-no-auto-foundations PROPERTIES COMPILE_FLAGS -DNO_AUTO_FOUNDATIONS) +# add a separete executable for the 'no card just moved' version of the solver +add_executable(solvitaire-no-card-just-moved ${main} ${sources}) +target_link_libraries(solvitaire-no-card-just-moved LINK_PUBLIC ${Boost_LIBRARIES} -lprofiler) +set_target_properties(solvitaire-no-card-just-moved PROPERTIES COMPILE_FLAGS -DNO_CARD_JUST_MOVED) + ############################################################################### ## testing #################################################################### -- cgit v1.2.1 From 5c168518766037ef7129cc28febdd03afb8e11ee Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 24 Feb 2018 11:10:54 +0000 Subject: Add card just moved optimisation to game logic --- optimisation-test.py | 16 ++++++++++------ src/main/game/game_state.cpp | 12 +++++++++--- src/main/game/game_state.h | 6 +++++- src/main/game/game_state.legal_moves.cpp | 20 ++++++++++++++++---- src/main/solver/solver.cpp | 13 ++++++++++--- src/main/solver/solver.h | 1 + 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/optimisation-test.py b/optimisation-test.py index 6f936ef..3703905 100644 --- a/optimisation-test.py +++ b/optimisation-test.py @@ -1,13 +1,17 @@ import sys from subprocess import run, CalledProcessError, DEVNULL, PIPE, Popen -runCount = 7500 +runCount = 1000 # Loops through each canonical solitaire -for optimisation in ["-no-reduced-state", - "-no-pile-symmetry", - "-no-auto-foundations"]: +for optimisation in [ +# "-no-reduced-state", +# "-no-pile-symmetry", +# "-no-auto-foundations", + "-no-card-just-moved" +]: + for preset in [ "simple-alpha-star", "simple-bakers-dozen", @@ -58,8 +62,8 @@ for optimisation in ["-no-reduced-state", except CalledProcessError: print("Error running " + preset + " with seed " + str(attempt) + "!") sys.exit() - except: - print("Unknown error in script") + except Exception as e: + print("Unknown error in script: ", e) sys.exit() run("pkill solvitaire", shell=True) diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 322bfcc..6067c15 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -228,11 +228,11 @@ bool game_state::move::is_dominance() const { ///////////////////////// void game_state::make_move(const move m) { - assert(m.from < piles.size()); - assert(m.to < piles.size()); - // Handles special stock-to-tableau-piles move if (rules.stock_deal_t == sdt::TABLEAU_PILES && m.from == stock) { + assert(m.from == stock); + assert(m.count == 255 ); + for (pile_ref tab_pr = original_tableau_piles.front(); tab_pr < pile_ref(original_tableau_piles.front() + m.count); tab_pr++) { @@ -241,6 +241,9 @@ void game_state::make_move(const move m) { } // If this is not a built-pile move else if (m.count == 1 || m.is_dominance()) { + assert(m.from < piles.size()); + assert(m.to < piles.size()); + place_card(m.to, take_card(m.from)); #ifndef NO_AUTO_FOUNDATIONS update_auto_foundation_moves(m.to); @@ -248,7 +251,10 @@ void game_state::make_move(const move m) { } // If this is a built-pile move else { + assert(m.from < piles.size() ); + assert(m.to < piles.size() ); assert(m.count <= rules.max_rank); + // Adds the cards to the 'to' pile for (auto pile_idx = m.count; pile_idx-- > 0;) { place_card(m.to, piles[m.from][pile_idx]); diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index b403bc7..aeabc10 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -49,7 +49,11 @@ public: void place_card(pile_ref, card); card take_card(pile_ref); - std::vector get_legal_moves(); + std::vector get_legal_moves( +#ifndef NO_CARD_JUST_MOVED + move +#endif + ); // Inspects state bool is_solved() const; diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index c1a87e4..99431e3 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -13,7 +13,11 @@ typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; typedef sol_rules::stock_deal_type sdt; -vector game_state::get_legal_moves() { +vector game_state::get_legal_moves( +#ifndef NO_CARD_JUST_MOVED + move parent_move +#endif +) { // The next legal moves vector moves; @@ -24,8 +28,16 @@ vector game_state::get_legal_moves() { // Cycles through each pile which we may be able to remove a card from for (pile_ref rem_ref = 0; rem_ref < piles.size(); rem_ref++) { - // Never removes a card from the hole, the waste, or an empty pile - if ((rules.hole && rem_ref == hole) || piles[rem_ref].empty()) continue; + // Never removes a card from the hole, an empty pile, or the pile we + // just moved a card to + if ((rules.hole && rem_ref == hole) + || piles[rem_ref].empty() +#ifndef NO_CARD_JUST_MOVED + || (rem_ref == parent_move.to + && parent_move.count <= 1 + && parent_move.from != stock) +#endif + ) continue; // If we have a foundations card as the rem_ref if (rules.foundations @@ -100,7 +112,7 @@ game_state::move game_state::get_stock_tableau_move() const { : piles[stock].size(); assert(stock_moves > 0 && stock_moves <= piles[stock].size()); - return {stock, 0, stock_moves}; + return {stock, 255, stock_moves}; } // If a tableau pile is empty and the space policy is diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 9f0e1f3..ab24824 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -19,7 +19,7 @@ solver::solver(const game_state& gs) , init_state(gs) , state(gs) , states_searched(1) - , root(nullptr, game_state::move(0, 0)) + , root(nullptr, game_state::move(255, 255, 255)) , current_node(&root) { } @@ -48,8 +48,7 @@ bool solver::run() { bool is_new_state = cache.insert(state); if (is_new_state) { // Gets the legal moves in the current state - vector next_moves = - state.get_legal_moves(); + vector next_moves = get_next_moves(); // If there are none, reverts to the last node with children if (next_moves.empty()) { @@ -84,6 +83,14 @@ void solver::add_children(std::vector& moves) { } } +vector solver::get_next_moves() { +#ifndef NO_CARD_JUST_MOVED + return state.get_legal_moves(current_node->move); +#else + return state.get_legal_moves(); +#endif +} + void solver::add_child(game_state::move move) { current_node->children.emplace_back(current_node, move); } diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 518ba2b..80d4ef5 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -33,6 +33,7 @@ private: bool revert_to_last_node_with_children(); void add_children(std::vector&); void add_child(game_state::move); + std::vector get_next_moves(); const game_state init_state; game_state state; -- cgit v1.2.1