Optimization with positive integer parameters

I need to solve a problem that involves comparing two matrices with the same number of columns. One of them is managed until a better match is obtained. The way I evaluate the differences between the two matrices is rather confusing, and I still have to complete it. I am currently very interested in finding a search / optimization algorithm that works only with positive integers. I created a simple example with a simple function to maximize. Say I have a dataset D.

 D <- data.frame(rbind(c(1,1,1),
                       c(1,1,0),c(1,1,0),c(1,1,0),c(1,0,0),
                       c(0,0,0),c(1,0,0),c(1,0,0),c(1,1,0),
                       c(1,0,0),c(1,1,1),c(1,1,0),c(1,0,0),
                       c(1,0,0),c(1,0,1)))

I want to find which permutation Dx gives me the lowest absolute difference.

Dx<-data.frame(rbind(c(1,1,0),c(1,0,0),c(0,0,0),c(1,1,0)))

So I could go through all possible permutations using the function below

    library(combinat)
    SPACE <- t(as.data.frame(list(permn(1:3))))
    f <- function(x){
      if(anyDuplicated(x)>0){return(0)}
      Dist<-NA
      for (i in 1:nrow(D)){
        Dist[i]<-sum(abs(Dx[,x]-t(D[i,])))} 
    return(sum(Dist))}
apply(SPACE,1,f)

. 2 , :

  • SPACE -
  • apply .

A B , . , 1 14 R .

- . A. , SPACE (.. ), , .

library(NMOF)
gridSearch(f, rep(list(seq(1,ncol(D))),ncol(D)))

, , B, . , , , 15 ?

, (.. ), R, (, , ) (, 1-2 ), ? , , . optim() method="SANN", . , , , , . ( , ), , , D2, - ?

   #D2
D<-cbind(D,D,D,D,D)
ncol(D)
Dx<-cbind(Dx,Dx,Dx,Dx,Dx)
#examples 
f(c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15))
f(c(13,2,4,3,5,6,7,8,9,10,11,12,1,14,15))

EDIT: , , ( ) , . , , , , , ... ' m, , (, ) f , D2, .

+3
2

f , , . , gaoptim, f(p) p Dx:

library(gaoptim)
myGA = GAPerm(f, ncol(Dx), popSize=10)
myGA$evolve(10)
myGA
# Results for 10 Generations:
# Mean Fitness:
#    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#    95.0   107.4   115.6   112.4   118.3   120.6 
# 
# Best Fitness:
#    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#     125     125     125     125     125     125
# 
# Best individual:
# [1] 3 1 2
# 
# Best fitness value:
# [1] 125

125, , .

+3

, - , , - . " ", .

, . ( ), , .

R, lp.assign lpSolve, :

# Build cost matrix
costs <- as.matrix(dist(t(D), method="manhattan"))
costs
#    X1 X2 X3
# X1  0  7 11
# X2  7  0  6
# X3 11  6  0

# Solve assignment problem
library(lpSolve)
solution <- lp.assign(costs)$solution
apply(solution > 0.999, 2, which)
# [1] 1 2 3

, 1, 2, 3 .

+2

All Articles