-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTorchbringerClient.java
58 lines (50 loc) · 1.82 KB
/
TorchbringerClient.java
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
import org.example.rl.util.RLPercept;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
public class TorchBringerClient {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public TorchBringerClient(int port) {
try {
clientSocket = new Socket(InetAddress.getLocalHost(), port);
out = new PrintWriter(clientSocket.getOutputStream());
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (Exception e) {
e.printStackTrace();
}
}
public void initialize(String configPath) {
try {
String jsonData = new String(Files.readAllBytes(Paths.get(configPath)));
JSONObject request = new JSONObject();
request.put("method", "initialize");
request.put("config", new JSONObject(jsonData));
out.println(request.toString());
System.out.println(in.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList<Double> step(RLPercept percept) {
JSONObject request = new JSONObject();
request.put("method", "step");
request.put("state", percept.getState());
request.put("reward", percept.getReward());
request.put("terminal", percept.isTerminal());
out.println(request.toString());
try {
return ((ArrayList<ArrayList<Double>>) new JSONObject(in.readLine()).get("action")).get(0);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}