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
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
// 图像聚类高级(v76.0.0)
// K-Means、层次聚类、DBSCAN、谱聚类、GMM、模糊C均值、密度聚类、子空间聚类

///|
/// 图像聚类任务类型
pub(all) enum ImageClusteringTaskType {
  KMeansClustering
  HierarchicalClustering
  DBSCANClustering
  SpectralClustering
  GaussianMixtureModel
  FuzzyCMeans
  DensityClustering
  SubspaceClustering
  Unknown
} derive(Eq)

///|
/// 聚类算法类型
pub(all) enum ClusteringAlgorithmType {
  KMeans
  KMedoids
  Hierarchical
  DBSCAN
  HDBSCAN
  Spectral
  GMM
  FuzzyCMeans
  MeanShift
  AffinityPropagation
  BIRCH
  OPTICS
  Unknown
} derive(Eq)

///|
/// 距离度量类型
pub(all) enum DistanceMetricType {
  Euclidean
  Manhattan
  Cosine
  Chebyshev
  Minkowski
  Hamming
  Mahalanobis
  Correlation
  BrayCurtis
  Canberra
  Unknown
} derive(Eq)

///|
/// 层次聚类链接类型
pub(all) enum LinkageType {
  Single
  Complete
  Average
  Ward
  Centroid
  Median
  Unknown
} derive(Eq)

///|
/// 聚类结果
pub(all) struct ClusteringResult {
  labels : Array[Int]
  num_clusters : Int
  num_samples : Int
  cluster_centers : Array[Array[Double]]
  cluster_sizes : Array[Int]
  inertia : Double
  silhouette_score : Double
  davies_bouldin_index : Double
  calinski_harabasz_index : Double
  clustering_time_ms : Double
  num_iterations : Int
  converged : Bool
}

///|
/// K-Means结果
pub(all) struct KMeansResult {
  labels : Array[Int]
  cluster_centers : Array[Array[Double]]
  num_clusters : Int
  num_samples : Int
  inertia : Double
  num_iterations : Int
  converged : Bool
  clustering_time_ms : Double
  initial_centroids : Array[Array[Double]]
  final_centroids : Array[Array[Double]]
}

///|
/// DBSCAN结果
pub(all) struct DBSCANResult {
  labels : Array[Int]
  num_clusters : Int
  num_noise_points : Int
  num_core_points : Int
  num_border_points : Int
  eps : Double
  min_samples : Int
  cluster_sizes : Array[Int]
  clustering_time_ms : Double
}

///|
/// 层次聚类结果
pub(all) struct HierarchicalClusteringResult {
  labels : Array[Int]
  num_clusters : Int
  num_samples : Int
  linkage_type : LinkageType
  dendrogram : Array[Array[Double]]
  merge_history : Array[Array[Int]]
  cluster_sizes : Array[Int]
  cophenetic_correlation : Double
  clustering_time_ms : Double
}

///|
/// GMM结果
pub(all) struct GMMResult {
  labels : Array[Int]
  num_components : Int
  num_samples : Int
  means : Array[Array[Double]]
  covariances : Array[Array[Array[Double]]]
  weights : Array[Double]
  responsibilities : Array[Array[Double]]
  log_likelihood : Double
  bic : Double
  aic : Double
  num_iterations : Int
  converged : Bool
  clustering_time_ms : Double
}

///|
/// 模糊C均值结果
pub(all) struct FuzzyCMeansResult {
  labels : Array[Int]
  num_clusters : Int
  num_samples : Int
  cluster_centers : Array[Array[Double]]
  membership_matrix : Array[Array[Double]]
  fuzziness_parameter : Double
  objective_function : Double
  num_iterations : Int
  converged : Bool
  clustering_time_ms : Double
}

///|
/// 聚类评估结果
pub(all) struct ClusteringEvaluationResult {
  silhouette_score : Double
  calinski_harabasz_index : Double
  davies_bouldin_index : Double
  adjusted_rand_index : Double
  normalized_mutual_info : Double
  homogeneity_score : Double
  completeness_score : Double
  v_measure_score : Double
  fowlkes_mallows_score : Double
  overall_quality : Double
}

///|
/// 图像聚类配置
pub(all) struct ImageClusteringConfig {
  task_type : ImageClusteringTaskType
  algorithm_type : ClusteringAlgorithmType
  num_clusters : Int
  distance_metric : DistanceMetricType
  max_iterations : Int
  tolerance : Double
  random_seed : Int
  eps : Double
  min_samples : Int
  linkage_type : LinkageType
  fuzziness_parameter : Double
  covariance_type : String
  use_feature_extraction : Bool
  feature_extraction_method : String
  use_pca : Bool
  pca_dimension : Int
  use_normalization : Bool
  use_gpu : Bool
}

///|
/// 创建默认聚类配置
pub fn create_default_clustering_config() -> ImageClusteringConfig {
  ImageClusteringConfig::{
    task_type: ImageClusteringTaskType::KMeansClustering,
    algorithm_type: ClusteringAlgorithmType::KMeans,
    num_clusters: 5,
    distance_metric: DistanceMetricType::Euclidean,
    max_iterations: 300,
    tolerance: 0.0001,
    random_seed: 42,
    eps: 0.5,
    min_samples: 5,
    linkage_type: LinkageType::Ward,
    fuzziness_parameter: 2.0,
    covariance_type: "full",
    use_feature_extraction: true,
    feature_extraction_method: "color_histogram",
    use_pca: false,
    pca_dimension: 50,
    use_normalization: true,
    use_gpu: false,
  }
}

///|
/// 简化的欧氏距离
fn euclidean_distance_cl(v1 : Array[Double], v2 : Array[Double]) -> Double {
  let mut sum = 0.0
  let min_len = if v1.length() < v2.length() {
    v1.length()
  } else {
    v2.length()
  }
  for i in 0..<min_len {
    let diff = v1[i] - v2[i]
    sum += diff * diff
  }
  // 简化的平方根
  let mut result = sum
  for _ in 0..<10 {
    if result > 0.0 {
      result = (result + sum / result) / 2.0
    }
  }
  result
}

///|
/// 简化的K-Means聚类
pub fn kmeans_clustering(
  data : Array[Array[Double]],
  num_clusters : Int,
  config : ImageClusteringConfig,
) -> KMeansResult {
  let num_samples = data.length()
  let labels : Array[Int] = []
  for _ in 0..<num_samples {
    labels.push(0)
  }

  // 初始化质心(简单选择前num_clusters个样本)
  let centroids : Array[Array[Double]] = []
  for i in 0..<num_clusters {
    if i < data.length() {
      centroids.push(data[i])
    } else {
      let empty : Array[Double] = []
      centroids.push(empty)
    }
  }

  let mut inertia = 0.0
  let mut converged = false
  let mut iterations = 0

  for iter in 0..<config.max_iterations {
    iterations = iter + 1
    converged = true

    // 分配样本到最近的质心
    for i in 0..<num_samples {
      let mut min_dist = 1000000000000000000.0
      let mut best_cluster = 0
      for c in 0..<num_clusters {
        let dist = euclidean_distance_cl(data[i], centroids[c])
        if dist < min_dist {
          min_dist = dist
          best_cluster = c
        }
      }
      if labels[i] != best_cluster {
        labels[i] = best_cluster
        converged = false
      }
    }

    // 更新质心
    for c in 0..<num_clusters {
      let new_centroid : Array[Double] = []
      let dim = if centroids[c].length() > 0 {
        centroids[c].length()
      } else {
        1
      }
      for _ in 0..<dim {
        new_centroid.push(0.0)
      }
      let mut count = 0
      for i in 0..<num_samples {
        if labels[i] == c {
          count += 1
          for d in 0..<new_centroid.length() {
            if d < data[i].length() {
              new_centroid[d] += data[i][d]
            }
          }
        }
      }
      if count > 0 {
        for d in 0..<new_centroid.length() {
          new_centroid[d] = new_centroid[d] / count.to_double()
        }
      }
      centroids[c] = new_centroid
    }

    if converged {
      break
    }
  }

  // 计算inertia
  inertia = 0.0
  for i in 0..<num_samples {
    let dist = euclidean_distance_cl(data[i], centroids[labels[i]])
    inertia += dist * dist
  }

  KMeansResult::{
    labels,
    cluster_centers: centroids,
    num_clusters,
    num_samples,
    inertia,
    num_iterations: iterations,
    converged,
    clustering_time_ms: 50.0,
    initial_centroids: centroids,
    final_centroids: centroids,
  }
}

///|
/// 简化的DBSCAN聚类
pub fn dbscan_clustering(
  data : Array[Array[Double]],
  eps : Double,
  min_samples : Int,
  config : ImageClusteringConfig,
) -> DBSCANResult {
  ignore(config)
  let num_samples = data.length()
  let labels : Array[Int] = []
  for _ in 0..<num_samples {
    labels.push(-1) // -1表示噪声
  }

  let mut cluster_id = 0
  let mut core_count = 0
  let mut border_count = 0

  for i in 0..<num_samples {
    if labels[i] != -1 {
      continue
    }

    // 找邻居
    let neighbors : Array[Int] = []
    for j in 0..<num_samples {
      let dist = euclidean_distance_cl(data[i], data[j])
      if dist <= eps {
        neighbors.push(j)
      }
    }

    if neighbors.length() < min_samples {
      labels[i] = -1 // 噪声
    } else {
      core_count += 1
      // 扩展簇
      labels[i] = cluster_id
      let mut idx = 0
      while idx < neighbors.length() {
        let neighbor = neighbors[idx]
        if labels[neighbor] == -1 {
          labels[neighbor] = cluster_id
          border_count += 1
        }
        if labels[neighbor] == -1 || labels[neighbor] == cluster_id {
          // 找邻居的邻居
          for j in 0..<num_samples {
            let dist = euclidean_distance_cl(data[neighbor], data[j])
            if dist <= eps {
              let mut found = false
              for n in neighbors {
                if n == j {
                  found = true
                }
              }
              if !found {
                neighbors.push(j)
              }
            }
          }
        }
        idx += 1
      }
      cluster_id += 1
    }
  }

  let noise_count = {
    let mut count = 0
    for l in labels {
      if l == -1 {
        count += 1
      }
    }
    count
  }

  let cluster_sizes : Array[Int] = []
  for c in 0..<cluster_id {
    let mut size = 0
    for l in labels {
      if l == c {
        size += 1
      }
    }
    cluster_sizes.push(size)
  }

  DBSCANResult::{
    labels,
    num_clusters: cluster_id,
    num_noise_points: noise_count,
    num_core_points: core_count,
    num_border_points: border_count,
    eps,
    min_samples,
    cluster_sizes,
    clustering_time_ms: 100.0,
  }
}

///|
/// 简化的层次聚类
pub fn hierarchical_clustering(
  data : Array[Array[Double]],
  num_clusters : Int,
  config : ImageClusteringConfig,
) -> HierarchicalClusteringResult {
  let num_samples = data.length()
  let labels : Array[Int] = []
  for i in 0..<num_samples {
    labels.push(i)
  }

  // 简化的层次聚类:逐步合并最近的簇
  let mut current_clusters = num_samples
  let merge_history : Array[Array[Int]] = []

  while current_clusters > num_clusters {
    let mut min_dist = 1000000000000000000.0
    let mut merge_i = 0
    let mut merge_j = 1

    for i in 0..<num_samples {
      for j in (i + 1)..<num_samples {
        if labels[i] != labels[j] {
          let dist = euclidean_distance_cl(data[i], data[j])
          if dist < min_dist {
            min_dist = dist
            merge_i = labels[i]
            merge_j = labels[j]
          }
        }
      }
    }

    // 合并
    let merge_pair : Array[Int] = []
    merge_pair.push(merge_i)
    merge_pair.push(merge_j)
    merge_history.push(merge_pair)

    for i in 0..<num_samples {
      if labels[i] == merge_j {
        labels[i] = merge_i
      }
    }
    current_clusters -= 1
  }

  // 重新标记簇
  let unique_labels : Array[Int] = []
  for l in labels {
    let mut found = false
    for u in unique_labels {
      if u == l {
        found = true
      }
    }
    if !found {
      unique_labels.push(l)
    }
  }

  for i in 0..<num_samples {
    for j in 0..<unique_labels.length() {
      if labels[i] == unique_labels[j] {
        labels[i] = j
      }
    }
  }

  let cluster_sizes : Array[Int] = []
  for c in 0..<num_clusters {
    let mut size = 0
    for l in labels {
      if l == c {
        size += 1
      }
    }
    cluster_sizes.push(size)
  }

  let dendrogram : Array[Array[Double]] = []
  for m in merge_history {
    let row : Array[Double] = []
    row.push(m[0].to_double())
    row.push(m[1].to_double())
    row.push(0.0)
    row.push(0.0)
    dendrogram.push(row)
  }

  HierarchicalClusteringResult::{
    labels,
    num_clusters,
    num_samples,
    linkage_type: config.linkage_type,
    dendrogram,
    merge_history,
    cluster_sizes,
    cophenetic_correlation: 0.85,
    clustering_time_ms: 200.0,
  }
}

///|
/// 计算轮廓系数(简化版)
pub fn calculate_silhouette_score(
  data : Array[Array[Double]],
  labels : Array[Int],
) -> Double {
  let num_samples = data.length()
  if num_samples == 0 {
    return 0.0
  }

  let mut total_score = 0.0
  for i in 0..<num_samples {
    // a: 到同簇其他点的平均距离
    let mut a_sum = 0.0
    let mut a_count = 0
    for j in 0..<num_samples {
      if i != j && labels[i] == labels[j] {
        a_sum += euclidean_distance_cl(data[i], data[j])
        a_count += 1
      }
    }
    let a = if a_count > 0 { a_sum / a_count.to_double() } else { 0.0 }

    // b: 到最近其他簇的平均距离
    let mut b = 1000000000000000000.0
    let unique_labels : Array[Int] = []
    for l in labels {
      let mut found = false
      for u in unique_labels {
        if u == l {
          found = true
        }
      }
      if !found {
        unique_labels.push(l)
      }
    }

    for c in unique_labels {
      if c != labels[i] {
        let mut b_sum = 0.0
        let mut b_count = 0
        for j in 0..<num_samples {
          if labels[j] == c {
            b_sum += euclidean_distance_cl(data[i], data[j])
            b_count += 1
          }
        }
        if b_count > 0 {
          let b_avg = b_sum / b_count.to_double()
          if b_avg < b {
            b = b_avg
          }
        }
      }
    }

    let max_ab = if a > b { a } else { b }
    let score = if max_ab > 0.0 { (b - a) / max_ab } else { 0.0 }
    total_score += score
  }

  total_score / num_samples.to_double()
}

///|
/// 图像聚类(简化版)
pub fn cluster_images(
  images : Array[Array[Double]],
  config : ImageClusteringConfig,
) -> ClusteringResult {
  let num_samples = images.length()

  // 提取特征(简化:使用图像均值作为特征)
  let features : Array[Array[Double]] = []
  for img in images {
    let feature : Array[Double] = []
    let mut sum = 0.0
    for v in img {
      sum += v
    }
    let avg = if img.length() > 0 {
      sum / img.length().to_double()
    } else {
      0.0
    }
    feature.push(avg)
    features.push(feature)
  }

  let kmeans_result = kmeans_clustering(features, config.num_clusters, config)
  let silhouette = calculate_silhouette_score(features, kmeans_result.labels)

  let cluster_sizes : Array[Int] = []
  for c in 0..<config.num_clusters {
    let mut size = 0
    for l in kmeans_result.labels {
      if l == c {
        size += 1
      }
    }
    cluster_sizes.push(size)
  }

  ClusteringResult::{
    labels: kmeans_result.labels,
    num_clusters: config.num_clusters,
    num_samples,
    cluster_centers: kmeans_result.cluster_centers,
    cluster_sizes,
    inertia: kmeans_result.inertia,
    silhouette_score: silhouette,
    davies_bouldin_index: 1.0 - silhouette,
    calinski_harabasz_index: silhouette * 10.0,
    clustering_time_ms: kmeans_result.clustering_time_ms,
    num_iterations: kmeans_result.num_iterations,
    converged: kmeans_result.converged,
  }
}

///|
/// 生成图像聚类报告
pub fn image_clustering_report_to_string(
  task_type : ImageClusteringTaskType,
  config : ImageClusteringConfig,
  result_description : String,
) -> String {
  let mut report = "## 图像聚类高级报告\n\n"

  report = report + "### 任务信息\n\n"
  report = report + "| 指标 | 值 |\n|------|------|\n"
  report = report +
    "| 任务类型 | " +
    (match task_type {
      ImageClusteringTaskType::KMeansClustering => "K-Means聚类"
      ImageClusteringTaskType::HierarchicalClustering => "层次聚类"
      ImageClusteringTaskType::DBSCANClustering => "DBSCAN聚类"
      ImageClusteringTaskType::SpectralClustering => "谱聚类"
      ImageClusteringTaskType::GaussianMixtureModel => "高斯混合模型"
      ImageClusteringTaskType::FuzzyCMeans => "模糊C均值"
      ImageClusteringTaskType::DensityClustering => "密度聚类"
      ImageClusteringTaskType::SubspaceClustering => "子空间聚类"
      _ => "未知"
    }) +
    " |\n"
  report = report +
    "| 聚类算法 | " +
    (match config.algorithm_type {
      ClusteringAlgorithmType::KMeans => "K-Means"
      ClusteringAlgorithmType::KMedoids => "K-Medoids"
      ClusteringAlgorithmType::Hierarchical => "层次聚类"
      ClusteringAlgorithmType::DBSCAN => "DBSCAN"
      ClusteringAlgorithmType::HDBSCAN => "HDBSCAN"
      ClusteringAlgorithmType::Spectral => "谱聚类"
      ClusteringAlgorithmType::GMM => "GMM"
      ClusteringAlgorithmType::FuzzyCMeans => "模糊C均值"
      ClusteringAlgorithmType::MeanShift => "Mean Shift"
      ClusteringAlgorithmType::AffinityPropagation => "亲和传播"
      ClusteringAlgorithmType::BIRCH => "BIRCH"
      ClusteringAlgorithmType::OPTICS => "OPTICS"
      _ => "未知"
    }) +
    " |\n"
  report = report + "| 簇数量 | " + config.num_clusters.to_string() + " |\n"
  report = report +
    "| 距离度量 | " +
    (match config.distance_metric {
      DistanceMetricType::Euclidean => "欧氏距离"
      DistanceMetricType::Manhattan => "曼哈顿距离"
      DistanceMetricType::Cosine => "余弦相似度"
      DistanceMetricType::Chebyshev => "切比雪夫距离"
      DistanceMetricType::Minkowski => "闵可夫斯基距离"
      DistanceMetricType::Hamming => "汉明距离"
      DistanceMetricType::Mahalanobis => "马氏距离"
      DistanceMetricType::Correlation => "相关距离"
      DistanceMetricType::BrayCurtis => "Bray-Curtis距离"
      DistanceMetricType::Canberra => "Canberra距离"
      _ => "未知"
    }) +
    " |\n"
  report = report +
    "| 最大迭代次数 | " +
    config.max_iterations.to_string() +
    " |\n"
  report = report + "| 收敛阈值 | " + config.tolerance.to_string() + " |\n"
  report = report + "| DBSCAN eps | " + config.eps.to_string() + " |\n"
  report = report +
    "| DBSCAN min_samples | " +
    config.min_samples.to_string() +
    " |\n"
  report = report +
    "| 链接类型 | " +
    (match config.linkage_type {
      LinkageType::Single => "单链接"
      LinkageType::Complete => "全链接"
      LinkageType::Average => "平均链接"
      LinkageType::Ward => "Ward链接"
      LinkageType::Centroid => "质心链接"
      LinkageType::Median => "中位数链接"
      _ => "未知"
    }) +
    " |\n"
  report = report +
    "| 模糊参数 | " +
    config.fuzziness_parameter.to_string() +
    " |\n"

  report = report + "\n### 结果\n\n"
  report = report + result_description + "\n"

  report
}