Skip to content

Commit

Permalink
🆕 提交命令行工具
Browse files Browse the repository at this point in the history
  • Loading branch information
bupticybee committed Jan 24, 2020
1 parent 5e368a7 commit abe52ac
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 35 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 37 additions & 9 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions RiverSolver.iml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.8" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.9" level="project" />
<orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.47" level="project" />
<orderEntry type="library" name="Maven: net.sourceforge.argparse4j:argparse4j:0.8.1" level="project" />
</component>
</module>
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
<artifactId>argparse4j</artifactId>
<version>0.8.1</version>
</dependency>
</dependencies>


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/icybee/riversolver/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Config {
String tree_builder_json = null;
String solver_type;

Config(String input_file) throws FileNotFoundException,ClassNotFoundException{
public Config(String input_file) throws FileNotFoundException,ClassNotFoundException{
Yaml yaml_reader = new Yaml();
File config_file = new File(input_file);
FileInputStream fileInputStream = new FileInputStream(config_file);
Expand Down
161 changes: 161 additions & 0 deletions src/main/java/icybee/riversolver/commandline/CommandlineSolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package icybee.riversolver.commandline;

import icybee.riversolver.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import icybee.riversolver.compairer.Compairer;
import icybee.riversolver.ranges.PrivateCards;
import icybee.riversolver.solver.MonteCarolAlg;
import icybee.riversolver.solver.ParrallelCfrPlusSolver;
import icybee.riversolver.solver.Solver;
import icybee.riversolver.trainable.CfrPlusTrainable;
import icybee.riversolver.trainable.CfrTrainable;
import icybee.riversolver.trainable.DiscountedCfrTrainable;
import icybee.riversolver.utils.PrivateRangeConverter;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;


public class CommandlineSolver {

static Config loadConfig(String conf_name){
ClassLoader classLoader = CommandlineSolver.class.getClassLoader();
File file = new File(conf_name);

Config config = null;
try {
config = new Config(file.getAbsolutePath());
}catch(Exception e){
throw new RuntimeException();
}
return config;
}

public static void main(String[] args) throws Exception {
ArgumentParser parser = ArgumentParsers.newFor("CommandlineSolver").build()
.defaultHelp(true)
.description("use command line to solve poker cfr");
parser.addArgument("-c", "--config")
.help("route to the config file");
parser.addArgument("-p1", "--player1_range")
.help("player1 range str,like 'KT,K9,K8,K7,K6,QJ,QT,Q9,Q8,Q7,Q6,JT,J9' ");
parser.addArgument("-p2", "--player2_range")
.help("player2 range str,like 'KT,K9,K8,K7,K6,QJ,QT,Q9,Q8,Q7,Q6,JT,J9' ");
parser.addArgument("-b", "--initial_board")
.help("the board card when the game start");
parser.addArgument("-n", "--iteration_number")
.help("iteration number the cfr algorithm would run");
parser.addArgument("-i", "--print_interval")
.help("calculate best respond ev every other print_interval iterations of cfr");
parser.addArgument("-d", "--debug")
.setDefault(false)
.help("open debug mode");
parser.addArgument("-o", "--output_strategy_file")
.setDefault((Object) null)
.help("where to output strategy json");
parser.addArgument("-l", "--logfile")
.setDefault((Object) null)
.help("calculate best respond ev every other print_interval iterations of cfr");
parser.addArgument("-a", "--algorithm")
.choices("discounted_cfr", "cfr", "cfr_plus").setDefault("discounted_cfr")
.help("cfr algorithm type");
parser.addArgument("-m", "--monte_carol")
.choices("none", "public").setDefault("none")
.help("(experimental)whether to use monte carol algorithm");
parser.addArgument("-t", "--threads")
.setDefault(-1)
.help("multi thread thread number");

Namespace ns = null;
try {
ns = parser.parseArgs(args);
} catch (ArgumentParserException e) {
parser.handleError(e);
System.exit(1);
}

String config_file = ns.getString("config");
String player1_range = ns.getString("player1_range");
String player2_range = ns.getString("player2_range");
String initial_board_str = ns.getString("initial_board");
String[] initial_board_arr = initial_board_str.split(",");
int[] initial_board = Arrays.stream(initial_board_arr).map(e -> Card.strCard2int(e)).mapToInt(i->i).toArray();
int iteration_number = Integer.parseInt(ns.getString("iteration_number"));
int print_interval = Integer.parseInt(ns.getString("print_interval"));
boolean debug = Boolean.valueOf(ns.getString("debug"));
String output_strategy_file = ns.getString("output_strategy_file");
String logfile = ns.getString("logfile");

String algorithm_str = ns.getString("algorithm");
Class<?> algorithm;
switch(algorithm_str){
case "cfr":
algorithm = CfrTrainable.class;
break;
case "cfr_plus":
algorithm = CfrPlusTrainable.class;
break;
case "discounted_cfr":
algorithm = DiscountedCfrTrainable.class;
break;
default:
throw new RuntimeException(String.format("algorithm not found :%s",algorithm_str));
}
String monte_coral_str = ns.getString("monte_carol");
MonteCarolAlg monte_coral;
switch(monte_coral_str){
case "none":
monte_coral = MonteCarolAlg.NONE;
break;
case "public":
monte_coral = MonteCarolAlg.PUBLIC;
break;
default:
throw new RuntimeException(String.format("monte coral type not found :%s",monte_coral_str));
}
int threads = Integer.parseInt(ns.getString("threads"));


Config config = loadConfig(config_file);
Deck deck = SolverEnvironment.deckFromConfig(config);
Compairer compairer = SolverEnvironment.compairerFromConfig(config);
GameTree game_tree = SolverEnvironment.gameTreeFromConfig(config,deck);

PrivateCards[] player1Range = PrivateRangeConverter.rangeStr2Cards(player1_range,initial_board);
PrivateCards[] player2Range = PrivateRangeConverter.rangeStr2Cards(player2_range,initial_board);

Solver solver = new ParrallelCfrPlusSolver(game_tree
, player1Range
, player2Range
, initial_board
, compairer
, deck
, iteration_number
, debug
, print_interval
, logfile
, algorithm
, monte_coral
, threads
);
Map train_config = new HashMap();
solver.train(train_config);

String strategy_json = solver.getTree().dumps(false).toJSONString();
File output_file = new File(output_strategy_file);
FileWriter writer = new FileWriter(output_file);
writer.write(strategy_json);
writer.flush();
writer.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ public class CfrPlusRiverSolver extends Solver{
Class<?> trainer;
int[] round_deal;

public enum MonteCarolAlg {
NONE,
PUBLIC
}
MonteCarolAlg monteCarolAlg;

PrivateCards[] playerHands(int player){
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/icybee/riversolver/solver/MonteCarolAlg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package icybee.riversolver.solver;

public enum MonteCarolAlg {
NONE,
PUBLIC
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public class ParrallelCfrPlusSolver extends Solver{
int nthreads;
ForkJoinPool pool;

public enum MonteCarolAlg {
NONE,
PUBLIC
}
MonteCarolAlg monteCarolAlg;

PrivateCards[] playerHands(int player){
Expand Down Expand Up @@ -119,7 +115,7 @@ public ParrallelCfrPlusSolver(
Class<?> trainer,
MonteCarolAlg monteCarolAlg,
int nthreads
) throws BoardNotFoundException{
) {
super(tree);
//if(board.length != 5) throw new RuntimeException(String.format("board length %d",board.length));
this.initial_board = initial_board;
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/icybee/riversolver/SolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import icybee.riversolver.exceptions.BoardNotFoundException;
import icybee.riversolver.ranges.PrivateCards;
import icybee.riversolver.solver.CfrPlusRiverSolver;
import icybee.riversolver.solver.MonteCarolAlg;
import icybee.riversolver.solver.ParrallelCfrPlusSolver;
import icybee.riversolver.solver.Solver;
import icybee.riversolver.trainable.DiscountedCfrTrainable;
Expand Down Expand Up @@ -388,7 +389,7 @@ public void cfrSolverTest() throws Exception{
, 10
,logfile_name
, DiscountedCfrTrainable.class
,CfrPlusRiverSolver.MonteCarolAlg.NONE
,MonteCarolAlg.NONE
);
Map train_config = new HashMap();
solver.train(train_config);
Expand Down Expand Up @@ -452,7 +453,7 @@ public void cfrTurnSolverTest() throws BoardNotFoundException,Exception{
, 10
,logfile_name
, DiscountedCfrTrainable.class
,CfrPlusRiverSolver.MonteCarolAlg.NONE
,MonteCarolAlg.NONE
);
Map train_config = new HashMap();
solver.train(train_config);
Expand Down Expand Up @@ -516,7 +517,7 @@ public void cfrFlopSolverTest() throws BoardNotFoundException,Exception{
, 10
,logfile_name
, DiscountedCfrTrainable.class
,CfrPlusRiverSolver.MonteCarolAlg.NONE
,MonteCarolAlg.NONE
);
Map train_config = new HashMap();
solver.train(train_config);
Expand Down Expand Up @@ -580,7 +581,7 @@ public void cfrFlopSolverPcsTest() throws BoardNotFoundException,Exception{
, 10
,logfile_name
, DiscountedCfrTrainable.class
,CfrPlusRiverSolver.MonteCarolAlg.PUBLIC
,MonteCarolAlg.PUBLIC
);
Map train_config = new HashMap();
solver.train(train_config);
Expand Down Expand Up @@ -644,7 +645,7 @@ public void parrallelCfrTurnSolverTest() throws BoardNotFoundException,Exception
, 10
,logfile_name
, DiscountedCfrTrainable.class
,ParrallelCfrPlusSolver.MonteCarolAlg.NONE
, MonteCarolAlg.NONE
,2
);
Map train_config = new HashMap();
Expand Down
Loading

0 comments on commit abe52ac

Please sign in to comment.