package rule import ( "math/rand" "time" ) // 滑块组件-初始化并控制概率赋值1的二维数组 func initializeArrayWithProbability(rows, cols int, probability float64) [][]int { array := make([][]int, rows) rand.Seed(time.Now().UnixNano()) // 使用当前时间作为随机数种子 for i := range array { array[i] = make([]int, cols) for j := range array[i] { // 生成0到99之间的随机数,如果小于等于概率*100,则赋值为1 if rand.Intn(100) <= int(probability*100) { array[i][j] = 1 } } } return array } // 滑块组件-统计滑动窗口内值为1的元素数量 func countOnesInWindow(window [][]int) int { count := 0 for _, row := range window { for _, val := range row { if val == 1 { count++ break // 一旦找到一个元素为1,跳出内层循环 } } } return count } // 滑块组件-统计滑动窗口内至少有一个元素为1的行数 func countRowsWithOneInWindow(window [][]int) int { count := 0 for _, row := range window { for _, val := range row { if val == 1 { count++ break // 一旦找到一个元素为1,跳出内层循环 } } } return count } // 滑块组件-统计最高行数以及滑块坐标 func CountMaxRows(modelMatrix [][]int, windowRows, windowCols int) (int, int, int) { maxRowCountWindowCols := 0 maxRowCount := 0 // 统计滑动窗口内值为1的元素数量和至少有一个元素为1的行数 for i := 0; i <= 1440-windowCols; i++ { window := make([][]int, windowRows) for j := range window { window[j] = modelMatrix[j][i : i+windowCols] } //onesCount := countOnesInWindow(window) rowsWithOneCount := countRowsWithOneInWindow(window) if rowsWithOneCount == 0 { continue } if rowsWithOneCount > maxRowCount { maxRowCount = rowsWithOneCount maxRowCountWindowCols = i } //fmt.Printf("从第 %d 列到第 %d 列,值为1的元素数量为:%d,至少有一个元素为1的行数为:%d\n", i, i+windowCols-1, onesCount, rowsWithOneCount) } //fmt.Println("documentNumber: ", info.DocumentNumber) //fmt.Println("maxRowCount: ", maxRowCount) //fmt.Println("maxRowCountWindowCols: ", maxRowCountWindowCols, maxRowCountWindowCols+windowCols-1) return maxRowCount, maxRowCountWindowCols, maxRowCountWindowCols + windowCols - 1 }