允许小违规的函数的argmax

时间:2017-09-12 15:22:24

标签: math

我有一个函数可以返回一组对象中具有最佳评级的对象。 enter image description here

现在我想获得最接近某一点的最佳评级对象。基本上我想允许对公式进行小的违规,这样如果o_1比o_2更接近给定点,即使 enter image description here

enter image description here

如果

enter image description here

对于一些小且固定的$\epsilon$.

目前我在一个程序中用大约5个if语句来实现,但是我想知道是否有一种方法可以用更简洁的方式用数学公式表示它?如果您遇到过这样的问题,您是如何编写代码的?

以下是我所拥有的代码片段,我知道可以对if语句进行一些重构,我想让案例更清晰。是否有一种更优雅的解决方法?

public VisualDevice MostSuitableDevice(GameObject u, GameObject t, List<VisualDevice> D)
{
    float maxSuitability = 0;
    VisualDevice mostSuitableDevice = null;
    float epsMaxSuitability = 0;
    VisualDevice epsMSD = null;


    foreach (VisualDevice d in D)
    {
        float suitability = Rating(d, u, t);
        bool closerDevice = mostSuitableDevice != null ? Vector3.Distance(d.GetLightPosition(), t.transform.position) < Vector3.Distance(mostSuitableDevice.GetLightPosition(), t.transform.position) : false;

        if (suitability > maxSuitability && closerDevice)
        {
            maxSuitability = suitability;
            mostSuitableDevice = d;
        }
        else if (suitability > maxSuitability + epsilon)
        {
            maxSuitability = suitability;
            mostSuitableDevice = d;
        }
        else if (suitability + epsilon > maxSuitability && closerDevice)
        {
            closerDevice = epsMSD != null ? Vector3.Distance(d.GetLightPosition(), t.transform.position) < Vector3.Distance(epsMSD.GetLightPosition(), t.transform.position) : false;
            if (suitability > epsMaxSuitability && closerDevice)
            {
                epsMaxSuitability = suitability;
                epsMSD = d;
            }
            else if (suitability > epsMaxSuitability + epsilon)
            {
                epsMaxSuitability = suitability;
                epsMSD = d;
            }
            else if (suitability + epsilon > epsMaxSuitability && closerDevice)
            {
                epsMaxSuitability = suitability;
                epsMSD = d;
            }
        }            

    }
    bool epsCloser = epsMSD != null && mostSuitableDevice!=null ? Vector3.Distance(epsMSD.GetLightPosition(), t.transform.position) < Vector3.Distance(mostSuitableDevice.GetLightPosition(), t.transform.position) : false;
    if (epsMaxSuitability + epsilon > maxSuitability && epsCloser)
    {
        maxSuitability = epsMaxSuitability;
        mostSuitableDevice= epsMSD;
    }

    return mostSuitableDevice;
}

1 个答案:

答案 0 :(得分:0)

首先找到最高评分rmax。然后使用o从所有对象Rating(o) >= rmax - epsilon中选择最接近给定点的对象。 像那样:

float maxSuitability = D.Max( d => Rating(d, u, t));
float minDistance = float.PositiveInfinity;
VisualDevice mostSuitableDevice = null;
foreach (VisualDevice d in D)
{
    float suitability = Rating(d, u, t);
    if (suitability < maxSuitability - epsilon)
        continue;
    float distance = Vector3.Distance(...
    if (distance < minDistance)
    {
        minDistance = distance;
        mostSuitableDevice = d;
    }
}