java - 离散化困境

标签 java math reinforcement-learning calc

我目前正在研究著名的Mountain Car problem来自强化学习。这个问题具有连续性,这意味着我有两个变量:一个位置 - 范围从 -1.2 到 0.5,速度 - 范围从 -0.07 到 0.07。我有 3 种可能的 Action - 反向加速、前进加速和空档, Action 会导致在适当方向上改变位置。由于加速度的计算方式,我的位置变量是连续的,这意味着我无法使用查找表,因此我尝试将位置-速度轴划分为矩形扇区,将位置划分为宽度为 0.05 的桶,将速度划分为长度为 0.005 的桶,为每个扇区分配一个索引,我是这样做的:

public int discretiseObservation(Observation observation) {
    double position = observation.getDouble(0) ;
    double velocity = observation.getDouble(1);

    boolean positionNegativeFlag = position < 0;
    boolean velocityNegativeFlag = velocity < 0;

    double absolutePosition = Math.abs(position);
    double absoluteVelocity = Math.abs(velocity);

    double discretePosition = Math.floor(absolutePosition / 0.05);
    double discreteVelocity = Math.floor(absoluteVelocity / 0.005);

    if(velocityNegativeFlag) {
        discreteVelocity += 14;
    }

    if(positionNegativeFlag) {
        discretePosition += 10;
    }

    return (int)discretePosition * 28 + (int)discreteVelocity;
}

但是这种方案会导致某些扇区具有相同的索引号。您知道如何离散这两个连续变量吗?

更新:抱歉忘记提及,当位置或速度超过最大值或最小值时,我将其设置回最大值或最小值

最佳答案

所有这些符号检查让事情变得过于复杂了。另外,您应该避免使用魔术常量 - 给它们指定有意义的名称。离散化代码应如下所示:

double normalize(double value, double min, double max) {
    return (value - min) / (max - min);
}

int clamp(int value, int min, int max) {
    if (value < min) value = min;
    if (value > max) value = max;
    return value;
}

int discretize(double value, double min, double max, int binCount) {
    int discreteValue = (int) (binCount * normalize(value, min, max));
    return clamp(discreteValue, 0, binCount - 1);
}

public int discretizeObservation(Observation observation ) {
    int position = discretize(observation.getDouble(0), minPosition, maxPosition, positionBinCount);
    int velocity = discretize(observation.getDouble(1), minVelocity, maxVelocity, velocityBinCount);
    return position * velocityBinCount + velocity;
}

关于java - 离散化困境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10793034/

相关文章:

java - mahout Recommende Evaluator - 测试部分数据

c - C 中的简单减法代码没有答案

java - 数学表达式到java方法?

neural-network - 神经网络真的是废弃软件吗?

python - 提供了断言错误 : The algorithm only supports <class 'gym.spaces.box.Box' > as action spaces but Box(-1. 0, 1.0, (3,), float32)

python - CartPole 的 Deep Q 分数停留在 9 分

java - 不使用 while(true) 从文件反序列化多个对象

java - Web 应用程序中的 OpenCV

java - 使用 slf4j-log4j12 使用 Log4j 设置配置文件

algorithm - 程序可以用来简化代数表达式吗?