- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我已经成功实现了 Mitchell 的最佳候选算法。 Mitchell 的最佳候选算法通过创建 k 个候选样本并从 k 个中选出最佳样本来生成新的随机样本。 这里的“最佳”样本被定义为距离先前样本最远的样本。该算法近似于泊松盘采样,产生比均匀随机采样更自然的外观(更好的蓝噪声光谱特性)。
我正在努力改进它,尤其是在速度方面。 所以我想到的第一个想法是只将候选样本与最后添加的元素进行比较,而不是将它们与之前的整个样本进行比较。这会使泊松盘采样产生偏差,但可能会产生一些有趣的结果。
这是我实现的主要部分
public class MitchellBestCandidateII extends JFrame {
private List<Point> mitchellPoints = new ArrayList<Point>();
private Point currentPoint;
private int currentPointIndex =0;
private boolean isBeginning = true;
private Point[] candidatesBunch = new Point[MAX_CANDIDATES_AT_TIME];
public MitchellBestCandidateII() {
computeBestPoints();
initComponents();
}
computeBestPoints
方法计算点的方式与 Mitchell 算法不同,因为它只将候选点与最后添加的点进行比较,而不是将其与整个样本进行比较。
private void computeBestPoints() {
do {
if (isBeginning) {
currentPoint = getRandomPoint();
mitchellPoints.add(currentPoint);
isBeginning = false;
currentPointIndex = 0;
}
setCandidates();
Point bestCandidate = pickUpCandidateFor(currentPoint);
mitchellPoints.add(bestCandidate);
currentPoint = bestCandidate;
currentPointIndex++;
} while (currentPointIndex <MAX_NUMBER_OF_POINTS);
}
private Point pickUpCandidateFor(Point p) {
double biggestDistance = 0.0D;
Point result = null;
for (int i = 0; i < MAX_CANDIDATES_AT_TIME; i++) {
double d = distanceBetween(p, candidatesBunch[i]);
if (biggestDistance < d) {
biggestDistance = d;
result = candidatesBunch[i];
}
}
return result;
}
setCandidates
方法生成随机候选人。只有其中一个最终会成为样本的一部分:其他的将被丢弃。
private void setCandidates() {
for (int i = 0; i < MAX_CANDIDATES_AT_TIME; i++) {
candidatesBunch[i] = getRandomPoint();
}
}
private Point getRandomPoint() {
return new Point(Randomizer.getHelper().nextInt(SCREEN_WIDTH), Randomizer.getHelper().nextInt(SCREEN_HEIGHT));
}
initComponents
设置 JFrame 和 JPanel 并将要绘制的点列表传递给 JPanel
private void initComponents() {
this.setSize(SCREEN_WIDTH,SCREEN_HEIGHT);
PaintPanel panel = new PaintPanel(mitchellPoints);
panel.setPreferredSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT));
this.setContentPane(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
distanceBetween
方法使用数学公式计算两点之间的距离。
public double distanceBetween(Point p1, Point p2) {
double deltaX = p1.getX() - p2.getX();
double deltaY = p1.getY() - p2.getY();
double deltaXSquare = Math.pow(deltaX, 2);
double deltaYSquare = Math.pow(deltaY, 2);
return Math.sqrt(deltaXSquare + deltaYSquare);
}
}
这是执行的一个例子:
每次运行似乎都会产生相同类型的点分布,正如您在上图中看到的那样,这些点似乎在避开中心区域。我不明白为什么它会这样。有人可以帮助我理解这种行为吗?是否有任何其他方法(或已知算法)可以显着改进 Mitchell 的最佳候选算法?我的 Mitchell 最佳候选算法(不是上面的代码)的实现正在接受审查 Code Review
感谢您的帮助。
最佳答案
Every run seems to produce the same type of points distribution and as you can see in the pictures above the points seems to be avoiding the central area. I can't understand why it is behaving this way. Can someone help me to understand this behavior?
正如在@pens-fan-69 answer 中指出的那样,基本上,如果您根据新点的选择来添加它与前一个点的距离(一个点的完全相反的恰好相反的是它本身),您最终会在空间的边缘之间振荡。
Is there any other approach (or known algorithm) that significantly improve Mitchell's best candidate algorithm?
对于您描述的问题,我认为专门用于在 K 维空间数据建模并允许在占用空间中快速搜索给定新坐标的最近邻居的数据结构是有意义的。 <强> K-D Tree 是这样的结构:
In computer science, a k-d tree (short for k-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space. k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches). k-d trees are a special case of binary space partitioning trees.
通常,在构造 K-D 树时,这是使用一组(排序的)数据点完成的,并使用沿该轴的其余点之间的中值沿其中一个维度轴递归划分(分区)搜索空间.
我添加了一个非常简单和天真的实现,它只包含您的问题中使用的那些操作,并且不对插入执行树重新平衡。由于插入点的特殊性质(与现有点的最大距离),这似乎工作得很好,见下图(每轮评估 500、10 个候选点,左图和右图分别总共 5000、1000 点) .
它是用 C# 编写的,因为我已经有了一些可用的部分,但是将其转换为 Java 应该非常简单。我省略了 Points
类的代码。
// A very naive partial K-D Tree implementation with K = 2 for Points.
public class TwoDTree
{
private Node _root;
public void Insert(Point coordinate)
{
_root = Node.Insert(coordinate, _root, 0);
}
public Point FindNearest(Point to, out double bestDistance)
{
bestDistance = double.MaxValue;
var best = Node.FindNearest(to, _root, 0, null, ref bestDistance);
return best != null ? best.Coordinate : null;
}
public IEnumerable<Point> GetPoints()
{
if (_root != null)
return _root.GetPoints();
return Enumerable.Empty<Point>();
}
private class Node
{
private Node _left;
private Node _right;
public Node(Point coord)
{
Coordinate = coord;
}
public readonly Point Coordinate;
public IEnumerable<Point> GetPoints()
{
if (_left != null)
{
foreach (var pt in _left.GetPoints())
yield return pt;
}
yield return Coordinate;
if (_right != null)
{
foreach (var pt in _right.GetPoints())
yield return pt;
}
}
// recursive insert (non-balanced).
public static Node Insert(Point coord, Node root, int level)
{
if (root == null)
return new Node(coord);
var compareResult = ((level % 2) == 0)
? coord.X.CompareTo(root.Coordinate.X)
: coord.Y.CompareTo(root.Coordinate.Y);
if (compareResult > 0)
root._right = Insert(coord, root._right, level + 1);
else
root._left = Insert(coord, root._left, level + 1);
return root;
}
public static Node FindNearest(Point coord, Node root, int level, Node best, ref double bestDistance)
{
if (root == null)
return best;
var axis_dif = ((level % 2) == 0)
? coord.X - root.Coordinate.X
: coord.Y - root.Coordinate.Y;
// recurse near & maybe far as well
var near = axis_dif <= 0.0d ? root._left : root._right;
best = Node.FindNearest(coord, near, level + 1, best, ref bestDistance);
if (axis_dif * axis_dif < bestDistance)
{
var far = axis_dif <= 0.0d ? root._right : root._left;
best = Node.FindNearest(coord, far, level + 1, best, ref bestDistance);
}
// do we beat the old best.
var dist = root.Coordinate.DistanceTo(coord);
if (dist < bestDistance)
{
bestDistance = dist;
return root;
}
return best;
}
}
}
// Mitchell Best Candidate algorithm, using the K-D Tree.
public class MitchellBestCandidate
{
private const int MaxX = 420;
private const int MaxY = 320;
private readonly int _maxCandidates;
private readonly int _maxPoints;
private readonly Random _rnd;
private readonly TwoDTree _tree = new TwoDTree();
public MitchellBestCandidate(int maxPoints, int maxCandidatesPerRound)
{
_maxPoints = maxPoints;
_maxCandidates = maxCandidatesPerRound;
_rnd = new Random();
}
public IEnumerable<Point> CurrentPoints
{
get { return _tree.GetPoints(); }
}
public void Generate()
{
_tree.Insert(GetRandomPoint(_rnd, MaxX, MaxY));
for (var i = 1; i < _maxPoints; i++)
{
var bestDistance = double.MinValue;
var bestCandidate = default(Point);
for (var ci = 0; ci < _maxCandidates; ci++)
{
var distance = default(double);
var candidate = GetRandomPoint(_rnd, MaxX, MaxY);
var nearest = _tree.FindNearest(candidate, out distance);
if (distance > bestDistance)
{
bestDistance = distance;
bestCandidate = candidate;
}
}
_tree.Insert(bestCandidate);
}
}
private static Point GetRandomPoint(Random rnd, int maxX, int maxY)
{
return new Point(rnd.Next(maxX), rnd.Next(maxY));
}
}
关于java - 改进 Mitchell 的最佳候选算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29853162/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!