Skip to content

Commit

Permalink
Integrated seeding. Modify fitness function of exp 5 for simplified i…
Browse files Browse the repository at this point in the history
…ntro example.
  • Loading branch information
openscenario committed Sep 27, 2024
1 parent f2927d1 commit a113397
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion default_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def getExp5() -> Experiment:
"velocity_ego",
"orientation_ped",
"velocity_ped"],
fitness_function=FitnessMinDistanceVelocityFrontOnly(),
fitness_function=FitnessMinDistanceVelocity(),
critical_function=CriticalAdasDistanceVelocity(),
simulate_function=DummySimulator.simulate,
simulation_time=10,
Expand Down
3 changes: 2 additions & 1 deletion opensbt/algorithm/nsga2_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def __init__(self,
"Crossover probability" : str(config.prob_crossover),
"Crossover eta" : str(config.eta_crossover),
"Mutation probability" : str(config.prob_mutation),
"Mutation eta" : str(config.eta_mutation)
"Mutation eta" : str(config.eta_mutation),
"Seed" : str(config.seed)
}

self.algorithm = NSGA2(
Expand Down
9 changes: 7 additions & 2 deletions opensbt/algorithm/nsga2dt_optimizer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import random
from opensbt.model_ga.result import SimulationResult
from opensbt.evaluation.critical import *
from pymoo.termination import get_termination
Expand Down Expand Up @@ -64,6 +65,8 @@ def run(self) -> SimulationResult:
'''Initial conditions (initial region)'''
xl = problem.xl
xu = problem.xu

random.seed(config.seed)

# sampling = FloatRandomSampling()
sampling = LHS() # Latin Hypercube Sampling
Expand Down Expand Up @@ -137,7 +140,7 @@ def run(self) -> SimulationResult:
res = minimize(sub_problem,
algorithm,
termination,
seed=1,
seed=config.seed,
save_history=True,
verbose=True)

Expand Down Expand Up @@ -178,7 +181,9 @@ def run(self) -> SimulationResult:
"Crossover probability": str(config.prob_crossover),
"Crossover eta": str(config.eta_crossover),
"Mutation probability": str(config.prob_mutation),
"Mutation eta": str(config.eta_mutation)}
"Mutation eta": str(config.eta_mutation),
"Seed" : str(config.seed)
}

result = self._create_result(problem, hist_holder, inner_algorithm, execution_time)
self.res = result
Expand Down
5 changes: 4 additions & 1 deletion opensbt/algorithm/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Optimizer(ABC):
algorithm_name: str

parameters: str

config: SearchConfiguration

@abstractmethod
def __init__(self, problem: SimulationProblem, config: SearchConfiguration):
Expand All @@ -21,4 +23,5 @@ def run(self) -> SimulationResult:
self.algorithm,
self.termination,
save_history=self.save_history,
verbose=True)
verbose=True,
seed = self.config.seed)
6 changes: 5 additions & 1 deletion opensbt/algorithm/ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from pathlib import Path
from typing import List

import random

from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter
from pymoo.core.problem import Problem
Expand Down Expand Up @@ -63,7 +65,9 @@ def __init__(self,
log.info(f"Initialized algorithm with config: {config.__dict__}")

def run(self) -> SimulationResult:


random.seed(config.seed)

problem = self.problem
sample_size = self.sample_size
sampled = self.sampling_type()(problem,sample_size)
Expand Down
1 change: 1 addition & 0 deletions opensbt/algorithm/ps_rand.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self,
problem: Problem,
config: SearchConfiguration,
sampling_type = FloatRandomSampling):

super().__init__(
problem = problem,
config = config,
Expand Down
4 changes: 4 additions & 0 deletions opensbt/experiment/search_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class SearchConfiguration(object):
nadir = None
ideal = None

seed = None


#TODO create a search configuration file specific for each algorithm
class DefaultSearchConfiguration(SearchConfiguration):
Expand All @@ -38,3 +40,5 @@ class DefaultSearchConfiguration(SearchConfiguration):
# metrics
ref_point_hv = None
nadir = ref_point_hv

seed = None
4 changes: 4 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
help='Whether to use the simuator\'s visualization. This feature is useful for debugging and demonstrations, however it reduces the search performance.')
parser.add_argument('-info', dest='show_info', action='store_true',
help='Names of all defined experiments.')
parser.add_argument('-s', dest='seed', action='store', default=None, type=int,
help='Seed for randomized operations.')

args = parser.parse_args()

Expand Down Expand Up @@ -149,6 +151,8 @@
problem.design_names = args.design_names
if not args.do_visualize is None:
problem.do_visualize = args.do_visualize
if not args.seed is None:
config.seed = args.seed

####### Run algorithm

Expand Down

0 comments on commit a113397

Please sign in to comment.