//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的更多相关文章

  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. tmux命令

    tmux 新建一个会话,进入tmux tmux new -s 'main' 新建一个会话'main',进入tmux' tmux a -t 'main' 进入tmux会话'main' exit 退出当前 ...

  2. Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration info

    Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the ...

  3. Apache 浏览器访问限制配置

    浏览器访问限制配置 user_agent收入的浏览器中,我们通过百度,谷歌很容易就可以查到相关的一些资料,方便了我们对知识的查找,但在某些特定情况下,我们并不希望有人可以通过某写搜索引擎直接访问到我们 ...

  4. 基于Bootstrap的jQuery登录表单

    在线演示 本地下载

  5. hibernate配置文件的详解

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-configurati ...

  6. tomcat绿色版——运行一闪而过的解决方法

    首先配置好jdk的环境变量 %JAVA_HOME%\bin;注意:一定是英文状态下的分号结尾 %TOMCAT%\bin;注意:一定是英文状态下的分号结尾 service.bat install ser ...

  7. code for 1 - 分治

    2017-08-02 17:23:14 writer:pprp 题意:将n分解为n/2, n%2, n/2三部分,再将n/2分解..得到一个序列只有0和1,给出[l, r]问l到r有几个1 题解:分治 ...

  8. ubuntu 支持中文

    1.cat /usr/share/i18n/SUPPORTED 说明:查看系统支持的字符集,你需要注意的是支持字符集的格式,如对中文会有以下一些显示(我的系统如此,我不知是否普遍) zh_CN.GB1 ...

  9. 从reduce函数说起...

    reduce函数:  方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值, 最终返回的要看函数内部return的内容. 1. 累加器: var ar ...

  10. PHP 404伪装

    <?php function GetCurUrl() { if(!empty($_SERVER["REQUEST_URI"])) { $scriptName = $_SERV ...