def main():
    # for measuring the required time for reading input files
    inputreaderstart = datetime.datetime.now()
    foutlog = open('%s_log.txt' % options.type, 'w')
    foutlog.write(
        'The stochastic user equilibrium traffic assignment will be executed with the %s model.\n'
        % options.type)
    foutlog.write(
        'All vehicular releasing times are determined randomly(uniform).\n')

    matrices = options.mtxpsfile.split(",")
    parser = make_parser()

    if options.verbose:
        print("Reading net")
    print('net file:', options.netfile)
    net = Net()
    sumolib.net.readNet(options.netfile, net=net)
    parser.setContentHandler(DistrictsReader(net))
    parser.parse(options.confile)
    if options.sigfile:
        parser.setContentHandler(ExtraSignalInformationReader(net))
        parser.parse(options.sigfile)
    foutlog.write('- Reading network: done.\n')
    foutlog.write('number of total startVertices:%s\n' % net.getstartCounts())
    foutlog.write('number of total endVertices:%s\n' % net.getendCounts())
    if options.verbose:
        print(net.getfullEdgeCounts(), "edges read (internal edges included)")

    if options.curvefile:
        updateCurveTable(options.curvefile)

    if options.hours == 24.:
        assignHours = 16.
    else:
        assignHours = options.hours

    for edge in net.getEdges():
        if edge._lanes:
            edge.getCapacity()
            edge.getAdjustedCapacity(net)
            edge.estcapacity *= assignHours
            edge.getConflictLink()

    if options.dijkstra == 'boost':
        net.createBoostGraph()
    if options.verbose:
        print("after link reduction:", net.getfullEdgeCounts(), "edges read")

    # calculate link travel time for all district connectors
    getConnectionTravelTime(net._startVertices, net._endVertices)

    foutlog.write('- Initial calculation of link parameters : done.\n')
    # the required time for reading the network
    timeForInput(inputreaderstart)

    if options.debug:
        outputNetwork(net)

    # initialize the map for recording the number of the assigned vehicles
    AssignedVeh = {}
    # initialize the map for recording the number of the assigned trips
    AssignedTrip = {}
    smallDemand = []
    linkChoiceMap = {}
    odPairsMap = {}
    for start, startVertex in enumerate(net._startVertices):
        AssignedVeh[startVertex] = {}
        AssignedTrip[startVertex] = {}
        smallDemand.append([])
        for end, endVertex in enumerate(net._endVertices):
            AssignedVeh[startVertex][endVertex] = 0
            AssignedTrip[startVertex][endVertex] = 0.
            smallDemand[-1].append(0.)

    # initialization
    vehID = 0
    matrixSum = 0.0
    lohse = (options.type == "lohse")
    incremental = (options.type == "incremental")
    checkKPaths = False

    if not incremental and options.kPaths > 1:
        checkKPaths = True
    if not incremental:
        net.initialPathSet()

    starttime = datetime.datetime.now()
    # initialize the file for recording the routes
    if options.odestimation:
        net.getDetectedEdges(options.outputdir)
    else:
        foutroute = open('routes.rou.xml', 'w')
        print("""<?xml version="1.0"?>
<!-- generated on %s by $Id: Assignment.py 25261 2017-07-19 06:52:21Z behrisch $ -->
<routes>""" % starttime,
              file=foutroute)

    # for counter in range (0, len(matrices)):
    for counter, matrix in enumerate(matrices):
        # delete all vehicle information related to the last matrix for saving
        # the disk space
        vehicles = []
        iterInterval = 0
        matrixPshort, startVertices, endVertices, CurrentMatrixSum, begintime, assignPeriod, Pshort_EffCells, matrixSum, smallDemandRatio = getMatrix(
            net, options.verbose, matrix, matrixSum, options.demandscale)
        options.hours = float(assignPeriod)
        smallDemandPortion = math.ceil(
            float(options.maxiteration) / 2. * smallDemandRatio)
        if float(smallDemandPortion) != 0.:
            iterInterval = math.ceil(
                float(options.maxiteration) / float(smallDemandPortion))

        departtime = begintime * 3600

        if options.verbose:
            print('the analyzed matrices:', counter)
            print('Begintime:', begintime, "O'Clock")
            print('departtime', departtime)
            print('Matrix und OD Zone already read for Interval', counter)
            print('CurrentMatrixSum:', CurrentMatrixSum)

        foutlog.write('Reading matrix and O-D zones: done.\n')
        foutlog.write('Matrix und OD Zone already read for Interval:%s\n' %
                      counter)
        foutlog.write('CurrentMatrixSum:%s\n' % CurrentMatrixSum)
        foutlog.write('number of current startVertices:%s\n' %
                      len(startVertices))
        foutlog.write('number of current endVertices:%s\n' % len(endVertices))

        if options.odestimation:
            linkChoiceMap.clear()
            odPairsMap.clear()
            linkChoiceMap = initLinkChoiceMap(net, startVertices, endVertices,
                                              matrixPshort, linkChoiceMap,
                                              odPairsMap)

        for edge in net.getEdges():
            edge.flow = 0.
            edge.helpflow = 0.
            edge.actualtime = edge.freeflowtime
            edge.helpacttime = edge.freeflowtime
            edge.fTT = 0.
            edge.TT = 0.
            edge.delta = 0.
            edge.helpacttimeEx = 0.

        # the number of origins, the umber of destinations and the number of
        # the OD pairs
        origins = len(startVertices)
        dests = len(endVertices)
        ODpairs = origins * dests

        # output the origin and destination zones and the number of effective
        # OD pairs
        if options.debug:
            # matrixCounter)
            outputODZone(startVertices, endVertices, Pshort_EffCells, counter)

        if incremental:
            print('begin the incremental assignment!')
            iter = 0
            options.lamda = 0.
            while iter < options.maxiteration:
                foutlog.write('- Current iteration(not executed yet):%s\n' %
                              iter)
                iter += 1
                if iterInterval != 0 and operator.mod(iter, iterInterval) == 0:
                    assignSmallDemand = True
                else:
                    assignSmallDemand = False
                for start, startVertex in enumerate(startVertices):
                    targets = set()
                    for end, endVertex in enumerate(endVertices):
                        if assignSmallDemand and matrixPshort[start][
                                end] > 0. and matrixPshort[start][end] < 1.:
                            smallDemand[start][end] = matrixPshort[start][
                                end] / float(smallDemandPortion)

                        if matrixPshort[start][end] > 1. or (
                                assignSmallDemand
                                and smallDemand[start][end] > 0.):
                            targets.add(endVertex)

                    if len(targets) > 0:
                        if options.dijkstra == 'boost':
                            D, P = dijkstraBoost(net._boostGraph,
                                                 startVertex.boost)
                        elif options.dijkstra == 'plain':
                            D, P = dijkstraPlain(startVertex, targets)
                        elif options.dijkstra == 'extend':
                            D, P = dijkstra(startVertex, targets)
                        vehID, smallDemand, linkChoiceMap = doIncAssign(
                            net, vehicles, options.verbose,
                            options.maxiteration, options.odestimation,
                            endVertices, start, startVertex, matrixPshort,
                            smallDemand, D, P, AssignedVeh, AssignedTrip,
                            vehID, assignSmallDemand, linkChoiceMap,
                            odPairsMap)

                if options.dijkstra != 'extend':
                    linkMap = net._fullEdges
                else:
                    linkMap = net._edges
                for edge in linkMap.itervalues():
                    edge.getActualTravelTime(options, False)
                    if options.dijkstra == 'boost':
                        edge.boost.weight = edge.helpacttime
        else:
            print('begin the', options.type, " assignment!")
            # initialization for the clogit and the lohse assignment model
            iter_outside = 1
            newRoutes = 1
            stable = False
            first = True
            # begin the traffic Assignment
            while newRoutes > 0:
                foutlog.write('- SUE iteration:%s\n' % iter_outside)
                # Generate the effective routes als intital path solutions,
                # when considering k shortest paths (k is defined by the user.)
                if checkKPaths:
                    checkPathStart = datetime.datetime.now()
                    newRoutes = net.calcKPaths(options.verbose, options.kPaths,
                                               newRoutes, startVertices,
                                               endVertices, matrixPshort,
                                               options.gamma)
                    checkPathEnd = datetime.datetime.now() - checkPathStart
                    foutlog.write(
                        '- Time for finding the k-shortest paths: %s\n' %
                        checkPathEnd)
                    foutlog.write(
                        '- Finding the k-shortest paths for each OD pair: done.\n'
                    )
                    if options.verbose:
                        print('iter_outside:', iter_outside)
                        print('number of k shortest paths:', options.kPaths)
                        print('number of new routes:', newRoutes)

                elif not checkKPaths and iter_outside == 1 and counter == 0:
                    print('search for the new path')
                    newRoutes = net.findNewPath(startVertices, endVertices,
                                                newRoutes, matrixPshort,
                                                options.gamma, lohse,
                                                options.dijkstra)

                checkKPaths = False

                if options.verbose:
                    print('iter_outside:', iter_outside)
                    print('number of new routes:', newRoutes)

                stable = False
                iter_inside = 1
                while not stable:
                    if options.verbose:
                        print('iter_inside:', iter_inside)
                    stable = doSUEAssign(net, options, startVertices,
                                         endVertices, matrixPshort,
                                         iter_inside, lohse, first)
                    # The matrixPlong and the matrixTruck should be added when
                    # considering the long-distance trips and the truck trips.
                    if lohse:
                        stable = doLohseStopCheck(net, options, stable,
                                                  iter_inside,
                                                  options.maxiteration,
                                                  foutlog)

                    iter_inside += 1

                    if options.verbose:
                        print('stable:', stable)

                newRoutes = net.findNewPath(startVertices, endVertices,
                                            newRoutes, matrixPshort,
                                            options.gamma, lohse,
                                            options.dijkstra)

                first = False
                iter_outside += 1

                if newRoutes < 3 and iter_outside > int(
                    (options.maxiteration) / 2):
                    newRoutes = 0

                if iter_outside > options.maxiteration:
                    print('The max. number of iterations is reached!')
                    foutlog.write(
                        'The max. number of iterations is reached!\n')
                    foutlog.write(
                        'The number of new routes and the parameter stable will be set to zero and True respectively.\n'
                    )
                    print('newRoutes:', newRoutes)
                    stable = True
                    newRoutes = 0

            # update the path choice probability and the path flows as well as
            # generate vehicle data
            vehID = doSUEVehAssign(net, vehicles, options, counter,
                                   matrixPshort, startVertices, endVertices,
                                   AssignedVeh, AssignedTrip, vehID, lohse)

        # output the generated vehicular releasing times and routes, based on
        # the current matrix
        print('done with the assignment')  # debug
        if options.odestimation:
            linkChoicesOutput(net, startVertices, endVertices, matrixPshort,
                              linkChoiceMap, odPairsMap, options.outputdir,
                              starttime)
        else:
            sortedVehOutput(vehicles, departtime, options, foutroute)

    if not options.odestimation:
        foutroute.write('</routes>\n')
        foutroute.close()

    # output the global performance indices
    assigntime = outputStatistics(net, starttime, len(matrices))

    foutlog.write(
        '- Assignment is completed and all required information is generated. '
    )
    foutlog.close()

    if options.verbose:
        print('Duration for traffic assignment:', assigntime)
        print('Total assigned vehicles:', vehID)
    print('Total number of the assigned trips:', matrixSum)
def main(options):
    parser = make_parser()
    isBZ2 = False
    dataDir = options.datadir
    districts = os.path.join(dataDir, options.districtfile)
    matrix = os.path.join(dataDir, options.mtxfile)
    netfile = os.path.join(dataDir, options.netfile)
    print 'generate Trip file for:', netfile

    if "bz2" in netfile:
        netfile = bz2.BZ2File(netfile)
        isBZ2 = True

    matrixSum = 0.
    tripList = []
    net = Net()
    odConnTable = {}
    vehIDtoODMap = {}

    sumolib.net.readNet(options.netfile, net=net)

    if isBZ2:
        parser.parse(StringIO.StringIO(netfile.read()))
        netfile.close()
    else:
        parser.parse(netfile)

    parser.setContentHandler(DistrictsReader(net))
    parser.parse(districts)

    matrixPshort, startVertices, endVertices, currentMatrixSum, begin, period = getMatrix(
        net, options.debug, matrix, matrixSum)[:6]

    for edge in net.getEdges():
        edge.helpacttime = 0.

    if options.debug:
        print len(net._edges), "edges read"
        print len(net._startVertices), "start vertices read"
        print len(net._endVertices), "target vertices read"
        print 'currentMatrixSum:', currentMatrixSum

    if options.getconns:
        if options.debug:
            print 'generate odConnTable'
        for start, startVertex in enumerate(startVertices):
            if startVertex._id not in odConnTable:
                odConnTable[startVertex._id] = {}

            for source in startVertex.sourceConnNodes:
                targets = net.getTargets()
                D, P = dijkstraPlain(source, targets)
                for end, endVertex in enumerate(endVertices):
                    if startVertex._id != endVertex._id and matrixPshort[start][end] > 0.:
                        if endVertex._id not in odConnTable[startVertex._id]:
                            odConnTable[startVertex._id][endVertex._id] = []
                        net.checkRoute(
                            startVertex, endVertex, start, end, P, odConnTable, source, options)
    else:
        if options.debug:
            print 'import and use the given odConnTable'
        sys.path.append(options.datadir)
        from odConnTables import odConnTable

    # output trips
    if options.verbose:
        print 'output the trip file'
    vehID = 0
    subVehID = 0
    random.seed(42)
    matrixSum = 0.
    fouttrips = file(options.tripfile, 'w')
    fouttrips.write('<?xml version="1.0"?>\n')
    print >> fouttrips, """<!-- generated on %s by $Id: generateTripsXml.py 18096 2015-03-17 09:50:59Z behrisch $ -->
    """ % datetime.datetime.now()
    fouttrips.write("<tripdefs>\n")

    if options.demandscale != 1.:
        print 'demand scale %s is used.' % options.demandscale
        for start in range(len(startVertices)):
            for end in range(len(endVertices)):
                matrixPshort[start][end] *= options.demandscale

    for start, startVertex in enumerate(startVertices):
        for end, endVertex in enumerate(endVertices):
            if startVertex._id != endVertex._id and matrixPshort[start][end] > 0.:
                counts = 0.
                if options.odestimation:
                    if matrixPshort[start][end] < 1.:
                        counts, vehID, tripList, vehIDtoODMap = addVeh(
                            counts, vehID, begin, period, odConnTable, startVertex, endVertex, tripList, vehIDtoODMap)
                    else:
                        matrixSum += matrixPshort[start][end]
                        while (counts < float(math.ceil(matrixPshort[start][end])) and (matrixPshort[start][end] - counts) > 0.5 and float(subVehID) < matrixSum)or float(subVehID) < matrixSum:
                            counts, vehID, tripList, vehIDtoODMap = addVeh(
                                counts, vehID, begin, period, odConnTable, startVertex, endVertex, tripList, vehIDtoODMap)
                            subVehID += 1
                else:
                    matrixSum += matrixPshort[start][end]
                    while (counts < float(math.ceil(matrixPshort[start][end])) and (matrixPshort[start][end] - counts) > 0.5 and float(vehID) < matrixSum) or float(vehID) < matrixSum:
                        counts, vehID, tripList, vehIDtoODMap = addVeh(
                            counts, vehID, begin, period, odConnTable, startVertex, endVertex, tripList, vehIDtoODMap)
    if options.debug:
        print 'total demand:', matrixSum
        print vehID, 'trips generated'
    tripList.sort(key=operator.attrgetter('depart'))

    departpos = "free"
    if __name__ == "__main__":
        departpos = options.departpos
    for trip in tripList:
        fouttrips.write('   <trip id="%s" depart="%s" from="%s" to="%s" fromtaz="%s" totaz="%s" departlane="free" departpos="%s" departspeed="max"/>\n'
                        % (trip.label, trip.depart, trip.sourceEdge, trip.sinkEdge, trip.sourceDistrict, trip.sinkDistrict, departpos))
    fouttrips.write("</tripdefs>")
    fouttrips.close()

    return odConnTable, vehIDtoODMap
Example #3
0
def main(options):
    parser = make_parser()
    isBZ2 = False
    dataDir = options.datadir
    districts = os.path.join(dataDir, options.districtfile)
    matrix = os.path.join(dataDir, options.mtxfile)
    netfile = os.path.join(dataDir, options.netfile)
    print('generate Trip file for:', netfile)

    if "bz2" in netfile:
        netfile = bz2.BZ2File(netfile)
        isBZ2 = True

    matrixSum = 0.
    tripList = []
    net = Net()
    odConnTable = {}
    vehIDtoODMap = {}

    sumolib.net.readNet(options.netfile, net=net)

    if isBZ2:
        parser.parse(StringIO.StringIO(netfile.read()))
        netfile.close()
    else:
        parser.parse(netfile)

    parser.setContentHandler(DistrictsReader(net))
    parser.parse(districts)

    matrixPshort, startVertices, endVertices, currentMatrixSum, begin, period = getMatrix(
        net, options.debug, matrix, matrixSum)[:6]

    for edge in net.getEdges():
        edge.helpacttime = 0.

    if options.debug:
        print(len(net._edges), "edges read")
        print(len(net._startVertices), "start vertices read")
        print(len(net._endVertices), "target vertices read")
        print('currentMatrixSum:', currentMatrixSum)

    if options.getconns:
        if options.debug:
            print('generate odConnTable')
        for start, startVertex in enumerate(startVertices):
            if startVertex._id not in odConnTable:
                odConnTable[startVertex._id] = {}

            for source in startVertex.sourceConnNodes:
                targets = net.getTargets()
                D, P = dijkstraPlain(source, targets)
                for end, endVertex in enumerate(endVertices):
                    if startVertex._id != endVertex._id and matrixPshort[start][end] > 0.:
                        if endVertex._id not in odConnTable[startVertex._id]:
                            odConnTable[startVertex._id][endVertex._id] = []
                        net.checkRoute(
                            startVertex, endVertex, start, end, P, odConnTable, source, options)
    else:
        if options.debug:
            print('import and use the given odConnTable')
        sys.path.append(options.datadir)
        from odConnTables import odConnTable

    # output trips
    if options.verbose:
        print('output the trip file')
    vehID = 0
    subVehID = 0
    random.seed(42)
    matrixSum = 0.
    fouttrips = open(options.tripfile, 'w')
    fouttrips.write('<?xml version="1.0"?>\n')
    print("""<!-- generated on %s by $Id: generateTripsXml.py 22608 2017-01-17 06:28:54Z behrisch $ -->
    """ % datetime.datetime.now(), file=fouttrips)
    fouttrips.write("<tripdefs>\n")

    if options.demandscale != 1.:
        print('demand scale %s is used.' % options.demandscale)
        for start in range(len(startVertices)):
            for end in range(len(endVertices)):
                matrixPshort[start][end] *= options.demandscale

    for start, startVertex in enumerate(startVertices):
        for end, endVertex in enumerate(endVertices):
            if startVertex._id != endVertex._id and matrixPshort[start][end] > 0.:
                counts = 0.
                if options.odestimation:
                    if matrixPshort[start][end] < 1.:
                        counts, vehID, tripList, vehIDtoODMap = addVeh(
                            counts, vehID, begin, period, odConnTable, startVertex, endVertex, tripList, vehIDtoODMap)
                    else:
                        matrixSum += matrixPshort[start][end]
                        while (counts < float(math.ceil(matrixPshort[start][end])) and (matrixPshort[start][end] - counts) > 0.5 and float(subVehID) < matrixSum)or float(subVehID) < matrixSum:
                            counts, vehID, tripList, vehIDtoODMap = addVeh(
                                counts, vehID, begin, period, odConnTable, startVertex, endVertex, tripList, vehIDtoODMap)
                            subVehID += 1
                else:
                    matrixSum += matrixPshort[start][end]
                    while (counts < float(math.ceil(matrixPshort[start][end])) and (matrixPshort[start][end] - counts) > 0.5 and float(vehID) < matrixSum) or float(vehID) < matrixSum:
                        counts, vehID, tripList, vehIDtoODMap = addVeh(
                            counts, vehID, begin, period, odConnTable, startVertex, endVertex, tripList, vehIDtoODMap)
    if options.debug:
        print('total demand:', matrixSum)
        print(vehID, 'trips generated')
    tripList.sort(key=operator.attrgetter('depart'))

    departpos = "free"
    if __name__ == "__main__":
        departpos = options.departpos
    for trip in tripList:
        fouttrips.write('   <trip id="%s" depart="%s" from="%s" to="%s" fromtaz="%s" totaz="%s" departlane="free" departpos="%s" departspeed="max"/>\n'
                        % (trip.label, trip.depart, trip.sourceEdge, trip.sinkEdge, trip.sourceDistrict, trip.sinkDistrict, departpos))
    fouttrips.write("</tripdefs>")
    fouttrips.close()

    return odConnTable, vehIDtoODMap
Example #4
0
def main():
    # for measuring the required time for reading input files
    inputreaderstart = datetime.datetime.now()
    foutlog = file('%s_log.txt' % options.type, 'w')
    foutlog.write('The stochastic user equilibrium traffic assignment will be executed with the %s model.\n' % options.type)
    foutlog.write('All vehicular releasing times are determined randomly(uniform).\n')
  
    matrices = options.mtxpsfile.split(",")
    parser = make_parser()
    
    if options.verbose:
        print "Reading net"
    print 'net file:', options.netfile
    net = Net()
    sumolib.net.readNet(options.netfile, net=net)
    parser.setContentHandler(DistrictsReader(net))
    parser.parse(options.confile)
    if options.sigfile:
        parser.setContentHandler(ExtraSignalInformationReader(net))
        parser.parse(options.sigfile)
    foutlog.write('- Reading network: done.\n')
    foutlog.write('number of total startVertices:%s\n' % net.getstartCounts())
    foutlog.write('number of total endVertices:%s\n' % net.getendCounts())
    if options.verbose:
        print net.getfullEdgeCounts(), "edges read (internal edges included)"

    if options.curvefile:
        updateCurveTable(options.curvefile)
        
    if options.hours == 24.:
        assignHours = 16.
    else:
        assignHours = options.hours 

    for edge in net.getEdges():
        if edge._lanes:
            edge.getCapacity()
            edge.getAdjustedCapacity(net)
            edge.estcapacity *= assignHours
            edge.getConflictLink()

    if options.dijkstra == 'boost':
        net.createBoostGraph()
    if options.verbose:
        print "after link reduction:", net.getfullEdgeCounts(), "edges read"

    # calculate link travel time for all district connectors 
    getConnectionTravelTime(net._startVertices, net._endVertices)
            
    foutlog.write('- Initial calculation of link parameters : done.\n')
    # the required time for reading the network     
    timeForInput(inputreaderstart)
    
    if options.debug:
        outputNetwork(net)
        
    # initialize the map for recording the number of the assigned vehicles
    AssignedVeh = {}
    # initialize the map for recording the number of the assigned trips
    AssignedTrip = {}
    smallDemand = []
    linkChoiceMap = {}
    odPairsMap = {}
    for start, startVertex in enumerate(net._startVertices):
        AssignedVeh[startVertex]={}
        AssignedTrip[startVertex]={}
        smallDemand.append([])
        for end, endVertex in enumerate(net._endVertices):
            AssignedVeh[startVertex][endVertex] = 0
            AssignedTrip[startVertex][endVertex] = 0.
            smallDemand[-1].append(0.)
            
    # initialization
    vehID = 0
    matrixSum = 0.0
    lohse = (options.type == "lohse")
    incremental = (options.type == "incremental")
    checkKPaths = False
    
    if not incremental and options.kPaths > 1:
        checkKPaths = True
    if not incremental:
        net.initialPathSet()
 
    starttime = datetime.datetime.now()
    # initialize the file for recording the routes
    if options.odestimation:
        net.getDetectedEdges(options.outputdir)
    else:
        foutroute = open('routes.rou.xml', 'w')
        print >> foutroute, """<?xml version="1.0"?>
<!-- generated on %s by $Id: Assignment.py 11700 2012-01-10 22:20:15Z behrisch $ -->
<routes>""" % starttime
    
    for counter, matrix in enumerate(matrices):  #for counter in range (0, len(matrices)):
        # delete all vehicle information related to the last matrix for saving the disk space
        vehicles = []
        iterInterval = 0
        matrixPshort, startVertices, endVertices, CurrentMatrixSum, begintime, assignPeriod, Pshort_EffCells, matrixSum, smallDemandRatio = getMatrix(net, options.verbose, matrix, matrixSum, options.demandscale)
        options.hours = float(assignPeriod)
        smallDemandPortion = math.ceil(float(options.maxiteration)/2. * smallDemandRatio)
        if float(smallDemandPortion) != 0.:
            iterInterval = math.ceil(float(options.maxiteration) / float(smallDemandPortion))
        
        departtime = begintime * 3600
        
        if options.verbose:
            print 'the analyzed matrices:', counter
            print 'Begintime:', begintime, "O'Clock"
            print 'departtime', departtime
            print 'Matrix und OD Zone already read for Interval', counter
            print 'CurrentMatrixSum:', CurrentMatrixSum
        
        foutlog.write('Reading matrix and O-D zones: done.\n')
        foutlog.write('Matrix und OD Zone already read for Interval:%s\n' %counter)
        foutlog.write('CurrentMatrixSum:%s\n' %CurrentMatrixSum)
        foutlog.write('number of current startVertices:%s\n' %len(startVertices))
        foutlog.write('number of current endVertices:%s\n' %len(endVertices))
               
        if options.odestimation:
            linkChoiceMap.clear()
            odPairsMap.clear()
            linkChoiceMap = initLinkChoiceMap(net, startVertices, endVertices, matrixPshort, linkChoiceMap, odPairsMap)

        for edge in net.getEdges():
            edge.flow = 0.
            edge.helpflow = 0.
            edge.actualtime = edge.freeflowtime
            edge.helpacttime = edge.freeflowtime
            edge.fTT = 0.
            edge.TT = 0.
            edge.delta = 0.
            edge.helpacttimeEx = 0.
                
        # the number of origins, the umber of destinations and the number of the OD pairs
        origins = len(startVertices)
        dests = len(endVertices)
        ODpairs = origins * dests
        
        # output the origin and destination zones and the number of effective OD pairs
        if options.debug:
            outputODZone(startVertices, endVertices, Pshort_EffCells, counter) # matrixCounter)  
        
        if incremental:
            print 'begin the incremental assignment!'
            iter = 0
            options.lamda = 0.
            while iter < options.maxiteration:
                foutlog.write('- Current iteration(not executed yet):%s\n' %iter)
                iter += 1
                if iterInterval != 0 and operator.mod(iter,iterInterval) == 0:
                    assignSmallDemand = True
                else:
                    assignSmallDemand = False
                for start, startVertex in enumerate(startVertices):
                    targets = set()
                    for end, endVertex in enumerate(endVertices):
                        if assignSmallDemand and matrixPshort[start][end] > 0. and matrixPshort[start][end] < 1.:
                            smallDemand[start][end] = matrixPshort[start][end]/float(smallDemandPortion)
                            
                        if matrixPshort[start][end] > 1. or (assignSmallDemand and smallDemand[start][end] > 0.):
                            targets.add(endVertex)

                    if len(targets) > 0:
                        if options.dijkstra == 'boost':
                            D,P = dijkstraBoost(net._boostGraph, startVertex.boost)
                        elif options.dijkstra == 'plain':
                            D,P = dijkstraPlain(startVertex, targets)
                        elif options.dijkstra == 'extend':
                            D,P = dijkstra(startVertex, targets)
                        vehID, smallDemand, linkChoiceMap = doIncAssign(net, vehicles, options.verbose, options.maxiteration, options.odestimation,
                                            endVertices, start, startVertex, matrixPshort, smallDemand,
                                            D, P, AssignedVeh, AssignedTrip, vehID, assignSmallDemand, linkChoiceMap, odPairsMap)

                if options.dijkstra != 'extend':
                    linkMap = net._fullEdges
                else:
                    linkMap = net._edges
                for edge in linkMap.itervalues():
                    edge.getActualTravelTime(options, False)
                    if options.dijkstra == 'boost':
                        edge.boost.weight = edge.helpacttime
        else:
            print 'begin the', options.type, " assignment!"
            # initialization for the clogit and the lohse assignment model
            iter_outside = 1
            newRoutes = 1
            stable = False
            first = True
            # begin the traffic Assignment
            while newRoutes > 0:
                foutlog.write('- SUE iteration:%s\n' %iter_outside)
                # Generate the effective routes als intital path solutions, when considering k shortest paths (k is defined by the user.)
                if checkKPaths:
                    checkPathStart = datetime.datetime.now() 
                    newRoutes = net.calcKPaths(options.verbose, options.kPaths, newRoutes, startVertices, endVertices, matrixPshort, options.gamma)
                    checkPathEnd = datetime.datetime.now() - checkPathStart
                    foutlog.write('- Time for finding the k-shortest paths: %s\n' %checkPathEnd)
                    foutlog.write('- Finding the k-shortest paths for each OD pair: done.\n')
                    if options.verbose:
                        print 'iter_outside:', iter_outside
                        print 'number of k shortest paths:', options.kPaths 
                        print 'number of new routes:', newRoutes
                
                elif not checkKPaths and iter_outside == 1 and counter == 0:
                    print 'search for the new path'
                    newRoutes = net.findNewPath(startVertices, endVertices, newRoutes, matrixPshort, options.gamma, lohse, options.dijkstra)
                
                checkKPaths = False
                
                if options.verbose:
                    print 'iter_outside:', iter_outside
                    print 'number of new routes:', newRoutes
                
                stable = False
                iter_inside = 1
                while not stable:
                    if options.verbose:
                        print 'iter_inside:', iter_inside
                    stable = doSUEAssign(net, options, startVertices, endVertices, matrixPshort, iter_inside, lohse, first)
                    # The matrixPlong and the matrixTruck should be added when considering the long-distance trips and the truck trips.
                    if lohse:
                        stable = doLohseStopCheck(net, options, stable, iter_inside, options.maxiteration, foutlog)

                    iter_inside += 1
    
                    if options.verbose:
                        print 'stable:', stable
                    
                newRoutes = net.findNewPath(startVertices, endVertices, newRoutes, matrixPshort, options.gamma, lohse, options.dijkstra)

                first = False    
                iter_outside += 1
    
                if newRoutes < 3 and iter_outside > int((options.maxiteration)/2):
                    newRoutes = 0
                    
                if iter_outside > options.maxiteration:
                    print 'The max. number of iterations is reached!'
                    foutlog.write('The max. number of iterations is reached!\n')
                    foutlog.write('The number of new routes and the parameter stable will be set to zero and True respectively.\n')
                    print 'newRoutes:', newRoutes 
                    stable = True
                    newRoutes = 0
    
            # update the path choice probability and the path flows as well as generate vehicle data 	
            vehID = doSUEVehAssign(net, vehicles, options, counter, matrixPshort, startVertices, endVertices, AssignedVeh, AssignedTrip, vehID, lohse)

       # output the generated vehicular releasing times and routes, based on the current matrix
        print 'done with the assignment' # debug
        if options.odestimation:
            linkChoicesOutput(net, startVertices, endVertices, matrixPshort, linkChoiceMap, odPairsMap, options.outputdir, starttime)
        else:
            sortedVehOutput(vehicles, departtime, options, foutroute)

    if not options.odestimation:
        foutroute.write('</routes>\n')
        foutroute.close()

    # output the global performance indices
    assigntime = outputStatistics(net, starttime, len(matrices))
    
    foutlog.write('- Assignment is completed and all required information is generated. ')
    foutlog.close()

    if options.verbose:
        print 'Duration for traffic assignment:', assigntime
        print 'Total assigned vehicles:', vehID
    print 'Total number of the assigned trips:', matrixSum
Example #5
0
def main(options):
    parser = make_parser()
    isBZ2 = False
    dataDir = options.datadir
    districts = os.path.join(dataDir, options.districtfile)
    matrix = os.path.join(dataDir, options.mtxfile)
    netfile = os.path.join(dataDir, options.netfile)

    print "generate Trip file for:", netfile

    if "bz2" in netfile:
        netfile = bz2.BZ2File(netfile)
        isBZ2 = True

    matrixSum = 0.0
    tripList = []
    net = Net()
    odConnTable = {}
    vehIDtoODMap = {}

    parser.setContentHandler(NetworkReader(net))
    if isBZ2:
        parser.parse(StringIO.StringIO(netfile.read()))
        netfile.close()
    else:
        parser.parse(netfile)

    parser.setContentHandler(DistrictsReader(net))
    parser.parse(districts)

    matrixPshort, startVertices, endVertices, currentMatrixSum, begin, period = getMatrix(
        net, options.debug, matrix, matrixSum
    )[:6]

    if options.debug:
        print len(net._edges), "edges read"
        print len(net._startVertices), "start vertices read"
        print len(net._endVertices), "target vertices read"
        print "currentMatrixSum:", currentMatrixSum

    if options.getconns:
        if options.debug:
            print "generate odConnTable"
        for start, startVertex in enumerate(startVertices):
            if startVertex.label not in odConnTable:
                odConnTable[startVertex.label] = {}
            for source in startVertex.sourceConnNodes:
                targets = net.getTargets()
                D, P = dijkstraPlain(source, targets)
                for end, endVertex in enumerate(endVertices):
                    if startVertex.label != endVertex.label and matrixPshort[start][end] > 0.0:
                        if endVertex.label not in odConnTable[startVertex.label]:
                            odConnTable[startVertex.label][endVertex.label] = []
                        net.checkRoute(startVertex, endVertex, start, end, P, odConnTable, source, options)
    else:
        if options.debug:
            print "import and use the given odConnTable"
        sys.path.append(options.datadir)
        from odConnTables import odConnTable

    # output trips
    if options.verbose:
        print "output the trip file"
    vehID = 0
    subVehID = 0
    random.seed(42)
    matrixSum = 0.0
    fouttrips = file(options.tripfile, "w")
    fouttrips.write('<?xml version="1.0"?>\n')
    print >> fouttrips, """<!-- generated on %s by $Id: generateTripsXml.py 12897 2012-10-26 07:44:19Z melde $ -->
    """ % datetime.datetime.now()
    fouttrips.write("<tripdefs>\n")

    # if hasattr(options, "scale"):
    if options.demandscale != 1.0:
        print "demand scale %s is used." % options.demandscale
        for start in range(len(startVertices)):
            for end in range(len(endVertices)):
                matrixPshort[start][end] *= options.demandscale

    for start, startVertex in enumerate(startVertices):
        for end, endVertex in enumerate(endVertices):
            if startVertex.label != endVertex.label and matrixPshort[start][end] > 0.0:
                counts = 0.0
                if options.odestimation:
                    if matrixPshort[start][end] < 1.0:
                        counts, vehID, tripList, vehIDtoODMap = addVeh(
                            counts, vehID, begin, period, odConnTable, startVertex, endVertex, tripList, vehIDtoODMap
                        )
                    else:
                        matrixSum += matrixPshort[start][end]
                        while (
                            counts < float(math.ceil(matrixPshort[start][end]))
                            and (matrixPshort[start][end] - counts) > 0.5
                            and float(subVehID) < matrixSum
                        ) or float(subVehID) < matrixSum:
                            counts, vehID, tripList, vehIDtoODMap = addVeh(
                                counts,
                                vehID,
                                begin,
                                period,
                                odConnTable,
                                startVertex,
                                endVertex,
                                tripList,
                                vehIDtoODMap,
                            )
                            subVehID += 1
                else:
                    matrixSum += matrixPshort[start][end]
                    while (
                        counts < float(math.ceil(matrixPshort[start][end]))
                        and (matrixPshort[start][end] - counts) > 0.5
                        and float(vehID) < matrixSum
                    ) or float(vehID) < matrixSum:
                        counts, vehID, tripList, vehIDtoODMap = addVeh(
                            counts, vehID, begin, period, odConnTable, startVertex, endVertex, tripList, vehIDtoODMap
                        )
    if options.debug:
        print "total demand:", matrixSum
        print vehID, "trips generated"
    tripList.sort(key=operator.attrgetter("depart"))

    departpos = "free"
    if __name__ == "__main__":
        departpos = options.departpos
    for trip in tripList:
        fouttrips.write(
            '   <trip id="%s" depart="%s" from="%s" to="%s" fromtaz="%s" totaz="%s" departlane="free" departpos="%s" departspeed="max"/>\n'
            % (
                trip.label,
                trip.depart,
                trip.sourceEdge,
                trip.sinkEdge,
                trip.sourceDistrict,
                trip.sinkDistrict,
                departpos,
            )
        )
    fouttrips.write("</tripdefs>")
    fouttrips.close()

    return odConnTable, vehIDtoODMap