 # Algorithm for distance minimization

Hello.
I have a distance matrix and some const points in 3D space.
I want to find the other points by distance matrix, but don't change const points.
Example:
point1c = 0.25, 0.8, 0.4
point2c = 0.11, 0.5, 0.3
point3m = random
point4m = random
....
It means I know distances between const points and I don't need to change positions of these points. I must change only mutable points by distances between const and mutable points and distances between mutable and mutable points.
Maybe someone can give me advice. Thank you.

this is ambigous, what principle do you use to 'find other points' ?

this is ambigous, what principle do you use to 'find other points' ?

Just distance minimization, but question: 'how?'
In the distance matrix, I have distance between point1c and point2c as 0.345832, so
dmatrix = dmatrxi = 0.345832, that is answer ,I don't need to change positions of point1c and point2c.
For points 3, 4, 5 I can generate random numbers from 0 to 1. Only I need to find a combination of mutable points when min difference distance between those points and const points and values in distance matrix.
distance(point_i, point_j) - dmatrix[i][j] = ~0 -- answer
It looks like const points have a solid position and we change positions of mutable points to find good positions according to the matrix.

I can provide more details if you are interested.

I'm really lost at what your are trying to do
A clear statement of the goal is priceless, you should bear in mind that I lack any of the context you have for your motivations do do anything... starter code and data isnt a bad idea either.

I don't have any code. It's a totally theoretical question. How I can write code if I don't know what to do.
Primitive solution is a brute force algorithm, but it's horrible.

I think my describe is pure.

X : input vectors
X = 0.25, 0.8, 0.4 - const
X = 0.11, 0.5, 0.3 - const
X = 0.43, 0.3, 0.1 - mutable(random generated)
X = 0.11, 0.4, 0.2 - mutable(random generated)
X = 0.23, 0.3, 0.3 - mutable(random generated)

dm : distance matrix
0 0.345832 0.393372 0.172916 0.393775
0.345832 0 0.393775 0.172916 0.393372
0.393372 0.393775 0 0.353553 0.707107
0.172916 0.172916 0.353553 0 0.353553
0.393775 0.393372 0.707107 0.353553 0

I must change coords{x, y, z} in X, X, X to have distance between points like in distance matrix (or very close).

Y: output vectors
X = 0.25, 0.8, 0.4 - const
X = 0.11, 0.5, 0.3 - const
X = 0.??, 0.?, 0.? - mutable
X = 0.??, 0.?, 0.? - mutable
X = 0.??, 0.?, 0.? - mutable

I tihnk I did it.

``````
library(tidyverse)
point1c = c(0.25, 0.8, 0.4)
point2c = c(0.11, 0.5, 0.3)
# point3m = random
# point4m = random
# point5m = random
(initial_matrix<- matrix(data=c(point1c,point2c),
nrow=3) %>% t())

eval_distance <- function(m,target){

mat <- (dist(m) %>% as.matrix())
ltri <-   mat[lower.tri(mat)]
sub_dists2 <-ltri-target
res <- sum (sub_dists2^2)^.5
cat ("\ndistance ", res,"\n")
invisible(res)
}

(initial_dist <- eval_distance(initial_matrix,0))
set.seed(42)

(full_matrix_1 <- matrix(
data=c(point1c,point2c,sample.int(100,6, replace=TRUE)/100),
nrow=3) %>% t())

eval_distance(full_matrix_1,initial_dist)

compose_new_matrix_and_eval <- function(p,printmatrix=FALSE,return_only_eval=TRUE){
p3x <- p
p3y <- p
p3z <- p
p4x <- p
p4y <- p
p4z <- p
p5x <- p
p5y <- p
p5z <- p

dlist <- c(point1c,
point2c,
p3x,p3y,p3z,
p4x,p4y,p4z,
p5x,p5y,p5z)

new_mtrx <- matrix(
data=dlist,
nrow=3) %>% t()
if(printmatrix) print(new_mtrx)

e_dist <- eval_distance(new_mtrx,initial_dist)
if(return_only_eval)
return(e_dist)
else
return(list(eval_distance=e_dist,
matrix =new_mtrx ))
}

optim_result <- optim(sample.int(100,9, replace=TRUE)/100,
compose_new_matrix_and_eval,control=list(maxit=10000))

optim_result

(final_result <- compose_new_matrix_and_eval(optim_result\$par,
printmatrix = TRUE,
return_only_eval = FALSE))

eval_distance(final_result\$matrix,initial_dist) %>% as.numeric()
final_result\$matrix
dist(final_result\$matrix)
``````
``````> final_result\$matrix
[,1]      [,2]      [,3]
[1,] 0.2500000 0.8000000 0.4000000
[2,] 0.1100000 0.5000000 0.3000000
[3,] 0.2036149 0.4945767 0.5810320
[4,] 0.4761734 0.5779608 0.5022559
[5,] 0.3950926 0.5248427 0.2226770
> dist(final_result\$matrix)
1         2         3         4
2 0.3458323
3 0.3580608 0.2962636
4 0.3330346 0.4255212 0.2957138
5 0.3580598 0.2964352 0.4074286 0.2959054``````

I tihnk I did it.

I can't get an idea of your method.

I provided complete code, and example output, you can run it and think about it.
I don't have energy for anything else today.
I do note that the ability to find a good fit, is almost perfect with 4 points, but adding the 5th, makes it much harder to find equidistant pairings for all points

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.