我想询问.Net中是否有内置函数来获取等式的最小和最大输出。例如:
float a,b,c,d,e,f,g;
float result() {
return a*b/c + d/(e-d) + f/g;
} //how to know the possible min and max value of result()?
我知道域(a,b,c,d,e,f,g的最小值和最大值;其中c,(ed),g不是0),如何从result()获取输出范围? 到目前为止,我尝试手动运行原始嵌套循环,如: figure: Output range of a function(< - 请看图)。 但我想要的是更快的计算(也许是内置函数?)。 在该图像中生成结果的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TEST {
class Program {
static void Main(string[] args) {
ThreatRange();
Console.ReadLine();
}
static void ThreatRange() {
int a0 = 1, a1 = 10; // I know the min and max value for each variable
int b0 = 1, b1 = 10;
int c0 = 1, c1 = 10;
int d0 = 1, d1 = 10;
int e0 = 1, e1 = 10;
int f0 = 1, f1 = 10;
int g0 = 1, g1 = 10;
float threat = 0, threatmin = 9999, threatmax = 0;
for (int a = a0; a <= a1; a++) {
for (int b = b0; b <= b1; b++) {
for (int c = c0; c <= c1; c++) {
for (int d = d0; d <= d1; d++) {
for (int e = e0; e <= e1; e++) {
for (int f = f0; f <= f1; f++) {
for (int g = g0; g <= g1; g++) {
threat = a * b / (float)c + d / (float)Math.Max(1, Math.Abs(e - d)) + (d / (float)Math.Max(1, Math.Abs(e - d))) * (f / g);
// I want to know the min and max output from the equation above
if (threat < threatmin) threatmin = threat;
if (threat > threatmax) threatmax = threat;
Console.WriteLine(a + "," + b + "," + c + "," + d + "," + e + "," + f + "," + g);
Console.WriteLine("Min: " + threatmin + ". Max: " + threatmax);
}
}
}
}
}
}
}
}
}
}
请原谅我,如果我问错误的关键词,我不是母语为英语,我的数学也不错。 :( 如果你知道任何工具可以给我一个c#以外的解决方案,请告诉我。
答案 0 :(得分:1)
我们追随以下的极端值(最小值和最大值)。
a*b/c + d/(e-d) + f/g;
在这种情况下,如果获得每个部分的局部最大值,那么我们可以获得全局最大值。既然你说你分别知道a,b,c,d,e,f,g的范围。
Max(a*b/c) + Max(d/(e-d)) + Max(f/g);
因此
A = Max(a*b/c) = Max(a) * Max(b) / Min(c)
B = Max(d/(e-d)) = Max(d) / (Max(e) - Min(d))
C = Max(f/g) = Max(f) / Max(g)
X = Min(a*b/c) = Min(a) * Min(b) / Max(c)
Y = Min(d/(e-d)) = Min(d) / (Min(e) - Max(d))
Z = Min(f/g) = Min(f) / Min(g)
因此
Max = A + B + C
Min = X + Y + Z
但是请注意,如果存在任何算法,那么获取任意函数的最小值和最大值将是一个很大的挑战