Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate distance to smoothed line

I'm trying to find the distance of a point (in 4 dimensions, only 2 are shown here) (any coloured crosses in the figure) to a supposed Pareto frontier (black line). This line represents the best Pareto frontier representation during an optimization process.

Pareto = [[0.3875575798354123, -2.4122340425531914], [0.37707675586149786, -2.398936170212766], [0.38176077842761763, -2.4069148936170213], [0.4080534133844003, -2.4914285714285715], [0.35963459448268725, -2.3631532329495126], [0.34395217638838566, -2.3579931972789114], [0.32203302106516224, -2.344858156028369], [0.36742404637441123, -2.3886054421768708], [0.40461156254852226, -2.4141156462585034], [0.36387868122767975, -2.375], [0.3393199109776927, -2.348404255319149]]

Right now, I calculate the distance from any point to the Pareto frontier like this:

def dominates(row, rowCandidate):
return all(r >= rc for r, rc in zip(row, rowCandidate))

def dist2Pareto(pareto,candidate):
    listDist = []

    dominateN = 0
    dominatePoss = 0
    if len(pareto) >= 2:
        for i in pareto:
            if i != candidate:
                dominatePoss += 1
                dominate = dominates(candidate,i)
                if dominate == True:
                    dominateN += 1
                listDist.append(np.linalg.norm(np.array(i)-np.array(candidate)))

        listDist.sort()

        if dominateN == len(pareto):
            print "beyond"            
            return listDist[0]  
        else:
            return listDist[0]

Where I calculate the distance to each point of the black line, and retrieve the shortest distance (distance to the closest point of the known Frontier).

However, I feel I should calculate the distance to the closest line segment instead. How would I go about achieving this?

enter image description here

like image 237
Lucien S. Avatar asked Nov 09 '22 01:11

Lucien S.


1 Answers

The formula for the coordinates of the nearest point on the line is given here. Specifically, you are interested in the one called "line defined by two points". For posterity, the formula is:

Formula for distance between a line defined by two points, and a third point

Because the frontier is relatively simple, you can loop through each two-point line segment in the frontier, and calculate the closest distance for each, keeping the smallest. You could introduce other constraints / pre-computations to limit the number of calculations required.

like image 77
Benjamin Avatar answered Nov 14 '22 22:11

Benjamin