Optimization problem

Install package

library(devtools)
install_github("hamy12398/ballsdMN")
### Load package
library("BOOOM")
library(Rcpp)
library(testthat)
library(RcppArmadillo)
library(pracma)

Function augment

BOOOM_opt(Mat, func,
 desired_min = -10^(60), 
  s_init = 1, 
  no_runs = 1000, 
  max_iter = 10000, 
  rho = 2, 
  phi = 10^(-20), 
  tol_fun = 10^(-6), 
  tol_fun_2 = 10^(-15), 
  desired_improv_rate = 10^(-20), 
  improv_rate_period = 20, 
  total_iter = 10000000, 
  print_output = 0
)

Example

Create function

Firstly, we need to have the function of multiple row orthognal matrices that needs to be optimized. For example, we generate a function: finds an row orthogonal A that minimize the least square difference (L2 norm) between A and a given constant matrix (B)

## Create constant matrix B dimension 5x5
Mat_B = list()
for ( i in 1:3){
  mat <- randortho(5)
  Mat_B[[i]] <- mat
} 
Mat_B
## [[1]]
##            [,1]       [,2]       [,3]       [,4]        [,5]
## [1,]  0.2154170  0.2004185  0.1873450 -0.6851848 -0.63941503
## [2,] -0.6804706  0.0247309  0.4102292  0.3606922 -0.48781275
## [3,] -0.5935054  0.1055514 -0.7247361 -0.3329649 -0.02241095
## [4,]  0.2623309 -0.6178461 -0.4207463  0.2885350 -0.53774422
## [5,] -0.2636078 -0.7525596  0.3071548 -0.4542080  0.25202389
## 
## [[2]]
##            [,1]        [,2]       [,3]       [,4]        [,5]
## [1,]  0.1254617 -0.08544999  0.8974851 -0.4129853  0.03035201
## [2,]  0.3066282  0.28370281 -0.2884925 -0.5433525  0.66860449
## [3,]  0.3675769  0.46814763 -0.1424442 -0.3469806 -0.71066101
## [4,] -0.5616959  0.78133357  0.2078460  0.1281943  0.11992441
## [5,] -0.6630410 -0.28734418 -0.2186371 -0.6303823 -0.18062627
## 
## [[3]]
##             [,1]        [,2]       [,3]       [,4]       [,5]
## [1,] -0.11066919 -0.07499812 -0.3909510  0.8456838  0.3377926
## [2,]  0.39614187  0.56470133 -0.1090162 -0.2204696  0.6809498
## [3,] -0.07888133 -0.74005095  0.1458765 -0.2490960  0.6023069
## [4,]  0.25222943 -0.24239749 -0.8631336 -0.3134572 -0.1853878
## [5,] -0.87234282  0.26278459 -0.2626654 -0.2755138  0.1583075
## Create function need to be optimized
calculate_l2_norm_mm <- function(matrices1) {
  ## identify number of matrix and dimension
  sum_squares <- 0
  set.seed(1)
  
  # Initialize sum of squared differences
  for (i in 1:3) {
    # Calculate the difference between corresponding matrices
    diff_matrix <- matrices1[[i]] - Mat_B[[i]]
    
    # Calculate the sum of squared differences for this pair
    sum_squares <- sum_squares + sum(diff_matrix^2)
  }
  
  # Calculate the square root of the sum of squared differences
  l2_norm <- sqrt(sum_squares)
  
  return(l2_norm)
}

Initial input matrix

### Generate an inital input (row of orthognal)
matrix1 <- randortho(5)
matrix2 <- randortho(5)
matrix3 <- randortho(5)

Mat_A=list(matrix1,matrix2,matrix3)
Mat_A
## [[1]]
##            [,1]        [,2]       [,3]         [,4]       [,5]
## [1,]  0.5757865  0.65012057  0.4736613  0.008831053  0.1462196
## [2,]  0.5159878 -0.14196822 -0.3009820 -0.689603118 -0.3840039
## [3,] -0.2610782  0.69130273 -0.6589617 -0.103109918  0.0952707
## [4,]  0.2720606 -0.26504580 -0.2375228 -0.130698910  0.8844402
## [5,]  0.5099487 -0.09507647 -0.4409163  0.704738031 -0.1996243
## 
## [[2]]
##              [,1]          [,2]        [,3]        [,4]        [,5]
## [1,] -0.312845540 -0.6087234167 -0.72088713 -0.06956638 -0.08405795
## [2,] -0.037213247  0.0009997181 -0.09244646  0.98944201  0.10522518
## [3,]  0.291402949 -0.7356257294  0.52138693  0.09292943 -0.30571048
## [4,] -0.004809744 -0.2961196118  0.15088119 -0.08567149  0.93924720
## [5,]  0.903218706  0.0249552449 -0.42091084 -0.01376760  0.07885254
## 
## [[3]]
##            [,1]       [,2]        [,3]       [,4]        [,5]
## [1,] -0.7120219  0.6865112  0.04274324 -0.1381982 -0.02831116
## [2,] -0.5404552 -0.4218094  0.11527032  0.6731502  0.25212415
## [3,] -0.3336773 -0.3775701 -0.47614259 -0.1597454 -0.70275880
## [4,]  0.2268370  0.3886419 -0.71697121  0.5301682  0.04874884
## [5,]  0.1952882  0.2391182  0.49409137  0.4702906 -0.66286160
#### a Mat A matrix is an row orthognal matrix where the off-diagonal elements close to 0
Mat_A[[1]] %*% t(Mat_A[[1]])
##               [,1]          [,2]          [,3]          [,4]          [,5]
## [1,]  1.000000e+00 -8.326673e-17  1.578598e-16 -1.942890e-16 -2.324529e-16
## [2,] -8.326673e-17  1.000000e+00 -6.938894e-18  2.220446e-16  0.000000e+00
## [3,]  1.578598e-16 -6.938894e-18  1.000000e+00 -1.387779e-17  2.428613e-17
## [4,] -1.942890e-16  2.220446e-16 -1.387779e-17  1.000000e+00 -1.110223e-16
## [5,] -2.324529e-16  0.000000e+00  2.428613e-17 -1.110223e-16  1.000000e+00

Result

The optimized matrices list should be closed to the constant matrix B above, as their square of difference goes to 0.

result = BOOOM_opt((Mat_A), calculate_l2_norm_mm)
## ********** TASK COMPLETE ****************
## Final Obj function value :2.82985
result
## [[1]]
##             [,1]       [,2]        [,3]       [,4]       [,5]
## [1,]  0.30777642  0.4679596 -0.31557418 -0.5633424 -0.5189854
## [2,] -0.53910028  0.4342436 -0.35956585  0.5471907 -0.3034767
## [3,] -0.74369205 -0.3294997  0.09306586 -0.5310940 -0.2182428
## [4,]  0.24146948 -0.6782762 -0.30715074  0.2610141 -0.5649459
## [5,] -0.05708933 -0.1543298 -0.81738761 -0.1817647  0.5213082
## 
## [[2]]
##            [,1]        [,2]       [,3]       [,4]         [,5]
## [1,]  0.1116870 -0.07261373  0.8900034 -0.4359786  0.008364309
## [2,]  0.2930043  0.28885258 -0.2860755 -0.5441317  0.672899823
## [3,]  0.3969191  0.43610657 -0.1748889 -0.3416045 -0.710624038
## [4,] -0.5432051  0.80240653  0.1957783  0.1283791  0.079130344
## [5,] -0.6701178 -0.27793210 -0.2390387 -0.6169835 -0.189440574
## 
## [[3]]
##             [,1]        [,2]       [,3]        [,4]        [,5]
## [1,] -0.53600621 -0.41722691 -0.3940861  0.60744402 -0.11969531
## [2,] -0.53945894 -0.18808525 -0.1159158 -0.74451846 -0.32536717
## [3,]  0.02691869 -0.65492543  0.1466574 -0.18983512  0.71610215
## [4,]  0.46014259 -0.07511051 -0.8616008 -0.19699996  0.03824095
## [5,] -0.45741182  0.59663786 -0.2596063 -0.04310079  0.60460282