Halcon实现一维DBSCAN算法

procedure DBSCAN (: : Props, Epsilon, MinPoints: Classification)

* init
l := |Props|
tuple_gen_const(l, 0,clusters)
tuple_gen_const(l, 0, points)
tuple_gen_const(l, 1, pointTypes)
tuple_gen_const(l, 0, visiteds)
corePoints := []

* calculate points
for i := 0 to l-1 by 1
    clusters[i] := i
    for j := i+1 to l-1 by 1
        tuple_fabs(Props[i]-Props[j], delta)
        if(delta             points[i] := points[i]+1
            points[j] := points[j]+1
        endif
    endfor
endfor

*core point
for i := 0 to l-1 by 1
    if(points[i]>= MinPoints)
        pointTypes[i] := 3
        corePoints := [corePoints, i]
    endif
endfor

* joint core point
n := |corePoints|-1
corePts.at(l-1) := []
for i := 0 to n by 1
    for j := i+1 to n by 1
        tuple_fabs(Props[corePoints[i]]-Props[corePoints[j]], delta)
        if(delta             corePts.at(i) := [corePts.at(i),j]
            corePts.at(j) := [corePts.at(j),i]
        endif
    endfor
endfor

for i:= 0 to n by 1
    ps :=[]
    if(visiteds[corePoints[i]] == 1)
        continue
    else
        ps := [ps, i]
        while(|ps|>0)
            id := ps[|ps|-1]
            visiteds[corePoints[id]] := 1
            tuple_remove(ps, |ps|-1, ps)
            m := |corePts.at(id)|-1
            corePt := corePts.at(id)
            for j := 0 to m by 1
                if(visiteds[corePoints[corePt[j]]] == 1)
                    continue
                else
                    clusters[corePoints[corePt[j]]] := clusters[corePoints[id]]
                    visiteds[corePoints[corePt[j]]] := 1
                    ps :=[ps, corePt[j]]
                endif
            endfor
        endwhile
    endif
endfor

* border point, joint border point to core point

for i := 0 to l-1 by 1
    if (pointTypes[i] == 3)
        continue
    else
        for j := 0 to n by 1
            tuple_fabs(Props[i]-Props[corePoints[j]], delta)
            if(delta                 pointTypes[i] := 2
                clusters[i] := clusters[corePoints[j]]
                break
            endif
        endfor
    endif
endfor

labels :=[]
Classification.at(0) := []
for i := 0 to l-1 by 1
    cluster := clusters[i]
    tuple_find(labels, cluster,Indices)
    if(Indices>=0)        
        Classification.at(Indices) := [Classification.at(Indices), i]
    else
        labels :=[labels, cluster]
        Classification.at(|labels|-1) := i
    endif
endfor
return()

你可能感兴趣的:(机器学习)