---
# System prepended metadata

title: 計算式格式
tags: [實作, Yunikorn]

---

# 計算式格式
- [goTest教學](https://pjchender.dev/golang/note-golang-test/)

## 致瑋
### 數值運算 

- 平均數
    - 給一個浮點數陣列，回傳平均值
```go
func GetMean(objects []float64) float64{
	sum := 0.0
	n := len(objects)
	for i := 0; i < n; i++ {
		sum += objects[i]
	}

	return sum / float64(n)
}
```

- 標準差
    - 給一個浮點數陣列以及平均數，回傳標準差
![image](https://hackmd.io/_uploads/ry5U28jXA.png)


```go
func GetStdDev(objects []float64, meanOfObjects float64) float64{
	n := len(objects)
	sum := 0.0
	for i := 0; i < n; i++ {
		sum += math.Pow(math.Abs(objects[i] - meanOfObjects), 2)
	}

	return math.Sqrt(sum / float64(n))
}
```


- 標準化
    - 給三個數字分別是該物件數值、平均數、標準差，回傳標準化後的z分數
![image](https://hackmd.io/_uploads/S1mv28jXA.png)


```go
func GetStandardizationScore(object, meanOfObjects, stdDevOfObjects float64) float64{
	return (object - meanOfObjects) / stdDevOfObjects
}
```
- test function
```go
func TestGetMean (t *testing.T) {
	objects := [...]float64{-1.0, 0.0, 1.0}
	mean := GetMean(objects[:])
	expect := 0.0
	assert.Equal(t, mean, expect)
}

func TestGetStdDev (t *testing.T) {
	objects := [...]float64{-1.0, 0.0, 1.0}
	mean := GetMean(objects[:])
	stdDev := GetStdDev(objects[:], mean)
	expect := 0.816496580927726
	assert.Equal(t, stdDev, expect)
}

func TestGetStandardizationScore (t *testing.T) {
	object := 0.5
	objects := [...]float64{-1.0, 0.0, 1.0}
	mean := GetMean(objects[:])
	stdDev := GetStdDev(objects[:], mean)
	standardizationScore := GetStandardizationScore(object, mean, stdDev)
	expect := 0.6123724356957945
	assert.Equal(t, standardizationScore, expect)
}
```


## 沛德 
### 節點運算 

- 取最佳解
    - 定義為標準差最小的值和平均數最小的值
    - 給標準差陣列跟平均數陣列，回傳最小的標準差、最小的平均數
![image](https://hackmd.io/_uploads/Hkngc4UXR.png)

```go
func GetBestSol(meansOfObjects, stdDevsOfObjects []float64) (float64, float64){ //(minMean, minStdDev)
   var minMean ,minStdDev float64
   minMean = meansOfObjects[0]
   minStdDev = stdDevsOfObjects[0]

   for i := 1; i < len(meansOfObjects); i++ {
      if meansOfObjects[i] < minMean{
         minMean = meansOfObjects[i]
      }
   }

   for i := 1; i < len(stdDevsOfObjects); i++ {
      if stdDevsOfObjects[i] < minStdDev{
         minStdDev = stdDevsOfObjects[i]
      }
   }
   return minMean,minStdDev
}
```

- 取最差解
    - 定義為標準差最大的值和平均數最大的值 
    - 給標準差陣列跟平均數陣列，回傳最大的標準差、最大的平均數
![image](https://hackmd.io/_uploads/SJZch8j70.png)


```go
func GetWorstSol(meansOfObjects, stdDevsOfObjects []float64) (float64, float64){ //(maxMean, maxStdDev)
 	var maxMean ,maxStdDev float64
   maxMean = meansOfObjects[0]
   maxStdDev = stdDevsOfObjects[0]

   for i := 1; i < len(meansOfObjects); i++ {
      if meansOfObjects[i] > maxMean{
      maxMean = meansOfObjects[i]
      }
   }

   for i := 1; i < len(stdDevsOfObjects); i++ {
      if stdDevsOfObjects[i] > maxStdDev{
      maxStdDev = stdDevsOfObjects[i]
      }   
   }
   return maxMean,maxStdDev
}
```

- 歐式距離計算
    - 給 x 跟 y 兩個陣列，回傳兩者的歐式距離
![image](https://hackmd.io/_uploads/B1_q2Ui7C.png)


```go
func GetEuclideanDistance(x, y []float64) float64{  
   var distance float64
   for i := 0; i < len(x); i++ {
      distance = distance + (x[i]-y[i]) * (x[i]-y[i])
   }
   distance = math.Sqrt(distance)
   return distance
}
```

- RC值(與最佳解貼近度)計算
    - 給與最佳解距離和與最差解距離，回傳 RC值
    - $d_{best}$ 代表與最佳解的距離，$d_{worst}$ 代表與最差解的距離
![image](https://hackmd.io/_uploads/Hkpq28jQC.png)

- test function
```go
func TestGetBestSol(t *testing.T) {
	meansOfObjects := [...]float64{1, 2, 3}
	stdDevsOfObjectsOfObjects := [...]float64{1, 2, 3}
	minMean, minStdDev := GetBestSol(meansOfObjects[:], stdDevsOfObjectsOfObjects[:])
	t.Errorf(fmt.Sprintf("mean: %v\n stdDev: %v", minMean, minStdDev))
}//expect = 1

func TestGetWorstSol(t *testing.T) {
	meansOfObjects := [...]float64{1, 2, 3}
	stdDevsOfObjects := [...]float64{1, 2, 3}
	maxMean, maxStdDev := GetWorstSol(meansOfObjects[:], stdDevsOfObjects[:])
	t.Errorf(fmt.Sprintf("mean: %v\n stdDev: %v", maxMean, maxStdDev))
}//expect = 3

func TestGetEuclideanDistance(t *testing.T) {
	x := [...]float64{4, 5, 6, 7}
	y := [...]float64{1, 2, 3, 4}
	distance := GetEuclideanDistance(x[:], y[:])
	t.Errorf(fmt.Sprintf("distance: %v\n", distance))
}//expect = 6

func TestGetRCVal(t *testing.T) {
	best := 1.0
	worst := 4.0
	rc := GetRCVal(best, worst)
	t.Errorf(fmt.Sprintf("rc: %v\n", rc))
} //expect = 0.8
```