def execute(self, action):
        #print('execute')
        run = Assembler(self.carID)

        self.output = run.getTensor()
        if self.output is None:
            term = True
            #print('is none')
            return self.getObservation(), term, 0 #20000

        rew = Reward('ego', run.getTraffic(), self.output, run.getPrioritizedTraffic())

        coll, term = rew.collision()
        if term is True:
            cost = coll
            return self.getObservation(), term, cost

        # over, term = rew.overtime()
        # if term is True:
        #     cost = over
        #     return self.getObservation(), term, cost

        traci.vehicle.setSpeedMode('ego', 0)
        num_vec = traci.vehicle.getIDList()
        for i in range(len(num_vec)):
            if num_vec[i] != 'ego':
                traci.vehicle.setLaneChangeMode(num_vec[i], 512)
        carID = 'ego'
        act = Action(carID)
        #print(action)
        if action == 0:
            act.decelerate()
            #print('dec')
        elif action == 1:
            act.accelerate()
            #print('acc')
        #elif action == 2:
         #   act.emergency()
        else:
            act.remain()
            #print('rem')

        gap, term = rew.emergency_gap()
        #wary = rew.wary_before_intersection()

        #brake = rew.emergency_brake()
        #print(self.output[0:20])
        #print(gap)
        cost = rew.optimum_speed_deviation() + gap #+ over
        traci.simulationStep()
        #print(self.getObservation())
        #print(term)

        return self.getObservation(), term, cost
Ejemplo n.º 2
0
    def execute(self, action):
        run = Assembler(self.carID)

        self.output = run.getTensor()
        #print(run.getTensor())

        if self.output is None:
            term = True
            return self.getObservation(), term, 0

        rew = Reward('ego', run.getTraffic(), self.output)

        coll, term = rew.collision()
        if term is True:
            cost = coll
            return self.getObservation(), term, cost

        traci.vehicle.setSpeedMode('ego', 0)
        num_vec = traci.vehicle.getIDList()
        for i in range(len(num_vec)):
            if num_vec[i] != 'ego':
                traci.vehicle.setLaneChangeMode(num_vec[i], 512)

            ###
            #if i % 2 == 0:
            #traci.vehicle.setSpeedMode(num_vec[i], 23)

        carID = 'ego'
        act = Action(carID)
        if action == 0:
            act.decelerate()
            # print('dec')
        elif action == 1:
            act.accelerate()
            # print('acc')
        else:
            act.remain()
            # print('rem')
        gap, term = rew.emergency_gap()
        wary = rew.wary_before_intersection()

        #win = rew.target()
        brake = rew.emergency_brake()
        cost = rew.optimum_speed_deviation() + brake + gap + wary
        traci.simulationStep()
        #print(self.output)

        return self.getObservation(), term, cost
    def execute(self, action):
        run = Assembler(self.carID)
        self.output = run.getTensor()
        #print(run.getTensor())

        if self.output is None:
            term = True
            return self.getObservation(), term, 0
        else:
            self.co2.append(traci.vehicle.getFuelConsumption(self.carID))

        rew = Reward('ego', run.getTraffic(), self.output,
                     run.getPrioritizedTraffic())

        coll, term = rew.collision_test()
        if term is True:
            self.setCollisionCounter()
            cost = coll
            return self.getObservation(), term, cost

        traci.vehicle.setSpeedMode('ego', 0)
        num_vec = traci.vehicle.getIDList()
        disruption = []
        for i in range(len(num_vec)):
            if num_vec[i] != 'ego':
                if traci.vehicle.getAcceleration(num_vec[i]) < 0:
                    disruption.append(traci.vehicle.getAcceleration(
                        num_vec[i]))

                traci.vehicle.setLaneChangeMode(num_vec[i], 512)

        self.disruptionIntensity.append(sum(disruption))
        ###
        #if i % 2 == 0:
        #traci.vehicle.setSpeedMode(num_vec[i], 23)

        carID = 'ego'
        act = Action(carID)
        if action == 0:
            act.decelerate()
            #print('dec')
        elif action == 1:
            act.accelerate()
            #print('acc')
        #elif action == 2:
        #   print('eme')
        #  act.emergency()
        else:
            act.remain()
            #print('rem')
        #print(traci.vehicle.getSpeed(self.carID))
        #gap, term = rew.emergency_gap_test()
        #wary = rew.wary_before_intersection()

        #win = rew.target()
        #brake = rew.emergency_brake()
        cost = rew.optimum_speed_deviation()  #+ gap #+ brake # #+ wary
        traci.simulationStep()
        #print(self.output)
        self.terminationTime.append(traci.simulation.getCurrentTime())
        #print(traci.simulation.getCurrentTime())

        return self.getObservation(), term, cost