【问题标题】:Jaccard score/distance or percentage overlapJaccard 得分/距离或重叠百分比
【发布时间】:2014-01-14 16:44:54
【问题描述】:

我希望能够计算一个矩形相对于矩形网格的 Jaccard 分数/距离(距离为 1 分)。我的网格是 50x50(总共 1625625 个矩形)。

我能够在 0.34 秒内针对所有这些计算输入矩形的分数,但这还不够快,因为我要么需要能够处理 10k 矩形,要么将结果存储在数据库中(更新 10 秒每次调用数千行)。所以我希望让数据库为我做计算,而不必从数据库中提取任何东西,但是我想不出没有游标如何做到这一点......

sourceRectangles 包含我的单个矩形(虽然实际上会有 10k),rectangles 包含我的网格,temporaryRectangleList 包含分数的总和。

Dictionary<UInt32, Rectangle> temporaryRectangleList = new Dictionary<UInt32, Rectangle>();
foreach (var sourceRectangle in sourceRectangles)
{
    foreach (var rectangle in rectangles)
    {
        // For each rectangle within the group
        //foreach (var rectangle in group)
        //{
        int max_MinX = Math.Max(sourceRectangle.MinX, rectangle.MinX);
        int min_MaxX = Math.Min(sourceRectangle.MaxX, rectangle.MaxX);

        // There is an overlap
        //if (max_MinX < min_MaxX)
        //{
        int max_MinY = Math.Max(sourceRectangle.MinY, rectangle.MinY);
        int min_MaxY = Math.Min(sourceRectangle.MaxY, rectangle.MaxY);


        // Calculate the area of the overlap
        int area = ((min_MaxX - max_MinX)*(min_MaxY - max_MinY));
        // Store the Jaccard score
        var score = (double) area/((sourceRectangle.Area + rectangle.Area) - area);

        if (temporaryRectangleList.ContainsKey(rectangle.ID))
        {
            temporaryRectangleList[rectangle.ID].Weight += score;
        }
        else
        {
            temporaryRectangleList.Add(rectangle.ID, new Rectangle(rectangle, score));
        }
    }
}

我需要能够在字典中查找项目,因为我需要通过矩形的 ID 从中提取数据。

如果你认为你可以加快 C# 速度以更快(10k 矩形处理

很遗憾,SQL表太大,这里不能转储,所以我只能给你结构:

USE [Rectangles]
GO

/****** Object:  Table [dbo].[PreProcessed]    Script Date: 14/01/2014 16:39:33 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[PreProcessed](
    [ID] [int] NOT NULL,
    [MinX] [int] NOT NULL,
    [MinY] [int] NOT NULL,
    [MaxX] [int] NOT NULL,
    [MaxY] [int] NOT NULL,
    [Area] [int] NOT NULL,
 CONSTRAINT [PK_PreProcessed] PRIMARY KEY CLUSTERED 
(
    [ID] ASC,
    [Area] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

矩形类:

public class Rectangle
{
    public Rectangle(UInt32 id, int minX, int maxX, int minY, int maxY, double weight)
    {
        ID = id;
        MinX = minX;
        MaxX = maxX;
        MinY = minY;
        MaxY = maxY;
        Area = (maxX - minX)*(maxY - minY);
        Weight = weight;
    }

    public Rectangle(Rectangle input, double weight)
    {
        ID = input.ID;
        MinX = input.MinX;
        MaxX = input.MaxX;
        MinY = input.MinY;
        MaxY = input.MaxY;
        Area = input.Area;
        Weight = weight;
    }

    public int Area { get; set; }
    public int MinX { get; set; }
    public int MaxX { get; set; }
    public int MinY { get; set; }
    public int MaxY { get; set; }

    public UInt32 ID { get; set; }
    public double Weight { get; set; }
}

【问题讨论】:

  • 如果你可以切换内部和外部循环,你可以摆脱字典查找,你可以内联 Math.Max 和 Math.Min。在我的小测试中,我通过将 Retangle 从类更改为结构(并使成员字段而不是属性)获得了最大的好处。

标签: c# sql performance distance overlap


【解决方案1】:

SQL Server 具有geometry 数据类型。这有计算多边形交集和并集的方法。

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2021-12-17
    • 2017-04-24
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    相关资源
    最近更新 更多