def test_list_param_combinations(): # Two possible combinations d1 = {'a': [1, 2]} assert set(map(lambda x: getitem(x, 'a'), list_param_combinations(d1))) == set([1, 2]) # Two possible combinations with two specified values d2 = {'b': [3], 'c': [4, 5]} assert set(map(lambda x: getitem(x, 'b'), list_param_combinations(d2))) == set([3, 3]) assert set(map(lambda x: getitem(x, 'c'), list_param_combinations(d2))) == set([4, 5]) # Four combinations with two specified values d3 = {'d': [6, 7], 'e': [8, 9]} assert sorted( map(lambda x: json.dumps(x, sort_keys=True), list_param_combinations(d3))) == sorted( map(lambda x: json.dumps(x, sort_keys=True), [{ 'd': 6, 'e': 8 }, { 'd': 7, 'e': 8 }, { 'd': 6, 'e': 9 }, { 'd': 7, 'e': 9 }]))
def main(): ####################### # Create the campaign # ####################### script = 'wifi-multi-tos' ns_path = '../../../../../Desktop/ns-3' campaign_dir = "/tmp/sem-test/wifi-plotting-example" campaign = sem.CampaignManager.new(ns_path, script, campaign_dir, runner_type='ParallelRunner', overwrite=False, check_repo=False) print(campaign) # This prints out the campaign settings ################### # Run simulations # ################### # These are the available parameters # We specify each parameter as an array containing the desired values params = { 'nWifi': [1, 3], 'distance': [1, 10], 'useRts': ['false', 'true'], 'useShortGuardInterval': ['false', 'true'], 'mcs': list(range(2, 8, 2)), 'channelWidth': ['20'], 'simulationTime': [4], } runs = 2 # Number of runs to perform for each combination # Actually run the simulations # This will also print a progress bar campaign.run_missing_simulations(sem.list_param_combinations(params), runs=runs) ################################## # Exporting and plotting results # ################################## # Create a folder for the figures figure_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'figures') if not os.path.isdir(figure_path): os.makedirs(figure_path) # We need to define a function to parse the results. This function will # then be passed to the get_results_as_xarray function, that will call it # on every result it needs to export. def get_average_throughput(result): stdout = result['output']['stdout'] m = re.match('.*throughput: [-+]?([0-9]*\.?[0-9]+).*', stdout, re.DOTALL).group(1) return float(m) # Reduce multiple runs to a single value (or tuple) results = campaign.get_results_as_xarray(params, get_average_throughput, 'AvgThroughput', runs) # We can then visualize the object that is returned by the function # print(results) # Statistics can easily be computed from the xarray structure results_average = results.reduce(np.mean, 'runs') results_std = results.reduce(np.std, 'runs') print(results_average) # Plot lines with error bars plt.figure(figsize=[6, 6], dpi=100) legend_entries = [] for useShortGuardInterval in params['useShortGuardInterval']: for useRts in params['useRts']: avg = results_average.sel( nWifi=1, distance=1, useShortGuardInterval=useShortGuardInterval, useRts=useRts) std = results_std.sel(nWifi=1, distance=1, useShortGuardInterval=useShortGuardInterval, useRts=useRts) plt.errorbar(x=params['mcs'], y=np.squeeze(avg), yerr=6 * np.squeeze(std)) legend_entries += [ 'SGI = %s, RTS = %s' % (useShortGuardInterval, useRts) ] plt.legend(legend_entries) plt.xlabel('MCS') plt.ylabel('Throughput [Mbit/s]') plt.savefig(os.path.join(figure_path, 'throughput.png')) # Assess the influence of nWifi and distance parameters plt.figure(figsize=[8, 8], dpi=300) subplot_idx = 1 for nWifi in params['nWifi']: for distance in params['distance']: stacked_params = results.sel( nWifi=nWifi, distance=distance, channelWidth='20', simulationTime=4).stack(sgi_rts=('useShortGuardInterval', 'useRts')).reduce( np.mean, 'runs') plt.subplot(2, 2, subplot_idx) stacked_params.plot.line(x='mcs', add_legend=True) subplot_idx += 1 plt.savefig(os.path.join(figure_path, 'throughputComparison.png'))
def test_list_param_combinations(): # Two possible combinations d1 = {'a': [1, 2]} assert set(map(lambda x: getitem(x, 'a'), list_param_combinations(d1))) == set([1, 2]) # Two possible combinations with two specified values d2 = {'b': [3], 'c': [4, 5]} assert set(map(lambda x: getitem(x, 'b'), list_param_combinations(d2))) == set([3, 3]) assert set(map(lambda x: getitem(x, 'c'), list_param_combinations(d2))) == set([4, 5]) # Four combinations with two specified values d3 = {'d': [6, 7], 'e': [8, 9]} assert sorted(map(lambda x: json.dumps(x, sort_keys=True), list_param_combinations(d3))) == sorted( map(lambda x: json.dumps(x, sort_keys=True), [{'d': 6, 'e': 8}, {'d': 7, 'e': 8}, {'d': 6, 'e': 9}, {'d': 7, 'e': 9}])) params = { 'a': [1], 'b': [1, 2, 3], } params_more = { 'a': [3], 'b': [1, 2, 3], } params_same_as_above = { 'a': [1, 3], 'b': [1, 2, 3], } assert (sorted(map(lambda x: json.dumps(x, sort_keys=True), list_param_combinations(params_same_as_above))) == sorted(map(lambda x: json.dumps(x, sort_keys=True), list_param_combinations([params, params_more])))) params_with_lambda = { 'a': [1, 3], 'b': lambda p: [10 * p['a']] } assert (sorted(map(lambda x: json.dumps(x, sort_keys=True), list_param_combinations(params_with_lambda))) == sorted(map(lambda x: json.dumps(x, sort_keys=True), [{'a': 1, 'b': 10}, {'a': 3, 'b': 30}]))) params_with_lambda_returning_list = { 'a': [1, 3], 'b': lambda p: list(range(p['a'])), } assert (sorted(map(lambda x: json.dumps(x, sort_keys=True), list_param_combinations(params_with_lambda_returning_list))) == sorted(map(lambda x: json.dumps(x, sort_keys=True), [{'a': 1, 'b': 0}, {'a': 3, 'b': 0}, {'a': 3, 'b': 1}, {'a': 3, 'b': 2}]))) params_with_two_lambdas = { 'a': [1, 3], 'b': lambda p: list(range(p['a'])), 'c': lambda p: [10 * p['b']] } assert (sorted(map(lambda x: json.dumps(x, sort_keys=True), list_param_combinations(params_with_two_lambdas))) == sorted(map(lambda x: json.dumps(x, sort_keys=True), [{'a': 1, 'b': 0, 'c': 0}, {'a': 3, 'b': 0, 'c': 0}, {'a': 3, 'b': 1, 'c': 10}, {'a': 3, 'b': 2, 'c': 20}])))
'harq': [False, True], 'rlcAm': [True, False], 'fixedTti': False, 'sched': ['ns3::MmWaveAsyncHbfMacScheduler', 'ns3::MmWavePaddedHbfMacScheduler'], 'bfmod': 'ns3::MmWaveMMSESpectrumBeamforming', 'nLayers': 4, 'useTCP': False } campaign.run_missing_simulations( sem.list_param_combinations(bf_comparison_sigle_layer)) campaign.run_missing_simulations( sem.list_param_combinations(bf_comparison_multi_layer)) campaign.run_missing_simulations( sem.list_param_combinations(tcp_sched_comparison_sigle_layer)) campaign.run_missing_simulations( sem.list_param_combinations(tcp_sched_comparison_multi_layer)) campaign.run_missing_simulations( sem.list_param_combinations(udp_sched_comparison_sigle_layer)) campaign.run_missing_simulations( sem.list_param_combinations(udp_sched_comparison_multi_layer)) campaign = sem.CampaignManager.load(campaign_dir, runner_type="ParallelRunner", check_repo=False)
ns_path = './' script = 'lte' campaign_dir = ns_path + 'sem' results_dir = ns_path + 'results' parser = argparse.ArgumentParser(description='SEM script') parser.add_argument('-o', '--overwrite', action='store_true', help='Overwrite previous campaign') args = parser.parse_args() campaign = sem.CampaignManager.new(ns_path, script, campaign_dir, overwrite=args.overwrite, check_repo=True) print(campaign) param_combinations = { 'algo' : ['kmeans', 'meanshift', 'dbscan', 'hdbscan'], 'enablePrediction' : 'true', 'numUAVs' : 3, 'numUes' : 75, 'seedValue' : 10000, 'useCa' : 'false' } campaign.run_missing_simulations(sem.list_param_combinations(param_combinations),10) result_param = { 'algo' : ['kmeans', 'meanshift', 'dbscan', 'hdbscan'] } campaign.save_to_folders(result_param, results_dir, 10)
'remMode': 0, 'scen': 4 } result_param = { 'enableSCs': ['false', 'true'], 'enablePrediction': ['true', 'false'], 'epsQOS': [450], 'nENB': [4], 'nUABS': [6, 8], 'nUE': [100, 200] } if not args.save: campaign.run_missing_simulations( sem.list_param_combinations(param_combinations), 33) campaign.save_to_folders(result_param, results_dir, 33) param_combinations['nENB'] = 2 result_param['nENB'] = [2] if not args.save: campaign.run_missing_simulations( sem.list_param_combinations(param_combinations), 33) campaign.save_to_folders(result_param, results_dir, 33) if args.no_traces: os.system("find " + results_dir + " -name UlInterferenceStats.txt -delete")
def main(): import sem import numpy as np import os import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from scipy.interpolate import interp1d # Define campaign parameters ############################ script = 'lorawan-sem-example' ns_path = '../../ns-3' campaign_dir = "/tmp/sem-test/lorawan-par" # Create campaign ################# campaign = sem.CampaignManager.new(ns_path, script, campaign_dir, runner_type='ParallelRunner', overwrite=True, check_repo=False) print(campaign) # Run simulations ################# # Parameter space ################# nDevices_values = [100, 500, 1000, 2000, 4000] radius_values = [5500] simulationTime_values = [600] appPeriod_values = [600] runs = 1 param_combinations = { 'nDevices': nDevices_values, 'radius': radius_values, 'simulationTime': simulationTime_values, 'appPeriod': appPeriod_values, 'print': False, } campaign.run_missing_simulations( sem.list_param_combinations(param_combinations), runs=runs) print("Simulations done.") #################################### # View results of first simulation # #################################### figure_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'figures') if not os.path.isdir(figure_path): os.makedirs(figure_path) for result in [campaign.db.get_complete_results({'nDevices': 4000})[0]]: print(result) dtypes = { 'endDevices': (float, float, int), 'occupiedReceptionPaths': (float, int), 'packets': (float, int, float, int, float, int) } string_to_number = {'R': 0, 'U': 1, 'N': 2, 'I': 3} converters = { 'packets': { 5: lambda x: string_to_number[x.decode('UTF-8')] } } parsed_result = sem.utils.automatic_parser(result, dtypes, converters) # Plot network topology plt.figure(figsize=[6, 6], dpi=300) positions = np.array(parsed_result['endDevices']) plt.scatter(positions[:, 0], positions[:, 1], s=2, c=positions[:, 2]) plt.scatter(0, 0, s=20, marker='^', c='black') plt.xlim([-radius_values[0], radius_values[0]]) plt.ylim([-radius_values[0], radius_values[0]]) plt.title("Network topology") plt.savefig(os.path.join(figure_path, 'networkTopology.png')) # Plot gateway occupation metrics plt.figure(figsize=[6, 6], dpi=300) path_occupancy = np.array(parsed_result['occupiedReceptionPaths']) t = np.linspace(path_occupancy[0, 0], 5, num=1001, endpoint=True) plt.plot( t, interp1d(path_occupancy[:, 0], path_occupancy[:, 1], kind='nearest')(t)) packets = np.array(parsed_result['packets']) # Plot successful packets successful_packets = packets[:, 5] == 0 plt.scatter(packets[successful_packets, 0], np.zeros([sum(successful_packets)]), s=40, c='green', marker='^') # Plot failed packets failed_packets = packets[:, 5] != 0 plt.scatter(packets[failed_packets, 0], np.zeros([sum(failed_packets)]), s=40, c='red', marker='^') plt.xlim([0, 5]) plt.title("Occupied reception paths") plt.savefig(os.path.join(figure_path, 'receptionPaths.png')) ################################# # Plot probabilities of success # ################################# # This is the function that we will pass to the export function def get_outcome_probabilities(result): # Parse all files into lists parsed_result = sem.utils.automatic_parser(result, dtypes, converters) # Get the file we are interested in outcomes = np.array(parsed_result['packets'])[:, 5] successful = sum(outcomes == 0) interfered = sum(outcomes == 1) no_more_receivers = sum(outcomes == 2) under_sensitivity = sum(outcomes == 3) total = outcomes.shape[0] return [ successful / total, interfered / total, no_more_receivers / total, under_sensitivity / total ] metrics = [ 'successful', 'interfered', 'no_more_receivers', 'under_sensitivity' ] results = campaign.get_results_as_xarray(param_combinations, get_outcome_probabilities, metrics, runs) plt.figure(figsize=[6, 6], dpi=300) for metric in metrics: plt.plot( param_combinations['nDevices'], results.reduce(np.mean, 'runs').sel(radius=5500, simulationTime=600, appPeriod=600, metrics=metric)) plt.xlabel("Number of End Devices") plt.ylabel("Probability") plt.legend( ["Success", "Interfered", "No more receivers", "Under sensitivity"]) plt.savefig(os.path.join(figure_path, 'outcomes.png'))