RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.critterbot.environment; 00002 00003 import java.io.File; 00004 import java.io.IOException; 00005 import java.net.ServerSocket; 00006 import java.util.ArrayList; 00007 import java.util.List; 00008 00009 import org.rlcommunity.critterbot.simulator.SimulatorMain; 00010 00011 import rlpark.plugin.critterbot.data.CritterbotLabels; 00012 import rlpark.plugin.critterbot.internal.CritterbotConnection; 00013 import rlpark.plugin.rltoys.envio.observations.ObsFilter; 00014 import rlpark.plugin.rltoys.utils.Command; 00015 00016 public class CritterbotSimulator extends CritterbotEnvironment { 00017 static public class SimulatorCommand { 00018 final public int port; 00019 final public Command command; 00020 00021 public SimulatorCommand(Command command, int port) { 00022 this.port = port; 00023 this.command = command; 00024 } 00025 } 00026 00027 static boolean remoteDebugging = false; 00028 private static String jarPath = null; 00029 static final String[] remoteDebugingArgs = { "-Xdebug", 00030 "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044" }; 00031 private SimulatorCommand command; 00032 00033 public CritterbotSimulator(SimulatorCommand command) { 00034 super(new CritterbotConnection("localhost", command.port)); 00035 this.command = command; 00036 } 00037 00038 static public SimulatorCommand startSimulator() { 00039 if (jarPath == null) 00040 setJarPath(getDefaultSimulatorJarPath()); 00041 int port = findFreePort(); 00042 String[] commandLine = buildSimulatorCommandLine(port); 00043 System.out.println("Running: " + toCommandLineString(commandLine)); 00044 Command command = new Command(CritterbotSimulator.class.getSimpleName(), commandLine); 00045 try { 00046 command.start(); 00047 } catch (IOException e) { 00048 e.printStackTrace(); 00049 } 00050 return new SimulatorCommand(command, port); 00051 } 00052 00053 private static int findFreePort() { 00054 int port = 3284; 00055 ServerSocket socket; 00056 try { 00057 socket = new ServerSocket(0); 00058 port = socket.getLocalPort(); 00059 socket.close(); 00060 } catch (IOException e) { 00061 e.printStackTrace(); 00062 System.err.println("Problem to find a free port: using port by default"); 00063 } 00064 return port; 00065 } 00066 00067 static protected String[] buildSimulatorCommandLine(int port) { 00068 String absJarPath = new File(jarPath).getAbsolutePath(); 00069 List<String> commandLine = new ArrayList<String>(); 00070 commandLine.add("java"); 00071 if (remoteDebugging) 00072 for (String parameter : remoteDebugingArgs) 00073 commandLine.add(parameter); 00074 commandLine.add("-jar"); 00075 commandLine.add(absJarPath); 00076 String[] simulatorParameters = { "-p", String.valueOf(port), "-nv" }; 00077 for (String parameter : simulatorParameters) 00078 commandLine.add(parameter); 00079 String[] result = new String[commandLine.size()]; 00080 commandLine.toArray(result); 00081 return result; 00082 } 00083 00084 static private String toCommandLineString(String[] commandLine) { 00085 StringBuilder result = new StringBuilder(); 00086 for (String arg : commandLine) { 00087 result.append(arg); 00088 result.append(" "); 00089 } 00090 return result.substring(0, result.length() - 1); 00091 } 00092 00093 static private String getDefaultSimulatorJarPath() { 00094 String filePath = SimulatorMain.class.getResource(SimulatorMain.class.getSimpleName() + ".class").getFile(); 00095 int classIndex = filePath.indexOf("/" + SimulatorMain.class.getCanonicalName().replace('.', '/')); 00096 String pathToFile = filePath.substring(filePath.indexOf('/'), classIndex); 00097 if (pathToFile.endsWith("!")) 00098 pathToFile = filePath.substring(0, filePath.length() - 1); 00099 return pathToFile; 00100 } 00101 00102 public static void setJarPath(String jarPath) { 00103 CritterbotSimulator.jarPath = jarPath; 00104 } 00105 00106 @Override 00107 public ObsFilter getDefaultFilter() { 00108 return new ObsFilter(legend(), CritterbotLabels.Motor, CritterbotLabels.RotationVel, CritterbotLabels.IRDistance, 00109 CritterbotLabels.Light, CritterbotLabels.Accel); 00110 } 00111 00112 @Override 00113 public void close() { 00114 super.close(); 00115 if (command != null) { 00116 command.command.kill(); 00117 command = null; 00118 } 00119 } 00120 }