def fitness_score_individual(X: np.ndarray, instance: ProblemInstance): devices = [] filtered_devices = instance.filtered_devices for index, device in enumerate(filtered_devices): if X[index]: devices.append(device) if len(devices) == 0: # because we minimize and only have values < 0 this will be worst return 1 requirements = calculate_requirements(devices) # emd tries to show how far the requirements are away from each other het = calculate_diff_entropy(base_requirements(), requirements) try: fet, counter = sum_fet(devices, instance) mean_fets.append(fet / counter) except ValueError: # shouldn't happen, because GA uses the filtered devices # signals that a device is in the solution that can't execute the function, i.e. has no FET assigned return 1 # performance factor, this favors faster settings performance = (fet / counter) * instance.performance_weight # variety factor, this favors more general settings if het == 0: variety = 0 else: variety = (het / 16) + len(devices) / len(filtered_devices) variety = variety * instance.variety_weight fitness = -(performance + variety) return fitness
def create_result_enumeration(scores: List[Tuple[List[bool], float]], duration, instance: ProblemInstance, settings, algorithm_params): best_solution = min(scores, key=lambda l: l[1]) device_types = [] for included, device_type in zip(best_solution[0], instance.filtered_devices): if included: device_types.append(device_type.id) devices = [] for device in instance.state.devices: if device.id in device_types: devices.append(device) try: fet, counter = sum_fet(devices, instance) if counter == 0: fet = -1 else: fet /= counter reqs = calculate_requirements(devices) successful = fet != -1 return Result(instance, duration, reqs, devices, fet, successful, {'function': best_solution[1]}, [], {}, {}) except ValueError: # shouldn't happen, because GA uses the filtered devices # signals that a device is in the solution that can't execute the function, i.e. has no FET assigned raise BadSolutionError
def create_result(model: geneticalgorithm2, duration: float, instance: ProblemInstance, settings: GaRunSettings, algorithm_params: GaAlgorithmParams) -> Result: devices = [] for index, device in enumerate(instance.filtered_devices): if model.best_variable[index] == 1: devices.append(device) try: fet, counter = sum_fet(devices, instance) if counter == 0: fet = -1 else: fet /= counter reqs = calculate_requirements(devices) successful = fet != -1 return Result(instance, duration, reqs, devices, fet, successful, model.output_dict, model.report, asdict(settings), asdict(algorithm_params)) except ValueError: # shouldn't happen, because GA uses the filtered devices # signals that a device is in the solution that can't execute the function, i.e. has no FET assigned raise BadSolutionError
def test_generated_settings(): settings_path = '/mnt/ssd1data/Documents/code/python/hw_mapping_gen_settings' settings_folder = '2020_11_24_19_29_13_archsteps-6_steps-5_percentage-0.1_500_random_extract' settings_folder = os.path.join(settings_path, settings_folder) settings = read_generator_settings(settings_folder)[:10] num_devices = 100 all_devices = [] ether_nodes = [] all_devices_by_types = [] for index, setting in enumerate(settings): all_devices.append(generate_devices(num_devices, setting)) ether_nodes.append(convert_to_ether_nodes(all_devices[index])) devices_by_type = defaultdict(list) for devices in ether_nodes: for device in devices: devices_by_type[device.name[:device.name.rindex('_')]].append( device) all_devices_by_types.append(devices_by_type) scores = [] for devices in all_devices: scores.append(calculate_requirements(devices)) pass
def main(): configs = [(500, hybridbalanced_settings, 'hybrid_balanced'), (1000, hybridbalanced_jetson_settings, 'hybrid_balanced_jetson'), (2000, edgegpu_settings, 'edge_gpu'), (2500, edgesbc_settings, 'edge_sbc'), (1000, edgetpu_settings, 'edge_tpu'), (500, edgecloudlet_settings, 'edge_cloudlet'), (500, cloudgpu_settings, 'cloud_gpu'), (500, cloudcpu_settings, 'cloud_cpu')] # # configs = [ # (5, hybridbalanced_settings, 'hybrid_balanced'), # (10, hybridbalanced_jetson_settings, 'hybrid_balanced_jetson'), # (20, edgegpu_settings, 'edge_gpu'), # (25, edgesbc_settings, 'edge_sbc'), # (10, edgetpu_settings, 'edge_tpu'), # (5, edgecloudlet_settings, 'edge_cloudlet'), # (5, cloudgpu_settings, 'cloud_gpu'), # (5, cloudcpu_settings, 'cloud_cpu') # ] dfs = [] for num, settings, name in configs: devices = generate_devices(num, settings) ether_nodes = convert_to_ether_nodes(devices) folder = './data/collections/collection_12_18_2020/devices/' score = calculate_diff_entropy( xeon_reqs(), calculate_requirements(convert_to_devices(ether_nodes))) # score = calculate_emd(xeon_reqs(), calculate_requirements(convert_to_devices(ether_nodes))) file = os.path.join(folder, f'{name}_score_{round(score, 3)}.pkl') with open(file, 'wb') as fd: pickle.dump(devices, fd) device_statistic_file = os.path.join(folder, f'{name}_device_statistics.csv') c = count_devices(ether_nodes) count = counter_to_csv(c) count_dict = convert_to_dict(c) df = pd.DataFrame(data=count_dict) df = df.sort_values(by='device_type') df.index = df['device_type'] df = df.drop('device_type', axis=1) df = df.drop('count', axis=1) df['scenario'] = f'{name} ({num} devices)' df['score'] = round(score, 3) df['percentage'] *= 100 dfs.append(df) with open(device_statistic_file, 'w') as fd: fd.writelines(count) concat = pd.concat(dfs) devices = concat.index.unique() a = concat.set_index(['scenario', concat.index]).unstack() start = len(devices) + 1 end = len(a.columns) drop_columns = [a.columns[i] for i in list(range(start, end))] a = a.drop(columns=drop_columns, axis=1) a.columns = [format_device(d) for d in devices.to_list()] + ['score'] a.to_csv(os.path.join(folder, 'summary_devices.csv')) a.to_latex(os.path.join(folder, 'summary_devices.tex')) pass
gpu_mhz={}, ) }) use_predefined_devices = True if use_predefined_devices: with open( 'data/collections/collection_01_04_2021/ga_devices/hybrid_balanced_score_7.384.pkl', 'rb') as fd: devices = pickle.load(fd) else: num_devices = 100 devices = generate_devices(num_devices, test_settings) print(len(devices)) print(heterogeneity_score(base_reqs, calculate_requirements(devices))) ether_nodes = convert_to_ether_nodes(devices) print(ether_nodes[0]) device_types = np.unique( list(map(lambda e: e.name[:e.name.rindex('_')], ether_nodes))) devices_by_type = defaultdict(list) for device in ether_nodes: devices_by_type[device.name[:device.name.rindex('_')]].append(device) print('\navailable nodes') for device_type in device_types: print(device_type, len(devices_by_type[device_type])) print(len(ether_nodes))
def calculate_heterogeneity(devices): requirements = calculate_requirements(devices) return calculate_diff_entropy(base_requirements(), requirements)