Exemplo n.º 1
0
def run_scenario(scenario, result_dir):
    # scenario name
    sce = scenario.__name__
    sce_nice_name = sce.replace('_', ' ').title()

    # prepare input data
    data = rivus.read_excel(data_spreadsheet)
    vertex = pdshp.read_shp(vertex_shapefile)
    edge = prepare_edge(edge_shapefile, building_shapefile)

    # apply scenario function to input data
    data, vertex, edge = scenario(data, vertex, edge)

    log_filename = os.path.join(result_dir, sce+'.log')

    # create & solve model
    model = rivus.create_model(
        data, vertex, edge,
        peak_multiplier=lambda x:scale_peak_demand(x, peak_demand_prefactor))
    
    # scale peak demand according to pickled urbs findings
    #reduced_peak = scale_peak_demand(model, peak_demand_prefactor)
    #model.peak = reduced_peak
    
    prob = model.create()
    optim = SolverFactory('gurobi')
    optim = setup_solver(optim, logfile=log_filename)
    result = optim.solve(prob, tee=True)
    prob.load(result)

    # report
    rivus.save(prob, os.path.join(result_dir, sce+'.pgz'))
    rivus.report(prob, os.path.join(result_dir, sce+'.xlsx'))
    
    # plot without buildings
    rivus.result_figures(prob, os.path.join(result_dir, sce))
    
    # plot with buildings and to_edge lines
    more_shapefiles = [{'name': 'to_edge',
                        'color': rivus.to_rgb(192, 192, 192),
                        'shapefile': to_edge_shapefile,
                        'zorder': 1,
                        'linewidth': 0.1}]
    rivus.result_figures(prob, os.path.join(result_dir, sce+'_bld'), 
                         buildings=(building_shapefile, False),
                         shapefiles=more_shapefiles)
    return prob
Exemplo n.º 2
0
def replot(directory):
    """Recreate result figures for all pickled rivus results in directory
    
    Args:
        directory: a directory with 1 or multiple pickled rivus instances
        
    Returns:
        Nothing
    """
    glob_pattern = os.path.join(directory, '*.pgz')
    pickle_filenames = glob.glob(glob_pattern)

    data_dir = os.path.join('data', os.path.basename(directory).split('-')[0])
    # if directory = 'result/moosh' try to find a suitable building shapefile
    # in 'data/moosh'
    buildings = None
    building_filename = os.path.join(data_dir, 'building')
    if os.path.exists(building_filename + '.shp'):
        buildings = (building_filename, False)  # if True, color buildings

    # if data/.../to_edge exists, paint it
    shapefiles = None
    to_edge_filename = os.path.join(data_dir, 'to_edge')
    if os.path.exists(to_edge_filename + '.shp'):
        shapefiles = [{
            'name': 'to_edge',
            'color': rivus.to_rgb(192, 192, 192),
            'shapefile': to_edge_filename,
            'zorder': 1,
            'linewidth': 0.1
        }]

    for pf in pickle_filenames:
        prob = rivus.load(pf)
        figure_basename = os.path.splitext(pf)[0]
        if buildings:
            figure_basename += '_bld'
        rivus.result_figures(prob,
                             figure_basename,
                             buildings=buildings,
                             shapefiles=shapefiles)
Exemplo n.º 3
0
def replot(directory):
    """Recreate result figures for all pickled rivus results in directory
    
    Args:
        directory: a directory with 1 or multiple pickled rivus instances
        
    Returns:
        Nothing
    """
    glob_pattern = os.path.join(directory, '*.pgz')
    pickle_filenames = glob.glob(glob_pattern)
    
    data_dir = os.path.join('data', os.path.basename(directory).split('-')[0])
    # if directory = 'result/moosh' try to find a suitable building shapefile
    # in 'data/moosh'
    buildings = None
    building_filename = os.path.join(data_dir, 'building')
    if os.path.exists(building_filename+'.shp'):
        buildings = (building_filename, False)  # if True, color buildings
        
    # if data/.../to_edge exists, paint it
    shapefiles = None
    to_edge_filename = os.path.join(data_dir, 'to_edge')
    if os.path.exists(to_edge_filename+'.shp'):
        shapefiles = [{'name': 'to_edge',
                       'color': rivus.to_rgb(192, 192, 192),
                       'shapefile': to_edge_filename,
                       'zorder': 1,
                       'linewidth': 0.1}]

    for pf in pickle_filenames:
        prob = rivus.load(pf)
        figure_basename = os.path.splitext(pf)[0]
        if buildings:
            figure_basename += '_bld'
        rivus.result_figures(prob, figure_basename, 
                             buildings=buildings,
                             shapefiles=shapefiles)
Exemplo n.º 4
0
# load nodes
vertex = pdshp.read_shp(vertex_shapefile)

# load spreadsheet data
data = rivus.read_excel(data_spreadsheet)

# create & solve model
prob = rivus.create_model(data, vertex, edge)
if PYOMO3:
    prob = prob.create()  # no longer needed in Pyomo 4
optim = SolverFactory('glpk')
optim = setup_solver(optim)
result = optim.solve(prob, tee=True)
if PYOMO3:
    prob.load(result)  # no longer needed in Pyomo 4

# load results
costs, Pmax, Kappa_hub, Kappa_process = rivus.get_constants(prob)
source, flows, hub_io, proc_io, proc_tau = rivus.get_timeseries(prob)

result_dir = os.path.join('result', os.path.basename(base_directory))

# create result directory if not existing already
if not os.path.exists(result_dir):
    os.makedirs(result_dir)

rivus.save(prob, os.path.join(result_dir, 'prob.pgz'))
rivus.report(prob, os.path.join(result_dir, 'prob.xlsx'))
rivus.result_figures(prob, os.path.join(result_dir, 'plot'))

Exemplo n.º 5
0
# load nodes
vertex = pdshp.read_shp(vertex_shapefile)

# load spreadsheet data
data = rivus.read_excel(data_spreadsheet)

# create & solve model
prob = rivus.create_model(data, vertex, edge)
if PYOMO3:
    prob = prob.create()  # no longer needed in Pyomo 4
optim = SolverFactory('glpk')
optim = setup_solver(optim)
result = optim.solve(prob, tee=True)
if PYOMO3:
    prob.load(result)  # no longer needed in Pyomo 4

# load results
costs, Pmax, Kappa_hub, Kappa_process = rivus.get_constants(prob)
source, flows, hub_io, proc_io, proc_tau = rivus.get_timeseries(prob)

result_dir = os.path.join('result', os.path.basename(base_directory))

# create result directory if not existing already
if not os.path.exists(result_dir):
    os.makedirs(result_dir)

rivus.save(prob, os.path.join(result_dir, 'prob.pgz'))
rivus.report(prob, os.path.join(result_dir, 'prob.xlsx'))
rivus.result_figures(prob, os.path.join(result_dir, 'plot'))