I have the following code. I want to write it mathematically. Any help would be appreciated.
int in1=1, in2=-1;
double totalDistance=0.0;
double min_dis = Double.MAX_VALUE;
for(int i=0;i<N1;i++){
for(int j=0;j<N2;j++){
dis = calculateDistance(Solutions(i),Solutions(j));
totalDistance = dis;
if(totalDistance< min_dis){
min_dis=totalDistance;
in1=i;
in2=j;
}
}
}
print(in1, in2)
Mathematically, I write it as follows: \begin{equation} argmin_{i,j \in \{1,2, \dots, n_1\} \times \{1,2, \dots, n_2\}} dis(S_i, S_j) \end{equation} $\times$ refers to Cartesian product between sets and ${dis(S_i, S_j)}$ is the distance.
I want to know is it correct mathematical representation. If it is correct, I want to extend it to n loops. Following is an example of 3 loops.
int in1=1, in2=-1, in3=-1;
double totalDistance=0.0;
double min_dis = Double.MAX_VALUE;
for(int i=0;i<N1;i++){
for(int j=0;j<N2;j++){
double dis1 = calculateDistance(**S1**(i), **S2**(j));
for(int z=0;z<N3;z++){
double dis2 = calculateDistance(**S2**(j), **S3**(z));
totalDistance = dis1+ dis2;
if(totalDistance< min_dis){
min_dis=totalDistance;
in1=i;
in2=j;
in3=z;
}
}
}
}
print(in1 in2 in3);
How can I represent the generalized version in mathematical notation?
Thanks in advance.
Define the set of minimizers:
$S = \arg\min_{ x \in \mathbb{N}^n} \{ \sum_{i=0}^{n-2} dis(S_{x_i},S_{x_{i+1}}) : x_i < n_i \; i = 0,1,\ldots,n-2 \}$
The solution is the lexicographic first element of $S$:
$\min\{x \in S \}$
where $\min$ performs lexicographic minimization. This returns the same element of $S$ that your code outputs.