我有一個函數返回一組對象中具有最佳評分的對象。 功能的argmax允許小違規
現在我想獲得最接近某一點的最佳評級的對象。基本上我想允許小違反公式,這樣如果O-1比0-2即使
如果
目前,我在一個程序中實現了約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;
}
什麼是「親密度」的措施? – MBo
您的問題並不清楚,正如前面的評論所暗示的。請出示你的「約5條if語句」來澄清這個問題,並表明你已經在這個問題上做了大量的工作。關於「最接近某一點」的更多解釋也會有所幫助。 –
@MBo只是對目標對象/位置的永久性失去 – Girauder