├── code_coverage.info ├── .gitignore ├── src ├── Log.h ├── Agent.h ├── ParallelTree.h ├── ParallelRoot.h ├── main.cpp ├── config.h ├── TreeNode.h ├── Policy.h ├── MCTS.h └── board.h ├── script ├── cov.sh └── record.sh ├── CMakeLists.txt.in ├── CMakeLists.txt ├── .github └── workflows │ └── cmake.yml ├── unittest └── MCTS_UnitTest.cpp ├── README.md └── LICENSE /code_coverage.info: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | mcts 2 | build 3 | result.txt 4 | *.info 5 | -------------------------------------------------------------------------------- /src/Log.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | using namespace std; 4 | 5 | // store one game's log 6 | class Log { 7 | public: 8 | Log() : cost_time(0.0), search_count(0) {}; 9 | 10 | void printLog() { 11 | cout << "Total time cost: " << cost_time << " ms.\n"; 12 | cout << "Total search count: " << search_count << " times.\n\n"; 13 | } 14 | 15 | double cost_time; 16 | int search_count; 17 | 18 | }; -------------------------------------------------------------------------------- /script/cov.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | PROJECT_ROOT=$(pwd | rev | cut -d / -f 1 --complement | rev ) 3 | PROJECT_ROOT=$PROJECT_ROOT/build 4 | 5 | cd $PROJECT_ROOT 6 | 7 | # execute unittest 8 | ctest 9 | 10 | cd $PROJECT_ROOT/CMakeFiles/MCTS_UnitTest.dir/uinttest 11 | gcov MCTS_UnitTest.cpp.gcno 12 | lcov --capture --directory . --output-file gtest_coverage.info 13 | genhtml gtest_coverage.info --output-directory CODE_COVERAGE 14 | cd CODE_COVERAGE 15 | xdg-open index.html 16 | -------------------------------------------------------------------------------- /CMakeLists.txt.in: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.12) 2 | 3 | project(googletest-download NONE) 4 | 5 | include(ExternalProject) 6 | ExternalProject_Add(googletest 7 | GIT_REPOSITORY https://github.com/google/googletest.git 8 | GIT_TAG master 9 | SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src" 10 | BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build" 11 | CONFIGURE_COMMAND "" 12 | BUILD_COMMAND "" 13 | INSTALL_COMMAND "" 14 | TEST_COMMAND "" 15 | ) 16 | -------------------------------------------------------------------------------- /src/Agent.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "board.h" 3 | #include "Log.h" 4 | 5 | #include 6 | 7 | using namespace std; 8 | class Agent { 9 | 10 | public: 11 | Agent() : piece(BLACK) , step(0) {}; 12 | Agent(PIECE piece) : piece(piece), step(0) {}; 13 | 14 | Pair take_action(board &b, Pair (*Policy)(board&, const PIECE&,const EnvParameter&, Log &log), Log &log, EnvParameter &envParam) { 15 | 16 | Pair mv = Policy(b, piece, envParam, log); 17 | 18 | EXEC_STATE S = b.move(mv.prev, mv.next, piece); 19 | step++; 20 | if (S == FAIL) { 21 | return {}; 22 | } else { 23 | return mv; 24 | } 25 | }; 26 | inline int getStep() const { return step; } 27 | 28 | protected: 29 | const PIECE piece; 30 | int step; 31 | 32 | }; -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | option(COVERAGE "COVERAGE" OFF) 4 | 5 | # Download and unpack googletest at configure time 6 | configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt) 7 | execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . 8 | RESULT_VARIABLE result 9 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) 10 | if(result) 11 | message(FATAL_ERROR "CMake step for googletest failed: ${result}") 12 | endif() 13 | execute_process(COMMAND ${CMAKE_COMMAND} --build . 14 | RESULT_VARIABLE result 15 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) 16 | if(result) 17 | message(FATAL_ERROR "Build step for googletest failed: ${result}") 18 | endif() 19 | 20 | # Prevent overriding the parent project's compiler/linker 21 | # settings on Windows 22 | set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) 23 | 24 | # Add googletest directly to our build. This defines 25 | # the gtest and gtest_main targets. 26 | add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src 27 | ${CMAKE_CURRENT_BINARY_DIR}/googletest-build 28 | EXCLUDE_FROM_ALL) 29 | 30 | # The gtest/gtest_main targets carry header search path 31 | # dependencies automatically when using CMake 2.8.11 or 32 | # later. Otherwise we have to add them here ourselves. 33 | if (CMAKE_VERSION VERSION_LESS 2.8.11) 34 | include_directories("${gtest_SOURCE_DIR}/include") 35 | endif() 36 | 37 | # set c++ edition 38 | set(CMAKE_CXX_STANDARD 17) 39 | set(CMAKE_CXX_STANDARD_REQUIRED True) 40 | 41 | # set path 42 | set(TEST unittest/) 43 | set(SRC src/) 44 | # enable ctest 45 | enable_testing() 46 | 47 | # link against gtest or gtest_main 48 | add_executable(MCTS_UnitTest ${TEST}/MCTS_UnitTest.cpp) 49 | target_link_libraries(MCTS_UnitTest gtest_main) 50 | add_test(NAME MCTS_Test COMMAND MCTS_UnitTest) 51 | 52 | project(mcts) 53 | if (COVERAGE) 54 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -fopenmp -O3 -g -Wall -fsanitize=leak --coverage" ) 55 | else() 56 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -fopenmp -O3 -g -Wall" ) 57 | endif() 58 | add_executable(mcts ${SRC}/main.cpp) -------------------------------------------------------------------------------- /script/record.sh: -------------------------------------------------------------------------------- 1 | # !/bin/sh 2 | 3 | GAMES=10 4 | BLACK=0 5 | WHITE=0 6 | PLAYER_CNT=0 7 | PLAYER_TIME=0 8 | ENV_CNT=0 9 | ENV_TIME=0 10 | 11 | PROJECT_ROOT=$(pwd | rev | cut -d / -f 1 --complement | rev ) 12 | PROJECT_ROOT=$PROJECT_ROOT/build 13 | # /home/cyctw/Documents/PP/FinalPP/Parallel-MCTS/build 14 | 15 | cd $PROJECT_ROOT 16 | 17 | for i in $(seq 1 $GAMES); 18 | do 19 | ./mcts $@ > result.txt 20 | 21 | game_player_cnt=`sed -n '/Player/,/Envir/p' tesst | grep "Total search count: " | cut -d ":" -f2 | cut -d " " -f2` 22 | game_env_cnt=`sed -n '/Envir/,/times/p' tesst | grep "Total search count: " | cut -d ":" -f2 | cut -d " " -f2` 23 | 24 | PLAYER_CNT=`echo $PLAYER_CNT + $game_player_cnt | bc` 25 | ENV_CNT=`echo $ENV_CNT + $game_env_cnt | bc` 26 | 27 | game_player_time=`sed -n '/Player/,/Envir/p' result.txt | grep "Total time cost: " | cut -d ":" -f2 | cut -d " " -f2` 28 | game_env_time=`sed -n '/Envir/,/times/p' tesst | grep "Total time cost: " | cut -d ":" -f2 | cut -d " " -f2` 29 | 30 | PLAYER_TIME=`echo $PLAYER_TIME + $game_player_time | bc` 31 | ENV_TIME=`echo $ENV_TIME + $game_env_time | bc` 32 | 33 | winner=`cat result.txt | grep "winner:" | cut -d ":" -f2` 34 | if [ $winner = "Black" ]; then 35 | BLACK=$((BLACK+1)) 36 | elif [ $winner = "White" ]; then 37 | WHITE=$((WHITE+1)) 38 | else 39 | BLACK=`echo $BLACK + 0.5 | bc` 40 | WHITE=`echo $WHITE + 0.5 | bc` 41 | fi 42 | done 43 | AVG_Player_step=`echo $PLAYER_CNT/$PLAYER_TIME | bc -l` 44 | AVG_Env_step=`echo $ENV_CNT/$ENV_TIME | bc -l ` 45 | 46 | echo "In ${GAMES} Games" 47 | # echo `cat result.txt | sed -n '/simulationCount:/,/+/p' | head -n -1` 48 | echo `cat result.txt | sed '1,1!d'` 49 | echo `cat result.txt | sed '2,2!d'` 50 | echo `cat result.txt | sed '3,3!d'` 51 | echo `cat result.txt | sed '4,4!d'` 52 | echo `cat result.txt | sed '5,5!d'` 53 | echo 54 | echo 55 | echo "The average win rate of black is ${BLACK}/${GAMES}" 56 | 57 | echo "The total simulation count of black is ${PLAYER_CNT}" 58 | echo "The total simulation count of white is ${ENV_CNT}" 59 | 60 | echo "The total time of black is ${PLAYER_TIME}" 61 | echo "The total time of white is ${ENV_TIME}" 62 | 63 | echo "The average steps of black is $AVG_Player_step steps/(ms)" 64 | echo "The average steps of white is $AVG_Env_step steps/(ms)" 65 | 66 | 67 | -------------------------------------------------------------------------------- /.github/workflows/cmake.yml: -------------------------------------------------------------------------------- 1 | name: ParallelMCTS 2 | 3 | on: [push] 4 | 5 | env: 6 | # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) 7 | BUILD_TYPE: Release 8 | 9 | jobs: 10 | build: 11 | # The CMake configure and build commands are platform agnostic and should work equally 12 | # well on Windows or Mac. You can convert this to a matrix build if you need 13 | # cross-platform coverage. 14 | # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | 20 | - name: Create Build Environment 21 | # Some projects don't allow in-source building, so create a separate build directory 22 | # We'll use this as our working directory for all subsequent commands 23 | run: cmake -E make_directory ${{runner.workspace}}/build 24 | 25 | - name: Configure CMake 26 | # Use a bash shell so we can use the same syntax for environment variable 27 | # access regardless of the host operating system 28 | shell: bash 29 | working-directory: ${{runner.workspace}}/build 30 | # Note the current convention is to use the -S and -B options here to specify source 31 | # and build directories, but this is only available with CMake 3.13 and higher. 32 | # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 33 | run: cmake -DCOVERAGE=ON $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE 34 | 35 | - name: Build 36 | working-directory: ${{runner.workspace}}/build 37 | shell: bash 38 | # Execute the build. You can specify a specific target with "--target " 39 | run: cmake --build . --config $BUILD_TYPE 40 | 41 | - name: Test 42 | working-directory: ${{runner.workspace}}/build 43 | shell: bash 44 | # Execute tests defined by the CMake configuration. 45 | # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail 46 | run: ctest 47 | 48 | - name: buildCov 49 | working-directory: ${{runner.workspace}}/build/CMakeFiles/MCTS_UnitTest.dir/unittest 50 | shell: bash 51 | 52 | run: | 53 | sudo apt-get install lcov 54 | gcov MCTS_UnitTest.cpp.gcno 55 | lcov --capture --directory . --output-file gtest_coverage.info 56 | genhtml gtest_coverage.info --output-directory CODE_COVERAGE 57 | - name: codecov 58 | working-directory: ${{runner.workspace}} 59 | shell: bash 60 | run: bash <(curl -s https://codecov.io/bash) 61 | -------------------------------------------------------------------------------- /src/ParallelTree.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | class ParallelTree{ 5 | public: 6 | static void treeParallization(MonteCarloTree *tree, EnvParameter env) { 7 | 8 | if (env.time > 0) { 9 | auto start = chrono::steady_clock::now(); 10 | auto end = chrono::steady_clock::now(); 11 | auto diff_time = chrono::duration(end - start).count(); 12 | 13 | while (diff_time < env.time) 14 | { 15 | tree->parallelTree_tree_policy(); 16 | end = chrono::steady_clock::now(); 17 | diff_time = chrono::duration(end - start).count(); 18 | } 19 | } else if (env.simulation_counts > 0) { 20 | int count_sim = 0; 21 | 22 | while(count_sim < env.simulation_counts) { 23 | tree->parallelTree_tree_policy(); 24 | count_sim++; 25 | } 26 | } 27 | } 28 | static void Pthread(MonteCarloTree *tree, EnvParameter env) { 29 | 30 | std::thread workers[ env.thread_num ]; 31 | for(int i=1; i < env.thread_num; i++) { 32 | workers[i] = std::thread(ParallelTree::treeParallization, tree, env); 33 | } 34 | 35 | ParallelTree::treeParallization(tree, env); 36 | for(int i=1; i < env.thread_num; i++) { 37 | workers[i].join(); 38 | } 39 | } 40 | /***** Pthread *****/ 41 | 42 | /***** OpenMP *****/ 43 | static void OMP(MonteCarloTree *tree, const EnvParameter &env) { 44 | omp_set_num_threads(env.thread_num); 45 | 46 | if (env.simulation_counts > 0) { 47 | #pragma omp parallel for 48 | for (int i = 0; i < env.thread_num; ++i ){ 49 | int count_sim = 0; 50 | while (count_sim < env.simulation_counts) 51 | { 52 | tree->parallelTree_tree_policy(); 53 | count_sim++; 54 | } 55 | } 56 | } 57 | else if (env.time > 0) { 58 | #pragma omp parallel for 59 | for (int i = 0; i < env.thread_num; ++i ){ 60 | auto start = chrono::steady_clock::now(); 61 | auto end = chrono::steady_clock::now(); 62 | auto diff_time = chrono::duration(end - start).count(); 63 | 64 | while (diff_time < env.time) 65 | { 66 | tree->parallelTree_tree_policy(); 67 | end = chrono::steady_clock::now(); 68 | diff_time = chrono::duration(end - start).count(); 69 | } 70 | } 71 | } 72 | } 73 | /***** OpenMP *****/ 74 | }; -------------------------------------------------------------------------------- /src/ParallelRoot.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class ParallelRoot{ 4 | public: 5 | /*** pthread ***/ 6 | static void rootParallization(MonteCarloTree *tree, EnvParameter env) { 7 | 8 | if (env.time > 0) { 9 | auto start = chrono::steady_clock::now(); 10 | auto end = chrono::steady_clock::now(); 11 | auto diff_time = chrono::duration(end - start).count(); 12 | while (diff_time < env.time) 13 | { 14 | tree->tree_policy(); 15 | end = chrono::steady_clock::now(); 16 | diff_time = chrono::duration(end - start).count(); 17 | } 18 | } else if (env.simulation_counts > 0) { 19 | int count_sim = 0; 20 | while(count_sim < env.simulation_counts) { 21 | tree->tree_policy(); 22 | count_sim++; 23 | } 24 | } 25 | // cout << "Count sim: " << count_sim << '\n'; 26 | } 27 | 28 | static void Pthread(MonteCarloTree *trees, EnvParameter env) { 29 | 30 | std::thread workers[ env.thread_num ]; 31 | for(int i=1; i < env.thread_num; i++) { 32 | workers[i] = std::thread(ParallelRoot::rootParallization, &trees[i], env); 33 | } 34 | 35 | ParallelRoot::rootParallization(&trees[0], env); 36 | for(int i=1; i < env.thread_num; i++) { 37 | workers[i].join(); 38 | } 39 | 40 | } 41 | /*** pthread ***/ 42 | 43 | /*** OpenMP ***/ 44 | static void OMP(MonteCarloTree *trees, const EnvParameter &env) { 45 | 46 | omp_set_num_threads(env.thread_num); 47 | 48 | if (env.time > 0) { 49 | #pragma omp parallel for 50 | for(int i = 0; i < env.thread_num; i++){ 51 | auto start = chrono::steady_clock::now(); 52 | auto end = chrono::steady_clock::now(); 53 | auto diff_time = chrono::duration(end - start).count(); 54 | 55 | while (diff_time < env.time) 56 | { 57 | trees[i].tree_policy(); 58 | end = chrono::steady_clock::now(); 59 | diff_time = chrono::duration(end - start).count(); 60 | } 61 | // cout << "Count: " << count_sim << '\n'; 62 | } 63 | } 64 | else if (env.simulation_counts > 0) { 65 | #pragma omp parallel for 66 | for(int i = 0; i < env.thread_num; i++){ 67 | int count_sim = 0; 68 | 69 | while (count_sim < env.simulation_counts) { 70 | trees[i].tree_policy(); 71 | count_sim++; 72 | } 73 | // cout << "Count: " << count_sim << '\n'; 74 | } 75 | } 76 | 77 | } 78 | }; -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "Agent.h" 7 | #include "MCTS.h" 8 | #include "Policy.h" 9 | #include "Log.h" 10 | 11 | using namespace std; 12 | 13 | auto setPolicy(string policy) { 14 | // cout << policy << '\n'; 15 | switch (policy[0]) { 16 | case 'S': 17 | case 's': 18 | return Policy::MCTS_Serial; 19 | case 'L': 20 | case 'l': 21 | return Policy::MCTS_Parallel_Leaf; 22 | break; 23 | case 'R': 24 | case 'r': 25 | return Policy::MCTS_Parallel_Root; 26 | break; 27 | case 'T': 28 | case 't': 29 | return Policy::MCTS_Parallel_Tree; 30 | break; 31 | default: 32 | std::cerr << "Black Policy not defined!\n"; 33 | exit(0); 34 | } 35 | } 36 | 37 | int main(int argc, char *argv[]) { 38 | 39 | /***** Parameter Setting *****/ 40 | 41 | // default parameters 42 | envParam = {1000, -1, 4, "Tree", "Serial", "openmp", "openmp"}; 43 | setEnvParameter(argc, argv); 44 | cout << "simulationCount: " << envParam.simulation_counts << '\n'; 45 | cout << "time: " << envParam.time << " ms" << '\n'; 46 | cout << "thread num: " << envParam.thread_num << '\n'; 47 | cout << "Black policy: " << envParam.black_policy << '\n'; 48 | cout << "White policy: " << envParam.white_policy << '\n'; 49 | cout << "Black method: " << envParam.black_method << '\n'; 50 | cout << "White method: " << envParam.white_method << '\n'; 51 | cout << "\n\n"; 52 | const int limitStep = LIMIT_STEP; 53 | auto black_policy = setPolicy( envParam.black_policy ); 54 | auto white_policy = setPolicy( envParam.white_policy ); 55 | 56 | /***** Parameter Setting *****/ 57 | 58 | Log playerLog, envirLog; 59 | 60 | // Game Parameter 61 | board cur_board; 62 | Agent player{BLACK}, envir{WHITE}; 63 | WIN_STATE outcome; 64 | 65 | while (true) { 66 | 67 | Pair mv; 68 | PIECE p = cur_board.take_turn(); 69 | 70 | if ( p == BLACK ) { 71 | mv = player.take_action(cur_board, black_policy, playerLog, envParam); 72 | } 73 | else { 74 | mv = envir.take_action(cur_board, white_policy, envirLog, envParam); 75 | } 76 | 77 | // Game over 78 | if (mv == Pair{}) { 79 | outcome = cur_board.compare_piece(); 80 | break; 81 | } else if (player.getStep() > limitStep ) { 82 | outcome = cur_board.compare_piece(); 83 | break; 84 | } 85 | cout << cur_board << '\n'; 86 | } 87 | cout << "winner: "; 88 | if (outcome == BLACK_WIN) { 89 | cout << "Black\n"; 90 | } else if (outcome == WHITE_WIN) { 91 | cout << "White\n"; 92 | } else { 93 | cout << "Draw\n"; 94 | } 95 | 96 | cout << "Player\n\n"; 97 | playerLog.printLog(); 98 | 99 | cout << "Envir\n\n"; 100 | envirLog.printLog(); 101 | 102 | } -------------------------------------------------------------------------------- /src/config.h: -------------------------------------------------------------------------------- 1 | #define LIMIT_STEP 500 // step limits in a game 2 | #define PRINT_TREE 0 // Print tree for debugging 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct EnvParameter{ 8 | int simulation_counts; 9 | double time; 10 | int thread_num; 11 | std::string black_policy; 12 | std::string white_policy; 13 | std::string black_method; 14 | std::string white_method; 15 | 16 | 17 | } envParam; 18 | 19 | void usage() { 20 | cout << "Usage: mcts [options]\n"; 21 | cout << "\nNote: -c (count) & -t (time) options can't be specified simultaneously.\n\n"; 22 | cout << " -c --simCount Set simulation count per step. default: 1000\n"; 23 | cout << " -t --simTime Set simulation time per step. default: -1\n"; 24 | cout << " -T --threadNum Set number of thread num. default: 4\n"; 25 | cout << " -p --policy Set Player's policy \n"\ 26 | " (options: Serial, Leaf, Root, Tree) default: Tree Serial)\n\n"; 27 | 28 | cout << " -m --method Set Player's arallel methods \n"\ 29 | " (options: openmp, pthread) default: openmp)\n\n"; 30 | cout << " -? --help Help message.\n"; 31 | } 32 | void setEnvParameter(int argc, char** argv) { 33 | int opt; 34 | static struct option long_options[] = { 35 | {"simCount", 0, 0, 'c'}, {"simTime", 0, 0, 't'}, {"threadNum", 0, 0, 'T'}, {"policy", 0, 0, 'p'}, {"method", 0, 0, 'm'}, {"help", 0, 0, '?'}, {0, 0, 0, 0}}; 36 | 37 | while ((opt = getopt_long(argc, argv, "c:t:T:p:m:?", long_options, NULL)) != EOF) 38 | { 39 | int index; 40 | switch (opt) 41 | { 42 | case 'c': 43 | envParam.simulation_counts = atoi(optarg); 44 | envParam.time = -1; 45 | break; 46 | case 't': 47 | envParam.time = atoi(optarg) * 1000; // convert s to ms 48 | envParam.simulation_counts = -1; 49 | break; 50 | case 'T': 51 | envParam.thread_num = atoi(optarg); 52 | break; 53 | case 'p': 54 | index = optind - 1; 55 | if (index + 1 < argc) { 56 | if ( argv[index+1][0] == '-') { 57 | envParam.black_policy = envParam.white_policy = argv[index]; 58 | } else { 59 | envParam.black_policy = argv[index]; 60 | envParam.white_policy = argv[index+1]; 61 | } 62 | } else { 63 | envParam.black_policy = envParam.white_policy = argv[index]; 64 | } 65 | break; 66 | case 'm': 67 | index = optind - 1; 68 | if (index + 1 < argc) { 69 | if ( argv[index+1][0] == '-') { 70 | envParam.black_method = envParam.white_method = argv[index]; 71 | } else { 72 | envParam.black_method = argv[index]; 73 | envParam.white_method = argv[index+1]; 74 | } 75 | } else { 76 | envParam.black_method = envParam.white_method = argv[index]; 77 | } 78 | break; 79 | case '?': 80 | usage(); exit(0); 81 | break; 82 | default: 83 | usage(); exit(0); 84 | } 85 | } 86 | } 87 | 88 | -------------------------------------------------------------------------------- /src/TreeNode.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "board.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | class MonteCarloTree; 13 | 14 | class TreeNode { 15 | private: 16 | // TreeNode* child; 17 | std::unique_ptr child; 18 | PIECE color; 19 | Pair move; 20 | std::size_t child_size; 21 | 22 | mutable std::shared_mutex m_mutex; 23 | 24 | public: 25 | std::atomic total_count; 26 | std::atomic win_count; 27 | std::atomic virtual_loss; 28 | // double means; 29 | 30 | public: 31 | friend class MonteCarloTree; 32 | 33 | TreeNode() : child(nullptr), child_size(0), total_count(0), win_count(0), virtual_loss(0) {} 34 | 35 | ~TreeNode() {} 36 | 37 | TreeNode& operator = (const TreeNode &node) { 38 | this->child_size = node.child_size; 39 | this->child = std::make_unique(this->child_size); 40 | 41 | std::copy(node.child.get(), node.child.get() + node.child_size, child.get() ); 42 | 43 | this->color = node.color; 44 | this->move = node.move; 45 | 46 | 47 | this->total_count.store(node.total_count.load()); 48 | this->win_count.store(node.win_count.load()); 49 | this->virtual_loss.store(node.virtual_loss.load()); 50 | return *this; 51 | } 52 | 53 | /*** For root parallelization ***/ 54 | 55 | Pair get_move(){ 56 | return move; 57 | } 58 | std::unique_ptr get_child(){ 59 | return std::move(child); 60 | } 61 | std::size_t get_child_size(){ 62 | return child_size; 63 | } 64 | /*** For root parallelization ***/ 65 | 66 | void init_TreeNode (const Pair &m, const PIECE &piece) { 67 | this->color = piece; 68 | this->move = m; 69 | } 70 | 71 | void addresult (const WIN_STATE &result) { 72 | if (result == DRAW) 73 | total_count++; 74 | else if ( (result == BLACK_WIN && color==BLACK) || (result == WHITE_WIN && color==WHITE) ) { 75 | total_count++; 76 | win_count++; 77 | } 78 | else 79 | total_count++; 80 | virtual_loss--; 81 | } 82 | 83 | void expand(board &b) { 84 | 85 | const PIECE& c = b.take_turn(); 86 | std::vector mv = b.get_available_move(c); 87 | 88 | child_size = mv.size(); 89 | 90 | if (child_size == 0) 91 | return; 92 | 93 | child = std::make_unique(child_size); 94 | int idx = 0; 95 | 96 | for (auto &m : mv) { 97 | child[idx].init_TreeNode(m, c); 98 | idx++; 99 | } 100 | } 101 | 102 | void expandLock(board &b) { 103 | 104 | 105 | 106 | /*----- Critical Section -----*/ 107 | std::unique_lock wLock(m_mutex); 108 | 109 | // check if other thread has expanded the leaf or not 110 | if (child != nullptr) { 111 | return; 112 | } 113 | 114 | const PIECE& c = b.take_turn(); 115 | std::vector mv = b.get_available_move(c); 116 | 117 | child_size = mv.size(); 118 | if (child_size == 0) 119 | return; 120 | 121 | child = std::make_unique(child_size); 122 | int idx = 0; 123 | 124 | for (auto &m : mv) { 125 | child[idx].init_TreeNode(m, c); 126 | idx++; 127 | } 128 | /*----- Critical Section -----*/ 129 | } 130 | 131 | Pair best_child() { 132 | std::size_t best_child_idx = -1; 133 | double most_visit = INT_MIN; 134 | double tmp; 135 | 136 | for (std::size_t i = 0 ; i < child_size; ++i) { 137 | tmp = child[i].total_count; 138 | if (tmp > most_visit) { 139 | most_visit = tmp; 140 | best_child_idx = i; 141 | } 142 | } 143 | //printf("max: %lf\n", most_visit); 144 | // no more step can go 145 | if (child_size == 0) { 146 | return {}; 147 | } 148 | 149 | auto best_child { this->child.get() + best_child_idx }; 150 | 151 | return std::move( best_child->move ); 152 | } 153 | void showchild() { 154 | std::cerr << std::setw(6) <<"from:\t "; 155 | std::cerr << std::setw(10) << "to:\t\t"; 156 | std::cerr << "win rate "; 157 | std::cerr << "total_count\n"; 158 | for(std::size_t i=0; i < child_size; ++i) { 159 | if (child[i].total_count > 0) { 160 | std::cerr << "(" << child[i].move.prev/6 << ", "<< child[i].move.prev%6 << ")\t\t(" << child[i].move.next/6 << ", " < 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | class Policy { 15 | public: 16 | static Pair MCTS_Serial(board &before, const PIECE &piece, const EnvParameter &env, Log &log) { 17 | 18 | MonteCarloTree tree; 19 | tree.reset(before); 20 | auto start = chrono::steady_clock::now(); 21 | auto end = chrono::steady_clock::now(); 22 | auto diff_time = chrono::duration(end - start).count(); 23 | 24 | if ( env.simulation_counts > 0 ) { 25 | int count_sim = 0; 26 | start = chrono::steady_clock::now(); 27 | while (count_sim < env.simulation_counts) { 28 | tree.tree_policy(); 29 | count_sim++; 30 | } 31 | end = chrono::steady_clock::now(); 32 | diff_time = chrono::duration(end - start).count(); 33 | log.cost_time += diff_time; 34 | } 35 | else if ( env.time > 0 ){ 36 | 37 | while(diff_time < env.time ){ 38 | tree.tree_policy(); 39 | end = chrono::steady_clock::now(); 40 | diff_time = chrono::duration(end - start).count(); 41 | } 42 | log.cost_time += diff_time; 43 | } 44 | log.search_count += tree.root->total_count; 45 | 46 | if (PRINT_TREE) 47 | tree.root->showchild(); 48 | 49 | Pair best_move = tree.root->best_child(); 50 | 51 | //std::cerr << "choose: (" << best_move.prev/6 << ", "<< best_move.prev%6 << ") to (" << best_move.next/6 << ", " <(end - start).count(); 65 | 66 | if (env.time > 0) { 67 | 68 | while (diff_time < env.time) 69 | { 70 | tree.parallelLeaf_tree_policy(env); 71 | end = chrono::steady_clock::now(); 72 | diff_time = chrono::duration(end - start).count(); 73 | } 74 | log.cost_time += diff_time; 75 | } 76 | else if(env.simulation_counts > 0) { 77 | start = chrono::steady_clock::now(); 78 | while (count_sim < env.simulation_counts) { 79 | tree.parallelLeaf_tree_policy(env); 80 | count_sim++; 81 | } 82 | end = chrono::steady_clock::now(); 83 | diff_time = chrono::duration(end - start).count(); 84 | log.cost_time += diff_time; 85 | } 86 | // cout << "Parallel count:" << tree.root->total_count << "\n"; 87 | log.search_count += tree.root->total_count; 88 | 89 | if (PRINT_TREE) 90 | tree.root->showchild(); 91 | 92 | Pair best_move = tree.root->best_child(); 93 | return best_move; 94 | } 95 | /***** leaf parallelization *****/ 96 | 97 | /***** Root parallelization *****/ 98 | static Pair MCTS_Parallel_Root(board &before, const PIECE &piece, const EnvParameter &env, Log &log) { 99 | 100 | MonteCarloTree trees[env.thread_num]; 101 | for( int i = 0; i < env.thread_num; i++ ) { 102 | trees[i].reset(before); 103 | } 104 | 105 | Pair best_move; 106 | auto start = chrono::steady_clock::now(); 107 | if (piece == BLACK) { 108 | if(env.black_method[0] == 'p') 109 | ParallelRoot::Pthread(trees, env); 110 | else if (env.black_method[0] == 'o') 111 | ParallelRoot::OMP(trees, env); 112 | } else { 113 | if(env.white_method[0] == 'p') 114 | ParallelRoot::Pthread(trees, env); 115 | else if (env.white_method[0] == 'o') 116 | ParallelRoot::OMP(trees, env); 117 | } 118 | auto end = chrono::steady_clock::now(); 119 | auto diff_time = chrono::duration(end-start).count(); 120 | 121 | map bag; 122 | bag.clear(); 123 | int Vcount = 0; 124 | 125 | // aggregate count result 126 | for(int thread_idx = 0; thread_idx < env.thread_num; thread_idx++){ 127 | // get child & count 128 | auto child = trees[ thread_idx ].root->get_child(); 129 | Vcount += trees[ thread_idx ].root->total_count; 130 | size_t child_size = trees[ thread_idx ].root->get_child_size(); 131 | 132 | if (child_size == 0) { 133 | return {}; 134 | } 135 | 136 | for(size_t child_idx = 0; child_idx < child_size; child_idx++){ 137 | Pair move = child[ child_idx ].get_move(); 138 | bag[move] += child[ child_idx ].total_count; 139 | } 140 | } 141 | log.search_count += Vcount; 142 | log.cost_time += diff_time; 143 | 144 | int maxCount = 0; 145 | for(auto mp : bag) { 146 | if (PRINT_TREE) 147 | std::cout << (int)mp.first.prev << ", " << (int)mp.first.next << ": " << mp.second << '\n'; 148 | if (mp.second > maxCount) { 149 | maxCount = mp.second; 150 | best_move = mp.first; 151 | } 152 | } 153 | 154 | return best_move; 155 | } 156 | /***** Root parallelization *****/ 157 | 158 | 159 | /***** Tree parallelization *****/ 160 | static Pair MCTS_Parallel_Tree(board &before, const PIECE &piece, const EnvParameter &env, Log &log) { 161 | MonteCarloTree tree; 162 | tree.reset(before); 163 | 164 | auto start = chrono::steady_clock::now(); 165 | if (piece == BLACK) { 166 | if (env.black_method[0] == 'p') { 167 | ParallelTree::Pthread(&tree, env); 168 | } else if (env.black_method[0] == 'o') { 169 | ParallelTree::OMP(&tree, env); 170 | } 171 | } else { 172 | if (env.white_method[0] == 'p') { 173 | ParallelTree::Pthread(&tree, env); 174 | } else if (env.white_method[0] == 'o') { 175 | ParallelTree::OMP(&tree, env); 176 | } 177 | } 178 | auto end = chrono::steady_clock::now(); 179 | auto diff_time = chrono::duration(end-start).count(); 180 | // cout << "Parallel count:" << tree.root->total_count << "\n"; 181 | log.search_count += tree.root->total_count; 182 | log.cost_time += diff_time; 183 | 184 | if (PRINT_TREE) 185 | tree.root->showchild(); 186 | 187 | Pair best_move = tree.root->best_child(); 188 | return best_move; 189 | } 190 | /***** Tree parallelization *****/ 191 | 192 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://github.com/CyCTW/PP-Final_Project/workflows/ParallelMCTS/badge.svg) 2 | [![codecov](https://codecov.io/gh/CyCTW/Parallel-MCTS/branch/master/graph/badge.svg?token=LPORRSHSZT)](https://codecov.io/gh/CyCTW/Parallel-MCTS) 3 | 4 | # Parallel Monte Carlo Tree Search 5 | This repo implements three methods of Parallel Monte Carlo Tree Search with C++. 6 | 7 | **The Three parallel methods:** 8 | - Leaf parallelization 9 | - Root parallelization 10 | - Tree parallelization 11 | 12 | Also, we analyze different methods' performance against each other in [Surakarta](https://en.wikipedia.org/wiki/Surakarta_(game)) Game. 13 | If you're interested in details, here are the links to our search: 14 | - [Experiment report](https://hackmd.io/2I8fuKY0TY-sRNQU4Dcuog). 15 | - [Slides](https://docs.google.com/presentation/d/11DV9MijsDh2fgR8W5I8kdCT4u630Bibq6ZfMPpffD6U/edit#slide=id.p) 16 | ## Features 17 | - CI pipeline (Autobuild, Autotest) 18 | - Code coverage check 19 | ## Structure 20 | ``` 21 | Parallel-MCTS/ 22 | ├── build 23 | ├── CMakeLists.txt 24 | ├── CMakeLists.txt.in 25 | ├── LICENSE 26 | ├── README.md 27 | ├── script 28 | │ ├── cov.sh 29 | │ └── record.sh 30 | ├── src 31 | │ ├── Agent.h 32 | │ ├── board.h 33 | │ ├── config.h 34 | │ ├── Log.h 35 | │ ├── main.cpp 36 | │ ├── MCTS.h 37 | │ ├── ParallelRoot.h 38 | │ ├── ParallelTree.h 39 | │ ├── Policy.h 40 | │ └── TreeNode.h 41 | └── unittest 42 | └── MCTS_UnitTest.cpp 43 | 44 | 4 directories, 17 files 45 | ``` 46 | ## Development Environment 47 | - Ubuntu 18.04 48 | - GCC 7.5.0 49 | - CMAKE 3.10 50 | - GoogleTest 1.10.0 (latest) 51 | 52 | ## Requirement 53 | ### Must 54 | - C++ 17 55 | - OpenMP 56 | - CMAKE (3.10) 57 | ### optional 58 | - lcov 59 | - gcov 60 | 61 | ## Build 62 | ### Construct build directory (optional, but recommened) 63 | ``` 64 | mkdir build 65 | ``` 66 | This step creates a `build` directory, which builds a clean environment for CMake compiling and building. 67 | ### Compile file with CMake 68 | ``` 69 | cd build 70 | cmake .. 71 | ``` 72 | or 73 | ``` 74 | cmake . 75 | ``` 76 | if you don't create the `build` directory. 77 | ### Build file 78 | ``` 79 | cmake --build . 80 | ``` 81 | or 82 | ``` 83 | make 84 | ``` 85 | This command will generate two execution file 86 | - `mcts` for executing main function. 87 | - `MCTS_UnitTest` for unit testing. 88 | 89 | ## Usage 90 | For the specific usage, you can type: 91 | ``` 92 | ./mcts -? 93 | ``` 94 | for detailed usage. 95 | 96 | For example, the command will print 97 | ``` 98 | Usage: mcts [options] 99 | 100 | Note: -c (count) & -t (time) options can't be specified simultaneously. 101 | 102 | -c --simCount Set simulation count per step. default: 1000 103 | -t --simTime Set simulation time per step. default: -1 104 | -T --threadNum Set number of thread num. default: 4 105 | -p --policy Set Player's policy 106 | (options: Serial, Leaf, Root, Tree) default: Tree Serial) 107 | 108 | -m --method Set Player's arallel methods 109 | (options: openmp, pthread) default: openmp) 110 | 111 | -? --help Help message. 112 | ``` 113 | 114 | ### Some quick examples: 115 | - Serial version (Black) v.s. Serial version (White) with simulation count 1000 116 | ``` 117 | ./mcts -c 1000 -p Serial 118 | ``` 119 | - ParallelTree version (Black) v.s. Serial version (White) with simulation time 2s 120 | ``` 121 | ./mcts -t 2 -p Tree Serial 122 | ``` 123 | *Note: we can't set Count and Time simultaneously, choose only one option.* 124 | 125 | - ParallelRoot version (Black) v.s. ParallelLeaf (White) with 4 thread, both use `openmp` implementation. 126 | ``` 127 | ./mcts -T 4 -p Root Tree -m openmp 128 | ``` 129 | 130 | - ParallelRoot version (Black) v.s. ParallelLeaf (White) with 4 thread. Use `openmp` (Black) and `pthread` (White) implementation. 131 | ``` 132 | ./mcts -T 4 -p Root Leaf -m openmp pthread 133 | ``` 134 | ## UnitTest (GoogleTest) 135 | ### command: 136 | ``` 137 | ./MCTS_UnitTest 138 | ``` 139 | or 140 | ``` 141 | ctest 142 | ``` 143 | Both command will run the same unittest script stored in `/unittest/MCTS_UnitTest.cpp` 144 | 145 | *Note:* 146 | *The first command runs the test script using Googletest's origin format.* 147 | 148 | *The second command runs the same test script, but from CMake.* 149 | ### Explanation 150 | Mainly, the test script checks the Monte Carlo Tree Search's simulation count and simulation time works correctly. 151 | 152 | ## CodeCoverage Report 153 | If you want to see the code coverage report, you should recompile the project with `-DCOVERAGE=ON` 154 | *Note: default option is `-DCOVERAGE=OFF` because this option will slow down the whole program.* 155 | 156 | First return to build directory. 157 | ``` 158 | cd /build 159 | ``` 160 | Then, recompile project with flag `-DCOVERAGE=OFF` 161 | ``` 162 | cmake -DCOVERAGE=ON .. 163 | ``` 164 | Rebuild the project 165 | ``` 166 | cmake --build . 167 | ``` 168 | Last, move to `script` folder and execute the `cov.sh` script. 169 | ``` 170 | cd /script 171 | ./cov.sh 172 | ``` 173 | *Note: you should install `lcov` and `gcov` before execute the script.* 174 | 175 | If everything works fine, the script will open a html file, which contains the code coverage report. 176 | ## Experiment record 177 | If you want to run multiple games on different policys, and record the result for further analysis, you can check our `record.sh` 178 | 179 | You can move to `script` directory to check the `record.sh` file. 180 | ``` 181 | cd /script 182 | ``` 183 | ### usage 184 | ``` 185 | Usage: ./record.sh [options] 186 | 187 | Note: -c (count) & -t (time) options can't be specified simultaneously. 188 | 189 | -c --simCount Set simulation count per step. default: 1000 190 | -t --simTime Set simulation time per step. default: -1 191 | -T --threadNum Set number of thread num. default: 4 192 | -p --policy Set Player's policy 193 | (options: Serial, Leaf, Root, Tree) default: Tree Serial) 194 | 195 | -m --method Set Player's arallel methods 196 | (options: openmp, pthread) default: openmp) 197 | 198 | ``` 199 | 200 | ### Some quick examples: 201 | - Serial version (Black) v.s. Serial version (White) with simulation count 1000 202 | ``` 203 | ./record.sh -c 1000 -p Serial 204 | ``` 205 | - ParallelTree version (Black) v.s. Serial version (White) with simulation time 2s 206 | ``` 207 | ./record.sh -t 2 -p Tree Serial 208 | ``` 209 | *Note: we can't set Count and Time simultaneously, choose only one option.* 210 | 211 | - ParallelRoot version (Black) v.s. ParallelLeaf (White) with 4 thread, both use `openmp` implementation. 212 | ``` 213 | ./record.sh -T 4 -p Root Tree -m openmp 214 | ``` 215 | 216 | - ParallelRoot version (Black) v.s. ParallelLeaf (White) with 4 thread. Use `openmp` (Black) and `pthread` (White) implementation. 217 | ``` 218 | ./record.sh -T 4 -p Root Leaf -m openmp pthread 219 | ``` 220 | -------------------------------------------------------------------------------- /src/MCTS.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "TreeNode.h" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | class MonteCarloTree { 13 | public: 14 | // std::vector path; 15 | std::unique_ptr root; 16 | 17 | board root_board; 18 | 19 | std::random_device rd; 20 | std::default_random_engine eng; 21 | double c_virtual_loss = 5; 22 | static constexpr double explore_parameter = sqrt(2.0); 23 | 24 | MonteCarloTree() : root(), root_board(), eng(rd()) {} 25 | 26 | private: 27 | TreeNode* UCB (TreeNode* n) { 28 | 29 | if(n->child_size == 0) return nullptr; 30 | 31 | constexpr double eps = 1e-3; 32 | double max_score = INT_MIN; 33 | std::size_t same_score[100]{}; 34 | std::size_t idx = 0; 35 | 36 | for (std::size_t i = 0; i < n->child_size; ++i) { 37 | TreeNode* childNode = n->child.get() + i; 38 | 39 | int child_win_count = childNode->win_count.load(); 40 | int child_total_count = childNode->total_count.load(); 41 | 42 | double virtual_loss = c_virtual_loss * n->virtual_loss.load(); 43 | 44 | const double exploit { child_win_count / (double)( child_total_count + 1.0) }; 45 | const double explore { sqrt( log( n->total_count.load() ) / (double)( child_total_count + 1.0) ) }; 46 | 47 | // without virtual loss 48 | // const double score { exploit + explore_parameter * explore }; 49 | 50 | // with virtual loss 51 | const double score { exploit + explore_parameter * explore - virtual_loss }; 52 | 53 | if ( (score <= (max_score + eps) ) && (score >= (max_score - eps) ) ) { 54 | same_score[idx] = i; 55 | idx++; 56 | } 57 | else if (score > max_score) { 58 | max_score = score; 59 | same_score[0] = i; 60 | idx = 1; 61 | } 62 | } 63 | 64 | std::shuffle(std::begin(same_score), std::begin(same_score) + idx, eng); 65 | std::size_t best_idx = same_score[0]; 66 | 67 | // add virtual loss 68 | n->virtual_loss++; 69 | 70 | return (n->child.get() + best_idx); 71 | } 72 | 73 | void select(board &b, std::vector &path) { 74 | 75 | TreeNode* current { root.get() }; 76 | 77 | // path.clear(); 78 | path.push_back(current); 79 | 80 | while (current->child != nullptr && current->child_size != 0) { 81 | // move to next children node 82 | current = UCB(current); 83 | path.push_back(current); 84 | 85 | b.move(current->move.prev, current->move.next, current->color); 86 | } 87 | } 88 | 89 | 90 | // bool GreedyRollout(board &b, PIECE color) { 91 | // std::vector ea { b.find_piece(color, EAT) }; 92 | // std::vector mv { b.find_piece(color, MOVE) }; 93 | 94 | // if (!ea.empty()) { 95 | // std::shuffle(ea.begin(), ea.end(), eng); 96 | // b.move(ea[0].prev, ea[0].next, color); 97 | // } 98 | // else if (!mv.empty()) { 99 | // std::shuffle(mv.begin(), mv.end(), eng); 100 | // b.move(mv[0].prev, mv[0].next, color); 101 | // } else { 102 | // return false; 103 | // } 104 | // return true; 105 | // } 106 | bool RandomRollout(board &b, PIECE color) { 107 | std::vector mvs { b.get_available_move(color) }; 108 | 109 | if (!mvs.empty()) { 110 | std::shuffle(mvs.begin(), mvs.end(), eng); 111 | b.move(mvs[0].prev, mvs[0].next, color); 112 | } else { 113 | return false; 114 | } 115 | return true; 116 | 117 | } 118 | 119 | WIN_STATE simulate(board b) { 120 | 121 | std::size_t count_step = 0; 122 | 123 | constexpr std::size_t limit_step = 500; 124 | while (true) { 125 | count_step++; 126 | // Game draw if exceed the limit step 127 | if (count_step > limit_step) { 128 | return b.compare_piece(); 129 | } 130 | 131 | const PIECE& color { b.take_turn() }; 132 | bool succ; 133 | // succ = GreedyRollout(b, color); 134 | succ = RandomRollout(b, color); 135 | 136 | if ( !succ ) { 137 | if (color == BLACK) 138 | return WHITE_WIN; 139 | else 140 | return BLACK_WIN; 141 | } 142 | 143 | 144 | } 145 | } 146 | 147 | void backpropogate(const WIN_STATE &result, std::vector &path) { 148 | for (auto &node : path) { 149 | node->addresult(result); 150 | } 151 | } 152 | 153 | public: 154 | 155 | void tree_policy() { 156 | // auto start = chrono::steady_clock::now(); 157 | board b {root_board}; 158 | TreeNode *current; 159 | std::vector path; 160 | 161 | select(b, path); 162 | 163 | TreeNode &leaf_node = *(path.back()); 164 | 165 | /** if "the leaf node have no child and have visit before" **/ 166 | if (leaf_node.child_size==0 && leaf_node.total_count.load() > 0){ 167 | 168 | leaf_node.expand(b); 169 | 170 | if (leaf_node.child_size != 0) { 171 | current = UCB(&leaf_node); 172 | path.push_back(current); 173 | b.move(current->move.prev, current->move.next, current->color); 174 | } 175 | // no step can go 176 | else { 177 | const WIN_STATE result = ( (leaf_node.color==WHITE) ? WHITE_WIN : BLACK_WIN); 178 | backpropogate(result, path); 179 | return; 180 | } 181 | } 182 | 183 | const WIN_STATE result { simulate(b) }; 184 | 185 | backpropogate(result, path); 186 | } 187 | 188 | void leafParallel(board b, std::vector &path) { 189 | auto result = simulate(b); 190 | backpropogate(result, path); 191 | } 192 | 193 | 194 | void leafPthread(board b, std::vector &path, EnvParameter env) { 195 | std::thread workers[ env.thread_num ]; 196 | for(int i=1; i < env.thread_num; i++) { 197 | workers[i] = std::thread(&MonteCarloTree::leafParallel, this, b, std::ref(path)); 198 | } 199 | 200 | leafParallel(b, path); 201 | for(int i=1; i < env.thread_num; i++) { 202 | workers[i].join(); 203 | } 204 | } 205 | 206 | void leafOMP(board b, std::vector &path, EnvParameter env) { 207 | omp_set_num_threads(env.thread_num); 208 | 209 | #pragma omp parallel for 210 | for (int i=0; i < env.thread_num; ++i){ 211 | 212 | auto result = simulate(b); 213 | 214 | #pragma omp critical 215 | backpropogate(result, path); 216 | } 217 | } 218 | void parallelLeaf_tree_policy(const EnvParameter &env) { 219 | // may have problem 220 | // int CountInSimulation = 0; 221 | board b {root_board}; 222 | TreeNode *current; 223 | std::vector path; 224 | 225 | select(b, path); 226 | 227 | TreeNode &leaf_node = *(path.back()); 228 | 229 | /** if "the leaf node have no child and have visit before" **/ 230 | if (leaf_node.child_size==0 && leaf_node.total_count.load() > 0){ 231 | 232 | leaf_node.expand(b); 233 | 234 | if (leaf_node.child_size != 0) { 235 | current = UCB(&leaf_node); 236 | path.push_back(current); 237 | b.move(current->move.prev, current->move.next, current->color); 238 | } 239 | // no step can go 240 | else { 241 | const WIN_STATE result = ( (leaf_node.color==WHITE) ? WHITE_WIN : BLACK_WIN); 242 | backpropogate(result, path); 243 | return; 244 | } 245 | } 246 | 247 | if (root->color == BLACK) { 248 | if (env.black_method[0] == 'o') { 249 | leafOMP(b, path, env); 250 | } 251 | else if (env.black_method[0] == 'p') { 252 | leafPthread(b, path, env); 253 | } 254 | } else { 255 | if (env.white_method[0] == 'o') { 256 | leafOMP(b, path, env); 257 | } 258 | else if (env.white_method[0] == 'p') { 259 | leafPthread(b, path, env); 260 | } 261 | } 262 | 263 | } 264 | 265 | 266 | void parallelTree_tree_policy() { 267 | // auto start = chrono::steady_clock::now(); 268 | board b {root_board}; 269 | TreeNode *current; 270 | std::vector path; 271 | 272 | select(b, path); 273 | 274 | TreeNode &leaf_node = *(path.back()); 275 | 276 | /** if "the leaf node have no child and have visit before" **/ 277 | if (leaf_node.child_size==0 && leaf_node.total_count.load() > 0){ 278 | 279 | leaf_node.expandLock(b); 280 | 281 | if (leaf_node.child_size != 0) { 282 | current = UCB(&leaf_node); 283 | path.push_back(current); 284 | b.move(current->move.prev, current->move.next, current->color); 285 | } 286 | // no step can go 287 | else { 288 | const WIN_STATE result = ( (leaf_node.color==WHITE) ? WHITE_WIN : BLACK_WIN); 289 | backpropogate(result, path); 290 | return; 291 | } 292 | } 293 | 294 | const WIN_STATE result { simulate(b) }; 295 | 296 | backpropogate(result, path); 297 | } 298 | 299 | 300 | void reset(board &b) { 301 | root_board = b; 302 | root = { std::make_unique() }; 303 | root->color = root_board.take_turn(); 304 | root->move = {-1, -1}; 305 | root->total_count.store(0); 306 | root->win_count.store(0); 307 | root->child = nullptr; 308 | root->child_size = 0; 309 | root->expand(b); 310 | } 311 | 312 | }; -------------------------------------------------------------------------------- /src/board.h: -------------------------------------------------------------------------------- 1 | /* 2 | Explanation: 3 | Surakarta Game board, implement functions related to board. 4 | */ 5 | 6 | #pragma once 7 | 8 | #include "config.h" 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | // #include 23 | 24 | class Pair { 25 | public: 26 | char prev; 27 | char next; 28 | Pair() : prev(-1), next(-1) {} 29 | Pair (char x, char y) : prev(x), next(y) {} 30 | 31 | bool operator==(const Pair &p) { 32 | return (p.prev == prev && p.next==next); 33 | } 34 | bool operator!=(const Pair &p) { 35 | return ( !((*this)==p) ); 36 | } 37 | bool operator<(const Pair &p) const { 38 | return (prev == p.prev ? next < p.next : prev > p.prev); 39 | } 40 | }; 41 | 42 | 43 | enum PIECE { 44 | BLACK, 45 | WHITE, 46 | SPACE = 9, 47 | UNKNOWN = -1 48 | }; 49 | enum WIN_STATE { 50 | BLACK_WIN, 51 | WHITE_WIN, 52 | DRAW 53 | }; 54 | enum EXEC_STATE { 55 | SUCCESS = 1, 56 | FAIL = -1 57 | }; 58 | enum DIRECTION { 59 | LEFT = -1, 60 | RIGHT = 1, 61 | UP = -2, 62 | DOWN = 2 63 | }; 64 | enum ACTION { 65 | MOVE, 66 | EAT 67 | }; 68 | 69 | const std::array Directions{LEFT, RIGHT, UP, DOWN}; 70 | 71 | 72 | class board { 73 | 74 | public: 75 | typedef std::array row; 76 | typedef std::array grid; 77 | private: 78 | grid tile; 79 | 80 | std::vector step_stack[2]; 81 | static const std::size_t stack_limit = 5; 82 | int step;//if odd, white's turn if even, black's turn 83 | 84 | public: 85 | static constexpr int SIZE { 36 }; 86 | static constexpr int COL { 6 }; 87 | static constexpr int repeat_move_limit {6}; 88 | 89 | static constexpr std::array corner_pos {0, 5, 30, 35}; 90 | // std::map > >mapping_circle; 91 | static std::unordered_map >mapping_circle; // declare below 92 | 93 | 94 | void init_board() { 95 | 96 | for (int i{0}; i < COL; ++i) { 97 | for (int j{0}; j < COL; ++j) { 98 | if (i <= 1) tile[i][j] = BLACK; 99 | if (i <= 3 && i > 1) tile[i][j] = SPACE; 100 | else if (i <= 5 && i > 3) tile[i][j] = WHITE; 101 | } 102 | } 103 | } 104 | 105 | board() : tile(), step(0) { 106 | // init_map(); 107 | init_board(); 108 | } 109 | 110 | PIECE& operator()(char i) { return tile[ i / 6 ][ i % 6 ]; } 111 | PIECE operator()(char i) const {return tile[ i / 6 ][ i % 6 ]; } 112 | 113 | PIECE& operator()(char x, char y) { return tile[x][y]; } 114 | PIECE operator()(char x, char y) const { return tile[x][y]; } 115 | 116 | board& operator =(const board &b) = default; 117 | 118 | // compare pieces on board 119 | inline WIN_STATE compare_piece() const { 120 | unsigned b_num = 0, w_num = 0; 121 | b_num = count_piece(BLACK); 122 | w_num = count_piece(WHITE); 123 | 124 | if (b_num == w_num) 125 | return DRAW; 126 | return (b_num > w_num) ? BLACK_WIN : WHITE_WIN; 127 | } 128 | 129 | inline PIECE take_turn() const { 130 | if (step % 2){ 131 | return WHITE; 132 | } 133 | else { 134 | return BLACK; 135 | } 136 | } 137 | 138 | inline unsigned count_piece(const PIECE &piece) const { 139 | unsigned total = 0; 140 | for (int i=0; i= 0); 161 | case DOWN : return (pos < 36); 162 | default: throw "UNKNOWN DIRECTION"; 163 | } 164 | } 165 | 166 | EXEC_STATE search (char &pos, bool pass, const PIECE &piece, char &count_step, DIRECTION dir) { 167 | 168 | do { 169 | count_step++; 170 | 171 | // search has passed through circle and can't find eatable piece 172 | // or collided with same color piece 173 | if (count_step >= 25 || (*this)(pos) == piece) { 174 | count_step = 0; 175 | return FAIL; 176 | } 177 | 178 | if( (*this)(pos) == (!piece)) { 179 | // pass: search route has passed a ring or not 180 | if(pass) 181 | return SUCCESS; 182 | else { 183 | count_step = 0; 184 | return FAIL; 185 | } 186 | } 187 | move_a_step(pos, dir); 188 | } while( detect_edge(pos, dir) ); 189 | 190 | // return to previous position 191 | move_a_step(pos, static_cast(-static_cast(dir) ) ); 192 | 193 | // pass through circles 194 | if ( mapping_circle.find(pos) != mapping_circle.end() ){ 195 | pass = true; 196 | char old_pos = pos; 197 | pos = mapping_circle[pos].first; 198 | return search( pos, pass, piece, count_step, mapping_circle[old_pos].second); 199 | } 200 | else { 201 | count_step = 0; 202 | return FAIL; 203 | } 204 | } 205 | 206 | std::vector check_eat (const char &origin_pos, const PIECE &piece) { 207 | char pos = origin_pos; 208 | const char prev_pos = pos; 209 | char count_step = 0; 210 | 211 | std::vector eatable; 212 | 213 | // corner pos is not on any track, can't eat any piece 214 | for (auto &p : corner_pos) 215 | if(p == pos) 216 | return {}; 217 | 218 | // search four different ways 219 | (*this)(pos) = SPACE; 220 | 221 | for (auto &dir : Directions) { 222 | if ( search(pos, false, piece, count_step, dir) != FAIL) eatable.push_back(pos); 223 | pos = prev_pos; count_step = 0; 224 | } 225 | 226 | (*this)(prev_pos) = piece; 227 | 228 | if (eatable.empty()) 229 | return {}; 230 | else{ 231 | return eatable; // return ramdomly 232 | } 233 | } 234 | // check whether the moves are available 235 | // NEW!! Add new rules: no return moves after 3 times. 236 | std::vector check_move (const char &pos, const PIECE &piece) { 237 | std::vector movable; 238 | 239 | // 8 directions 240 | char dir[8] {-7, -6, -5, -1, 1, 5, 6, 7}; 241 | const char no_move {0}; 242 | 243 | // pos in top row 244 | if (pos <= 5 && pos >= 0) 245 | dir[0] = dir[1] = dir[2] = no_move; 246 | 247 | // pos in left column 248 | if ( (pos % 6) == 0) 249 | dir[0] = dir[3] = dir[5] = no_move; 250 | 251 | // pos in right column 252 | if ( ( (pos+1) % 6) == 0) 253 | dir[2] = dir[4] = dir[7] = no_move; 254 | 255 | // pos in bottom row 256 | if (pos <= 35 && pos >= 30) 257 | dir[5] = dir[6] = dir[7] = no_move; 258 | 259 | for (auto &d : dir) { 260 | if (d == no_move) continue; 261 | auto next_pos = (*this)(pos + d); 262 | if ( next_pos == SPACE ){ 263 | //std::cerr << int(pos+d) << ' ' << int(forbidden_pos) << '\n'; 264 | movable.push_back(pos + d); 265 | } 266 | } 267 | return movable; 268 | } 269 | public: 270 | std::vector find_piece(const PIECE &piece, ACTION act) { 271 | std::vector eatable; 272 | 273 | for (char now_pos {0}; now_pos < SIZE; ++now_pos) { 274 | char tile = (*this)(now_pos); 275 | 276 | if (tile == piece) { 277 | std::vector eat_pos; 278 | if (act == MOVE) 279 | eat_pos = { check_move(now_pos, piece) }; 280 | else if (act == EAT) 281 | eat_pos = { check_eat(now_pos, piece) }; 282 | 283 | if (!eat_pos.empty()) { 284 | for (auto &next_pos : eat_pos) 285 | eatable.push_back( {now_pos, next_pos} ); 286 | } 287 | 288 | } 289 | } 290 | return eatable; 291 | } 292 | 293 | public: 294 | std::vector get_available_move(const PIECE &piece) { 295 | std::vector mv = find_piece(piece, MOVE); 296 | std::vector ea = find_piece(piece, EAT); 297 | ea.insert(ea.end(), mv.begin(), mv.end()); 298 | 299 | return ea; 300 | } 301 | 302 | 303 | // move or eat a piece 304 | EXEC_STATE move(const char prev_pos, const char place_pos, const PIECE &piece) { 305 | if (place_pos >= SIZE || place_pos < 0) return FAIL; 306 | 307 | (*this)(place_pos) = piece; 308 | (*this)(prev_pos) = SPACE; 309 | 310 | step++; 311 | 312 | return SUCCESS; 313 | } 314 | 315 | public: 316 | /* 317 | 0 1 2 3 4 5 318 | ┏━━━━━━━┓ ┏━━━━━━━┓ 319 | ┃ ┏━━━┓ ┃ ┃ ┏━━━┓ ┃ 320 | 0┃ ┃ 1 1 1 1 1 1 ┃ ┃ 321 | 1┃ ┗ 1 1 1 1 1 1 ┛ ┃ 322 | 2┗━━ 0 0 0 0 0 0 ━━┛ 323 | 3┏━━ 0 0 0 0 0 0 ━━┓ 324 | 4┃ ┏ 2 2 2 2 2 2 ┓ ┃ 325 | 5┃ ┃ 2 2 2 2 2 2 ┃ ┃ 326 | ┃ ┗━━━┛ ┃ ┃ ┗━━━┛ ┃ 327 | ┗━━━━━━━┛ ┗━━━━━━━┛ 328 | 329 | */ 330 | friend std::ostream& operator<<(std::ostream& out, const board& b) { 331 | out << "+--------------------+" << '\n'; 332 | out << " 0 1 2 3 4 5 " << '\n'; 333 | out << " ┏━━━━━━━┓ ┏━━━━━━━┓" << '\n'; 334 | out << " ┃ ┏━━━┓ ┃ ┃ ┏━━━┓ ┃" << '\n'; 335 | 336 | int idx = 0; 337 | for (auto &row : b.tile) { 338 | switch(idx) { 339 | case 0: out << "0 ┃ ┃"; break; 340 | case 1: out << "1 ┃ ┗"; break; 341 | case 2: out << "2 ┗━━"; break; 342 | case 3: out << "3 ┏━━"; break; 343 | case 4: out << "4 ┃ ┏"; break; 344 | case 5: out << "5 ┃ ┃"; break; 345 | } 346 | 347 | for (auto &t : row) { 348 | if (t==SPACE) 349 | out << std::setw(2) << 'x'; 350 | else 351 | out << std::setw(2) << static_cast(t); 352 | } 353 | 354 | switch(idx) { 355 | case 0: out << " ┃ ┃"; break; 356 | case 1: out << " ┛ ┃"; break; 357 | case 2: out << " ━━┛"; break; 358 | case 3: out << " ━━┓"; break; 359 | case 4: out << " ┓ ┃"; break; 360 | case 5: out << " ┃ ┃"; break; 361 | } 362 | out << '\n'; 363 | idx++; 364 | } 365 | out << " ┃ ┗━━━┛ ┃ ┃ ┗━━━┛ ┃" << '\n'; 366 | out << " ┗━━━━━━━┛ ┗━━━━━━━┛" << '\n'; 367 | out << "+--------------------+" << '\n'; 368 | return out; 369 | } 370 | 371 | }; 372 | std::unordered_map > board::mapping_circle = 373 | { 374 | { 1, { 6, RIGHT} }, { 2, {12, RIGHT} }, {3 , {17, LEFT } }, {4 , {11, LEFT } }, 375 | { 6, { 1, DOWN } }, {12, { 2, DOWN } }, {18, {32, UP } }, {24, {31, UP } }, 376 | {31, {24, RIGHT} }, {32, {18, RIGHT} }, {33, {23, LEFT } }, {34, {29, LEFT } }, 377 | {29, {34, UP } }, {23, {33, UP } }, {17, {3, DOWN } }, {11, { 4, DOWN } } 378 | }; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------