【问题标题】:Get possible output range of equation获取方程的可能输出范围
【发布时间】:2017-11-28 03:22:44
【问题描述】:

我想问一下.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、(e-d)、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# 之外的解决方案的工具,请告诉我。

【问题讨论】:

  • 我不明白你的问题和你的代码。但我相信你可以使用List&lt;int&gt;array
  • @MohitShrivastava 假设我想知道具有许多参数的方程的最小和最大输出值。有没有比使用许多嵌套循环更好的方法?
  • 是的 .. 如果将输入放入列表或数组中,您可以使用 LINQ ... 而不是嵌套循环 ..。相信我,它会是一两个班轮。
  • 如果没有exception,比如除以0,那么可能的最小值是Single.MinValue,可能的最大值是Single.MaxValue
  • 谢谢,伙计们。我现在要浏览一下。

标签: c# .net range equation


【解决方案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

但是请注意,如果存在任何算法,那么获取任意函数的最小值和最大值将是一个相当大的挑战

【讨论】:

    猜你喜欢
    • 2018-03-26
    • 2016-01-09
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多