1. //Q-learning 源码分析。
    import java.util.Random;
  2.  
  3. public class QLearning1
  4. {
  5. private static final int Q_SIZE = 6;
  6. private static final double GAMMA = 0.8;
  7. private static final int ITERATIONS = 10;
  8. private static final int INITIAL_STATES[] = new int[] {1, 3, 5, 2, 4, 0};
  9.  
  10. private static final int R[][] = new int[][] {{-1, -1, -1, -1, 0, -1},
  11. {-1, -1, -1, 0, -1, 100},
  12. {-1, -1, -1, 0, -1, -1},
  13. {-1, 0, 0, -1, 0, -1},
  14. {0, -1, -1, 0, -1, 100},
  15. {-1, 0, -1, -1, 0, 100}};
  16.  
  17. private static int q[][] = new int[Q_SIZE][Q_SIZE];
  18. private static int currentState = 0;
  19.  
  20. private static void train()
  21. {
  22. initialize();
  23.  
  24. // Perform training, starting at all initial states.
  25. for(int j = 0; j < ITERATIONS; j++)
  26. {
  27. for(int i = 0; i < Q_SIZE; i++)
  28. {
  29. episode(INITIAL_STATES[i]);
  30. } // i
  31. } // j
  32.  
  33. System.out.println("Q Matrix values:");
  34. for(int i = 0; i < Q_SIZE; i++)
  35. {
  36. for(int j = 0; j < Q_SIZE; j++)
  37. {
  38. System.out.print(q[i][j] + ",\t");
  39. } // j
  40. System.out.print("\n");
  41. } // i
  42. System.out.print("\n");
  43.  
  44. return;
  45. }
  46.  
  47. private static void test()
  48. {
  49. // Perform tests, starting at all initial states.
  50. System.out.println("Shortest routes from initial states:");
  51. for(int i = 0; i < Q_SIZE; i++)
  52. {
  53. currentState = INITIAL_STATES[i];
  54. int newState = 0;
  55. do
  56. {
  57. newState = maximum(currentState, true);
  58. System.out.print(currentState + ", ");
  59. currentState = newState;
  60. }while(currentState < 5);
  61. System.out.print("5\n");
  62. }
  63.  
  64. return;
  65. }
  66.  
  67. private static void episode(final int initialState)
  68. {
  69. currentState = initialState;
  70.  
  71. // Travel from state to state until goal state is reached.
  72. do
  73. {
  74. chooseAnAction();
  75. }while(currentState == 5);
  76.  
  77. // When currentState = 5, Run through the set once more for convergence.
  78. for(int i = 0; i < Q_SIZE; i++)
  79. {
  80. chooseAnAction();
  81. }
  82. return;
  83. }
  84.  
  85. private static void chooseAnAction()
  86. {
  87. int possibleAction = 0;
  88.  
  89. // Randomly choose a possible action connected to the current state.
  90. possibleAction = getRandomAction(Q_SIZE);
  91.  
  92. if(R[currentState][possibleAction] >= 0){
  93. q[currentState][possibleAction] = reward(possibleAction);
  94. currentState = possibleAction;
  95. }
  96. return;
  97. }
  98.  
  99. private static int getRandomAction(final int upperBound)
  100. {
  101. int action = 0;
  102. boolean choiceIsValid = false;
  103.  
  104. // Randomly choose a possible action connected to the current state.
  105. while(choiceIsValid == false)
  106. {
  107. // Get a random value between 0(inclusive) and 6(exclusive).
  108. action = new Random().nextInt(upperBound);
  109. if(R[currentState][action] > -1){
  110. choiceIsValid = true;
  111. }
  112. }
  113.  
  114. return action;
  115. }
  116.  
  117. private static void initialize()
  118. {
  119. for(int i = 0; i < Q_SIZE; i++)
  120. {
  121. for(int j = 0; j < Q_SIZE; j++)
  122. {
  123. q[i][j] = 0;
  124. } // j
  125. } // i
  126. return;
  127. }
  128.  
  129. private static int maximum(final int State, final boolean ReturnIndexOnly)
  130. {
  131. // If ReturnIndexOnly = True, the Q matrix index is returned.
  132. // If ReturnIndexOnly = False, the Q matrix value is returned.
  133. int winner = 0;
  134. boolean foundNewWinner = false;
  135. boolean done = false;
  136.  
  137. while(!done)
  138. {
  139. foundNewWinner = false;
  140. for(int i = 0; i < Q_SIZE; i++)
  141. {
  142. if(i != winner){ // Avoid self-comparison.
  143. if(q[State][i] > q[State][winner]){
  144. winner = i;
  145. foundNewWinner = true;
  146. }
  147. }
  148. }
  149.  
  150. if(foundNewWinner == false){
  151. done = true;
  152. }
  153. }
  154.  
  155. if(ReturnIndexOnly == true){
  156. return winner;
  157. }else{
  158. return q[State][winner];
  159. }
  160. }
  161.  
  162. private static int reward(final int Action)
  163. {
  164. return (int)(R[currentState][Action] + (GAMMA * maximum(Action, false)));
  165. }
  166.  
  167. public static void main(String[] args)
  168. {
  169. train();
  170. test();
  171. return;
  172. }
  173.  
  174. }

Reinforcement Learning Q-learning 算法学习-3的更多相关文章

  1. Reinforcement Learning Q-learning 算法学习-2

    在阅读了Q-learning 算法学习-1文章之后. 我分析了这个算法的本质. 算法本质个人分析. 1.算法的初始状态是随机的,所以每个初始状态都是随机的,所以每个初始状态出现的概率都一样的.如果训练 ...

  2. 增强学习(五)----- 时间差分学习(Q learning, Sarsa learning)

    接下来我们回顾一下动态规划算法(DP)和蒙特卡罗方法(MC)的特点,对于动态规划算法有如下特性: 需要环境模型,即状态转移概率\(P_{sa}\) 状态值函数的估计是自举的(bootstrapping ...

  3. 强化学习9-Deep Q Learning

    之前讲到Sarsa和Q Learning都不太适合解决大规模问题,为什么呢? 因为传统的强化学习都有一张Q表,这张Q表记录了每个状态下,每个动作的q值,但是现实问题往往极其复杂,其状态非常多,甚至是连 ...

  4. 机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集

    机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集 关键字:FPgrowth.频繁项集.条件FP树.非监督学习作者:米 ...

  5. 机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析

    机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析 关键字:Apriori.关联规则挖掘.频繁项集作者:米仓山下时间:2018 ...

  6. 机器学习实战(Machine Learning in Action)学习笔记————06.k-均值聚类算法(kMeans)学习笔记

    机器学习实战(Machine Learning in Action)学习笔记————06.k-均值聚类算法(kMeans)学习笔记 关键字:k-均值.kMeans.聚类.非监督学习作者:米仓山下时间: ...

  7. 机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN)

    机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN) 关键字:邻近算法(kNN: k Nearest Neighbors).python.源 ...

  8. 强化学习_Deep Q Learning(DQN)_代码解析

    Deep Q Learning 使用gym的CartPole作为环境,使用QDN解决离散动作空间的问题. 一.导入需要的包和定义超参数 import tensorflow as tf import n ...

  9. 如何用简单例子讲解 Q - learning 的具体过程?

    作者:牛阿链接:https://www.zhihu.com/question/26408259/answer/123230350来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  10. 机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据

    机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据 关键字:PCA.主成分分析.降维作者:米仓山下时间:2018-11-15机器学习实战(Ma ...

随机推荐

  1. tomcat web工程 jar包冲突解决方法

    目前在部署工程时,遇到了一个问题,报错信息如下: See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet ...

  2. directorjs和requirejs和artTemplate模板引擒建立的SPA框架

    分为4块:A : index.html壳子.    加载B  init-config.js,   加载D  header.html模板B : init-config.js 个人信息+路由配置+权限+渲 ...

  3. 双camera景深计算 (1)

    http://www.52rd.com/S_TXT/2016_6/TXT85047.HTM?WebShieldDRSessionVerify=Wz3h6srvu76qRI4MFxK8 前面介绍了双ca ...

  4. 使用Angularjs开发Web App 视频课程 --麦子学院课程

    前往搓这里: http://www.maiziedu.com/group/common/course/3271/ 查看课程搓这里:http://www.maiziedu.com/course/web/ ...

  5. HTML5 SVG世界地图

    在线演示 本地下载

  6. 20145235李涛《网络对抗》Exp8 Web基础

    基础问答 什么是表单 可以收集用户的信息和反馈意见,是网站管理者与浏览者之间沟通的桥梁. 表单包括两个部分:一部分是HTML源代码用于描述表单(例如,域,标签和用户在页面上看见的按钮),另一部分是脚本 ...

  7. JavaWeb JavaScript

    1.JavaScript概述 JavaScript是一种基于对象和事件驱动的脚本语言,原名叫做livescript.W3c组织开发的标准叫ECMAscipt 1.1JavaScript和Java的一些 ...

  8. iOS 那些年我们遇到的坑

    1坑: UITableView的第一个Cell下移

  9. 一篇文章学会spark-streaming

    版权申明:转载请注明出处.文章来源:bigdataer.net 1.什么是spark-streaming? 实际生产中会有许多应用到实时处理的场景,比如:实时监测页面点击,实时监测系统异常,实时监测来 ...

  10. Spring -- spring 和 hibernate 整合

    1.概述, 事务管理, 编程式和说明式事务管理 2. 事务属性 传播行为: 传播行为 意义 PROPAGATION_MANDATORY 该方法必须运行在一个事务中.如果当前事务不存在,将抛出一个异常. ...