zhangzengfei
2024-09-28 b1d7efd8c4ab9c4bf56f62e636a358a5182c09bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
}