IOHprofiler / IOHexperimenter

Experimentation procedure for Iterative Optimization Heuristics
Other
47 stars 24 forks source link

segmentation fault when passing trigger in place #194

Open rickwierenga opened 9 months ago

rickwierenga commented 9 months ago

Passing triggers in place instead of storing them in a variable will result in a segmentation fault when evaluating an algorithm:

import ioh
import numpy as np

def styblinski_tang(x: np.ndarray) -> float:
    return np.sum(np.power(x, 4) - (16 * np.power(x, 2)) + (5 * x)) / 2
ioh.problem.wrap_real_problem(
    styblinski_tang,                                     # Handle to the function
    name="StyblinskiTang",                               # Name to be used when instantiating
    optimization_type=ioh.OptimizationType.MIN, # Specify that we want to minimize
    lb=-5,                                               # The lower bound
    ub=5,                                                # The upper bound
)

triggers = [ioh.logger.trigger.Each(3)]

logger = ioh.logger.Analyzer(
    folder_name="my-experiment",       
    algorithm_name="random-search",    
    triggers = [ioh.logger.trigger.Each(1)], # <----------- CRASH
    #triggers=triggers # no crash
)
problem = ioh.get_problem("StyblinskiTang", instance=1, dimension=2)
problem.attach_logger(logger)

class RandomSearch:
    'Simple random search algorithm'
    def __init__(self, n: int, length: float = 0.0):
        self.n: int = n
        self.length: float = length

    def __call__(self, problem: ioh.problem.RealSingleObjective) -> None:
        'Evaluate the problem n times with a randomly generated solution'

        for _ in range(self.n):
            # We can use the problems bounds accessor to get information about the problem bounds
            x = np.random.uniform(problem.bounds.lb, problem.bounds.ub)
            self.length = np.linalg.norm(x)

            problem(x)   

algorithm = RandomSearch(10)
algorithm(problem) # <---- will crash on this line