Commit 4d2c221b authored by Mattis Hoppe's avatar Mattis Hoppe
Browse files

added nodeshutdown after number of episodes

parent 5e7ce0fd
Pipeline #501141 passed with stage
in 2 minutes and 23 seconds
......@@ -77,6 +77,10 @@ public class RLAutopilot extends EEComponent implements Inspectable {
public float[] state = null;
public float[] action = null;
//array with all state values that get sent
public float[] statePacket = new float[4];
//public int packageLength = 3;
//Instant lastTime = null;
public RLAutopilot(RLAutopilotProperties properties, EESystem eeSystem) {
......@@ -103,7 +107,6 @@ public class RLAutopilot extends EEComponent implements Inspectable {
}
this.upperSpeedLimitMsg = addPort(PortInformation.newOptionalInputDataPort(SpeedLimitService.UPPER_SPEED_LIMIT_MSG, SpeedLimitService.SPEED_LIMIT_TYPE, false));*/
}
@Override
......@@ -140,7 +143,16 @@ public class RLAutopilot extends EEComponent implements Inspectable {
}*/
}
public void updateStatePacket(){
this.statePacket[0] = (float) currentPosition.at(0);
this.statePacket[1] = (float) currentPosition.at(1);
this.statePacket[2] = (float) currentCompass;
this.statePacket[3] = (float) currentVelocity;
}
public float[] getStatePacket(){
return this.statePacket;
}
void setGas(Instant sendTime, double val) {
this.currentGas = val;
......
......@@ -29,6 +29,8 @@ import org.ros.namespace.GraphName;
import org.ros.node.AbstractNodeMain;
import org.ros.node.ConnectedNode;
import org.ros.node.NodeMain;
import org.ros.node.NodeMainExecutor;
import org.ros.node.DefaultNodeMainExecutor;
import org.ros.node.topic.Subscriber;
import org.ros.node.topic.Publisher;
import org.ros.node.Node;
......@@ -64,6 +66,9 @@ public class RLSimulationHandler extends AbstractNodeMain{
private boolean in_termination = true;
private boolean in_reset = false;
private boolean done = true;
private int episodeCounter = 0;
private int maxEpisodes = 1;
private NodeMainExecutor nodeExecutor;
......@@ -79,7 +84,8 @@ public class RLSimulationHandler extends AbstractNodeMain{
}
}
public RLSimulationHandler (SimulationConfig config, Instant simulationTime, World world, Pathfinding pathfinding, OsmMap map, RLVisualizer viz){
public RLSimulationHandler (SimulationConfig config, Instant simulationTime, World world, Pathfinding pathfinding, OsmMap map, RLVisualizer viz, NodeMainExecutor nodeExecutor){
this.nodeExecutor = nodeExecutor;
this.config = config;
this.simulationTime = simulationTime;
this.world = world;
......@@ -135,6 +141,11 @@ public class RLSimulationHandler extends AbstractNodeMain{
state_publisher.publish(state);
terminate_publisher.publish(terminated);
reward_publisher.publish(reward);
if(episodeCounter == maxEpisodes && in_termination){
nodeExecutor.shutdown();
return;
}
}
});
......@@ -176,6 +187,7 @@ public class RLSimulationHandler extends AbstractNodeMain{
}
public Result reset(){
++this.episodeCounter;
return this.setup();
}
......@@ -216,6 +228,7 @@ public class RLSimulationHandler extends AbstractNodeMain{
simulationTime = tu.newTime;
}
activeVehicle = 0;
updateStatePackets();
float[] simState = getState();
rewardCalc = new RLRewardCalculator(navigations, vehiclesArray);
float init_reward = rewardCalc.getReward();
......@@ -243,7 +256,7 @@ public class RLSimulationHandler extends AbstractNodeMain{
simulator.update(tu);
simulationTime = tu.newTime;
}
updateStatePackets();
float[] simState = getState();
float step_reward = rewardCalc.getReward();
boolean simTermination = this.getSimTermination();
......@@ -259,9 +272,20 @@ public class RLSimulationHandler extends AbstractNodeMain{
catch(ConcurrentModificationException ignore) {}
}
done = true;
/*if(episodeCounter == maxEpisodes && simTermination){
this.nodeExecutor.shutdown();
return null;
}*/
return new Result(step_reward, simState, simTermination);
}
private void updateStatePackets(){
for(int i = 0; i<autopilots.length; i++){
autopilots[i].updateStatePacket();
}
}
private void wait_done(){
while(!done);
done = false;
......@@ -277,6 +301,7 @@ public class RLSimulationHandler extends AbstractNodeMain{
private float[] getState(){
int vehicleCount = autopilots.length;
int stateLength = autopilots[0].state.length;
int statePacketLength = autopilots[0].getStatePacket().length;
float[] result;
if(!distributed){
......@@ -289,10 +314,15 @@ public class RLSimulationHandler extends AbstractNodeMain{
}
}
else{
result = new float[stateLength];
result = new float[stateLength + (vehicleCount-1) * statePacketLength];
for(int i = 0; i<stateLength; i++){
result[i] = autopilots[activeVehicle].state[i];
}
for(int i = 1; i< vehicleCount; i++){
for(int j = 0; j < statePacketLength; j++){
result[stateLength + i*statePacketLength + j] = autopilots[(activeVehicle + i) % vehicleCount].getStatePacket()[j];
}
}
}
return result;
......
......@@ -38,9 +38,8 @@ public class RLSimulationInit {
public void init() { //TaskStatus replaced by void?
NodeConfiguration rosNodeConfiguration = NodeConfiguration.newPrivate();
NodeMain rlSimulationHandler = new RLSimulationHandler(config, simulationTime, world, pathfinding, map, null);
NodeMainExecutor nodeMainExecuter = DefaultNodeMainExecutor.newDefault();
nodeMainExecuter.execute(rlSimulationHandler, rosNodeConfiguration);
NodeMainExecutor nodeMainExecutor = DefaultNodeMainExecutor.newDefault();
NodeMain rlSimulationHandler = new RLSimulationHandler(config, simulationTime, world, pathfinding, map, null, nodeMainExecutor);
nodeMainExecutor.execute(rlSimulationHandler, rosNodeConfiguration);
}
}
\ No newline at end of file
......@@ -74,9 +74,9 @@ public class RLVisualizer{
//System.out.println(simConfig == null);
NodeConfiguration rosNodeConfiguration = NodeConfiguration.newPrivate();
//System.out.println(pathfinding == null);
rlSimulationHandler = new RLSimulationHandler(simConfig, simTime, world, pathfinding, map, this);
NodeMainExecutor nodeMainExecuter = DefaultNodeMainExecutor.newDefault();
nodeMainExecuter.execute(rlSimulationHandler, rosNodeConfiguration);
NodeMainExecutor nodeMainExecutor = DefaultNodeMainExecutor.newDefault();
rlSimulationHandler = new RLSimulationHandler(simConfig, simTime, world, pathfinding, map, this, nodeMainExecutor);
nodeMainExecutor.execute(rlSimulationHandler, rosNodeConfiguration);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment