Exemplo n.º 1
0
def divideGivenPolygon(func, polyObj):
    '''
    Divide a polygon object given objective function
    
    Parameters
    ----------
    func: callable
        objective function
    polyObj: :class:`PolygonObj`
        the polygon which we want to split

    Returns
    -------
    list:
        the set of new polygons as well as the origin
    '''

    # TODO: think about the case where we only divide a single
    # dimension.

    # each polyObj carries it's own little polygon
    # defined by the inequalities
    A, b = polyObj.getInequality()

    polyList = list()

    ###
    #
    # HERE, we divide the polygon into little triangles
    #
    ###

    # We obtain our convex hull and the points at the vertices
    V, dualHull, G, h, x0 = constraintToVertices(A,
                                                 b,
                                                 x0=None,
                                                 full_output=True)
    # and the convex hull in the primal space
    hull = scipy.spatial.ConvexHull(V, False)
    # random information
    numFace = len(b)
    numSimplices = len(hull.simplices[:, 0])
    numDimension = len(V[0, :])
    numVertices = len(V[:, 0])

    ###
    #
    # Find the center simplex
    #
    ###

    # we are going to find the distance between the vertices and the
    # hyperplanes defined by the inequalities
    dToPlane = distanceToPlane(V, A, b)
    #distanceToPlane = abs(A.dot(V.T) - numpy.reshape(b,(numFace,1)))
    indexList = numpy.linspace(0, numVertices - 1, numVertices).astype(int)

    # holder for the new vertices
    X = numpy.zeros((numFace, numDimension))
    for i in range(0, numFace):
        # building our new triangle/tetrahedron
        # find points not on the plane, subject to machine precision
        onPlane = abs(dToPlane[i]) <= 1e-8
        indexOnPlane = indexList[onPlane]
        newV = V[indexOnPlane, :]
        # take the average
        X[i] = numpy.mean(newV, axis=0)

    # we are going to create our new simplex that sits in the middle
    # of the original simplex
    newPolyObj = PolygonObj(func, None, None, X)
    centerA, centerB = newPolyObj.getInequality()

    ###
    #
    # Find the corner simplex
    #
    ###

    # we need to find out the closest plane from the middle simplex to each vertex
    # and also the planes that connects to the vertex
    ## getting the transpose because we want to go down the list of vertices

    centerDToPlane = distanceToPlane(X, centerA, centerB)
    # the number of faces doesn't change for a n+1-simplex
    centerX = numpy.zeros((numFace, numDimension))
    verticesList = list()
    for i in range(0, numFace):
        # building our new triangle/tetrahedron
        # find points not on the plane, subject to machine precision
        onPlane = abs(centerDToPlane[i]) <= 1e-8
        indexOnPlane = indexList[onPlane]
        newV = X[indexOnPlane, :]
        # take the average
        centerX[i] = numpy.mean(newV, axis=0)
        verticesList.append(indexOnPlane)

    # the distance from the middle of the hyperplane that defines the center
    # simplex and the hyperplanes.  only used for identifying which point
    # sits on which plane
    centerDToCenterPlane = (centerA.dot(centerX.T) - centerB).T

    # create the set of index for our hyperplanes
    planeIndexList = numpy.linspace(0, numFace - 1, numFace).astype(int)
    # holders
    simplexFromCenter = numpy.zeros((numFace, numDimension))
    for i in range(0, numVertices):
        # going to find the planes where vertex i sits on
        planeMeetVertices = abs(dToPlane.T[i]) <= 1e-8
        # then the index of those planes
        planesMeetVertex = planeIndexList[planeMeetVertices]
        Atemp = A[planesMeetVertex, :]
        btemp = b[planesMeetVertex]
        # then the new plane that completes the convex hull/simplex
        # centerPlaneIndex = numpy.argmin(dVToS[i])
        # print "Vertex " +str(i)
        # print Atemp
        # print btemp
        # Atemp = numpy.append(Atemp,numpy.reshape(centerA[centerPlaneIndex],(1,numDimension)),axis=0)
        # btemp = numpy.append(btemp,centerB[centerPlaneIndex])

        # the set of distance between vertex i and the points lying on the hyperplane
        # of the center simplex
        dVToCenterX = V[i] - centerX
        # index with the minimum distance
        index = numpy.argmin(numpy.linalg.norm(dVToCenterX, axis=1))
        # index of center simplex that completes the corner simplex
        index = numpy.argmin(abs(centerDToCenterPlane[index]))
        # print "Location of V[i]"
        # print V[i]
        # print "dVToCenterX"
        # print dVToCenterX
        # print "Actual distance from vertices to the center simplex mid point"
        # print numpy.linalg.norm(dVToCenterX,axis=1)
        # print "index of center simplex that completes the corner simplex"
        # print index
        # print "and the location of that is "
        # print centerX[index]
        # print "vertices of center simplex"
        # print X[verticesList[index]]

        XTemp = numpy.append(X[verticesList[index]],
                             numpy.reshape(V[i], (1, numDimension)),
                             axis=0)
        # Atemp = numpy.append(Atemp,numpy.reshape(centerA[index],(1,numDimension)),axis=0)
        # btemp = numpy.append(btemp,centerB[index])

        # print V[i]
        # print planeMeetVertices
        # print A
        # print b
        # add the object to the list
        # print centerPlaneIndex
        polyList.append(PolygonObj(func, None, None, XTemp))
        # find the location for simplex i
        simplexFromCenter[i] = polyList[i].getLocation()

    ###
    #
    # Consolidate information
    #
    ###

    # add in the original and declare it has already been split
    #originalPolyObj = polyObj.splitThis()
    #polyList.append(copy.deepcopy(originalPolyObj))
    # this should be a deepcopy because we remove the original
    # object later using their hash
    #TODO: add in closest polygon
    #polyObj = polyObj.splitThis(polyList[1::])
    newPolyObj = newPolyObj.splitThis(polyList)
    y, r, j = closestVector(simplexFromCenter, newPolyObj.getGrad())
    polyList[j].setDirectionFromParent()
    polyList.append(newPolyObj)

    polyList.append(copy.deepcopy(polyObj.splitThis()))

    return polyList
Exemplo n.º 2
0
def divideGivenPolygon(func,polyObj):
    '''
    Divide a polygon object given objective function
    
    Parameters
    ----------
    func: callable
        objective function
    polyObj: :class:`PolygonObj`
        the polygon which we want to split

    Returns
    -------
    list:
        the set of new polygons as well as the origin
    '''

    # TODO: think about the case where we only divide a single
    # dimension.

    # each polyObj carries it's own little polygon
    # defined by the inequalities
    A,b = polyObj.getInequality()
        
    polyList = list()

    ###
    # 
    # HERE, we divide the polygon into little triangles
    # 
    ### 

    # We obtain our convex hull and the points at the vertices
    V, dualHull, G, h, x0 = constraintToVertices(A,b,x0=None,full_output=True)
    # and the convex hull in the primal space
    hull = scipy.spatial.ConvexHull(V,False)
    # random information
    numFace = len(b)
    numSimplices = len(hull.simplices[:,0])
    numDimension = len(V[0,:])
    numVertices = len(V[:,0])

    ###
    #
    # Find the center simplex
    #
    ###
    
    # we are going to find the distance between the vertices and the
    # hyperplanes defined by the inequalities
    dToPlane = distanceToPlane(V,A,b)
    #distanceToPlane = abs(A.dot(V.T) - numpy.reshape(b,(numFace,1)))
    indexList = numpy.linspace(0,numVertices-1,numVertices).astype(int)

    # holder for the new vertices
    X = numpy.zeros((numFace,numDimension))
    for i in range(0,numFace):
        # building our new triangle/tetrahedron
        # find points not on the plane, subject to machine precision
        onPlane = abs(dToPlane[i])<=1e-8
        indexOnPlane = indexList[onPlane]
        newV = V[indexOnPlane,:]
        # take the average
        X[i] = numpy.mean(newV,axis=0)

    # we are going to create our new simplex that sits in the middle
    # of the original simplex
    newPolyObj = PolygonObj(func,None,None,X)
    centerA,centerB = newPolyObj.getInequality()
    
    ###
    #
    # Find the corner simplex
    #
    ###
    
    # we need to find out the closest plane from the middle simplex to each vertex
    # and also the planes that connects to the vertex
    ## getting the transpose because we want to go down the list of vertices
    
    
    centerDToPlane = distanceToPlane(X,centerA,centerB)
    # the number of faces doesn't change for a n+1-simplex
    centerX = numpy.zeros((numFace,numDimension))
    verticesList = list()
    for i in range(0,numFace):
        # building our new triangle/tetrahedron
        # find points not on the plane, subject to machine precision
        onPlane = abs(centerDToPlane[i])<=1e-8
        indexOnPlane = indexList[onPlane]
        newV = X[indexOnPlane,:]
        # take the average
        centerX[i] = numpy.mean(newV,axis=0)
        verticesList.append(indexOnPlane)

    # the distance from the middle of the hyperplane that defines the center 
    # simplex and the hyperplanes.  only used for identifying which point
    # sits on which plane
    centerDToCenterPlane = (centerA.dot(centerX.T) - centerB).T

    # create the set of index for our hyperplanes
    planeIndexList = numpy.linspace(0,numFace-1,numFace).astype(int)
    # holders
    simplexFromCenter = numpy.zeros((numFace,numDimension))
    for i in range(0,numVertices):
        # going to find the planes where vertex i sits on
        planeMeetVertices = abs(dToPlane.T[i])<=1e-8
        # then the index of those planes
        planesMeetVertex = planeIndexList[planeMeetVertices]
        Atemp = A[planesMeetVertex,:]
        btemp = b[planesMeetVertex]
        # then the new plane that completes the convex hull/simplex
        # centerPlaneIndex = numpy.argmin(dVToS[i])
        # print "Vertex " +str(i)
        # print Atemp
        # print btemp
        # Atemp = numpy.append(Atemp,numpy.reshape(centerA[centerPlaneIndex],(1,numDimension)),axis=0)
        # btemp = numpy.append(btemp,centerB[centerPlaneIndex])

        # the set of distance between vertex i and the points lying on the hyperplane
        # of the center simplex
        dVToCenterX = V[i] - centerX
        # index with the minimum distance
        index = numpy.argmin(numpy.linalg.norm(dVToCenterX,axis=1))
        # index of center simplex that completes the corner simplex
        index = numpy.argmin(abs(centerDToCenterPlane[index]))
        # print "Location of V[i]"
        # print V[i]
        # print "dVToCenterX"
        # print dVToCenterX
        # print "Actual distance from vertices to the center simplex mid point"
        # print numpy.linalg.norm(dVToCenterX,axis=1)
        # print "index of center simplex that completes the corner simplex"
        # print index
        # print "and the location of that is "
        # print centerX[index]
        # print "vertices of center simplex"
        # print X[verticesList[index]]

        XTemp = numpy.append(X[verticesList[index]],numpy.reshape(V[i],(1,numDimension)),axis=0)
        # Atemp = numpy.append(Atemp,numpy.reshape(centerA[index],(1,numDimension)),axis=0)
        # btemp = numpy.append(btemp,centerB[index])

        
        # print V[i]
        # print planeMeetVertices
        # print A
        # print b
        # add the object to the list
        # print centerPlaneIndex
        polyList.append(PolygonObj(func,None,None,XTemp))
        # find the location for simplex i
        simplexFromCenter[i] = polyList[i].getLocation()
        
    ###
    #
    # Consolidate information
    #
    ###

    # add in the original and declare it has already been split
    #originalPolyObj = polyObj.splitThis()
    #polyList.append(copy.deepcopy(originalPolyObj))
    # this should be a deepcopy because we remove the original
    # object later using their hash
    #TODO: add in closest polygon
    #polyObj = polyObj.splitThis(polyList[1::])
    newPolyObj = newPolyObj.splitThis(polyList)
    y,r,j = closestVector(simplexFromCenter,newPolyObj.getGrad())
    polyList[j].setDirectionFromParent()
    polyList.append(newPolyObj)

    polyList.append(copy.deepcopy(polyObj.splitThis()))

    return polyList
Exemplo n.º 3
0
def triangulatePolygon(func, polyObj):
    '''
    Triangulate a polygon object given objective function
    
    Parameters
    ----------
    func: callable
        objective function
    polyObj: :class:`PolygonObj`
        the polygon which we want to split

    Returns
    -------
    list:
        the set of new simplex as well as the origin
    '''

    # TODO: think about the case where we only divide a single
    # dimension.

    # each polyObj carries it's own little polygon
    # defined by the inequalities
    A, b = polyObj.getInequality()

    polyList = list()

    ###
    #
    # HERE, we divide the polygon into little triangles
    #
    ###

    # We obtain our convex hull and the points at the vertices
    V, dualHull, G, h, x0 = constraintToVertices(A,
                                                 b,
                                                 x0=None,
                                                 full_output=True)
    # and the convex hull in the primal space
    # print "Reached Here"
    # t0 = time.time()
    hull = scipy.spatial.ConvexHull(V, False)

    # t1 = time.time()
    # print t1 - t0
    numFace = len(b)
    numSimplices = len(hull.simplices[:, 0])
    numDimension = len(V[0, :])
    numVertices = len(V[:, 0])

    # now we split it up given the center

    # we are going to find the distance between the vertices and the
    # hyperplanes defined by the inequalities
    dToPlane = distanceToPlane(V, A, b)
    #distanceToPlane = abs(A.dot(V.T) - numpy.reshape(b,(numFace,1)))
    indexList = numpy.linspace(0, numVertices - 1, numVertices).astype(int)

    # print indexList
    X = numpy.zeros((numFace, numDimension))
    fx = numpy.zeros(numFace)
    for i in range(0, numFace):
        # print "face number " +str(i)
        # building our new triangle/tetrahedron
        # print distanceToPlane[i]
        # find points not on the plane, subject to machine precision
        # notOnPlane = abs(dToPlane[i])>=1e-8
        onPlane = abs(dToPlane[i]) <= 1e-8
        # print "\n Face number " +str(i)
        # print notOnPlane
        #indexNotOnPlane = indexList[notOnPlane==False]
        indexOnPlane = indexList[onPlane]
        #print indexList[indexNotOnPlane]
        newV = V[indexList[indexOnPlane], :]
        newV = numpy.append(newV,
                            numpy.reshape(numpy.array(x0), (1, numDimension)),
                            axis=0)

        # newA,newb = verticesToConstraint(newV)
        # print "Object " +str(i)
        # print newV
        # add our new object
        # polyList.append(copy.deepcopy(PolygonObj(func,newA,newb)))

        polyList.append(copy.deepcopy(PolygonObj(func, None, None, newV)))
        X[i] = polyList[i].getLocation()
        fx[i] = polyList[i].getFx()

    #TODO: add in closest polygon
    newPolyObj = polyObj.splitThis(polyList)
    y, r, j = closestVector(X, newPolyObj.getGrad())
    polyList[j].setDirectionFromParent()

    # add in the original and declare it has already been split
    polyList.append(copy.deepcopy(newPolyObj))

    return polyList
Exemplo n.º 4
0
def triangulatePolygon(func,polyObj):
    '''
    Triangulate a polygon object given objective function
    
    Parameters
    ----------
    func: callable
        objective function
    polyObj: :class:`PolygonObj`
        the polygon which we want to split

    Returns
    -------
    list:
        the set of new simplex as well as the origin
    '''

    # TODO: think about the case where we only divide a single
    # dimension.

    # each polyObj carries it's own little polygon
    # defined by the inequalities
    A,b = polyObj.getInequality()
        
    polyList = list()

    ###
    # 
    # HERE, we divide the polygon into little triangles
    # 
    ### 

    # We obtain our convex hull and the points at the vertices
    V, dualHull, G, h, x0 = constraintToVertices(A,b,x0=None,full_output=True)
    # and the convex hull in the primal space
    # print "Reached Here"
    # t0 = time.time()
    hull = scipy.spatial.ConvexHull(V,False)

    # t1 = time.time()
    # print t1 - t0
    numFace = len(b)
    numSimplices = len(hull.simplices[:,0])
    numDimension = len(V[0,:])
    numVertices = len(V[:,0])

    # now we split it up given the center
    
    # we are going to find the distance between the vertices and the
    # hyperplanes defined by the inequalities
    dToPlane = distanceToPlane(V,A,b)
    #distanceToPlane = abs(A.dot(V.T) - numpy.reshape(b,(numFace,1)))
    indexList = numpy.linspace(0,numVertices-1,numVertices).astype(int)

    # print indexList
    X = numpy.zeros((numFace,numDimension))
    fx = numpy.zeros(numFace)
    for i in range(0,numFace):
        # print "face number " +str(i)
        # building our new triangle/tetrahedron
        # print distanceToPlane[i]
        # find points not on the plane, subject to machine precision
        # notOnPlane = abs(dToPlane[i])>=1e-8
        onPlane = abs(dToPlane[i])<=1e-8
        # print "\n Face number " +str(i)
        # print notOnPlane
        #indexNotOnPlane = indexList[notOnPlane==False]
        indexOnPlane = indexList[onPlane]
        #print indexList[indexNotOnPlane]
        newV = V[indexList[indexOnPlane],:]
        newV = numpy.append(newV,numpy.reshape(numpy.array(x0),(1,numDimension)),axis=0)

        # newA,newb = verticesToConstraint(newV)
        # print "Object " +str(i)
        # print newV
        # add our new object
        # polyList.append(copy.deepcopy(PolygonObj(func,newA,newb)))

        polyList.append(copy.deepcopy(PolygonObj(func,None,None,newV)))
        X[i] = polyList[i].getLocation()
        fx[i] = polyList[i].getFx()

    #TODO: add in closest polygon
    newPolyObj = polyObj.splitThis(polyList)
    y,r,j = closestVector(X,newPolyObj.getGrad())
    polyList[j].setDirectionFromParent()

    # add in the original and declare it has already been split
    polyList.append(copy.deepcopy(newPolyObj))
        
    return polyList