From 7b37110a67cdf6add945224915ebae7817fa444c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Mon, 9 Apr 2018 19:14:34 +0100 Subject: fin --- README.md | 99 ------------------------ build-instructions.md | 59 -------------- build-notes.md | 59 ++++++++++++++ src/main/evaluation/benchmark.cpp | 21 +++-- src/main/game/game_state.cpp | 22 +++--- src/main/input-output/input/sol_preset_types.cpp | 38 +-------- src/main/main.cpp | 31 +++++--- 7 files changed, 106 insertions(+), 223 deletions(-) delete mode 100644 README.md delete mode 100644 build-instructions.md create mode 100644 build-notes.md diff --git a/README.md b/README.md deleted file mode 100644 index 3655ce6..0000000 --- a/README.md +++ /dev/null @@ -1,99 +0,0 @@ -# Solvitaire -A solver for a variety of single-player, perfect -information solitaire card games. - -## Project Overview - -The solver should be able to correctly provide -solutions for some instances of solitaire games, and -be able to report other instances as unsolvable, -minimising the number of instances that are deemed -'intractable' (cannot be classified as solvable or -unsolvable within a reasonable time period). A key -study for this project will be how to do this -efficiently for a _variety_ of solitaire games. - -Perfect information games are an especially -important domain in artificial intelligence, and -some of the most high-profile successes in the field -of AI have been creating good players for games like -Chess, Checkers and Go, all of which are perfect -information games. However, relatively little work -has been done on creating programs to play -single-player perfect information games, and good -solvers do not yet exist for most solitaire games. - -Hence, this project will focus on creating a solver -which is general enough to be able to play a wide -range of solitaire games, whilst also maintaining a -high level of efficiency. This efficiency will be -achieved through a combination of language-level -optimisation, and through exploring different AI -search techniques. A specialised solver will be -written to accomplish this, in code rather than -using an existing constraint solving or planning -programs. - -## Objectives - -### Primary Objectives -- An application for solving single-player, perfect -information solitaire instances (software artefact). -- Some kind of schema or language for describing a -_wide_ range of solitaire games. -- The application must be able to solve valid games -expressed using the schema/language. -- Automated testing to demonstrate the correctness -of my classifications. -- An implementation of search techniques such as -'meta-moves'/'super moves' and eliminating -symmetrical states. -- An implementation of language-level/design -optimisations to improve the efficiency of my -search. -- An analysis in my report of the effects of the -above search techniques and implementation choices -on the performance of the solver. - -### Secondary Objectives - -- Optimise the search algorithm to eliminate -searching states in which certain moves/states -'dominate' others. -- Implement and evaluate more than one different -type of search (e.g. best first, iterative -deepening). **Objective adjusted:** This is no -longer deemed essential, as the focus in on -exhaustive search, but it may be interesting -to explore how iterative deepening performs. -- Experiment with and evaluate different designs -for the general heuristic for best-first search, -that works across a variety of games. -**Objective adjusted:** This is no longer relevant. - -### Tertiary Objectives - -Other tertiary objectives that satisfy the core -aims of the project would be reasonable -additions. Below are two particular instances of -interesting routes down which this project could -be taken: - -- An extension of the algorithm that aims to -provide shortened solutions (i.e. ones that -minimise the number of moves taken). -**Objective adjusted:** This may be done in -tandem with the iterative deepening objective. -- Calculating the solvability ratio for some the -games my application can solve. -**Objective adjusted:** Because of good progress -made, this is now very much a target for the -project. - -## Supervisor - -Ian Gent - -## Background - -["Search in the Patience Game Black Hole"](https://pn.host.cs.st-andrews.ac.uk/blackhole-revised-final.pdf) diff --git a/build-instructions.md b/build-instructions.md deleted file mode 100644 index ae6d386..0000000 --- a/build-instructions.md +++ /dev/null @@ -1,59 +0,0 @@ -# How to build / run the project - -The project uses the CMake build system generator. there are several stages for -building the project, running the program, and creating an installer. - -### Installing dependencies - -#### Upgrade GCC (if you don't have version 6) -`sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y` - -`sudo apt-get update -qq` - -`sudo apt-get install -qq g++-6` - -`sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-6` - -#### Boost - -Note: this doesn't have to (and probably shouldn't) be run from within the project, unlike the other commands listed here. - -`sudo wget -O boost_1_63_0.tar.gz http://sourceforge.net/projects/boost/files/boost/1.63.0/boost_1_63_0.tar.gz/download` - -`sudo tar xzvf boost_1_63_0.tar.gz` - -`cd boost_1_63_0/` - -`sudo ./bootstrap.sh --with-libraries=log` - -`sudo ./b2 link=shared threading=multi toolset=gcc optimization=space` - -`sudo ./b2 install` - -`cd ..` - -### Generate Build System - -`cmake [-G "CodeBlocks - Unix Makefiles"] -D CMAKE_CXX_COMPILER=g++-6 -DCMAKE_BUILD_TYPE=[DEBUG|RELEASE] -Bcmake-build-[debug|release] -H.` - -### Build project - -`cmake --build cmake-build-[debug|release]` - -### Run executable - -`./cmake-build-[debug|release]/bin/solvitaire` - -### Create project installer - -`cpack --config cmake-build-[debug|release]/CPackConfig.cmake` - -## Profiling - -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) -kcachegrind solvitaire.callgrind diff --git a/build-notes.md b/build-notes.md new file mode 100644 index 0000000..ae6d386 --- /dev/null +++ b/build-notes.md @@ -0,0 +1,59 @@ +# How to build / run the project + +The project uses the CMake build system generator. there are several stages for +building the project, running the program, and creating an installer. + +### Installing dependencies + +#### Upgrade GCC (if you don't have version 6) +`sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y` + +`sudo apt-get update -qq` + +`sudo apt-get install -qq g++-6` + +`sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-6` + +#### Boost + +Note: this doesn't have to (and probably shouldn't) be run from within the project, unlike the other commands listed here. + +`sudo wget -O boost_1_63_0.tar.gz http://sourceforge.net/projects/boost/files/boost/1.63.0/boost_1_63_0.tar.gz/download` + +`sudo tar xzvf boost_1_63_0.tar.gz` + +`cd boost_1_63_0/` + +`sudo ./bootstrap.sh --with-libraries=log` + +`sudo ./b2 link=shared threading=multi toolset=gcc optimization=space` + +`sudo ./b2 install` + +`cd ..` + +### Generate Build System + +`cmake [-G "CodeBlocks - Unix Makefiles"] -D CMAKE_CXX_COMPILER=g++-6 -DCMAKE_BUILD_TYPE=[DEBUG|RELEASE] -Bcmake-build-[debug|release] -H.` + +### Build project + +`cmake --build cmake-build-[debug|release]` + +### Run executable + +`./cmake-build-[debug|release]/bin/solvitaire` + +### Create project installer + +`cpack --config cmake-build-[debug|release]/CPackConfig.cmake` + +## Profiling + +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) +kcachegrind solvitaire.callgrind diff --git a/src/main/evaluation/benchmark.cpp b/src/main/evaluation/benchmark.cpp index f25e491..b8866ff 100644 --- a/src/main/evaluation/benchmark.cpp +++ b/src/main/evaluation/benchmark.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "benchmark.h" #include "../game/game_state.h" @@ -14,16 +15,14 @@ typedef chrono::microseconds microsec; void benchmark::run(const sol_rules &rules) { cout << "Seed " - "| Median Solution Time(μs) " - "| Median States Searched " - "| Median Backtracks " - "| Median (Non-Zero) Solution Depth " - "| Solvable/Unsolvable "; + "| Median/Mean Solution Time(μs)" + "| Median/Mean States Searched " + "| Solvable/Unsolvable "; multiset sol_times; multiset states_searched; - multiset sol_depths; - multiset backtracks; + int total_time = 0; + int total_states = 0; int total_solvable = 0; int total_unsolvable = 0; @@ -38,9 +37,9 @@ void benchmark::run(const sol_rules &rules) { chrono::duration_cast(end - start); sol_times.insert(static_cast(elapsed_micros.count())); + total_time += static_cast(elapsed_micros.count()); states_searched.insert(sol.get_states_searched()); - if (sol.get_final_depth() != 0) sol_depths.insert(sol.get_final_depth()); - backtracks.insert(sol.get_backtrack_count()); + total_states += sol.get_states_searched(); switch (result) { case solver::sol_state::solved: total_solvable++; @@ -55,9 +54,9 @@ void benchmark::run(const sol_rules &rules) { cout << "\n" << seed << " | " << *next(begin(sol_times), (sol_times.size())/2) + << "/" << total_time/sol_times.size() << " | " << *next(begin(states_searched), (states_searched.size())/2) - << " | " << *next(begin(backtracks), (backtracks.size())/2) - << " | " << *next(begin(sol_depths), (sol_depths.size())/2) + << "/" << total_states/sol_times.size() << " | " << total_solvable << "/" << total_unsolvable; } diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 58f5f68..2e79f41 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -464,8 +464,8 @@ bool game_state::is_valid_auto_foundation_move(pile_ref target_pile) const { // The max difference between the 'move up' card and the foundations of the // same colour and the other colour - int same_col_rank_diff = 0; - int other_col_rank_diff = 0; + int same_rank_diff = 0; + int other_rank_diff = 0; for (pile_ref pr : foundations) { if (pr == target_pile) continue; @@ -475,22 +475,24 @@ bool game_state::is_valid_auto_foundation_move(pile_ref target_pile) const { card c(s, r); auto rank_diff = int(move_up_card.get_rank() - r); - if (move_up_card.get_colour() == c.get_colour()) { - same_col_rank_diff = rank_diff; + if (move_up_card.get_suit() == c.get_suit() + || (rules.build_pol == pol::RED_BLACK + && move_up_card.get_colour() == c.get_colour())) { + same_rank_diff = rank_diff; } else { - other_col_rank_diff = max(other_col_rank_diff, rank_diff); + other_rank_diff = max(other_rank_diff, rank_diff); } } - bool other_col_within_1 = other_col_rank_diff <= 1; - bool other_col_within_2 = other_col_rank_diff <= 2; - bool same_col_within_3 = same_col_rank_diff <= 3; + bool other_within_1 = other_rank_diff <= 1; + bool other_within_2 = other_rank_diff <= 2; + bool same_within_3 = same_rank_diff <= 3; if (rules.build_pol == pol::RED_BLACK) { - return other_col_within_1 || (other_col_within_2 && same_col_within_3); + return other_within_1 || (other_within_2 && same_within_3); } else { assert(rules.build_pol == pol::ANY_SUIT); - return other_col_within_2; + return other_within_2; } } diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 0f2dd67..07c824b 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -894,10 +894,10 @@ map sol_preset_types::mp = { { "tableau piles": { - "count": 5, + "count": 4, "build policy": "any-suit" }, - "max rank": 6 + "max rank": 5 } )" }, @@ -908,10 +908,10 @@ map sol_preset_types::mp = { { "tableau piles": { - "count": 7, + "count": 6, "build policy": "red-black" }, - "max rank": 8 + "max rank": 7 } )" }, @@ -927,36 +927,6 @@ map sol_preset_types::mp = { }, "max rank": 9 } -)" - }, - { - "test-game-founds-removable", - - R"( - -{ - "tableau piles": { - "count": 7, - "build policy": "red-black" - }, - "max rank": 8, - "foundations removable": true -} -)" - }, - { - "test-game-diagonal-deal", - - R"( - -{ - "tableau piles": { - "count": 7, - "build policy": "red-black", - "diagonal deal": true - }, - "max rank": 8 -} )" } }; diff --git a/src/main/main.cpp b/src/main/main.cpp index a8080d5..6d06f5d 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -120,27 +120,38 @@ void solve_input_files(const vector input_files, const sol_rules& rules, void solve_game(const game_state& gs, bool short_sol, bool classify) { solver s(gs); solver* solv = &s; - bool solution; if (short_sol) { uint bound = 1; solver::sol_state ss; + bool solution = false; do { LOG_INFO("Depth: " << bound); solver s_(gs); solv = &s_; ss = s_.run_with_cutoff(none, bound++); + + if (ss == solver::sol_state::solved) { + if (!classify) solv->print_solution(); + cout << "Solved\n"; + solution = true; + } } while (ss == solver::sol_state::cutoff); - solution = ss == solver::sol_state::solved; - } else { - solution = solv->run() == solver::sol_state::solved; - } - if (solution) { - if (!classify) solv->print_solution(); - cout << "Solved\n"; + if (!solution) { + if (!classify) cout << "Deal:\n" << gs << "\n"; + cout << "No Possible Solution\n"; + } + } else { - if (!classify) cout << "Deal:\n" << gs << "\n"; - cout << "No Possible Solution\n"; + bool solution = solv->run() == solver::sol_state::solved; + + if (solution) { + if (!classify) solv->print_solution(); + cout << "Solved\n"; + } else { + if (!classify) cout << "Deal:\n" << gs << "\n"; + cout << "No Possible Solution\n"; + } } cout << "States Searched: " << solv->get_states_searched() << "\n"; -- cgit v1.2.1