对算法解的模糊/近似检查

发布于 2024-08-17 06:23:22 字数 241 浏览 3 评论 0原文

我们有人在我们拥有的一些超级计算机上运行模拟、测试等代码。更好的是,如果作为构建过程的一部分,我们不仅可以检查代码是否编译,而且输出是否与某些模式匹配,这将表明我们正在获得有意义的结果。

即研究人员可能知道 x 的值必须在某个范围内。如果不是,则代码中出现逻辑错误(假设它可以编译并且没有编译时错误)。

有没有针对此类事情的预先编写的包。代码是用 FORTRAN、C、C++ 等编写的。

任何具体或一般的建议将不胜感激。

We have people who run code for simulations, testing etc. on some supercomputers that we have. What would be nice is, if as part of a build process we can check that not only that the code compiles but that the ouput matches some pattern which will indicate we are getting meaningful results.

i.e. the researcher may know that the value of x must be within some bounds. If not, then a logical error has been made in the code (assuming it compiles and their is no compile time error).

Are there any pre-written packages for this kind of thing. The code is written in FORTRAN, C, C++ etc.

Any specific or general advice would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘羡 2024-08-24 06:23:22

我预计大多数单元测试框架都可以做到这一点;提供一个玩具测试数据集,并以各种不同的方式查看答案是否合理。

I expect most unit testing frameworks could do this; supply a toy test data set and see that the answer is sane in various different ways.

罗罗贝儿 2024-08-24 06:23:22

确保任何计算的结果值(无论是最终值还是中间值)满足某些约束的一个好方法是使用面向对象的编程语言(如 C++),并定义在内部强制执行您要检查的条件的数据类型。然后,您可以使用这些数据类型作为任何计算的返回值,以确保返回值满足所述条件。

让我们看一个简单的例子。假设您在 Airplane 类中有一个成员函数,作为飞行控制系统的一部分,该系统根据乘客数量和飞机当时拥有的燃油量来估计飞机实例的质量。声明 Airplane 类和planeMass() 成员函数的一种方法如下:

class Airplane {
public:

    ...

    int airplaneMass() const; // note the plain int return type

    ...

private:
    ...
};

但是,实现上述方法的更好方法是定义一个可用作函数返回值的类型 AirplaneMass类型而不是 intAirplaneMass 可以在内部确保(在其构造函数和任何重载运算符中)它封装的值满足某些约束。 AirplaneMass 数据类型的示例实现如下:

class AirplaneMass {
public:

    // AirplaneMass constructor
    AirplaneMass(int m) {
        if (m < MIN || m > MAX) {
            // throw exception or log constraint violation
        }

        // if the value of m meets the constraints,
        // assign it to the internal value.
        mass_ = m;
    }

    ...

    /* range checking should also be done in the implementation
       of overloaded operators. For instance, you may want to
       make sure that the resultant of the ++ operation for
       any instance of AirplaneMass also lies within the
       specified constraints. */

private:
    int mass_;
};

此后,您可以重新声明类 Airplane 及其飞机Mass() 成员函数,如下所示:

class Airplane {
public:

    ...

    AirplaneMass airplaneMass() const;
    // note the more specific AirplaneMass return type

    ...

private:
    ...
};

上面将确保planeMass() 返回的值在 MIN 和 MAX 之间。否则,将引发异常,或记录错误情况。

A good way to ensure that the resulting value of any computation (whether final or intermediate) meets certain constraints, is to use an object oriented programming language like C++, and define data-types that internally enforce the conditions that you are checking for. You can then use those data-types as the return value of any computation to ensure that said conditions are met for the value returned.

Let's look at a simple example. Assume that you have a member function inside of an Airplane class as a part of a flight control system that estimates the mass of the airplane instance as a function of the number passengers and the amount of fuel that plane has at that moment. One way to declare the Airplane class and an airplaneMass() member function is the following:

class Airplane {
public:

    ...

    int airplaneMass() const; // note the plain int return type

    ...

private:
    ...
};

However, a better way to implement the above, would be to define a type AirplaneMass that can be used as the function's return type instead of int. AirplaneMass can internally ensure (in it's constructor and any overloaded operators) that the value it encapsulates meets certain constraints. An example implementation of the AirplaneMass datatype could be the following:

class AirplaneMass {
public:

    // AirplaneMass constructor
    AirplaneMass(int m) {
        if (m < MIN || m > MAX) {
            // throw exception or log constraint violation
        }

        // if the value of m meets the constraints,
        // assign it to the internal value.
        mass_ = m;
    }

    ...

    /* range checking should also be done in the implementation
       of overloaded operators. For instance, you may want to
       make sure that the resultant of the ++ operation for
       any instance of AirplaneMass also lies within the
       specified constraints. */

private:
    int mass_;
};

Thereafter, you can redeclare class Airplane and its airplaneMass() member function as follows:

class Airplane {
public:

    ...

    AirplaneMass airplaneMass() const;
    // note the more specific AirplaneMass return type

    ...

private:
    ...
};

The above will ensure that the value returned by airplaneMass() is between MIN and MAX. Otherwise, an exception will be thrown, or the error condition will be logged.

萌面超妹 2024-08-24 06:23:22

这个月我必须这样做才能进行转换。我不知道这是否对您有帮助,但对我来说这似乎是一个非常简单的解决方案。

首先,我定义了一个容忍水平。 (Java-ish 示例代码...)

private static final double TOLERANCE = 0.000000000001D;

然后我定义了一个新的“areEqual”方法,该方法检查两个值之间的差异是否低于容差水平。

private static boolean areEqual(double a, double b) {
    return (abs(a - b) < TOLERANCE);
}

如果我在某处得到 false,则意味着检查可能失败。我可以调整容差,看看这只是精度问题还是确实是一个糟糕的结果。在我的情况下效果很好。

I had to do that for conversions this month. I don't know if that might help you, but it appeared quite simple a solution to me.

First, I defined a tolerance level. (Java-ish example code...)

private static final double TOLERANCE = 0.000000000001D;

Then I defined a new "areEqual" method which checks if the difference between both values is lower than the tolerance level or not.

private static boolean areEqual(double a, double b) {
    return (abs(a - b) < TOLERANCE);
}

If I get a false somewhere, it means the check has probably failed. I can adjust the tolerance to see if it's just a precision problem or really a bad result. Works quite well in my situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文