我的算法的准确性在数字较小时是完美的,而在数字较大时会下降,但并非总是如此

The accuracy of my algorithm is perfect with small numbers and drop when numbers are larger, but not always

有一个游戏,我必须在其中编写 AI 并与其他程序员竞争。我正在玩那个游戏。它包含一场比赛,您的 pod 必须按顺序通过一些检查站。

您有一些要在算法中使用的输入参数:

nextCheckpoint (x,y) -> nX,nY
lastCheckpoint (x,y) -> lnX,lnY
currentposition (x,y) -> x,y

代码的输出是方向和推力。

int pod::errorX(){
    int R[2];
    int X1;
    float b;
    int DeltaX;
    //Vector of the line between Checkpoints
    R[0] = nX-lnX;
    R[1] = nY-lnY;
    //calculation of the distance in the X axis between the ship and the line
    b =(R[0]*(lnX-x) + R[1]*(lnY-y))/((-pow(R[0],2))-(pow(R[1],2)));
    X1 = lnY + b*R[0];
    DeltaX = x-X1;
    cerr<<"Dx: "<<DeltaX<<endl;
    return DeltaX;

}

我也有等效的 pod::errorY()

最后:

DirectionX = nX-errorX();
DirectionY = nY-errorY();

这是我正在做的一些照片:

the theory of what should happen

我想这样做是因为大多数时候惯性使我的船绕检查站运行,这会消耗大量时间并使我失去比赛。

好吧,现在让我们面对这个问题,它有时有效,而其他人甚至不接近。

当输入较小时。 喜欢

nX= 2;
nY= 2;
lnX=0;
lnY=0;
x=0;
y=0;

输出为:

directionX = 3
directionY = 1

但是当它们变大时:

nX= 6553;
nY= 7817;
lnX=13044;
lnY=1908;
x=13921;
y=3530;

输出:

DirectionX=6940;
DirectionY=1907;

我不知道发生了什么。也许是因为 b 是小数?有没有我没看到的错误?

也许这段代码更适合测试,因为你不需要游戏:

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int main()
{

    int lnX = 13044;
    int lnY = 7817;
    int nX = 13044;
    int nY = 1908;

    int x = 13921;
    int y = 3530;

    int R[2]={};

    //Vector de la recta entre Checkpoints
    R[0] = nX-lnX;
    R[1] = nY-lnY;
    //puro algebra, para calcular el desfase de X
    int X1,DeltaX;
    long double b;
    b =(R[0]*(lnX-x) + R[1]*(lnY-y))/((-pow(R[0],2))-(pow(R[1],2)));
    X1 = lnY + b*R[0];
    DeltaX = x-X1;
    //desfase de y;
    int Y1,DeltaY;
    Y1 = lnY+b*R[1];
    DeltaY = y-Y1;

    //dirección
    int DirectionX;
    int DirectionY;

    DirectionX = nX-DeltaX;
    DirectionY = nY-DeltaY;
    cout<<X1<<" "<<Y1<<endl;
    cout<<DeltaX<<" "<<DeltaY<<endl;
    cout<<b<<" "<<DirectionX<< " "<<DirectionY<<endl;

这条线对吗?: X1 = lnY + b*R[0]; 或者应该是 X1 = lnX + b*R[0];