C++ 使总距离最短的最佳点位置(Optimum location of point to minimize total distance)

目录

[使用三元搜索------时间复杂度为 O(n * log(1/ε)),空间复杂度为 O(1)](#使用三元搜索——时间复杂度为 O(n * log(1/ε)),空间复杂度为 O(1))

示例代码

[使用黄金分割搜索------时间复杂度为 O(n * log(1/ε)),空间复杂度为 O(1)](#使用黄金分割搜索——时间复杂度为 O(n * log(1/ε)),空间复杂度为 O(1))

示例代码


给定一个大小为 2 * n 的点集 points \[\]\[\] ,其中每个点表示为 p, q,以及一条 由方程 ax + by + c = 0 表示的直线L\[\] 。

在直线L\[\]上找到一点 ,使得该点到所有给定点的欧氏距离之和最小。返回最小可能的距离之和,保留两位小数。

例如:

输入: n = 5,L\[\] = 1, -1, -3,points\[\]\[\] = \[-3, 2, -1, 0, -1, 2, 1, 2, 3, 4]输出: 20.77

说明:在给定的图中,x - y - 3 = 0 直线上的点的最佳位置是 (2, -1),它与其他点的总距离为 20.77,这是可获得的最小总距离。

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

输入: n = 3,L\[\] = 2, 1, 4,points\[\]\[\] = \[-1, 2, 1, 3, 2, 4]

输出: 11.20

说明: L\[\] 表示的直线为 2x + y + 4 = 0。该直线上的最优解约为 (-2.64, 1.27)。该最优解到所有给定点的欧氏距离之和约为 11.20,这是可能的最小值。

使用三元搜索------时间复杂度为 O(n * log(1/ε)),空间复杂度为 O(1)

因此,直接线性搜索并不合适,因为我们需要沿着直线采样许多点,而且没有固定的步长可以保证找到精确的最小值。

相反,我们可以使用单个变量 t 对线上的每个点进行参数化。这样就将原来的二维优化问题转化为一维问题。

如果 (x0, y0) 是直线上的任意一点,(dx, dy) 是沿直线方向的单位向量,则直线上的每一点都可以表示为:

对于给定的 t 值,我们计算从该点到所有给定点的距离之和。

所得函数是凸函数(单峰函数),这意味着它先递减直至达到最小值,然后递增。因此,我们无需检查所有可能的 t 值,即可使用三元搜索高效地找到最小值。

1、取直线 ax + by + c = 0,并找到该直线上的任意一点 (x0, y0)。

2、使用 (b, -a) 作为直线的方向向量,并将其归一化为单位向量 (dirX, dirY)。

3、将所有给定的点投影到线上,以确定参数 t 的搜索范围 低,高。

4、由于欧氏距离之和是凸函数,因此对 low, high 应用三元搜索。

5、对于每次迭代,计算 mid1 和 mid2 之间的总距离,并舍弃不能包含最小值的三分之一。

6、收敛后,取最终范围的中点,将其转换回 (x, y),并返回距离之和最小值。

示例代码

#include <bits/stdc++.h>

using namespace std;

// Calculate the sum of Euclidean distances from (x, y)

// to all the given points.

double getDistanceSum(vector<vector<int>> &points, double x, double y)

{

double sum = 0.0;

for (auto &point : points)

{

double dx = x - point0;

double dy = y - point1;

sum += sqrt(dx * dx + dy * dy);

}

return sum;

}

double findOptimumCost(vector<int> &L, vector<vector<int>> &points)

{

double a = L0;

double b = L1;

double c = L2;

// Find any point (x0, y0) lying on the line:

// ax + by + c = 0

double x0 = 0.0, y0 = 0.0;

if (b != 0)

y0 = -c / b;

else

x0 = -c / a;

// (b, -a) is a direction vector parallel to the line.

// Normalize it to get a unit direction vector.

double len = sqrt(a * a + b * b);

double dirX = b / len;

double dirY = -a / len;

// We represent every point on the line as:

//

// (x, y) = (x0 + t * dirX, y0 + t * dirY)

//

// Find the range of t values corresponding to the

// projections of all given points onto the line.

double low = 1e18;

double high = -1e18;

for (auto &point : points)

{

// Projection of (point - (x0, y0)) onto

// the unit direction vector.

double t = (point0 - x0) * dirX + (point1 - y0) * dirY;

low = min(low, t);

high = max(high, t);

}

// The sum-of-distances function is convex, so

// ternary search can be used to find its minimum.

double eps = 1e-7;

while (high - low > eps)

{

// Divide the current range into three parts.

double mid1 = low + (high - low) / 3.0;

double mid2 = high - (high - low) / 3.0;

// Point corresponding to mid1.

double x1 = x0 + mid1 * dirX;

double y1 = y0 + mid1 * dirY;

// Point corresponding to mid2.

double x2 = x0 + mid2 * dirX;

double y2 = y0 + mid2 * dirY;

// Calculate the total distance at both points.

double cost1 = getDistanceSum(points, x1, y1);

double cost2 = getDistanceSum(points, x2, y2);

// If cost1 is smaller, the minimum lies

// in the left two-thirds.

if (cost1 < cost2)

{

high = mid2;

}

// Otherwise, the minimum lies in the

// right two-thirds.

else

{

low = mid1;

}

}

// Take the middle of the final range as the

// approximate optimal value of t.

double t = (low + high) / 2.0;

// Convert t back to the corresponding point on the line.

double x = x0 + t * dirX;

double y = y0 + t * dirY;

// Return the minimum possible sum of distances.

return getDistanceSum(points, x, y);

}

int main()

{

vector<int> L = {1, -1, -3};

vector<vector<int>> points = {{-3, -2}, {-1, 0}, {-1, 2}, {1, 2}, {3, 4}};

double ans = findOptimumCost(L, points);

cout << fixed << setprecision(2) << ans << '\n';

return 0;

}

输出

20.77

使用黄金分割搜索------时间复杂度为 O(n * log(1/ε)),空间复杂度为 O(1)

三元搜索每次迭代都会检查两个新点,因此它会计算两次距离和。黄金分割搜索通过使用黄金分割率选择点来改进这一点。

这样,缩小范围后,就可以重复使用之前的某个点。因此,在每次迭代中,我们只需计算一个新点的距离和,从而在找到凸函数最小值的同时,提高了搜索效率。

1、找到直线上的一点(x0,y0),并求出单位方向向量(dirX,dirY)。

2、将所有给定的点投影到线上,以确定搜索范围低,高。

3、利用黄金分割率将范围分成两个点 x1 和 x2。

4、计算这两点之间的距离之和。

5、比较成本并缩短时间间隔;尽可能重复使用先前计算的成本。

6、继续进行,直到区间足够小,然后评估中点以获得最小成本。

示例代码

#include <bits/stdc++.h>

using namespace std;

// Calculate the sum of Euclidean distances from (x, y)

// to all the given points.

double getDistanceSum(vector<vector<int>> &points, double x, double y)

{

double sum = 0.0;

for (auto &point : points)

{

double dx = x - point0;

double dy = y - point1;

sum += sqrt(dx * dx + dy * dy);

}

return sum;

}

double findOptimumCost(vector<int> &L, vector<vector<int>> &points)

{

double a = L0;

double b = L1;

double c = L2;

// Find any point (x0, y0) lying on the line:

// ax + by + c = 0

double x0 = 0.0, y0 = 0.0;

if (b != 0)

y0 = -c / b;

else

x0 = -c / a;

// (b, -a) is a direction vector parallel to the line.

// Normalize it to get a unit direction vector.

double len = sqrt(a * a + b * b);

double dirX = b / len;

double dirY = -a / len;

// We represent every point on the line as:

//

// (x, y) = (x0 + t * dirX, y0 + t * dirY)

//

// Find the range of t values corresponding to the

// projections of all given points onto the line.

double low = 1e18;

double high = -1e18;

for (auto &point : points)

{

// Projection of (point - (x0, y0)) onto

// the unit direction vector.

double t = (point0 - x0) * dirX + (point1 - y0) * dirY;

low = min(low, t);

high = max(high, t);

}

// The sum-of-distances function is convex, so

// Golden Section Search can be used to find its minimum.

// Golden ratio value.

double phi = (sqrt(5.0) - 1.0) / 2.0;

double eps = 1e-7;

// Initial two points using the golden ratio.

double mid1 = high - phi * (high - low);

double mid2 = low + phi * (high - low);

// Calculate the total distance at both points.

double cost1 = getDistanceSum(points, x0 + mid1 * dirX, y0 + mid1 * dirY);

double cost2 = getDistanceSum(points, x0 + mid2 * dirX, y0 + mid2 * dirY);

while (high - low > eps)

{

// If cost1 is smaller, the minimum lies

// in the left part of the range.

if (cost1 < cost2)

{

high = mid2;

// Reuse the previous point and its cost.

mid2 = mid1;

cost2 = cost1;

// Calculate only one new point.

mid1 = high - phi * (high - low);

cost1 = getDistanceSum(points, x0 + mid1 * dirX, y0 + mid1 * dirY);

}

// Otherwise, the minimum lies in the

// right part of the range.

else

{

low = mid1;

// Reuse the previous point and its cost.

mid1 = mid2;

cost1 = cost2;

// Calculate only one new point.

mid2 = low + phi * (high - low);

cost2 = getDistanceSum(points, x0 + mid2 * dirX, y0 + mid2 * dirY);

}

}

// Take the middle of the final range as the

// approximate optimal value of t.

double t = (low + high) / 2.0;

// Convert t back to the corresponding point on the line.

double x = x0 + t * dirX;

double y = y0 + t * dirY;

// Return the minimum possible sum of distances.

return getDistanceSum(points, x, y);

}

int main()

{

vector<int> L = {1, -1, -3};

vector<vector<int>> points = {{-3, -2}, {-1, 0}, {-1, 2}, {1, 2}, {3, 4}};

double ans = findOptimumCost(L, points);

cout << fixed << setprecision(2) << ans << '\n';

return 0;

}

输出

20.77

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

相关推荐
星恒随风2 小时前
C++11 加餐(4)并发支持库——condition_variable 条件变量与生产者消费者模型
开发语言·c++
你不知道我是谁?2 小时前
GESP C++ 补充知识:浮点数格式化输出(%f/%e/%g)与 fmod 浮点余数实战
开发语言·c++
逐米时代3 小时前
「制造新解法」行业对标方法:借鉴而不照搬的最佳实践
人工智能·算法·制造
kyrie_sakura3 小时前
大模型学习32 --大模型微调3 -- 分布式训练算法
分布式·学习·算法
Rabitebla4 小时前
【Linux】 文件系统(五):挂载与软硬链接,多个文件系统怎样组成统一目录树
linux·运维·服务器·c++·笔记·学习·算法
m0_743106464 小时前
SAD-GS:从梯度邻域到频率驱动表达3D空间
人工智能·算法·计算机视觉·3d·几何学
csdn_aspnet5 小时前
Java 使总距离最短的最佳点位置(Optimum location of point to minimize total distance)
java·算法
WL学习笔记6 小时前
继承(C++)
c++·继承
专注仿真6 小时前
仿真智能体训练(评估指标篇):任务完成率算法详解与实战
人工智能·算法·机器学习·afsim智能体·仿真训练·afsim智能体训练·任务完成率算法