鱼C论坛

 找回密码
 立即注册
查看: 1921|回复: 1

希望打印的新的列表能够覆盖原来的列表显示,只用看create_environment函数

[复制链接]
发表于 2020-12-26 15:54:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 猪猪虾 于 2020-12-26 15:55 编辑

‘!!!!!!!!!!!
程序虽然长,但是都不用看,看create_environment(第一个定义的函数即可,只有这里是显示用的)
我想打印的列表就是env_list ,每次新生成的列表,覆盖掉原列表显示





  1. import numpy as np
  2. import pandas as pd
  3. import time

  4. def create_environment(state_x,state_y,episode,step_counter,table):
  5.     '''
  6.     就是创建游戏的简单画面,定义游戏的移动等
  7.     '''
  8.     env_list = [['#', '#', '#', '#'] ,
  9.                 ['#', '&', '&', '#'] ,
  10.                 ['#', '&', '@', '#'] ,
  11.                 ['#', '#', '#', '#'] ]
  12.     if state_x == 1000:
  13.         interraction = 'episode %d: total step_step_move: %d'%(episode + 1,step_counter)
  14.         print('\r{}'.format(interraction),end = '')
  15.         time.sleep(2)
  16.         print('\r                 ',end = '')
  17.     else:
  18.         env_list[state_x][state_y] = 'o'
  19.         #interraction = ''.join(env_list)
  20.         #print('\r{}'.format(interraction),end = '')
  21.         print("***************new way******************")
  22.         print("\n")
  23.         for i in range(4):
  24.             print(env_list[i])
  25.             print("\n")
  26.         time.sleep(Fresh_time)

  27. def get_evn_feedback(state_x,state_y,A):
  28.     '''
  29.     就是看环境会给我多少reward以及下一步的状态是什么
  30.     '''
  31.     if A == 'right':
  32.         #判断有没有超出边界
  33.         if state_x >= N_state_x or state_x < 0 or \
  34.            state_y >= N_state_y or state_y < 0:
  35.            R = -1
  36.            if state_x >= N_state_x:
  37.                state_x  =0
  38.            if state_x < 0:
  39.                state_x  = N_state_x-1
  40.            if state_y >= N_state_y:
  41.                state_y  = 0
  42.            if state_y < 0:
  43.                state_y  = N_state_y-1
  44.         
  45.         #踩到陷阱
  46.         elif (state_x,state_y) in [(1,1),(1,2),(2,1)]:
  47.             state_x = state_x
  48.             state_y += 1
  49.             R = -1
  50.         
  51.         #终点
  52.         elif (state_x,state_y)  == (2,2):
  53.             state_x = 1000
  54.             state_y = 1000
  55.             R = 1
  56.         
  57.         #其他情况不给奖励
  58.         else:
  59.             R = 0
  60.             if state_y - 1 <0:
  61.                 state_y = N_state_y-1
  62.             else:
  63.                 state_y -= 1
  64.             state_x = state_x
  65.             
  66.    
  67.     elif A == 'left':
  68.         #判断有没有超出边界
  69.         if state_x >= N_state_x or state_x < 0 or \
  70.            state_y >= N_state_y or state_y < 0:
  71.            R = -1
  72.            if state_x >= N_state_x:
  73.                state_x  =0
  74.            if state_x < 0:
  75.                state_x  = N_state_x-1
  76.            if state_y >= N_state_y:
  77.                state_y  =0
  78.            if state_y < 0:
  79.                state_y  = N_state_y-1
  80.         
  81.         #踩到陷阱
  82.         elif (state_x,state_y) in [(1,1),(1,2),(2,1)]:
  83.             state_x = state_x
  84.             state_y -= 1
  85.             R = -1
  86.         
  87.         #终点
  88.         elif (state_x,state_y)  == (2,2):
  89.             state_x = 1000
  90.             state_y = 1000
  91.             R = 1
  92.         
  93.         #其他情况不给奖励
  94.         else:
  95.             R = 0
  96.             state_x = state_x
  97.             if state_y +1 >= N_state_y:
  98.                 state_y = 0
  99.             else:
  100.                 state_y += 1
  101.             
  102.         
  103.     elif A == 'up':
  104.         #判断有没有超出边界
  105.         if state_x >= N_state_x or state_x < 0 or \
  106.            state_y >= N_state_y or state_y < 0:
  107.            R = -1
  108.            if state_x >= N_state_x:
  109.                state_x  =0
  110.            if state_x < 0:
  111.                state_x  = N_state_x -1
  112.            if state_y >= N_state_y:
  113.                state_y  =0
  114.            if state_y < 0:
  115.                state_y  = N_state_y -1
  116.         
  117.         #踩到陷阱
  118.         elif (state_x,state_y) in [(1,1),(1,2),(2,1)]:
  119.             state_x -= 1
  120.             state_y = state_y
  121.             R = -1
  122.         
  123.         #终点
  124.         elif (state_x,state_y)  == (2,2):
  125.             state_x = 1000
  126.             state_y = 1000
  127.             R = 1
  128.         
  129.         #其他情况不给奖励
  130.         else:
  131.             R = 0
  132.             if state_x - 1 < 0:
  133.                 state_x = N_state_x - 1
  134.             else:
  135.                 state_x -= 1            
  136.             state_y = state_y
  137.             
  138.             
  139.             
  140.             
  141.     else:
  142.             #判断有没有超出边界
  143.             if state_x >= N_state_x or state_x < 0 or \
  144.                state_y >= N_state_y or state_y < 0:
  145.                R = -1
  146.                if state_x >= N_state_x:
  147.                    state_x  =0
  148.                if state_x < 0:
  149.                    state_x  = N_state_x -1
  150.                if state_y >= N_state_y:
  151.                    state_y  =0
  152.                if state_y < 0:
  153.                    state_y  = N_state_y -1
  154.             
  155.             #踩到陷阱
  156.             elif (state_x,state_y) in [(1,1),(1,2),(2,1)]:
  157.                 state_x += 1
  158.                 state_y = state_y
  159.                 R = -1
  160.             
  161.             #终点
  162.             elif (state_x,state_y)  == (2,2):
  163.                 state_x = 1000
  164.                 state_y = 1000
  165.                 R = 1
  166.             #其他情况不给奖励
  167.             else:
  168.                 R = 0
  169.                 if state_x + 1 >= N_state_x:
  170.                     state_x = 0
  171.                 else:
  172.                     state_x += 1
  173.                 state_y = state_y
  174.                
  175.     return state_x,state_y,R

  176. def build_q_table(N_state_x,N_state_y):
  177.     table = np.zeros((N_state_x,N_state_y)) #表格的值都初始化为0            
  178.     return table

  179. #print(build_q_table(N_state,Action))

  180. def choose_action(state_x,state_y,q_table):
  181.     '''
  182.     state = (N_state_x,N_state_y)
  183.     下一步动作的选取要根据当前的状态以及已有的q_table来进行选择
  184.     选择的时候分为两种情况:
  185.     1.在90%的情况下,根据当前的状态以及已有的q_table来选择最优的action
  186.     2.10%的情况下,随机选择一个action
  187.     '''

  188.     score_of_4_dir,score_of_4_direction = get_neibor_q(state_x,state_y,q_table)
  189.    
  190.     #>0.9,也就是产生的随机数是在0.9~1之间,10%的情况下,随机选择下部一的动作
  191.     #全部为0的情况也随机选取下一步的状态
  192.     sign_all_0 = True
  193.     #判断上下左右四个分数是不是全部为0
  194.     for i in range(len(score_of_4_dir)):
  195.         if score_of_4_dir[i] != 0 and score_of_4_dir[i] != -100:
  196.            sign_all_0= False
  197.            break
  198.            
  199.     if np.random.uniform() > Greedy_plicy or sign_all_0 == True :
  200.         #socre = -100的位置表示已经超出了游戏边界,这个方向不能走,三级选择的方向里面需要排除掉这些,
  201.         #再进行随机选择
  202.         action_choice =  Action.copy()
  203.         for i in range(4):  #四个方向
  204.             if score_of_4_dir[i] == -100:
  205.                 action_choice[i] = 'prohibit'
  206.             
  207.         #一直随机挑选,不能选择标记为none的方向
  208.         action_name = ''
  209.         while action_name == 'prohibit' or action_name == '':
  210.             action_name = np.random.choice(action_choice)
  211.                                 
  212.     else:
  213.         #标记为none的地方对应的q表值为-100,不可能是最大值
  214.         
  215.         action_name = Action[score_of_4_dir.index(max(score_of_4_dir))]  #返回state_of_action里面较大值的索引   
  216.     return action_name





  217. def get_neibor_q(state_x,state_y,q_table):
  218.     '''
  219.     获取当前状态的上下左右四个状态的q值以及对应的坐标
  220.     '''
  221.     #当前的点不一定能向四个方向前进,可能到边上了
  222.     #分数在列表里面的存储顺序默认为 上下左右
  223.     position_of_4_direction = [[state_x-1, state_y],\
  224.                                [state_x+1, state_y],\
  225.                                [state_x, state_y-1],\
  226.                                [state_x, state_y+1]]
  227.     score_of_4_dir = []
  228.     for i in range(len(position_of_4_direction)):

  229.         if  position_of_4_direction[i][0] < 0 or position_of_4_direction[i][0] >= N_state_x \
  230.          or position_of_4_direction[i][1] < 0 or position_of_4_direction[i][1] >= N_state_y:
  231.             position_of_4_direction[i][0]  = -100
  232.             position_of_4_direction[i][1]  = -100  #设置成这个值,表示这个方向不能走
  233.             score_of_4_dir.append(-100)
  234.         else:
  235. #            print("position_of_4_direction[{}][0]={}  ".format(i,position_of_4_direction[i][0]))
  236. #            print("\n")
  237. #            print("position_of_4_direction[{}][0]={}".format(i,position_of_4_direction[i][1]))
  238. #            print("\n")
  239.             score_of_4_dir.append(q_table[position_of_4_direction[i][0],position_of_4_direction[i][1]])
  240.     return score_of_4_dir,position_of_4_direction
  241.    
  242.    
  243. def reforcement_learning():
  244.     table = build_q_table(N_state_x,N_state_y)
  245.     for episode in range(max_episodes):
  246.          state_x,state_y = 0,0
  247.       
  248.          step_counter =0
  249.          create_environment(state_x,state_y,episode,step_counter,table)
  250.          
  251.          while state_x != 1000:
  252.              A = choose_action(state_x,state_y,table)
  253.              state_x_,state_y_,R = get_evn_feedback(state_x,state_y,A)
  254.              q_predict = table[state_x,state_y]
  255.             
  256.              if state_x_ != 1000 :
  257.                  score_of_4_dir,position_of_4_direction = get_neibor_q(state_x,state_y,table)
  258.                  position_of_4_direction = get_neibor_q(state_x,state_y,table)
  259.                  reality = R +  discount_factor * max(score_of_4_dir)
  260.              else:
  261.                  reality= R  #discount_factor * max(table.iloc[S_,:])不存在了,已经终结没有下一个状态了
  262.                  state_x,state_y  = 1000,1000
  263.             
  264.              table[state_x,state_y] = table[state_x,state_y] + Learning_rate * (reality - q_predict)
  265.              state_x,state_y = state_x_,state_y_
  266.             
  267.              create_environment(state_x,state_y,episode,step_counter+1,table)
  268.              step_counter += 1
  269.     return table



  270. if __name__== "__main__":
  271.     N_state_x = 4  #状态的种类
  272.     N_state_y = 4
  273.     Action = ['up','down','left','right']
  274.     Greedy_plicy = 0.9   #多少比例择优选择action,其余比例下随机选择action
  275.     Learning_rate = 0.1   #学习率
  276.     discount_factor = 0.9  #未来奖励的衰减值
  277.     max_episodes = 50   #只玩13回合就结束
  278.     Fresh_time = 0.3    #0.3秒显示走一步
  279.     np.random.seed(2)  

  280.     q_table = reforcement_learning()  
  281.     print(q_table)
  282.         
  283.             
  284.    
  285.    
  286.    
  287.    
  288.    
  289.    
复制代码
1.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-27 01:39:26 | 显示全部楼层
不错,支持一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-29 19:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表