|  | 
|  | 1 | +import json | 
|  | 2 | + | 
|  | 3 | +import pathos | 
|  | 4 | +from tqdm import tqdm | 
|  | 5 | + | 
|  | 6 | +import sampo.scheduler | 
|  | 7 | +from sampo.backend.multiproc import MultiprocessingComputationalBackend | 
|  | 8 | + | 
|  | 9 | +from sampo.hybrid.population_tabu import TabuPopulationScheduler | 
|  | 10 | + | 
|  | 11 | +from sampo.hybrid.cycle import CycleHybridScheduler | 
|  | 12 | +from sampo.api.genetic_api import ScheduleGenerationScheme | 
|  | 13 | +from sampo.scheduler import HEFTScheduler, HEFTBetweenScheduler, TopologicalScheduler, GeneticScheduler | 
|  | 14 | +from sampo.hybrid.population import HeuristicPopulationScheduler, GeneticPopulationScheduler | 
|  | 15 | + | 
|  | 16 | +from sampo.generator.environment import get_contractor_by_wg | 
|  | 17 | +from sampo.generator import SimpleSynthetic | 
|  | 18 | + | 
|  | 19 | +from sampo.base import SAMPO | 
|  | 20 | +from sampo.schemas import WorkGraph | 
|  | 21 | + | 
|  | 22 | +def run_experiment(args): | 
|  | 23 | +    graph_size, iteration = args | 
|  | 24 | + | 
|  | 25 | +    heuristics = HeuristicPopulationScheduler([HEFTScheduler(), HEFTBetweenScheduler(), TopologicalScheduler()]) | 
|  | 26 | +    # genetic1 = TabuPopulationScheduler() | 
|  | 27 | +    genetic1 = GeneticPopulationScheduler(GeneticScheduler(mutate_order=0.2, | 
|  | 28 | +                                                           mutate_resources=0.2, | 
|  | 29 | +                                                           sgs_type=ScheduleGenerationScheme.Parallel)) | 
|  | 30 | +    genetic2 = GeneticPopulationScheduler(GeneticScheduler(mutate_order=0.001, | 
|  | 31 | +                                                           mutate_resources=0.001, | 
|  | 32 | +                                                           sgs_type=ScheduleGenerationScheme.Parallel)) | 
|  | 33 | + | 
|  | 34 | +    hybrid_combine = CycleHybridScheduler(heuristics, [genetic1, genetic2], max_plateau_size=1) | 
|  | 35 | +    hybrid_genetic1 = CycleHybridScheduler(heuristics, [genetic1], max_plateau_size=1) | 
|  | 36 | +    hybrid_genetic2 = CycleHybridScheduler(heuristics, [genetic2], max_plateau_size=1) | 
|  | 37 | + | 
|  | 38 | +    wg = WorkGraph.load('wgs', f'{graph_size}_{iteration}') | 
|  | 39 | +    contractors = [get_contractor_by_wg(wg)] | 
|  | 40 | + | 
|  | 41 | +    # SAMPO.backend = MultiprocessingComputationalBackend(n_cpus=10) | 
|  | 42 | +    SAMPO.backend.cache_scheduler_info(wg, contractors) | 
|  | 43 | +    SAMPO.backend.cache_genetic_info() | 
|  | 44 | + | 
|  | 45 | +    schedule_hybrid_combine = hybrid_combine.schedule(wg, contractors) | 
|  | 46 | +    schedule_genetic1 = hybrid_genetic1.schedule(wg, contractors) | 
|  | 47 | +    schedule_genetic2 = hybrid_genetic2.schedule(wg, contractors) | 
|  | 48 | + | 
|  | 49 | +    # print(f'Hybrid combine: {schedule_hybrid_combine.execution_time}') | 
|  | 50 | +    # print(f'Scheduler 1 cycled: {schedule_genetic1.execution_time}') | 
|  | 51 | +    # print(f'Scheduler 2 cycled: {schedule_genetic2.execution_time}') | 
|  | 52 | +    return schedule_hybrid_combine.execution_time, schedule_genetic1.execution_time, schedule_genetic2.execution_time | 
|  | 53 | + | 
|  | 54 | +if __name__ == '__main__': | 
|  | 55 | +    arguments = [(graph_size, iteration) for graph_size in [100, 200, 300, 400, 500] for iteration in range(5)] | 
|  | 56 | +    results = {graph_size: [] for graph_size in [100, 200, 300, 400, 500]} | 
|  | 57 | + | 
|  | 58 | +    with pathos.multiprocessing.Pool(processes=11) as p: | 
|  | 59 | +        r = p.map(run_experiment, arguments) | 
|  | 60 | + | 
|  | 61 | +        for (graph_size, _), (combined_time, time1, time2) in zip(arguments, r): | 
|  | 62 | +            results[graph_size].append((combined_time / time1, combined_time / time2)) | 
|  | 63 | + | 
|  | 64 | +    with open('hybrid_results.json', 'w') as f: | 
|  | 65 | +        json.dump(results, f) | 
0 commit comments