def SOLVER(roadnet, surplus, objectives):
    network = mygraph()
    capacity = {}
    supply = {i: 0. for i in roadnet.nodes()}
    cost = {}  # functions
    #
    oneway_offset = {}  # for one-way roads

    for i, j, road, data in roadnet.edges_iter(keys=True, data=True):
        supply[j] += surplus[road]
        cost_data = objectives[road]

        # edge construction
        if data.get('oneway', False):
            # if one-way road

            # record minimum allowable flow on road
            zmin = cost_data.max_key()
            oneway_offset[road] = zmin
            supply[i] -= zmin
            supply[j] += zmin

            # shift and record the cost function on a forward edge only
            cc = roadbm.costWrapper(cost_data)
            cc_offset = offsetWrapper(cc, zmin)
            network.add_edge(road, i, j)
            cost[road] = cc_offset

        else:
            # if bi-directional road... currently, instantiate two edges
            cc = roadbm.costWrapper(cost_data)
            ncc = negativeWrapper(
                cc)  # won't have to worry about the C(0) offset

            network.add_edge((road, +1), i, j)
            cost[(road, +1)] = cc
            #
            network.add_edge((road, -1), j, i)
            cost[(road, -1)] = ncc

    # we need to compute the size U of the first cvxcost algorithm phase
    #U = sum([ len( obj_dict ) for obj_dict in objectives.values() ])
    # below is almost certainly just as good a bound, but I'm a scaredy-cat
    U = sum([len(obj_dict) - 2 for obj_dict in objectives.values()])

    f = MinConvexCostFlow(network, {}, supply, cost, U)

    flow = {}
    for i, j, road in roadnet.edges_iter(keys=True):
        if road in oneway_offset:
            flow[road] = f[road] + oneway_offset[road]
        else:
            flow[road] = f[(road, +1)] - f[(road, -1)]

        flow[road] = int(flow[road])

    #print flow
    return flow
def SOLVER(roadnet, surplus, objectives):
    network = mygraph()
    capacity = {}
    supply = {i: 0.0 for i in roadnet.nodes()}
    cost = {}  # functions
    #
    oneway_offset = {}  # for one-way roads

    for i, j, road, data in roadnet.edges_iter(keys=True, data=True):
        supply[j] += surplus[road]
        cost_data = objectives[road]

        # edge construction
        if data.get("oneway", False):
            # if one-way road

            # record minimum allowable flow on road
            zmin = cost_data.max_key()
            oneway_offset[road] = zmin
            supply[i] -= zmin
            supply[j] += zmin

            # shift and record the cost function on a forward edge only
            cc = roadbm.costWrapper(cost_data)
            cc_offset = offsetWrapper(cc, zmin)
            network.add_edge(road, i, j)
            cost[road] = cc_offset

        else:
            # if bi-directional road... currently, instantiate two edges
            cc = roadbm.costWrapper(cost_data)
            ncc = negativeWrapper(cc)  # won't have to worry about the C(0) offset

            network.add_edge((road, +1), i, j)
            cost[(road, +1)] = cc
            #
            network.add_edge((road, -1), j, i)
            cost[(road, -1)] = ncc

    # we need to compute the size U of the first cvxcost algorithm phase
    # U = sum([ len( obj_dict ) for obj_dict in objectives.values() ])
    # below is almost certainly just as good a bound, but I'm a scaredy-cat
    U = sum([len(obj_dict) - 2 for obj_dict in objectives.values()])

    f = MinConvexCostFlow(network, {}, supply, cost, U)

    flow = {}
    for i, j, road in roadnet.edges_iter(keys=True):
        if road in oneway_offset:
            flow[road] = f[road] + oneway_offset[road]
        else:
            flow[road] = f[(road, +1)] - f[(road, -1)]

        flow[road] = int(flow[road])

    # print flow
    return flow
Пример #3
0
def discreteCostTex(S, T, ymin, ymax, zmin, zmax, zplus=None):
    segment = roadbm.ONESEGMENT(S, T)
    meas = roadbm.MEASURE(segment, ymin, ymax)
    obj = roadbm.OBJECTIVE(meas)
    Cf = roadbm.costWrapper(obj)

    Z = range(zmin, zmax + 1)
    C = [Cf(z) for z in Z]
    cmin, cmax = min(C), max(C)

    # obtain the 'star'
    ZMIN, ZMAX = -max(meas), -min(meas)
    CC = [(Cf(z), z) for z in range(ZMIN, ZMAX + 1)]
    COPT, ZOPT = min(CC)

    #str = "\\begin{tikzpicture}[x=1cm,y=.1cm]\n"
    str = ''
    # draw the axis
    # horizontal
    horz_level = np.floor(cmin)
    data = {'zmin': zmin, 'zmax': zmax, 'horz': horz_level}
    fmt = "\\draw [->] (%(zmin)d,%(horz)d) -- (%(zmax)f,%(horz)d) node [right] {$\\coordvar$} ;\n"
    str += fmt % data
    # vertical
    str += "\\draw [->] (0,%(cmin)f-1) -- (0,%(cmax)f) " % {
        'cmin': cmin,
        'cmax': cmax
    }
    str += "node [above] {$\\cost(\\numarcs)$} ;\n"
    # horizontal ticks
    for tick in np.arange(zmin, zmax + 1, 1):
        data = {'tick': tick, 'horz': horz_level}
        str += "\\draw (%(tick)d,%(horz)f-.1) -- (%(tick)f,%(horz)f+.1) " % data
        str += "node [below=5] {$%d$} ;\n" % tick

    # scatter
    for z, c in zip(Z, C):
        if z == ZOPT:
            str += '\\draw [fill] (%f,%f) node {$\\star$} ;\n' % (
                z, c)  # {$\\circ$}'
        else:
            str += '\\draw [fill] (%f,%f) circle (.05cm) ;\n' % (
                z, c)  # {$\\circ$}'

    if zplus is not None and zmin <= zplus and zplus <= zmax:
        str += '\\draw [] (%f,%f) circle (.2cm) ;\n' % (zplus, Cf(zplus)
                                                        )  # {$\\circ$}'

    #str += "\\end{tikzpicture}\n"
    return str
Пример #4
0
def discreteCostTex( S, T, ymin, ymax, zmin, zmax, zplus=None ) :
    segment = roadbm.ONESEGMENT( S, T )
    meas = roadbm.MEASURE( segment, ymin, ymax )
    obj = roadbm.OBJECTIVE( meas )
    Cf = roadbm.costWrapper( obj )
    
    Z = range(zmin,zmax+1)
    C = [ Cf(z) for z in Z ]
    cmin, cmax = min(C), max(C)
    
    # obtain the 'star'
    ZMIN, ZMAX = -max( meas ), -min( meas )
    CC = [ ( Cf(z), z ) for z in range(ZMIN,ZMAX+1) ]
    COPT, ZOPT = min(CC)
    
    
    #str = "\\begin{tikzpicture}[x=1cm,y=.1cm]\n"
    str = ''
    # draw the axis
    # horizontal
    horz_level = np.floor(cmin)
    data = { 'zmin' : zmin, 'zmax' : zmax, 'horz' : horz_level }
    fmt = "\\draw [->] (%(zmin)d,%(horz)d) -- (%(zmax)f,%(horz)d) node [right] {$\\coordvar$} ;\n" 
    str +=  fmt % data
    # vertical
    str += "\\draw [->] (0,%(cmin)f-1) -- (0,%(cmax)f) " % { 'cmin' : cmin, 'cmax' : cmax }
    str += "node [above] {$\\cost(\\numarcs)$} ;\n"
    # horizontal ticks
    for tick in np.arange(zmin,zmax+1,1) :
        data = { 'tick' : tick, 'horz' : horz_level }
        str += "\\draw (%(tick)d,%(horz)f-.1) -- (%(tick)f,%(horz)f+.1) " % data
        str += "node [below=5] {$%d$} ;\n" % tick
        
    # scatter
    for z, c in zip( Z, C ) :
        if z == ZOPT :
            str += '\\draw [fill] (%f,%f) node {$\\star$} ;\n' % (z,c)       # {$\\circ$}'
        else :
            str += '\\draw [fill] (%f,%f) circle (.05cm) ;\n' % (z,c)       # {$\\circ$}'
        
    if zplus is not None and zmin <= zplus and zplus <= zmax :
        str += '\\draw [] (%f,%f) circle (.2cm) ;\n' % (zplus,Cf(zplus))       # {$\\circ$}'
        
    #str += "\\end{tikzpicture}\n"
    return str
Пример #5
0
def costFunctionTex(S, T, ymin, ymax, z=None):
    if z is None:
        zplus = 0
    else:
        zplus = z
    """ C to tikz """
    segment = roadbm.ONESEGMENT(S, T)
    meas = roadbm.MEASURE(segment, ymin, ymax)
    obj = roadbm.OBJECTIVE(meas)
    Cf = roadbm.costWrapper(obj)

    str = "\\begin{tikzpicture}[x=2cm,y=.1cm]\n"

    # draw the axis
    ZMIN, ZMAX = -max(meas), -min(meas)
    WIDTH = ZMAX - ZMIN

    C = [(Cf(z), z) for z in range(ZMIN, ZMAX + 1)]
    CMIN, ZOPT = min(C)
    CMAX, _ = max(C)
    COPT = Cf(ZOPT)

    ZMIN = int(min(ZMIN, np.floor(zplus)))
    ZMAX = int(max(ZMAX, np.ceil(zplus)))

    # horizontal
    horz_level = np.floor(CMIN)
    data = {'zmin': ZMIN, 'zmax': ZMAX, 'horz': horz_level}
    fmt = "\\draw [->] (%(zmin)d,%(horz)d) -- (%(zmax)f,%(horz)d) node [right] {$\\coordvar$} ;\n"
    str += fmt % data
    # vertical
    str += "\\draw [->] (0,%(cmin)f-1) -- (0,%(cmax)f) " % {
        'cmin': CMIN,
        'cmax': CMAX
    }
    str += "node [above] {$\\cost(\\numarcs)$} ;\n"
    # horizontal ticks
    for tick in np.arange(ZMIN, ZMAX + 1, 1):
        data = {'tick': tick, 'horz': horz_level}
        str += "\\draw (%(tick)d,%(horz)f-.1) -- (%(tick)f,%(horz)f+.1) " % data
        str += "node [below=5] {$%d$} ;\n" % tick

    # draw dashed C curve
    def drawpieces(ZZ, style):
        str = ''
        for z1, z2 in zip(ZZ[:-1], ZZ[1:]):
            c1 = Cf(z1)
            c2 = Cf(z2)

            data = {'z1': z1, 'z2': z2, 'c1': c1, 'c2': c2, 'style': style}
            str += "\\draw [%(style)s] (%(z1)f,%(c1)f) -- (%(z2)f,%(c2)f) ;\n" % data

        return str

    # draw dashed C, all of it
    str += drawpieces(range(ZMIN, ZMAX + 1), 'dashed')
    # draw C so far
    lastz = int(np.floor(zplus))
    str += drawpieces(range(ZMIN, lastz + 1) + [zplus], 'thick')

    # add local vertical line
    str += texvline(zplus, horz_level - 1, max(CMAX, Cf(zplus)) + 1, 'dashed')
    # add optimal
    str += "\\draw node at (%(z)f,%(C)f) {$\\star$} ;\n" % {
        'z': ZOPT,
        'C': COPT
    }

    str += "\\end{tikzpicture}\n"
    return str
Пример #6
0
def costFunctionTex( S, T, ymin, ymax, z=None ) :
    if z is None :
        zplus = 0
    else :
        zplus = z
        
    """ C to tikz """
    segment = roadbm.ONESEGMENT( S, T )
    meas = roadbm.MEASURE( segment, ymin, ymax )
    obj = roadbm.OBJECTIVE( meas )
    Cf = roadbm.costWrapper( obj )
    
    str = "\\begin{tikzpicture}[x=2cm,y=.1cm]\n"
    
    # draw the axis
    ZMIN, ZMAX = -max( meas ), -min( meas )
    WIDTH = ZMAX - ZMIN
    
    C = [ ( Cf(z), z ) for z in range(ZMIN,ZMAX+1) ]
    CMIN, ZOPT = min(C)
    CMAX, _ = max(C)
    COPT = Cf(ZOPT)
    
    ZMIN = int( min( ZMIN, np.floor( zplus ) ) )
    ZMAX = int( max( ZMAX, np.ceil( zplus ) ) )
    
    # horizontal
    horz_level = np.floor(CMIN)
    data = { 'zmin' : ZMIN, 'zmax' : ZMAX, 'horz' : horz_level }
    fmt = "\\draw [->] (%(zmin)d,%(horz)d) -- (%(zmax)f,%(horz)d) node [right] {$\\coordvar$} ;\n" 
    str +=  fmt % data
    # vertical
    str += "\\draw [->] (0,%(cmin)f-1) -- (0,%(cmax)f) " % { 'cmin' : CMIN, 'cmax' : CMAX }
    str += "node [above] {$\\cost(\\numarcs)$} ;\n"
    # horizontal ticks
    for tick in np.arange(ZMIN,ZMAX+1,1) :
        data = { 'tick' : tick, 'horz' : horz_level }
        str += "\\draw (%(tick)d,%(horz)f-.1) -- (%(tick)f,%(horz)f+.1) " % data
        str += "node [below=5] {$%d$} ;\n" % tick
        
    # draw dashed C curve
    def drawpieces( ZZ, style ) :
        str = ''
        for z1,z2 in zip( ZZ[:-1], ZZ[1:] ) :
            c1 = Cf(z1)
            c2 = Cf(z2)
            
            data = { 'z1' : z1, 'z2' : z2, 'c1' : c1, 'c2' : c2, 'style' : style }
            str += "\\draw [%(style)s] (%(z1)f,%(c1)f) -- (%(z2)f,%(c2)f) ;\n" % data
            
        return str
            
    # draw dashed C, all of it
    str += drawpieces( range(ZMIN,ZMAX+1), 'dashed' )
    # draw C so far
    lastz = int( np.floor( zplus ) )
    str += drawpieces( range(ZMIN,lastz+1) + [ zplus ], 'thick' )
    
    # add local vertical line
    str += texvline( zplus, horz_level-1, max( CMAX, Cf(zplus) ) + 1, 'dashed' )
    # add optimal
    str += "\\draw node at (%(z)f,%(C)f) {$\\star$} ;\n" % { 'z' : ZOPT, 'C' : COPT }
    
    str += "\\end{tikzpicture}\n"
    return str