理论损失率计算公式:M/M/n/0
golang代码
package main
import (
"math"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
"fmt"
)
//输入lambda 和 miu 和服务台数,计算损失率
func calculatePLoss(Lambda,miu float64,n int) float64{
rou := Lambda/miu
rou_n := math.Pow(rou,float64(n))
fac := factorial(n)
var sum float64
for i := 0; i <= n; i++ {
sum += math.Pow(rou,float64(i))/float64(factorial(i))
}
return (rou_n/float64(fac))/sum
}
//计算阶乘
func factorial(n int) int{
if n == 1|| n == 0{
return 1
}else{
return n*factorial(n-1)
}
}
func main(){
//W自变量
var F int = 1
var W int = 1
var lambda float64 = 2
var miu float64 = 3
var s string = fmt.Sprintf("lambda=%v, miu=%v\n F=1(solid), F=2(dashed), F=3(dotted)",lambda, miu)
p, _ := plot.New()
p.Title.Text = s
p.X.Label.Text = "W"
p.Y.Label.Text = "Loss Rate"
points := plotter.XYs{
{1.0, calculatePLoss(lambda/float64(F),miu,1*W)},
{2.0, calculatePLoss(lambda/float64(F),miu,2*W)},
{3.0, calculatePLoss(lambda/float64(F),miu,3*W)},
{4.0, calculatePLoss(lambda/float64(F),miu,4*W)},
{5.0, calculatePLoss(lambda/float64(F),miu,5*W)},
{6.0, calculatePLoss(lambda/float64(F),miu,6*W)},
{7.0, calculatePLoss(lambda/float64(F),miu,7*W)},
{8.0, calculatePLoss(lambda/float64(F),miu,8*W)},
{9.0, calculatePLoss(lambda/float64(F),miu,9*W)},
{10.0, calculatePLoss(lambda/float64(F),miu,10*W)},
}
F = 2
points2 := plotter.XYs{
{1.0, calculatePLoss(lambda/float64(F),miu,1*W)},
{2.0, calculatePLoss(lambda/float64(F),miu,2*W)},
{3.0, calculatePLoss(lambda/float64(F),miu,3*W)},
{4.0, calculatePLoss(lambda/float64(F),miu,4*W)},
{5.0, calculatePLoss(lambda/float64(F),miu,5*W)},
{6.0, calculatePLoss(lambda/float64(F),miu,6*W)},
{7.0, calculatePLoss(lambda/float64(F),miu,7*W)},
{8.0, calculatePLoss(lambda/float64(F),miu,8*W)},
{9.0, calculatePLoss(lambda/float64(F),miu,9*W)},
{10.0, calculatePLoss(lambda/float64(F),miu,10*W)},
}
F = 3
points3 := plotter.XYs{
{1.0, calculatePLoss(lambda/float64(F),miu,1*W)},
{2.0, calculatePLoss(lambda/float64(F),miu,2*W)},
{3.0, calculatePLoss(lambda/float64(F),miu,3*W)},
{4.0, calculatePLoss(lambda/float64(F),miu,4*W)},
{5.0, calculatePLoss(lambda/float64(F),miu,5*W)},
{6.0, calculatePLoss(lambda/float64(F),miu,6*W)},
{7.0, calculatePLoss(lambda/float64(F),miu,7*W)},
{8.0, calculatePLoss(lambda/float64(F),miu,8*W)},
{9.0, calculatePLoss(lambda/float64(F),miu,9*W)},
{10.0, calculatePLoss(lambda/float64(F),miu,10*W)},
}
plotutil.AddLinePoints(p, points,points2,points3)
p.Save(5*vg.Inch, 5*vg.Inch, "test1.png")
F = 1
for j :=1;j<=5;j++ {
for i := 1; i<=5;i++ {
temp := lambda/float64(i)
fmt.Printf("F = %v, W = %v, LambdaSYS=%v,miuSYS=%v,rou=%v, Loss=%v\n",i,j,temp,miu,temp/miu,calculatePLoss(lambda/float64(i),miu,j))
}
}
}