Skip to content

Commit

Permalink
parametrizing max_plateau_steps in genetic algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Timotshak committed Jan 22, 2024
1 parent 3318fe1 commit c0f7848
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
7 changes: 4 additions & 3 deletions experiments/genetic_PSPLIB.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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])
Expand Down
5 changes: 5 additions & 0 deletions sampo/scheduler/genetic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions sampo/scheduler/genetic/schedule_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit c0f7848

Please sign in to comment.