给定平均向量的相似性矩阵,优化向量的重量

发布于 2025-01-21 10:18:11 字数 2082 浏览 1 评论 0 原文

我想解决优化问题,以搜索矢量组的最佳权重。您想给出一些有关如何解决R的建议吗?非常感谢。

问题如下。

给定有N组,我们知道他们的相似性矩阵在这些n组中。 s的尺寸为n*n。

在每个组中,都有k矢量

我们可以根据这些k矢量拟合平均向量。例如,平均矢量

基于每个组中的这些Avearge向量,我们可以计算相关性在这些Avearge矢量中。

对象是最小化相关矩阵和已知相似性矩阵S之间的差异。

I want to solve the optimazation problem to search best weights for groups of vectors. Would you like to give some suggestions about how to solve it by R? Thanks very much.

The problem is as follows.

Given there are N groups, we know their similarity matrix among these N groups. The dimension of S is N*N.

In each group, there are K vectors . There are M elements in each vector which value is 0 or 1. .

we can fit an average vector based on these K vectors. For example, average vector

Based on these avearge vectors in each group, we could calculate the correlation among these avearge vectors.

The object is to minimize the differene between correlation matrix C and known similarity matrix S.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏大泽ㄣ 2025-01-28 10:18:11

BeaCuse您没有提供任何我会生成随机的数据并演示您可以解决问题的方法。

相似性矩阵

N <- 6

S <- matrix(runif(N^2, -1, 1), ncol = N, nrow = N)
similarity_matrix <- (S + t(S)) / 2

n 是组数。相似性矩阵的每个值介于-1和1之间,矩阵都是对称的(您希望将其与协方差矩阵进行比较是有意义的)。

组向量

M <- 10
K <- 8

group_vectors <- replicate(N, replicate(K, sample(c(0, 1), M, TRUE)), FALSE)

m 是向量的维度, k 是每个组中的二进制向量的数量。

健身函数

fitness <- function(W, group_vectors, similarity_matrix){
  
  W <- as.data.frame(matrix(W, nrow = K, ncol = N))
  
  SS <- cov(
    mapply(function(x,y)  rowSums(sweep(x, 2, y, "*")), group_vectors, W)
  )
  
  sum(abs(SS - similarity_matrix))
}

适应性为给定权重计算所述协方差矩阵及其距离 samelity_matrix 的距离。

差异进化方法

res <- DEoptim::DEoptim(
  fn = fitness, 
  lower = rep(-1, K*N), 
  upper = rep(1, K*N),
  group_vectors = group_vectors,
  similarity_matrix = similarity_matrix,
  control = DEoptim::DEoptim.control(VTR = 0, itermax = 1000, trace = 50, NP = 100)
)

W <- matrix(res$optim$bestmem, nrow = K, ncol = N)

遗传算法方法

res <- GA::ga(
  type = "real-valued",
  fitness = function(W, ...) -fitness(W, ...),
  lower = rep(-1, K*N), 
  upper = rep(1, K*N),
  group_vectors = group_vectors,
  similarity_matrix = similarity_matrix,
  maxiter = 10000,
  run = 200
)

W <- matrix(res@solution[1,], nrow = K, ncol = N)

Beacuse you didn't provide any data I will generate random and demonstrate way you can approach your problem.

Similarity matrix:

N <- 6

S <- matrix(runif(N^2, -1, 1), ncol = N, nrow = N)
similarity_matrix <- (S + t(S)) / 2

N is number of groups. Each value of similarity matrix is between -1 and 1 and matrix is symmetric (beacuse you want to compare it to covariance matrix these makes sense).

group vectors:

M <- 10
K <- 8

group_vectors <- replicate(N, replicate(K, sample(c(0, 1), M, TRUE)), FALSE)

M is dimension of vector and K is number of binary vectors in each group.

fitness function

fitness <- function(W, group_vectors, similarity_matrix){
  
  W <- as.data.frame(matrix(W, nrow = K, ncol = N))
  
  SS <- cov(
    mapply(function(x,y)  rowSums(sweep(x, 2, y, "*")), group_vectors, W)
  )
  
  sum(abs(SS - similarity_matrix))
}

fitness for given weights calculates described covariance matrix and its distance from similarity_matrix.

differential evolution approach

res <- DEoptim::DEoptim(
  fn = fitness, 
  lower = rep(-1, K*N), 
  upper = rep(1, K*N),
  group_vectors = group_vectors,
  similarity_matrix = similarity_matrix,
  control = DEoptim::DEoptim.control(VTR = 0, itermax = 1000, trace = 50, NP = 100)
)

W <- matrix(res$optim$bestmem, nrow = K, ncol = N)

genetic algorithm approach

res <- GA::ga(
  type = "real-valued",
  fitness = function(W, ...) -fitness(W, ...),
  lower = rep(-1, K*N), 
  upper = rep(1, K*N),
  group_vectors = group_vectors,
  similarity_matrix = similarity_matrix,
  maxiter = 10000,
  run = 200
)

W <- matrix(res@solution[1,], nrow = K, ncol = N)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文