diff --git a/experiments/genetic_PSPLIB.py b/experiments/genetic_PSPLIB.py index eb402692..34ea5d12 100644 --- a/experiments/genetic_PSPLIB.py +++ b/experiments/genetic_PSPLIB.py @@ -1,11 +1,11 @@ import time import uuid -from multiprocessing import Pool import multiprocess as mp import numpy as np from sampo.scheduler import GeneticScheduler, HEFTScheduler, HEFTBetweenScheduler +from sampo.scheduler.genetic import ScheduleGenerationScheme from sampo.schemas.contractor import Contractor from sampo.schemas.graph import WorkGraph, GraphNode, EdgeType from sampo.schemas.requirements import WorkerReq @@ -59,7 +59,8 @@ def run_scheduler(wg_info): work_estimator = PSPlibWorkTimeEstimator([Time(t) for t in times]) # scheduler = HEFTScheduler(work_estimator=work_estimator) # scheduler = HEFTBetweenScheduler(work_estimator=work_estimator) - scheduler = GeneticScheduler(20, size_of_population=50, work_estimator=work_estimator) + scheduler = GeneticScheduler(20, size_of_population=50, work_estimator=work_estimator, + sgs_type=ScheduleGenerationScheme.Serial, only_lft_initialization=True) start = time.time() schedule = scheduler.schedule(wg, contractor) finish = time.time() @@ -87,7 +88,7 @@ def run_scheduler(wg_info): gap_sum = 0.0 cnt = 0 - for _ in range(1): + for _ in range(3): with mp.Pool(10) as pool: result = pool.starmap(run_scheduler, np.expand_dims(dataset, 1)) result = np.array([res[1].value for res in result]) diff --git a/sampo/scheduler/genetic/base.py b/sampo/scheduler/genetic/base.py index 0c5c0b39..368cd6ff 100644 --- a/sampo/scheduler/genetic/base.py +++ b/sampo/scheduler/genetic/base.py @@ -69,6 +69,7 @@ def __init__(self, self._verbose = verbose self._time_border = None + self._max_plateau_steps = None self._deadline = None def __str__(self) -> str: @@ -120,6 +121,9 @@ def set_use_multiprocessing(self, n_cpu: int): def set_time_border(self, time_border: int): self._time_border = time_border + def set_max_plateau_steps(self, max_plateau_steps: int): + self._max_plateau_steps = max_plateau_steps + def set_deadline(self, deadline: Time): """ Set the deadline of tasks @@ -254,6 +258,7 @@ def schedule_with_cache(self, assigned_parent_time, timeline, self._time_border, + self._max_plateau_steps, self._optimize_resources, deadline, self._only_lft_initialization, diff --git a/sampo/scheduler/genetic/schedule_builder.py b/sampo/scheduler/genetic/schedule_builder.py index db5e1f0f..8b8f9380 100644 --- a/sampo/scheduler/genetic/schedule_builder.py +++ b/sampo/scheduler/genetic/schedule_builder.py @@ -144,9 +144,10 @@ def build_schedule(wg: WorkGraph, n_cpu: int = 1, assigned_parent_time: Time = Time(0), timeline: Timeline | None = None, - time_border: int = None, + time_border: int | None = None, + max_plateau_steps: int | None = None, optimize_resources: bool = False, - deadline: Time = None, + deadline: Time | None = None, only_lft_initialization: bool = False, verbose: bool = True) \ -> tuple[ScheduleWorkDict, Time, Timeline, list[GraphNode]]: @@ -218,7 +219,7 @@ def build_schedule(wg: WorkGraph, generation = 1 plateau_steps = 0 new_generation_number = generation_number if not have_deadline else generation_number // 2 - max_plateau_steps = new_generation_number // 2 + max_plateau_steps = max_plateau_steps if max_plateau_steps is not None else new_generation_number while generation <= new_generation_number and plateau_steps < max_plateau_steps \ and (time_border is None or time.time() - global_start < time_border): @@ -309,7 +310,7 @@ def build_schedule(wg: WorkGraph, plateau_steps = 0 new_generation_number = generation_number - generation + 1 - max_plateau_steps = new_generation_number // 2 + max_plateau_steps = max_plateau_steps if max_plateau_steps is not None else new_generation_number best_fitness = hof[0].fitness.values[0] if len(pop) < population_size: