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 ...
随机推荐
- 在python中打开文件显示没有权限PermissionError: [Errno 13] Permission denied:
不多说了,我犯了低级错误 ,文件路径搞错了
- POJ 3463 Sightseeing (次短路)
题意:求两点之间最短路的数目加上比最短路长度大1的路径数目 分析:可以转化为求最短路和次短路的问题,如果次短路比最短路大1,那么结果就是最短路数目加上次短路数目,否则就不加. 求解次短路的过程也是基于 ...
- javascript Date对象 之 date初始化
javascript Date对象 --> 日期初始化: 总结: 日期初始化的 方式: 1. new Date( yyyy, M(+), d(+), h(+), m(+), s(+) ); 2. ...
- 利用同步网盘搭建个人或团队SVN服务器
这篇文章是以前写的,现在强烈推荐两个站.1.http://git.oschina.com 2.http://www.coding.net. 推荐理由:1.可创建私有项目.2.免费稳定.3.VS2013 ...
- SQL生成一串随机数
SELECT RIGHT (CONVERT(VARCHAR(20),CONVERT(DECIMAL(20,15),rand())),15) AS c_random_number
- HTML5世界地图
在线演示 本地下载
- MapReduce:输入是两个文件,file1代表工厂表,包含工厂名列和地址编号列;file2代表地址表,包含地址名列和地址编号列。要求从输入数据中找出工厂名和地址名的对应关系,输出"工厂名----地址名"表
文件如下: file1: Beijing Red Star Shenzhen Thunder Guangzhou Honda Beijing Rising Guangzhou Development ...
- 最牛技术 1秒启动Linux的窍门
1秒启动Linux可以实现吗?我们知道Linux系统开机并不算快,最少也需要11秒,但是,现在有一个技巧,可以1秒打开linux系统,到底是什么技术这么牛?请看下文详细介绍 尽可能快的启动系统,对于自 ...
- Linux中df命令查询磁盘信息和fdisk命令分区的用法
df - 报告文件系统磁盘空间的使用情况 总览 df [OPTION]... [FILE]... POSIX 选项: [-kP] GNU 选项 (最短方式): [-ahHiklmPv] [-t fs ...
- 谷歌浏览器和火狐浏览器设置跨域和https、http混用 Chrome
谷歌浏览器和火狐浏览器设置跨域和https.http混用 Chrome 添加启动项: 右键点击Chrome快捷方式,在目标一栏后添加启动项 允许跨域: --disable-web-securit ...