Skip to content

Commit

Permalink
Merge pull request #1171 from willmcgowan:GermanWhistForegame
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 684425354
Change-Id: I41d747837da27dcdf557d28136f53ce053fa2e07
  • Loading branch information
lanctot committed Oct 10, 2024
2 parents f3f2aaa + 192c9f6 commit a66063d
Show file tree
Hide file tree
Showing 11 changed files with 2,781 additions and 83 deletions.
167 changes: 84 additions & 83 deletions docs/games.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions open_spiel/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ add_executable(fsicfr_liars_dice fsicfr_liars_dice.cc ${OPEN_SPIEL_OBJECTS})

add_executable(gtp gtp.cc ${OPEN_SPIEL_OBJECTS})

add_executable(is_mcts_gwhist is_mcts_gwhist.cc ${OPEN_SPIEL_OBJECTS})

add_executable(matrix_example matrix_example.cc ${OPEN_SPIEL_OBJECTS})
add_test(matrix_example_test matrix_example)

Expand Down
85 changes: 85 additions & 0 deletions open_spiel/examples/is_mcts_gwhist.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2021 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <random>

#include "open_spiel/abseil-cpp/absl/random/distributions.h"
#include "open_spiel/algorithms/is_mcts.h"
#include "open_spiel/algorithms/mcts.h"
#include "open_spiel/spiel.h"
#include "open_spiel/spiel_bots.h"
#include "open_spiel/spiel_utils.h"

namespace open_spiel {
namespace {

constexpr const int kSeed = 9492110; // 93879211;

void PlayGWhist(int human_player, std::mt19937* rng, int num_rollouts) {
std::shared_ptr<const Game> game = LoadGame("german_whist_foregame");
std::random_device rd;
int eval_seed = rd();
int bot_seed = rd();
auto evaluator =
std::make_shared<algorithms::RandomRolloutEvaluator>(1, eval_seed);
auto bot = std::make_unique<algorithms::ISMCTSBot>(
bot_seed, evaluator, 0.7 * 13, num_rollouts,
algorithms::kUnlimitedNumWorldSamples,
algorithms::ISMCTSFinalPolicyType::kMaxVisitCount, true, false);
std::unique_ptr<State> state = game->NewInitialState();
while (!state->IsTerminal()) {
Action chosen_action = kInvalidAction;
if (state->IsChanceNode()) {
chosen_action =
SampleAction(state->ChanceOutcomes(), absl::Uniform(*rng, 0.0, 1.0))
.first;
} else if (state->CurrentPlayer() != human_player) {
chosen_action = bot->Step(*state);
} else {
std::cout << state->InformationStateString(human_player) << std::endl;
auto legal_actions = state->LegalActions();
for (int i = 0; i < legal_actions.size(); ++i) {
std::cout << state->ActionToString(legal_actions[i]) << ",";
}
std::cout << std::endl;
std::cout << "Input action:";
std::string input;
std::cin >> input;
chosen_action = state->StringToAction(input);
std::cout << std::endl;
}
state->ApplyAction(chosen_action);
}

std::cout << "Terminal state:" << std::endl;
std::cout << state->ToString() << std::endl;
std::cout << "Returns: " << absl::StrJoin(state->Returns(), " ") << std::endl;
}

} // namespace
} // namespace open_spiel

int main(int argc, char** argv) {
std::random_device rd;
std::mt19937 rng(rd());
int human_player;
int num_rollouts;
std::cout << "human_player:";
std::cin >> human_player;
std::cout << "\n";
std::cout << "num_rollouts:";
std::cin >> num_rollouts;
std::cout << "\n";
open_spiel::PlayGWhist(human_player, &rng, num_rollouts);
}
8 changes: 8 additions & 0 deletions open_spiel/games/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ set(GAME_SOURCES
euchre/euchre.h
first_sealed_auction/first_sealed_auction.cc
first_sealed_auction/first_sealed_auction.h
german_whist_foregame/german_whist_foregame.cc
german_whist_foregame/german_whist_foregame.h
gin_rummy/gin_rummy.cc
gin_rummy/gin_rummy.h
gin_rummy/gin_rummy_utils.cc
Expand Down Expand Up @@ -202,6 +204,7 @@ if (${OPEN_SPIEL_BUILD_WITH_ACPC})
set(GAME_SOURCES ${GAME_SOURCES} universal_poker/universal_poker.cc universal_poker/universal_poker.h)
endif()


add_library (games OBJECT ${GAME_SOURCES})

target_include_directories (games PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
Expand Down Expand Up @@ -434,6 +437,11 @@ add_executable(garnet_test mfg/garnet_test.cc ${OPEN_SPIEL_OBJECTS}
$<TARGET_OBJECTS:tests>)
add_test(garnet_test garnet_test)

add_executable(german_whist_foregame_test german_whist_foregame/german_whist_foregame_test.cc ${OPEN_SPIEL_OBJECTS}
$<TARGET_OBJECTS:tests>)
add_test(german_whist_foregame_test german_whist_foregame_test)
add_executable(german_whist_endgame german_whist_foregame/german_whist_endgame.cc ${OPEN_SPIEL_OBJECTS})

add_executable(gin_rummy_test gin_rummy/gin_rummy_test.cc ${OPEN_SPIEL_OBJECTS}
$<TARGET_OBJECTS:tests>)
add_test(gin_rummy_test gin_rummy_test)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <cstdint>
#include <iostream>
#include <random>
#include <vector>

#include "open_spiel/games/german_whist_foregame/german_whist_foregame.h"

int main() {
std::vector<std::vector<uint32_t>> bin_coeffs =
open_spiel::german_whist_foregame::BinCoeffs(
2 * open_spiel::german_whist_foregame::kNumRanks);
const uint32_t hard_threads =
8; // set this to take advantage of more cores on your machine//
open_spiel::german_whist_foregame::vectorNa tablebase =
open_spiel::german_whist_foregame::BuildTablebase(bin_coeffs,
hard_threads);
std::random_device rd;
int num_samples = 100;
if (open_spiel::german_whist_foregame::TestTablebase(num_samples, rd(),
tablebase, bin_coeffs)) {
std::cout << "Tablebase accurate" << std::endl;
} else {
std::cout << "Tablebase inaccurate" << std::endl;
}
std::cout << "Starting Saving Tablebase" << std::endl;
open_spiel::german_whist_foregame::StoreTTable("TTable13.txt", tablebase);
std::cout << "Finished Saving Tablebase" << std::endl;

return 0;
}
Loading

0 comments on commit a66063d

Please sign in to comment.