Function iaincspfnonopt

Description

Includes a new vertex in an existing SPF-Max in a non-optimal way.

Synopse

  • R = iaincspfnonopt(SPF, distances)
    • SPF: The SPF returned by one of SPF-constructing functions.
    • distances: A list containing the distances of the new sample to each of the existing samples in the SPF.
 1 def iaincspfnonopt(SPF, distances):
 2     """
 3     Parameters:
 4         SPF: the SPF returned by one of spf-constructing functions
 5         distances: a list containing the distances of the new sample to each of the existing samples in the SPF
 6 
 7     >>> from numpy import array
 8     >>> A = array([[10,  8,  9,  4,  6,  8],[ 8,  1,  6, 10,  4,  8],[ 9,  6, 10, 10,  3,  6],[ 4, 10, 10,  7,  4,  5],[ 6,  4,  3,  4,  2,  6],[ 8,  8,  6,  5,  6,  9]])
 9     >>> seeds = [3, 4]
10     >>> seeds_labels = [1, 2]
11     >>> SPF = iadijkstra(A, seeds, seeds_labels)
12     >>> distances = [2, 8, 9, 4, 10, 5]
13     >>> cost, parent, label = iaincspfnonopt(SPF, distances)
14     >>> print int(cost)
15     4
16     >>> print int(parent)
17     0
18     >>> print int(label)
19     1
20     """
21 
22     from numpy import array, newaxis, concatenate
23 
24     distances = array(distances)
25     costs = array(SPF[0])
26     labels = SPF[2]
27 
28     distances = distances[:, newaxis]
29     costs = costs[:, newaxis]
30 
31     paths = concatenate((distances, costs), axis=1)
32     newcosts = paths.max(axis=1)
33 
34     p = newcosts.argmin()
35     return newcosts[p], p, labels[p]

Examples

 1 from numpy import *
 2 from iaOPF import *
 3 
 4 from numpy import array
 5 A = array([[10,  8,  9,  4,  6,  8],[ 8,  1,  6, 10,  4,  8],[ 9,  6, 10, 10,  3,  6],[ 4, 10, 10,  7,  4,  5],[ 6,  4,  3,  4,  2,  6],[ 8,  8,  6,  5,  6,  9]])
 6 seeds = [3, 4]
 7 seeds_labels = [1, 2]
 8 SPF = iadijkstra(A, seeds, seeds_labels)
 9 distances = [2, 8, 9, 4, 10, 5]
10 cost, parent, label = iaincspfnonopt(SPF, distances)
11 print 'Cost to the new vertex: ', cost
12 print 'Parent chosen: ', parent
13 print 'Label assigned: ', label
Cost to the new vertex:  4.0
Parent chosen:  0
Label assigned:  1.0