Reinforcement Learning Q-learning 算法学习-3
- //Q-learning 源码分析。
import java.util.Random;- public class QLearning1
- {
- private static final int Q_SIZE = 6;
- private static final double GAMMA = 0.8;
- private static final int ITERATIONS = 10;
- private static final int INITIAL_STATES[] = new int[] {1, 3, 5, 2, 4, 0};
- private static final int R[][] = new int[][] {{-1, -1, -1, -1, 0, -1},
- {-1, -1, -1, 0, -1, 100},
- {-1, -1, -1, 0, -1, -1},
- {-1, 0, 0, -1, 0, -1},
- {0, -1, -1, 0, -1, 100},
- {-1, 0, -1, -1, 0, 100}};
- private static int q[][] = new int[Q_SIZE][Q_SIZE];
- private static int currentState = 0;
- private static void train()
- {
- initialize();
- // Perform training, starting at all initial states.
- for(int j = 0; j < ITERATIONS; j++)
- {
- for(int i = 0; i < Q_SIZE; i++)
- {
- episode(INITIAL_STATES[i]);
- } // i
- } // j
- System.out.println("Q Matrix values:");
- for(int i = 0; i < Q_SIZE; i++)
- {
- for(int j = 0; j < Q_SIZE; j++)
- {
- System.out.print(q[i][j] + ",\t");
- } // j
- System.out.print("\n");
- } // i
- System.out.print("\n");
- return;
- }
- private static void test()
- {
- // Perform tests, starting at all initial states.
- System.out.println("Shortest routes from initial states:");
- for(int i = 0; i < Q_SIZE; i++)
- {
- currentState = INITIAL_STATES[i];
- int newState = 0;
- do
- {
- newState = maximum(currentState, true);
- System.out.print(currentState + ", ");
- currentState = newState;
- }while(currentState < 5);
- System.out.print("5\n");
- }
- return;
- }
- private static void episode(final int initialState)
- {
- currentState = initialState;
- // Travel from state to state until goal state is reached.
- do
- {
- chooseAnAction();
- }while(currentState == 5);
- // When currentState = 5, Run through the set once more for convergence.
- for(int i = 0; i < Q_SIZE; i++)
- {
- chooseAnAction();
- }
- return;
- }
- private static void chooseAnAction()
- {
- int possibleAction = 0;
- // Randomly choose a possible action connected to the current state.
- possibleAction = getRandomAction(Q_SIZE);
- if(R[currentState][possibleAction] >= 0){
- q[currentState][possibleAction] = reward(possibleAction);
- currentState = possibleAction;
- }
- return;
- }
- private static int getRandomAction(final int upperBound)
- {
- int action = 0;
- boolean choiceIsValid = false;
- // Randomly choose a possible action connected to the current state.
- while(choiceIsValid == false)
- {
- // Get a random value between 0(inclusive) and 6(exclusive).
- action = new Random().nextInt(upperBound);
- if(R[currentState][action] > -1){
- choiceIsValid = true;
- }
- }
- return action;
- }
- private static void initialize()
- {
- for(int i = 0; i < Q_SIZE; i++)
- {
- for(int j = 0; j < Q_SIZE; j++)
- {
- q[i][j] = 0;
- } // j
- } // i
- return;
- }
- private static int maximum(final int State, final boolean ReturnIndexOnly)
- {
- // If ReturnIndexOnly = True, the Q matrix index is returned.
- // If ReturnIndexOnly = False, the Q matrix value is returned.
- int winner = 0;
- boolean foundNewWinner = false;
- boolean done = false;
- while(!done)
- {
- foundNewWinner = false;
- for(int i = 0; i < Q_SIZE; i++)
- {
- if(i != winner){ // Avoid self-comparison.
- if(q[State][i] > q[State][winner]){
- winner = i;
- foundNewWinner = true;
- }
- }
- }
- if(foundNewWinner == false){
- done = true;
- }
- }
- if(ReturnIndexOnly == true){
- return winner;
- }else{
- return q[State][winner];
- }
- }
- private static int reward(final int Action)
- {
- return (int)(R[currentState][Action] + (GAMMA * maximum(Action, false)));
- }
- public static void main(String[] args)
- {
- train();
- test();
- return;
- }
- }
Reinforcement Learning Q-learning 算法学习-3的更多相关文章
- Reinforcement Learning Q-learning 算法学习-2
在阅读了Q-learning 算法学习-1文章之后. 我分析了这个算法的本质. 算法本质个人分析. 1.算法的初始状态是随机的,所以每个初始状态都是随机的,所以每个初始状态出现的概率都一样的.如果训练 ...
- 增强学习(五)----- 时间差分学习(Q learning, Sarsa learning)
接下来我们回顾一下动态规划算法(DP)和蒙特卡罗方法(MC)的特点,对于动态规划算法有如下特性: 需要环境模型,即状态转移概率\(P_{sa}\) 状态值函数的估计是自举的(bootstrapping ...
- 强化学习9-Deep Q Learning
之前讲到Sarsa和Q Learning都不太适合解决大规模问题,为什么呢? 因为传统的强化学习都有一张Q表,这张Q表记录了每个状态下,每个动作的q值,但是现实问题往往极其复杂,其状态非常多,甚至是连 ...
- 机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集
机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集 关键字:FPgrowth.频繁项集.条件FP树.非监督学习作者:米 ...
- 机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析
机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析 关键字:Apriori.关联规则挖掘.频繁项集作者:米仓山下时间:2018 ...
- 机器学习实战(Machine Learning in Action)学习笔记————06.k-均值聚类算法(kMeans)学习笔记
机器学习实战(Machine Learning in Action)学习笔记————06.k-均值聚类算法(kMeans)学习笔记 关键字:k-均值.kMeans.聚类.非监督学习作者:米仓山下时间: ...
- 机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN)
机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN) 关键字:邻近算法(kNN: k Nearest Neighbors).python.源 ...
- 强化学习_Deep Q Learning(DQN)_代码解析
Deep Q Learning 使用gym的CartPole作为环境,使用QDN解决离散动作空间的问题. 一.导入需要的包和定义超参数 import tensorflow as tf import n ...
- 如何用简单例子讲解 Q - learning 的具体过程?
作者:牛阿链接:https://www.zhihu.com/question/26408259/answer/123230350来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- 机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据
机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据 关键字:PCA.主成分分析.降维作者:米仓山下时间:2018-11-15机器学习实战(Ma ...
随机推荐
- tomcat web工程 jar包冲突解决方法
目前在部署工程时,遇到了一个问题,报错信息如下: See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet ...
- directorjs和requirejs和artTemplate模板引擒建立的SPA框架
分为4块:A : index.html壳子. 加载B init-config.js, 加载D header.html模板B : init-config.js 个人信息+路由配置+权限+渲 ...
- 双camera景深计算 (1)
http://www.52rd.com/S_TXT/2016_6/TXT85047.HTM?WebShieldDRSessionVerify=Wz3h6srvu76qRI4MFxK8 前面介绍了双ca ...
- 使用Angularjs开发Web App 视频课程 --麦子学院课程
前往搓这里: http://www.maiziedu.com/group/common/course/3271/ 查看课程搓这里:http://www.maiziedu.com/course/web/ ...
- HTML5 SVG世界地图
在线演示 本地下载
- 20145235李涛《网络对抗》Exp8 Web基础
基础问答 什么是表单 可以收集用户的信息和反馈意见,是网站管理者与浏览者之间沟通的桥梁. 表单包括两个部分:一部分是HTML源代码用于描述表单(例如,域,标签和用户在页面上看见的按钮),另一部分是脚本 ...
- JavaWeb JavaScript
1.JavaScript概述 JavaScript是一种基于对象和事件驱动的脚本语言,原名叫做livescript.W3c组织开发的标准叫ECMAscipt 1.1JavaScript和Java的一些 ...
- iOS 那些年我们遇到的坑
1坑: UITableView的第一个Cell下移
- 一篇文章学会spark-streaming
版权申明:转载请注明出处.文章来源:bigdataer.net 1.什么是spark-streaming? 实际生产中会有许多应用到实时处理的场景,比如:实时监测页面点击,实时监测系统异常,实时监测来 ...
- Spring -- spring 和 hibernate 整合
1.概述, 事务管理, 编程式和说明式事务管理 2. 事务属性 传播行为: 传播行为 意义 PROPAGATION_MANDATORY 该方法必须运行在一个事务中.如果当前事务不存在,将抛出一个异常. ...