对算法解的模糊/近似检查
我们有人在我们拥有的一些超级计算机上运行模拟、测试等代码。更好的是,如果作为构建过程的一部分,我们不仅可以检查代码是否编译,而且输出是否与某些模式匹配,这将表明我们正在获得有意义的结果。
即研究人员可能知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我预计大多数单元测试框架都可以做到这一点;提供一个玩具测试数据集,并以各种不同的方式查看答案是否合理。
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.
确保任何计算的结果值(无论是最终值还是中间值)满足某些约束的一个好方法是使用面向对象的编程语言(如 C++),并定义在内部强制执行您要检查的条件的数据类型。然后,您可以使用这些数据类型作为任何计算的返回值,以确保返回值满足所述条件。
让我们看一个简单的例子。假设您在 Airplane 类中有一个成员函数,作为飞行控制系统的一部分,该系统根据乘客数量和飞机当时拥有的燃油量来估计飞机实例的质量。声明 Airplane 类和planeMass() 成员函数的一种方法如下:
但是,实现上述方法的更好方法是定义一个可用作函数返回值的类型 AirplaneMass类型而不是 int。 AirplaneMass 可以在内部确保(在其构造函数和任何重载运算符中)它封装的值满足某些约束。 AirplaneMass 数据类型的示例实现如下:
此后,您可以重新声明类 Airplane 及其飞机Mass() 成员函数,如下所示:
上面将确保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:
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:
Thereafter, you can redeclare class Airplane and its airplaneMass() member function as follows:
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.
这个月我必须这样做才能进行转换。我不知道这是否对您有帮助,但对我来说这似乎是一个非常简单的解决方案。
首先,我定义了一个容忍水平。 (Java-ish 示例代码...)
然后我定义了一个新的“areEqual”方法,该方法检查两个值之间的差异是否低于容差水平。
如果我在某处得到
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...)
Then I defined a new "areEqual" method which checks if the difference between both values is lower than the tolerance level or not.
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.