def Run(self):
        distance = 125
        #组装好的控制器,包含若干个子模块
        if self.pos != self.last_pos:
            uc = (self.veo * self.veo - self.last_veo * self.last_veo) / (
                2 * (self.pos - self.last_pos))  #测量计算得到的加速度
            detu0 = uc - self.last_out  #误差值
        else:
            detu0 = 0
        self.vbar = self.MTDProPro(self.pos, self.time, detu0)
        self.out = 0  #默认惰行
        #基于xgboost的智能学习算法
        s = self.pos
        sr = self.tripDestination - self.pos
        tr = self.tripTime - self.time
        sl = trc.getRoadspeedLimit(self.pos)  #路段限速
        gd = trc.getRoadGradinet(self.pos)  #道路坡度
        nsl = trc.getNextSpeedLimit(self.pos)  #下一限速
        nsld = trc.getSpeedLimitEndPoint(self.pos)  #当前限速区间剩余距离
        if self.pos < self.tripDestination - distance:
            character = [s, sr, self.tripTime, tr, sl, gd, nsl, nsld]
            xdata = xgb.DMatrix(np.mat(character))
            preds = self.bst.predict(xdata)
            self.out = max(preds[0], -1)  #计算阻力误差加速度
            if preds[0] > 0.99:
                self.out = 1
            elif preds[0] < 0.01 and preds[0] > -0.1:
                self.out = 0.0
#            elif preds[0]<-0.75:
#                    self.out=-1
#安全保护算法
        ebkv = self.trd.getEmerencyBrakeSpeed(self.pos)  #紧急制动速度
        if self.veo < self.vbar and self.pos < self.tripDestination - distance and self.out <= 0:
            self.out = self.last_out + self.dadt * self.dt
        if self.veo > ebkv and self.pos < self.tripDestination - distance:
            #超速紧急制动
            self.out = -1
        #精准停车算法
        if self.pos > self.tripDestination - distance and self.pos < self.tripDestination:
            up = -1 * self.veo * self.veo / (2 * sr)  #目标加速度
            self.out = up - detu0  #计算阻力误差加速度
            self.out = up
            self.out = max(-1.0, self.out)
        if self.pos > self.tripDestination:
            self.out = -1
        if self.out > 1:
            self.out = 1
        return
Exemplo n.º 2
0
 def __init__(self, time):
     print("path:" + sys.path[0])
     #线路静态信息
     self.startPoint = trc.SLStartPoint[0]
     self.endPoint = trc.SLStartPoint[-1]
     self.S = self.endPoint - self.startPoint
     self.max_speed = 80 / 3.6
     self.T = time  #运行总时间
     self.avg_speed = self.S / self.T  #平均速度
     self.dt = 0.2  #运行时间步长
     self.low = np.array([0, 0])
     self.high = np.array([self.S, self.max_speed])
     self.ac = 0.8  #最大加速度
     self.de = 1  #最大减速度
     self.viewer = None
     self.n_actions = 9  #-0.8 :0.2 :0.8
     self.n_features = 4  #用于训练的特征,和self.state相对应
     self.action_space = [
         '-0.8', '-0.6', '-0.4', '-0.2', '0', '0.2', '0.4', '0.6', '0.8'
     ]
     self.action_space = spaces.Discrete(9)  #下标从0开始
     self.observation_space = spaces.Box(self.low, self.high)
     self.seed()
     self.done = False
     self.filterFactor = 0.8
     #线路动态信息
     self.trd = trc.TrainAndRoadData()
     self.reset()
Exemplo n.º 3
0
 def RefreshTrainState(self):        
     #计算牵引力和制动力
     acc=0
     if self.aim_acc>0:
         #延时和一阶惯性系统 
         self.aim_acc=self.delayM.Step(self.aim_acc)
         traction_acc=self.Inertia.Step(self.aim_acc)      
         #限制环节
         max_traction_acc=trc.getTrateForce(self.speed)/self.weight
         traction_acc=min(max_traction_acc,traction_acc)
         self.true_traction_acc=traction_acc
     else:
         traction_acc=0
         self.true_traction_acc=traction_acc
     if self.aim_acc<0:    
         self.aim_acc=self.aim_acc*(-1)
         #延时和一阶惯性系统  
         self.aim_acc=self.delayM.Step(self.aim_acc)
         braking_acc=self.Inertia.Step(self.aim_acc)            
         #限制环节
         max_braking_acc=trc.getBrakeForce(self.speed)/self.weight
         braking_acc=min(max_braking_acc,braking_acc)  
         self.true_brake_acc=braking_acc
     else:
         braking_acc=0
         self.true_brake_acc=braking_acc
     #基本阻力和附加阻力对系统造成的干扰    
     anti_acc=trc.getAntiForce(self.speed,self.postion)/self.weight
     acc=traction_acc-anti_acc-braking_acc
     #更新列车状态
     self.TractionPower=traction_acc*self.weight*self.speed
     self.RegenerativePower=traction_acc*self.weight*self.speed
     self.RegenerativePower=braking_acc*self.weight*self.speed
     dacc_dt=(self.trueAcc-acc)/self.deltaTime
     self.Jerk=self.Jerk+abs(dacc_dt)*self.deltaTime
     self.trueAcc=acc
     dS=self.speed*self.deltaTime+0.5*self.trueAcc*self.deltaTime**2
     self.speed=self.speed+self.trueAcc*self.deltaTime
     self.postion=self.postion+dS
 def MTD(self, pos, time):
     # minimal-time distribution  algorithm
     #子模块1:时间分配算法
     tr = (self.tripTime - time) * 1.2  #真实剩余时间
     vlimEndPoint = trc.getSpeedLimitEndPoint(pos)  #获得限速的终点
     tnMin = self.trd.getCurrentSectionMinTime(pos)  #当前点到区间终点的获得花的最小时间
     tpMin = self.trd.getMinRestTime(pos)  #当前点到旅程终点最小预测时间
     if tpMin < 3:
         tpMin = 3
     trbar = tr * tnMin / tpMin  #预测当前点到区间终点应分配的时间
     if trbar < 3:
         trbar = 3
     vbar = (vlimEndPoint - pos) / trbar
     return vbar
 def MTDPro(self, pos, time):
     # minimal-time distribution  algorithm
     #子模块1:时间分配算法
     tr = (self.tripTime - time)  #真实剩余时间
     vlimEndPoint = trc.getSpeedLimitEndPoint(pos)  #获得限速的终点
     if self.trd.getEmerencyBrakeSpeed(vlimEndPoint) < self.veo:
         ta = 0
     else:
         ta = (self.trd.getEmerencyBrakeSpeed(vlimEndPoint) -
               self.veo) / self.MaxAcc
     trMax = tr - self.trd.getMinRestTime(vlimEndPoint) - ta  #当前点到区间终点的充裕时间
     if trMax < 5:
         trMax = 5
     vbar = (vlimEndPoint - pos) / trMax
     return vbar
 def __init__(self, start, destination, T, dt):
     super().__init__(dt, 0)
     #离线信息
     self.tripStart = start  #旅程起点
     self.tripDestination = destination  #旅程终点
     self.tripTime = T
     self.dv = 0.1  #允许速度误差
     self.MaxAcc = 1.5  #最大牵引加速度
     self.MinAcc = -1.5  #最大制动加速度
     self.dadt = 0.5  #加速度变化率,初始为0.5
     self.trd = trc.TrainAndRoadData()
     self.MinT = self.trd.getMinRestTime(self.tripDestination)
     #在线信息(状态)
     self.pos = self.tripStart  #位置
     self.veo = 0  #当前速度
     self.time = 0  #仿真时间步
     self.vbar = 0  #预测速度
 def __init__(self, start, destination, T, dt):
     super().__init__(dt, 0)
     #离线信息
     self.tripStart = start  #旅程起点
     self.tripDestination = destination  #旅程终点
     self.tripTime = T
     self.dv = 0.1  #允许速度误差
     self.MaxAcc = 1.5  #最大牵引加速度
     self.MinAcc = -1.5  #最大制动加速度
     self.dadt = 0.5  #加速度变化率,初始为0.5
     self.trd = trc.TrainAndRoadData()
     self.MinT = self.trd.getMinRestTime(self.tripDestination)
     #在线信息(状态)
     self.pos = self.tripStart  #位置
     self.veo = 0  #当前速度
     self.time = 0  #仿真时间步
     self.trueAcc = 0  #当前的加速度acc
     self.vbar = 0  #预测速度
     self.last_veo = 0
     self.last_pos = start
     self.last_out = 0
     self.step = 0
     #xgboost训练好的模型
     self.bst = xgb.Booster(model_file='./xgboost_algorithm/xgb.model')
        s = trainState['S']
        t = t + dt
        #统计数据
        vRecord.append(v)
        tRecord.append(t)
        DEList.append(dE / 100)
        SRecord.append(s - startPoint)
        accList.append(cmd_acc)
        vbarList.append(vbar)
        MisError = s - endPoint
    jerk = jerk / t
    varRecAry = np.mat([SRecord, vbarList, vRecord, tRecord, DEList, accList])
    varRec = pds.DataFrame(data=varRecAry.T,
                           columns=['s', 'vbar', 'v', 't', 'p', 'acc'])
    var = [t, Energy, jerk, MisError]
    return var, varRec


stl = 'xgboost'
#stl='PID'
#stl='expert'
var, res = Run(stl)
print('Simulation End')
res.to_csv('./simulation_output/' + stl + 'result.csv')
trc.plotSpeedLimitRoadGrad('relative')
plt.plot(res['s'], res['vbar'])  #画vbar-x
plt.plot(res['s'], res['v'])  #画v-x
plt.show()
plt.plot(res['s'], res['acc'])  #画acc
plt.show()
def TanslationBySimulation(switchPoint, index):
    #模拟列车运行,将工况控制点转换成(s,v,t,u)组合
    dt = 0.2  #时间步长
    startPoint = trc.SLStartPoint[0]
    endPoint = trc.SLStartPoint[-1]
    sl = trc.getRoadspeedLimit(startPoint)
    gd = trc.getRoadGradinet(startPoint)
    nsl = trc.getNextSpeedLimit(startPoint)
    nsld = trc.getSpeedLimitEndPoint(startPoint)
    vList = [0]  #速度
    sList = [startPoint]  #位置
    tList = [0]  #时间
    uList = [1]  #加速度
    gdList = [gd]  #坡度  千分
    slList = [sl]  #路段限速(m/s)
    nslList = [nsl]  #下一限速的值(m/s)
    nsldList = [nsld]  #下一限速的距离(m)
    train = trm.Train_model(startPoint, 0, 0.6, dt)
    PIDC = atoController.PidController(dt, 8, 10, 1)
    trd = trc.TrainAndRoadData()
    t = 0
    accList = [0]
    stateList = [2]
    state = 2
    acc = 0.1
    while True:
        t = t + dt
        laststate = state
        state = trc.getRunState(sList[-1], switchPoint)
        if state == 1 and laststate != 1:
            vbar = vList[-1]
        if state == 2:
            #牵引
            acc = 1
        if state == 1:
            #巡航
            acc = PIDC.Step(vbar, vList[-1])
        elif state == 0:
            #惰行
            acc = 0
        elif state == -1:
            #制动
            acc = -1
        if vList[-1] > trd.getEmerencyBrakeSpeed(sList[-1]):
            acc = -1
        out = train.Step(acc)
        trueAcc = out['acc']
        stateList.append(state)
        accList.append(acc)
        sl = trc.getRoadspeedLimit(out['S'])
        gd = trc.getRoadGradinet(out['S'])
        nsl = trc.getNextSpeedLimit(out['S'])
        nsld = trc.getSpeedLimitEndPoint(out['S'])
        vList.append(out['v'])
        sList.append(out['S'])
        tList.append(t)
        uList.append(acc)
        gdList.append(gd)  #坡度  千分
        slList.append(sl)  #路段限速(m/s)
        nslList.append(nsl)  #下一限速的值(m/s)
        nsldList.append(nsld)  #下一限速的距离(m)
        if out['S'] > endPoint or out['v'] < 0:
            break
    #保存数据
    plt.plot(sList, accList)
    plt.plot(sList, stateList)
    plt.show()
    trc.plotSpeedLimitRoadGrad('abstract')
    plt.plot(sList, vList)
    plt.show()
    plt.plot(tList, vList)
    plt.show()
    print('-------------------%d---------------------' % index)
    dataList = []
    for i in range(0, len(vList)):
        s = sList[i]
        #        t=tList[i]
        sr = endPoint - sList[i]
        t = tList[i]
        tr = tList[-1] - t
        vn = vList[i]
        un = uList[i]
        sr = round(sr, 2)
        tr = round(tr, 2)
        vn = round(vn, 2)
        sl = slList[i]
        gd = gdList[i]
        nsl = nslList[i]
        nsld = nsldList[i]
        line = [s, sr, tList[-1], tr, sl, gd, nsl, nsld, un]
        #如果list是一维的,则是以列的形式来进行添加,如果list是二维的则是以行的形式进行添加的
        dataList.append(line)
    tC = np.mat([sList, vList])
    targetCurve = pds.DataFrame(data=tC.T, columns=['s', 'v'])
    pData = pds.DataFrame(
        data=dataList,
        columns=['s', 'sr', 't', 'tr', 'sl', 'gd', 'nsl', 'nsld', 'un'])
    return pData, targetCurve, tList[-1]
Exemplo n.º 10
0
def run_train():
    total_step = 0
    Max_iteras = 3000
    for episode in range(Max_iteras):
        #训练5000次
        r1_max = 0
        step = 0
        r1 = 0
        pl = []  #位置
        vl = []  #速度
        ul = []  #加速度
        al = []  #动作
        # initial observation
        observation = env.reset()
        #env.bef_print()
        while True:
            # fresh env
            #env.render()
            # RL choose action based on observation

            action = RL.choose_action(observation)
            #强行推上曲线
            pos = observation[0] * env.S
            veo = observation[1] * env.max_speed
            if pos < 100 and veo < env.avg_speed:
                action = 8
            # RL take action and get next observation and reward
            observation_, E, reward, done, action = env.step(
                action)  # action =0-6 最后会被转换到转化为[-0.3, 0.3]

            r1 = r1 * 0.99 + reward

            RL.store_transition(observation, action, reward, observation_)
            if (total_step > 5000 and total_step % 32 == 0):
                RL.learn()
            # swap observation
            observation = observation_
            #			o1 =observation
            if episode % 20 == 0 or episode == Max_iteras - 1:
                pl.append(pos)
                vl.append(veo)
                ul.append(observation[3])
                al.append(action)
            # break while loop when end of this episode
            if done:
                #				env.subFilterFactor(Max_iteras)    #减少平滑因子
                r.append(r1)
                energy.append(E)
                print(observation_[2] * env.T, env.TErrorSum, env.filterFactor,
                      RL.epsilon)
                RL.increase_epsilon()
                tlist.append(observation_[2] * env.T)
                #曲线判定函数,决定是否保存曲线 :旅行距离是否合适,时间是否接近,以episode_speed.csv为 文件名
                if r1 > r1_max and episode > 1500 and episode % 20 == 0:
                    r1_max = r1
                    Curve = np.mat([pl, vl, ul, al])
                    CurveData = pd.DataFrame(
                        data=Curve.T, columns=['s', 'v', 'acc', 'action'])
                    CurveData.to_csv("./Curve/" + str(episode) +
                                     "_CurveData.csv")
                if episode == Max_iteras - 1:
                    print(r1)
                    #					f1 = open('datat.txt', 'r+')
                    #					f1.read()
                    #					print(episode, (step + 5)/5, file=f1)
                    #					f1.close()
                    r.append(r1)
                    print('Episode finished after {} timesteps'.format(
                        (step + 5) / 5))
                break
#			if (5000 > episode >= 4500):
#				 print(o1)
#				 f2 = open('vs.txt', 'r+')
#				 f2.close()
#				 break
            step += 1
            total_step += 1
        #最后打印结果
        print(episode)
        if episode % 20 == 0 or episode == Max_iteras - 1:
            trc.plotSpeedLimitRoadGrad('relative')
            mplt.plot(pl, vl)
            mplt.savefig("./img/" + str(episode) + "v-s.png")
            mplt.show()
            mplt.plot(pl, ul)
            mplt.savefig("./img/" + str(episode) + "u-s.png")
            mplt.show()
            draw_mean(al, str(episode) + "action-s")
#			mplt.savefig("./img/"+str(episode)+"action-s.png")
#			mplt.show()
    return