def _process_server_msg(self): try: buffer, _ = self.socket.recvfrom(TO_SOCKET_MSEC) _logger.debug('Received buffer {!r}.'.format(buffer)) if not buffer: return elif MSG_SHUTDOWN in buffer: _logger.info('Server requested shutdown.') self.stop() elif MSG_RESTART in buffer: _logger.info('Server requested restart of driver.') self.driver.on_restart() else: sensor_dict = self.serializer.decode(buffer) carstate = CarState(sensor_dict) _logger.debug(carstate) command = self.driver.drive(carstate) _logger.debug(command) buffer = self.serializer.encode(command.actuator_dict) _logger.debug('Sending buffer {!r}.'.format(buffer)) self.socket.sendto(buffer, self.hostaddr) except socket.error as ex: _logger.warning('Communication with server failed: {}.'.format(ex)) except KeyboardInterrupt: _logger.info('User requested shutdown.') self.stop()
def _process_server_msg(self): try: buffer, _ = self.socket.recvfrom(TO_SOCKET_MSEC) _logger.debug('Received buffer {!r}.'.format(buffer)) if not buffer: return elif MSG_SHUTDOWN in buffer: _logger.info('Server requested shutdown.') self.stop() elif MSG_RESTART in buffer: _logger.info('Server requested restart of driver.') self.driver.on_restart() else: sensor_dict = self.serializer.decode(buffer) carstate = CarState(sensor_dict) _logger.debug(carstate) self.evaluation['crashed'] = (abs( carstate.distance_from_center) > 0.9) self.evaluation[ 'stuck'] = carstate.speed_x < 5 and carstate.current_lap_time > 10 self.evaluation['time'] = carstate.current_lap_time self.evaluation['position'] = carstate.race_position self.evaluation['distance'] = carstate.distance_raced self.evaluation['steering'] += 1 self.evaluation['lapComplete'] = carstate.last_lap_time > 0 self.evaluation['lapTime'] = carstate.last_lap_time self.evaluation['avgSpeed'] = 0 if carstate.current_lap_time <= 0 or carstate.distance_from_start <= 0 else\ min(carstate.distance_raced, carstate.distance_from_start) / carstate.current_lap_time self.evaluation['fitness'] = self.getFitness() # if(self.evaluation['crashed'] or self.evaluation['stuck'] or self.evaluation['lapComplete']): # self.stop() command = self.driver.drive(carstate) self.evaluation['steering'] = ( self.evaluation['steering'] * (self.evaluation['iteration'] - 1) + abs(command.steering)) / self.evaluation['iteration'] # print('speed: {}, time: {}, distance: {}'.format(self.evaluation['avgSpeed'], carstate.current_lap_time, self.evaluation['distance'])) _logger.debug(command) buffer = self.serializer.encode(command.actuator_dict) _logger.debug('Sending buffer {!r}.'.format(buffer)) self.socket.sendto(buffer, self.hostaddr) except socket.error as ex: _logger.warning('Communication with server failed: {}.'.format(ex)) except KeyboardInterrupt: _logger.info('User requested shutdown.') self.stop()
def test_decode_server_message(): buffer = b'(angle 0.008838)' \ b'(curLapTime 4.052)' \ b'(damage 0)' \ b'(distFromStart 1015.56)' \ b'(distRaced 42.6238)' \ b'(fuel 93.9356)' \ b'(gear 3)' \ b'(lastLapTime 0)' \ b'(opponents 123.4 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200' \ b' 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200)' \ b'(racePos 1)' \ b'(rpm 4509.31)' \ b'(speedX 81.5135)' \ b'(speedY 0.40771)' \ b'(speedZ -2.4422)' \ b'(track 4.3701 4.52608 5.02757 6.07753 8.25773 11.1429 13.451 16.712 21.5022' \ b' 30.2855 51.8667 185.376 69.9077 26.6353 12.6621 8.2019 6.5479 5.82979 5.63029)' \ b'(trackPos 0.126012)' \ b'(wheelSpinVel 67.9393 68.8267 71.4009 71.7363)' \ b'(z 0.336726)' \ b'(focus 26.0077 27.9798 30.2855 33.0162 36.3006)' d = Serializer().decode(buffer) assert len(d) == 19 assert d['angle'] == '0.008838' assert d['wheelSpinVel'] == ['67.9393', '68.8267', '71.4009', '71.7363'] c = CarState(d) assert c.angle == 0.5063800993366215 assert c.current_lap_time == 4.052 assert c.damage == 0 assert c.distance_from_start == 1015.56 assert c.distance_raced == 42.6238 assert c.fuel == 93.9356 assert c.gear == 3 assert c.last_lap_time == 0.0 assert c.opponents == (123.4, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200) assert c.race_position == 1 assert c.rpm == 4509.31 assert c.speed_x == 22.64263888888889 assert c.speed_y == 0.11325277777777779 assert c.speed_z == -0.6783888888888889 assert c.distances_from_edge == (4.3701, 4.52608, 5.02757, 6.07753, 8.25773, 11.1429, 13.451, 16.712, 21.5022, 30.2855, 51.8667, 185.376, 69.9077, 26.6353, 12.6621, 8.2019, 6.5479, 5.82979, 5.63029) assert c.distance_from_center == 0.126012 assert c.wheel_velocities == (3892.635153073154, 3943.4794278130635, 4090.970223435639, 4110.1872278843275) assert c.z == 0.336726 assert c.focused_distances_from_edge == (26.0077, 27.9798, 30.2855, 33.0162, 36.3006) assert c.focused_distances_from_egde_valid # fake bad focus value: c.focused_distances_from_edge = (-1.0, -1.0, -1.0, -1.0, -1.0) assert not c.focused_distances_from_egde_valid
def _process_server_msg(self): try: buffer, _ = self.socket.recvfrom(TO_SOCKET_MSEC) _logger.debug('Received buffer {!r}.'.format(buffer)) if not buffer: return elif MSG_SHUTDOWN in buffer: _logger.info('Server requested shutdown.') self.stop() elif MSG_RESTART in buffer: _logger.info('Server requested restart of driver.') self.driver.on_restart() else: sensor_dict = self.serializer.decode(buffer) carstate = CarState(sensor_dict) _logger.debug(carstate) command = self.driver.drive(carstate) if self.data_file is not None: data_to_store = [] data_to_store += [ carstate.gear, carstate.rpm, carstate.speed_x, carstate.speed_y ] data_to_store += carstate.wheel_velocities data_to_store += [command.accelerator ] #, command.brake, command.steering] #data_to_store += [carstate.distance_from_center, carstate.angle] #data_to_store += carstate.distances_from_edge data_to_store = [str(v) for v in data_to_store] self.data_file.write(', '.join(data_to_store) + '\n') _logger.debug(command) buffer = self.serializer.encode(command.actuator_dict) _logger.debug('Sending buffer {!r}.'.format(buffer)) self.socket.sendto(buffer, self.hostaddr) except socket.error as ex: _logger.warning('Communication with server failed: {}.'.format(ex)) except KeyboardInterrupt: _logger.info('User requested shutdown.') self.stop()
def _process_server_msg(self): try: buffer, _ = self.socket.recvfrom(TO_SOCKET_MSEC) _logger.debug('Received buffer {!r}.'.format(buffer)) if not buffer: return elif MSG_SHUTDOWN in buffer: _logger.info('Server requested shutdown.') # if self.position==1: # meh=1000 # if self.position==2: # meh=500 # if self.position==3: # meh=200 # if self.position==4: # meh=100 with open(self.datafile,'w') as f: fitt=str(self.fitness+15) f.write(fitt) self.stop() elif MSG_RESTART in buffer: _logger.info('Server requested restart of driver.') self.driver.on_restart() else: sensor_dict = self.serializer.decode(buffer) carstate = CarState(sensor_dict) command = self.driver.drive(carstate) _logger.debug(carstate) # print(carstate) if command.steering!=-0.7 : #print("blah") self.notTurnFlag=False if carstate.current_lap_time>5: if carstate.distance_from_center<-1.2 or carstate.distance_from_center>1.2: self.crashed=True if (carstate.speed_x<2 and carstate.current_lap_time>6 ): self.stuck=True # if carstate.distance_raced%22784==0: # self.stack+=carstate.last_lap_time self.timespend=carstate.current_lap_time#+self.stack # self.fitness=carstate.distance_raced -carstate.current_lap_time*10 if self.crashed : with open(self.datafile,'w') as f: fitt=str(self.fitness-10) f.write(fitt) self.stop() elif self.stuck: with open(self.datafile,'w') as f: fitt=str(self.fitness - 15) f.write(fitt) self.stop() else: self.count+=1 self.sumspeed+=carstate.speed_x self.speed=self.sumspeed/self.count #self.roadError+=abs(carstate.distance_from_center) # print(carstate.race_position) # if carstate.speed_x<80: # self.speedReward-=0.01 # elif carstate.speed_x<110: # self.speedReward+=0.01 # else: # self.speedReward+=0.02 self.position=carstate.race_position # if carstate.distance_raced>200: # if self.position==1: # self.positionReward+=0.1 # if self.position==2: # self.positionReward+=0.5 # if self.position==3: # self.positionReward+=0.2 # if self.position==4: # self.positionReward+=0.1 if not self.notTurnFlag: if carstate.distance_raced>400: self.fitness=float((self.speed+float(carstate.distance_raced /100)) + self.positionReward+self.speedReward) +30 -self.roadError # return "andreas is meh ********************************" else: self.fitness=float(self.speed+float(carstate.distance_raced /100))+ self.positionReward+self.speedReward -self.roadError else: self.fitness=0 # print("this is the message",sensor_dict) _logger.debug(command) buffer = self.serializer.encode(command.actuator_dict) _logger.debug('Sending buffer {!r}.'.format(buffer)) self.socket.sendto(buffer, self.hostaddr) except socket.error as ex: _logger.warning('Communication with server failed: {}.'.format(ex)) except KeyboardInterrupt: _logger.info('User requested shutdown.') self.stop()
def _process_server_msg(self): try: buffer, _ = self.socket.recvfrom(TO_SOCKET_MSEC) _logger.debug('Received buffer {!r}.'.format(buffer)) if not buffer: return elif MSG_SHUTDOWN in buffer: _logger.info('Server requested shutdown.') # if self.position==1: # meh=1000 # if self.position==2: # meh=500 # if self.position==3: # meh=200 # if self.position==4: # meh=100 with open(self.datafile, 'w') as f: fitt = str(self.fitness) f.write(fitt) self.stop() elif MSG_RESTART in buffer: _logger.info('Server requested restart of driver.') self.driver.on_restart() else: sensor_dict = self.serializer.decode(buffer) carstate = CarState(sensor_dict) _logger.debug(carstate) command = self.driver.drive(carstate) # print(carstate) if carstate.current_lap_time > 5: if carstate.distance_from_center < -1 or carstate.distance_from_center > 1: self.crashed = True if (carstate.speed_x < 2 and carstate.current_lap_time > 5): self.stuck = True # if carstate.distance_raced%22784==0: # self.stack+=carstate.last_lap_time self.timespend = carstate.current_lap_time #+self.stack # self.fitness=carstate.distance_raced -carstate.current_lap_time*10 if self.crashed: with open(self.datafile, 'w') as f: fitt = str(carstate.distance_raced - 10000) f.write(fitt) self.stop() elif self.stuck: with open(self.datafile, 'w') as f: fitt = str(carstate.distance_raced - 100000) f.write(fitt) self.stop() else: self.count += 1 self.accelReward += command.accelerator if carstate.gear > 3 else 0 self.sumspeed += carstate.speed_x self.speed = self.sumspeed / self.count * 3.6 # print(carstate.race_position) self.position = carstate.race_position if carstate.distance_raced > 200: if self.position == 1: self.positionReward += 1 if self.position == 2: self.positionReward += 0.5 if self.position == 3: self.positionReward += 0.2 # if self.position==4: # self.meh+=0.1 if carstate.distance_raced > 400 and False: self.fitness = float( (self.speed + float(carstate.distance_raced / 500) ) + self.positionReward) + 30 + self.accelReward # return "andreas is meh ********************************" else: self.fitness = self.speed # if self.count<100: # command.accelerator=1 # print("this is the message",sensor_dict) _logger.debug(command) buffer = self.serializer.encode(command.actuator_dict) _logger.debug('Sending buffer {!r}.'.format(buffer)) self.socket.sendto(buffer, self.hostaddr) except socket.error as ex: _logger.warning('Communication with server failed: {}.'.format(ex)) except KeyboardInterrupt: _logger.info('User requested shutdown.') self.stop()