#!/usr/bin/env python """ Create an instance from preprocessed data. The instance are two traffic demand matrices: router to router (r2r) and router to CDN (r2cdn) and a CDN locations file (cdnl). You specify the sum of demands from most populous city with the maxdemand argument. Proportion of traffic directed to CDN is controlled by -c argument. If you specify -i, then indexes in alphabetically sorted names list will be used instead of actual names. """ from itertools import * def make_instance(args): populations = {} popf = open(args.populations) for line in popf.readlines(): name, pop = line.split() pop = int(pop) populations[name] = pop popf.close() traffic = {} servers = {} srvcaps = {} cdnf = open(args.cdns) for line in cdnf.readlines(): fields = line.split() fields.reverse() name = fields.pop() traffic[name] = float(fields.pop()) srvcaps[name] = float(fields.pop()) servers[name] = fields cdnf.close() ttf = sum(traffic.values()) for cdn in traffic: traffic[cdn] = float(traffic[cdn]) / ttf cities = populations.keys() cdns = traffic.keys() totalpop = sum(populations.values()) maxpop = max(populations.values()) print 'Most populous city is %s with %d folks' % ([city for city in populations if populations[city] == maxpop][0], maxpop) if args.i: cind = {enu[1]: enu[0] for enu in enumerate(sorted(cities))} sind = {enu[1]: enu[0] for enu in enumerate(sorted(cdns))} cities = range(len(cities)) cdns = range(len(cdns)) populations = {cind[city]: populations[city] for city in populations} traffic = {sind[cdn]: traffic[cdn] for cdn in traffic} servers = {sind[cdn]: [str(cind[city]) for city in servers[cdn]] for cdn in servers} srvcaps = {sind[cdn]: srvcaps[cdn] for cdn in srvcaps} r2r = open(args.r2r, 'w') if not args.n: print >> r2r, ' '.join(map(str, cities)) # Write the header for src in cities: basedemand = args.maxdemand * populations[src] / maxpop citydemand = basedemand * (1.0 - args.c) otherpop = totalpop - populations[src] if not args.n: print >> r2r, src, for dst in cities: if dst != src: print >> r2r, citydemand * populations[dst] / otherpop, else: print >> r2r, 0, print >> r2r r2r.close() r2cdn = open(args.r2cdn, 'w') if not args.n: print >> r2cdn, ' '.join(map(str, cdns)) # Write the header for src in cities: basedemand = args.maxdemand * populations[src] / maxpop cdndemand = basedemand * args.c if not args.n: print >> r2cdn, src, for cdn in cdns: print >> r2cdn, cdndemand * traffic[cdn], print >> r2cdn r2cdn.close() cdnl = open(args.cdnl, 'w') for cdn in cdns: if not args.n: print >> cdnl, cdn print >> cdnl, ' '.join(servers[cdn]) cdnl.close() cdnc = open(args.cdnc, 'w') for cdn in cdns: if not args.n: print >> cdnc, cdn cap = str(int(srvcaps[cdn] * args.c * traffic[cdn] * args.maxdemand * totalpop / maxpop)) print >> cdnc, ' '.join([cap for _ in servers[cdn]]) cdnc.close() from os.path import dirname path = dirname(args.r2r) rcount = open(path + "/rcount", 'w') print >> rcount, len(cities) rcount.close() ccount = open(path + "/ccount", 'w') print >> ccount, len(cdns) ccount.close() if __name__ == '__main__': import argparse parser = argparse.ArgumentParser( description = __doc__) parser.add_argument('populations', help="populations file") parser.add_argument('cdns', help="CDNs file") parser.add_argument('r2r', help="router-to-router demands output file") parser.add_argument('r2cdn', help="router-to-CDN demands output file") parser.add_argument('cdnl', help="CDN locations output file") parser.add_argument('cdnc', help="CDN capacities output file") parser.add_argument('maxdemand', help="max demands from one city", type=float) parser.add_argument('-c', help="proportion of traffic to CDN (default 0.5)", type=float, default=0.5) parser.add_argument('-i', help="output integer labels", action='store_true') parser.add_argument('-n', help="no table header", action='store_true') args = parser.parse_args()