-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLudiiPythonAI.java
More file actions
107 lines (84 loc) · 2.65 KB
/
LudiiPythonAI.java
File metadata and controls
107 lines (84 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package ludii_python_ai;
import java.io.File;
import java.net.URL;
import java.util.regex.Pattern;
import org.jpy.PyLib;
import org.jpy.PyModule;
import org.jpy.PyObject;
import game.Game;
import other.AI;
import other.context.Context;
import other.move.Move;
/**
* Example Java wrapper for a Ludii AI implemented in Python
*
* @author Dennis Soemers
*/
public class LudiiPythonAI extends AI
{
//-------------------------------------------------------------------------
/** Our player index */
protected int player = -1;
/** The "ludii_python.uct" Python Module */
private PyModule ludiiPythonModule = null;
/** This will hold our AI object (implemented in Python) */
private PyObject pythonAI = null;
/** Did we perform initialisation required for JPY? */
private boolean initialisedJpy = false;
//-------------------------------------------------------------------------
/**
* Constructor
*/
public LudiiPythonAI()
{
this.friendlyName = "Example Ludii Python AI";
}
//-------------------------------------------------------------------------
@Override
public Move selectAction
(
final Game game,
final Context context,
final double maxSeconds,
final int maxIterations,
final int maxDepth
)
{
return (Move) pythonAI.call
(
"select_action", game, context,
Double.valueOf(maxSeconds),
Integer.valueOf(maxIterations),
Integer.valueOf(maxDepth)
).getObjectValue();
}
@Override
public void initAI(final Game game, final int playerID)
{
this.player = playerID;
if (!initialisedJpy)
{
// We always expect this AI class to be in a JAR file. Let's find out where our JAR file is
final URL jarLoc = LudiiPythonAI.class.getProtectionDomain().getCodeSource().getLocation();
final String jarPath =
new File(jarLoc.getFile()).getParent()
.replaceAll(Pattern.quote("\\"), "/")
.replaceAll(Pattern.quote("file:"), "");
// Set JPY config property relative to this JAR path
System.setProperty("jpy.config", jarPath + "/libs/jpyconfig.properties");
// Make sure that Python is running
if (!PyLib.isPythonRunning())
{
// We expect the python code to be in the same directory as this JAR file;
// therefore, we include this path such that Python can discover our python code
PyLib.startPython(jarPath);
}
ludiiPythonModule = PyModule.importModule("ludii_python.uct");
initialisedJpy = true;
}
// Instantiate a new AI (implemented in the Python class "UCT")
pythonAI = ludiiPythonModule.call("UCT");
pythonAI.call("init_ai", game, Integer.valueOf(playerID));
}
//-------------------------------------------------------------------------
}