From 723ea591e61c1f861a7a0a22ae22d0a05e280b3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E8=8F=AF?= Date: Thu, 9 Mar 2023 09:56:12 +0800 Subject: [PATCH] go init --- bin/main.go | 56 ++++++++++++++++++++++++ go.mod | 3 ++ weight.go | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 bin/main.go create mode 100644 go.mod create mode 100644 weight.go diff --git a/bin/main.go b/bin/main.go new file mode 100644 index 0000000..493fe29 --- /dev/null +++ b/bin/main.go @@ -0,0 +1,56 @@ +package main +import ( + "fmt" + "git.satori.love/gameui/weight" +) + + +func main() { + // 创建一个权重模型用于存储类型A的集合倾向节点(极点)A的权重 + weightModel := weight.NewWeightModel() + // 添加权重 + weightModel.Add("A", 0.1) + weightModel.Add("B", 0.2) + weightModel.Add("C", 0.3) + weightModel.Add("D", 0.4) + // 排序 + weightModel.Weights.Sort() + // 计算权重总和 + for _, v := range weightModel.Weights { + weightModel.Total += v.Value + } + // 获取权重 + fmt.Println(weightModel.Get("A")) + // 获取权重 + fmt.Println(weightModel.GetWeight("A")) + // 获取权重列表 + fmt.Println(weightModel.GetWeightList()) + // 获取权重列表 + fmt.Println(weightModel.GetWeightListByValue()) + // 获取权重列表 + fmt.Println(weightModel.GetWeightListByValueDesc()) + + // 创建一个权重模型用于存储类型B的集合倾向节点(极点)B的权重 + weightModel2 := weight.NewWeightModel() + // 添加权重 + weightModel2.Add("A", 0.1) + weightModel2.Add("B", 0.2) + weightModel2.Add("C", 0.3) + weightModel2.Add("D", 0.4) + // 排序 + weightModel2.Weights.Sort() + // 计算权重总和 + for _, v := range weightModel2.Weights { + weightModel2.Total += v.Value + } + // 获取权重 + fmt.Println(weightModel2.Get("A")) + // 获取权重 + fmt.Println(weightModel2.GetWeight("A")) + // 获取权重列表 + fmt.Println(weightModel2.GetWeightList()) + // 获取权重列表 + fmt.Println(weightModel2.GetWeightListByValue()) + // 获取权重列表 + fmt.Println(weightModel2.GetWeightListByValueDesc()) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..086f89d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.satori.love/gameui/weight + +go 1.18 diff --git a/weight.go b/weight.go new file mode 100644 index 0000000..36f2a38 --- /dev/null +++ b/weight.go @@ -0,0 +1,123 @@ +package weight + +import ( + "sort" +) + +// Weight 权重 +type Weight struct { + // 权重值 + Value float64 + // 权重名称 + Name string +} + +// WeightList 权重列表 +type WeightList []Weight + +// Len 长度 +func (w WeightList) Len() int { + return len(w) +} + +// Less 小于 +func (w WeightList) Less(i, j int) bool { + return w[i].Value < w[j].Value +} + +// Swap 交换 +func (w WeightList) Swap(i, j int) { + w[i], w[j] = w[j], w[i] +} + +// Sort 排序 +func (w WeightList) Sort() { + sort.Sort(w) +} + +// WeightModel 权重模型 +type WeightModel struct { + // 权重列表 + Weights WeightList + // 权重总和 + Total float64 +} + +// NewWeightModel 创建权重模型 +func NewWeightModel() *WeightModel { + return &WeightModel{ + Weights: make(WeightList, 0), + } +} + +// Add 添加权重 +func (w *WeightModel) Add(name string, value float64) { + w.Weights = append(w.Weights, Weight{ + Name: name, + Value: value, + }) + w.Total += value +} + +// Get 获取权重 +func (w *WeightModel) Get(name string) float64 { + for _, weight := range w.Weights { + if weight.Name == name { + return weight.Value + } + } + return 0 +} + +// GetWeight 获取权重 +func (w *WeightModel) GetWeight(name string) float64 { + for _, weight := range w.Weights { + if weight.Name == name { + return weight.Value / w.Total + } + } + return 0 +} + +// GetWeightList 获取权重列表 +func (w *WeightModel) GetWeightList() WeightList { + w.Weights.Sort() + return w.Weights +} + +// GetWeightListByValue 获取权重列表 +func (w *WeightModel) GetWeightListByValue() WeightList { + w.Weights.Sort() + return w.Weights +} + +// GetWeightListByValueDesc 获取权重列表 +func (w *WeightModel) GetWeightListByValueDesc() WeightList { + w.Weights.Sort() + return w.Weights +} + +// GetWeightListByName 获取权重列表 +func (w *WeightModel) GetWeightListByName() WeightList { + w.Weights.Sort() + return w.Weights +} + +// GetWeightListByNameDesc 获取权重列表 +func (w *WeightModel) GetWeightListByNameDesc() WeightList { + w.Weights.Sort() + return w.Weights +} + +// GetWeightListByWeight 获取权重列表 +func (w *WeightModel) GetWeightListByWeight() WeightList { + w.Weights.Sort() + return w.Weights +} + +// GetWeightListByWeightDesc 获取权重列表 +func (w *WeightModel) GetWeightListByWeightDesc() WeightList { + w.Weights.Sort() + return w.Weights +} +