[R] optimization problem

Ravi Varadhan rvaradhan at jhmi.edu
Sat Jan 16 16:51:45 CET 2010


Klaus,

You also need to make a change in the main function, as shown below.

pMatrix.min <- function(A, B) {
# finds the permutation P of A such that ||PA - B|| is minimum
# in Frobenius norm
# Uses the linear-sum assignment problem (LSAP) solver
# in the "clue" package
# Returns P%*%A and the permutation vector `pvec' such that
# A[pvec, ] is the permutation of A closest to B
	n <- nrow(A)
	D <- matrix(NA, n, n)
	for (i in 1:n) {
	for (j in 1:n) {
#	D[j, i] <- sum(abs(B[j, ] - A[i, ]))
	D[j, i] <- sqrt(sum((B[j, ] - A[i, ])^2)) # correct Frobenius norm
	} }
vec <- c(solve_LSAP(D))
list(A=A[vec,], pvec=vec)
}

Hope this help,
Ravi.
____________________________________________________________________

Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology
School of Medicine
Johns Hopkins University

Ph. (410) 502-2619
email: rvaradhan at jhmi.edu


----- Original Message -----
From: Ravi Varadhan <rvaradhan at jhmi.edu>
Date: Saturday, January 16, 2010 10:00 am
Subject: Re: [R] optimization problem
To: Erwin Kalvelagen <erwin.kalvelagen at gmail.com>
Cc: r-help at stat.math.ethz.ch


> Thanks, Erwin, for pointing out this mistake.
> 
> Here is the correct function for Frobenius norm.  
> 
> Klaus - Just replace the old `dist' with the following one. 
> 
> dist <- function(A, B) {
>  # Frobenius norm of A - B
>   n <- nrow(A)
>   sqrt(sum((B - A)^2))
>  }
> 
> Ravi.
> 
> ____________________________________________________________________
> 
> Ravi Varadhan, Ph.D.
> Assistant Professor,
> Division of Geriatric Medicine and Gerontology
> School of Medicine
> Johns Hopkins University
> 
> Ph. (410) 502-2619
> email: rvaradhan at jhmi.edu
> 
> 
> ----- Original Message -----
> From: Erwin Kalvelagen <erwin.kalvelagen at gmail.com>
> Date: Saturday, January 16, 2010 2:35 am
> Subject: Re: [R] optimization problem
> To: r-help at stat.math.ethz.ch
> 
> 
> > Ravi Varadhan <rvaradhan <at> jhmi.edu> writes:
> > > dist <- function(A, B) { 
> > > # Frobenius norm of A - B	
> > > 	n <- nrow(A)
> > > 	sum(abs(B - A))
> > > }
> > > 
> > 
> > See  for a definition of the 
> > Frobenius norm.
> > 
> > 
> > Erwin
> > 
> > ----------------------------------------------------------------
> > Erwin Kalvelagen
> > Amsterdam Optimization Modeling Group
> > erwin at amsterdamoptimization.com
> > 
> > 
> > ______________________________________________
> > R-help at r-project.org mailing list
> > 
> > PLEASE do read the posting guide 
> > and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 
> PLEASE do read the posting guide 
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list