2017-09-12 90 views
0

我有一個函數返回一組對象中具有最佳評分的對象。 enter image description here功能的argmax允許小違規

現在我想獲得最接近某一點的最佳評級的對象。基本上我想允許小違反公式,這樣如果O-1比0-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; 
} 
+0

什麼是「親密度」的措施? – MBo

+0

您的問題並不清楚,正如前面的評論所暗示的。請出示你的「約5條if語句」來澄清這個問題,並表明你已經在這個問題上做了大量的工作。關於「最接近某一點」的更多解釋也會有所幫助。 –

+0

@MBo只是對目標對象/位置的永久性失去 – Girauder

回答

0

首先找到最高評分rmax。然後從所有對象oRating(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; 
    } 
} 
+0

我想我正是在做這件事,只是以一種複雜的方式。有任何關於如何改進代碼的建議? – Girauder

+0

@Girauder看到我的編輯 – Henrik

+0

我一開始並不完全相信它,但我越想它,它就越有意義。我得到的結果與我之前的結果略有不同,但很可能是因爲我之前的結果並不完全正確。你看到有什麼明顯的區別?我會再測試一下你的解決方案,然後接受你的回答 – Girauder