You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

pricing.go 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package providerPricing
  13. import (
  14. "gonum.org/v1/gonum/mat"
  15. "math"
  16. )
  17. func ComputeProfit(task *Task, tasksolution []int, resourcesolution []int, providerList []*Provider) float64 {
  18. var timeexecution int //记录任务的实际最大执行时间
  19. var costSum float64 //该任务在多个云厂商所需支付的成本总价
  20. for i, provider := range providerList {
  21. //如果该厂商分的任务为0,则直接跳过该厂商,循环到下一厂商
  22. if tasksolution[i] == 0 {
  23. continue
  24. }
  25. //先计算下该云厂商的执行时间ddl,并替换任务的最大执行时间,向上取整
  26. t := math.Ceil(float64(tasksolution[i])/float64(resourcesolution[i])) * float64(task.Time)
  27. if int(t) > timeexecution {
  28. timeexecution = int(t)
  29. }
  30. //计算前几份资源多执行任务
  31. forOneMoreTaskNUm := tasksolution[i] % resourcesolution[i]
  32. for j := 0; j < resourcesolution[i]; j++ {
  33. if j < forOneMoreTaskNUm {
  34. t = math.Ceil(float64(tasksolution[i])/float64(resourcesolution[i])) * float64(task.Time)
  35. } else {
  36. t = math.Floor(float64(tasksolution[i])/float64(resourcesolution[i])) * float64(task.Time)
  37. }
  38. //如果这份资源分的的任务数
  39. cost := (provider.CpuCost*task.Cpu + provider.MemCost*task.Mem + provider.DiskCost*task.Disk) * t * (math.Pow(float64(j+1), math.Log2(provider.LearnIndex)))
  40. costSum += cost
  41. }
  42. }
  43. //计算用户的支付价格pay
  44. pay := task.Pr
  45. if timeexecution == task.Time { //没有排队等待,且只有一个副本直接执行或者多个副本完全并行执行
  46. if pay < costSum {
  47. pay = costSum
  48. }
  49. } else if timeexecution >= task.T0 && timeexecution <= task.T1 { //有排队时间或者任务存在串行执行
  50. if task.T1 == task.T0 { //仅有一个副本,时间中有排队时间
  51. e := math.Exp(float64(-task.B) * float64(timeexecution-task.T1))
  52. pay = (1 - 1/(1+e)) * task.Pr
  53. } else { //多个副本
  54. e := math.Exp(float64(-task.B) * float64(timeexecution-task.T1) / float64(task.T1-task.T0))
  55. pay = (1 - 1/(1+e)) * task.Pr
  56. }
  57. if pay < costSum {
  58. pay = costSum
  59. }
  60. } else { //超出用户满意度的完全串行时间
  61. pay = 1 / 2 * task.Pr
  62. if pay < costSum {
  63. pay = costSum
  64. }
  65. }
  66. profitSum := pay - costSum
  67. return profitSum
  68. }
  69. func ComputeHighDegree(task *Task, resourcesolution []int, providerList []*Provider) float64 {
  70. var highDegreeSum float64
  71. // 依次计算每个云厂商的资源可用度
  72. for i, provider := range providerList {
  73. // 定义两个四维向量
  74. // 未来任务资源需求比例
  75. futureDemand := mat.NewVecDense(3, []float64{1, 1, 1})
  76. // 定义假设按此方案分配后的剩余资源可用量,时间虽然有差异,但是先按那个时刻算吧,这里可能还要改一下
  77. nowLeft_cpu := provider.CpuAvail - task.Cpu*float64(resourcesolution[i])
  78. nowLeft_mem := provider.MemAvail - task.Mem*float64(resourcesolution[i])
  79. nowLeft_disk := provider.DiskAvail - task.Disk*float64(resourcesolution[i])
  80. nowLeft := mat.NewVecDense(3, []float64{nowLeft_cpu, nowLeft_mem, nowLeft_disk})
  81. // 使用余弦相似度计算两个比值的相近度
  82. // 计算向量的内积
  83. dot_product := mat.Dot(futureDemand, nowLeft)
  84. // 计算向量的模长
  85. magnitude1 := mat.Norm(futureDemand, 2)
  86. magnitude2 := mat.Norm(nowLeft, 2)
  87. // 计算余弦相似度
  88. //临时处理被除数为0的特殊情况
  89. var cosineSimilarity = 0.0
  90. if magnitude1 != 0 && magnitude2 != 0 {
  91. cosineSimilarity = dot_product / (magnitude1 * magnitude2)
  92. }
  93. highDegreeSum += cosineSimilarity
  94. }
  95. return highDegreeSum / float64(len(providerList))
  96. }
  97. func Back_trace_task(ReplicaNum int, DoneReplicasNum int, providerList []*Provider, staclu int, res *[][]int, sum int) {
  98. //var count int = 0
  99. pnum := len(providerList)
  100. //所有的任务数都已经进行分配
  101. if DoneReplicasNum == ReplicaNum {
  102. var a []int
  103. for i := 0; i < pnum; i++ {
  104. a = append(a, providerList[i].CurReplicas)
  105. }
  106. *res = append(*res, a)
  107. //(*res)[0] = append((*res)[0], a)
  108. //count += 1
  109. return
  110. }
  111. //遍历完所有的云厂商序号
  112. if staclu >= pnum {
  113. return
  114. }
  115. if providerList[staclu].CurReplicas < providerList[staclu].MaxTaskCanRun {
  116. providerList[staclu].CurReplicas += 1
  117. Back_trace_task(ReplicaNum, DoneReplicasNum+1, providerList, staclu, res, sum)
  118. providerList[staclu].CurReplicas -= 1
  119. Back_trace_task(ReplicaNum, DoneReplicasNum, providerList, staclu+1, res, sum)
  120. } else {
  121. Back_trace_task(ReplicaNum, DoneReplicasNum, providerList, staclu+1, res, sum)
  122. }
  123. }
  124. func Back_trace_resource(list []int, i int, path []int, pathlist *[][]int) {
  125. if i == len(list) {
  126. var pathCopy = make([]int, len(path))
  127. copy(pathCopy, path)
  128. *pathlist = append(*pathlist, pathCopy)
  129. return
  130. }
  131. if list[i] == 0 {
  132. path = append(path, 0)
  133. Back_trace_resource(list, i+1, path, pathlist)
  134. path = path[:len(path)-1]
  135. } else {
  136. for j := 1; j < list[i]+1; j++ {
  137. path = append(path, j)
  138. Back_trace_resource(list, i+1, path, pathlist)
  139. path = path[:len(path)-1]
  140. }
  141. }
  142. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.