Skip to content

Commit

Permalink
Merge pull request #954 from votroto:master
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 488325092
Change-Id: Ife5f6c0fa19a035bee2b5ca8081b5c2986feedc3
  • Loading branch information
lanctot committed Nov 15, 2022
2 parents 400418e + f0c9c1d commit b03a197
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
18 changes: 18 additions & 0 deletions open_spiel/games/efg/commas.efg
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
EFG 2 R "commas" { "p1" "p2" }
"test different allowed commas in payoffs"

p "" 1 1 "" { "A" "B" } 0
c "" 1 "" { "s" 99/100 "c" 1/100 } 0
p "" 2 1 "" { "S" "C" } 0
t "" 1 "SS" { 5, 2, }
t "" 2 "SC" { 3 1 }
p "" 2 2 "" { "S" "C" } 0
t "" 1 "SS" { 5 2, }
t "" 2 "SC" { 3, 1 }
c "" 2 "" { "s" 1/100 "c" 99/100 } 0
p "" 2 1 "" { "S" "C" } 0
t "" 3 "CS" { 6, 3, }
t "" 4 "CC" { 4, 4 }
p "" 2 2 "" { "S" "C" } 0
t "" 3 "CS" { 6, 3 }
t "" 4 "CC" { 4, 4 }
37 changes: 35 additions & 2 deletions open_spiel/games/efg_game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,39 @@ bool EFGGame::ParseDoubleValue(const std::string& str, double* value) const {
}
}


std::string EFGGame::NextPayoffToken() {
std::string str = "";
bool seen_comma = false;

while (true) {
// Check stopping condition:
if (pos_ >= string_data_.length() ||
string_data_.at(pos_) == ',' ||
IsWhiteSpace(string_data_.at(pos_))) {
break;
}

str.push_back(string_data_.at(pos_));
AdvancePosition();
}

// Advance the position to the next token.
while (pos_ < string_data_.length()) {
if (!seen_comma && string_data_.at(pos_) == ',') {
seen_comma = true;
AdvancePosition();
continue;
}
if (!IsWhiteSpace(string_data_.at(pos_))) {
break;
}
AdvancePosition();
}

return str;
}

std::string EFGGame::NextToken() {
std::string str = "";
bool reading_quoted_string = false;
Expand Down Expand Up @@ -660,7 +693,7 @@ void EFGGame::ParseTerminalNode(Node* parent, Node* child, int depth) {
bool identical = true;
while (string_data_.at(pos_) != '}') {
double utility = 0;
SPIEL_EFG_PARSE_CHECK_TRUE(ParseDoubleValue(NextToken(), &utility));
SPIEL_EFG_PARSE_CHECK_TRUE(ParseDoubleValue(NextPayoffToken(), &utility));
child->payoffs.push_back(utility);
util_sum += utility;
if (!min_util_.has_value()) {
Expand Down Expand Up @@ -753,7 +786,7 @@ std::string EFGGame::GetInformationStateStringByNumber(Player player,

void EFGGame::ParseGame() {
// Skip any initial whitespace.
while (IsWhiteSpace(string_data_.at(pos_))) {
while (pos_ < string_data_.length() && IsWhiteSpace(string_data_.at(pos_))) {
AdvancePosition();
}
SPIEL_EFG_PARSE_CHECK_LT(pos_, string_data_.length());
Expand Down
1 change: 1 addition & 0 deletions open_spiel/games/efg_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class EFGGame : public Game {
std::unique_ptr<Node> NewNode() const;
void ParseGame();
void ParsePrologue();
std::string NextPayoffToken();
std::string NextToken();
void AdvancePosition();
std::string GetLine(int line) const;
Expand Down
22 changes: 22 additions & 0 deletions open_spiel/games/efg_game_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ namespace {

namespace testing = open_spiel::testing;

// Sample game from Gambit
const char* kCommasFilename = "open_spiel/games/efg/commas.efg";

const char* kSampleFilename = "open_spiel/games/efg/sample.efg";
const char* kKuhnFilename = "open_spiel/games/efg/kuhn_poker.efg";
const char* kLeducFilename = "open_spiel/games/efg/leduc_poker.efg";
Expand Down Expand Up @@ -94,6 +97,24 @@ void EFGGameSimpleForkFromData() {
testing::RandomSimTestNoSerialize(*game, 100);
}

void EFGGameCommasFromFile() {
absl::optional<std::string> file = FindFile(kCommasFilename, 2);
if (file != absl::nullopt) {
std::cout << "Found file: " << file.value() << "; running sim test.";
std::shared_ptr<const Game> game =
LoadGame("efg_game", {{"filename", GameParameter(file.value())}});
SPIEL_CHECK_TRUE(game != nullptr);
GameType type = game->GetType();
SPIEL_CHECK_EQ(type.dynamics, GameType::Dynamics::kSequential);
SPIEL_CHECK_EQ(type.information,
GameType::Information::kImperfectInformation);
SPIEL_CHECK_EQ(type.utility, GameType::Utility::kGeneralSum);
SPIEL_CHECK_EQ(type.chance_mode, GameType::ChanceMode::kExplicitStochastic);
SPIEL_CHECK_EQ(game->NumDistinctActions(), 4);
SPIEL_CHECK_EQ(game->NumPlayers(), 2);
}
}

void EFGGameSimTestsSampleFromFile() {
absl::optional<std::string> file = FindFile(kSampleFilename, 2);
if (file != absl::nullopt) {
Expand Down Expand Up @@ -191,6 +212,7 @@ int main(int argc, char** argv) {
open_spiel::Init("", &argc, &argv, true);
open_spiel::efg_game::EFGGameSimTestsSampleFromData();
open_spiel::efg_game::EFGGameSimTestsKuhnFromData();
open_spiel::efg_game::EFGGameCommasFromFile();
open_spiel::efg_game::EFGGameSimTestsSampleFromFile();
open_spiel::efg_game::EFGGameSimTestsKuhnFromFile();
open_spiel::efg_game::EFGGameSimTestsLeducFromFile();
Expand Down

0 comments on commit b03a197

Please sign in to comment.