简体   繁体   中英

GRG Nonlinear R

I want to transform my excel solver model into a model in R. I need to find 3 sets of coordinates which minimizes the distance to the 5 other given coordinates. I've made a program which calculates a distance matrix which outputs the minimal distance from each input to the given coordinates. I want to minimize this function by changing the input. Id est, I want to find the coordinates such that the sum of minimal distances are minimized. I tried several methods to do so, see the code below (Yes my distance matrix function might be somewhat cluncky, but this is because I had to reduce the input to 1 variable in order to run some algorithms such as nloprt (would get warnings otherwise). I've also seen some other questions (such as GRG Non-Linear Least Squares (Optimization) ) but they did not change/improve the solution.

# First half of p describes x coordinates, second half the y coordinates # yes thats cluncky
p<-c(2,4,6,5,3,2) # initial points

x_given <- c(2,2.5,4,4,5)
y_given <- c(9,5,7,1,2)

f <- function(Coordinates){ 

# Predining
Term_1                       <-     NULL
Term_2                       <-     NULL
x                            <-     NULL
Distance                     <-     NULL
min_prob                     <-     NULL
l                            <-     length(Coordinates)
l2                           <-     length(x_given)
half_length                  <-     l/2
s                            <-     l2*half_length
Distance_Matrix              <-     matrix(c(rep(1,s)), nrow=half_length)

# Creating the distance matrix
for (k in 1:half_length){
  for (i in 1:l2){
    Term_1[i]                <-     (Coordinates[k]-x_given[i])^2
    Term_2[i]                <-     (Coordinates[k+half_length]-y_given[i])^2
    Distance[i]              <-     sqrt(Term_1[i]+Term_2[i])
    Distance_Matrix[k,i]     <-     Distance[i]
  }
}
d                            <-     Distance_Matrix

# Find the minimum in each row, thats what we want to obtain ánd minimize
for (l in 1:nrow(d)){
  min_prob[l] <- min(d[l,])
}
som<-sum(min_prob)
return(som)
}

# Minimise
sol<-optim(p,f)
x<-sol$par[1:3]
y<-sol$par[4:6]
plot(x_given,y_given)
points(x,y,pch=19)

The solution however is clearly not that optimal. I've tried to use the nloptr function, but I'm not sure which algorithm to use. Which algorithm can I use or can I use/program another function which solves this problem? Thanks in advance (and sorry for the detailed long question)

Look at the output of optim . It reached the iteration limit and had not yet converged.

> optim(p, f)
$`par`
[1] 2.501441 5.002441 5.003209 5.001237 1.995857 2.000265

$value
[1] 0.009927249

$counts
function gradient 
     501       NA 

$convergence
[1] 1

$message
NULL

Although the result is not that different you will need to increase the number of iterations to get convergence. If that is still unacceptable then try different starting values.

> optim(p, f, control = list(maxit = 1000))
$`par`
[1] 2.502806 4.999866 5.000000 5.003009 1.999112 2.000000

$value
[1] 0.005012449

$counts
function gradient 
     755       NA 

$convergence
[1] 0

$message
NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM