for problem_name in problem_names: print(f"Loop {counter} of {total_count}") counter += 1 # build problem var_names = ["x" + str(i + 1) for i in range(n_var)] obj_names = ["f" + str(i + 1) for i in range(n_obj)] prob = wfg_problem( name=problem_name, n_of_objectives=n_obj, n_of_variables=n_var ) variables = variable_builder( names=var_names, initial_values=prob.lower, lower_bounds=prob.lower, upper_bounds=prob.upper, ) objective = VectorObjective(name=obj_names, evaluator=prob.eval) problem = MOProblem([objective], variables, None) problem.ideal = prob.ideal problem.nadir = ( abs(np.random.normal(size=n_obj, scale=0.15)) + 1 ) * prob.nadir true_nadir = prob.nadir scalar = asf(ideal=problem.ideal, nadir=true_nadir) # a posteriori a_post_rvea = RVEA( problem=problem, interact=False, n_gen_per_iter=gen, n_iterations=4 ) a_post_nsga = NSGAIII(
def test_problem_builder(name: str, n_of_variables: int = None, n_of_objectives: int = None) -> MOProblem: """Build test problems. Currently supported: ZDT1-4, ZDT6, and DTLZ1-7. Args: name (str): Name of the problem in all caps. For example: "ZDT1", "DTLZ4", etc. n_of_variables (int, optional): Number of variables. Required for DTLZ problems, but can be skipped for ZDT problems as they only support one variable value. n_of_objectives (int, optional): Required for DTLZ problems, but can be skipped for ZDT problems as they only support one variable value. Raises: ProblemError: When one of many issues occur while building the MOProblem instance. Returns: MOProblem: The test problem object """ problems = { "ZDT1": zdt.ZDT1, "ZDT2": zdt.ZDT2, "ZDT3": zdt.ZDT3, "ZDT4": zdt.ZDT4, "ZDT5": zdt.ZDT5, "ZDT6": zdt.ZDT6, "DTLZ1": dtlz.DTLZ1, "DTLZ2": dtlz.DTLZ2, "DTLZ3": dtlz.DTLZ3, "DTLZ4": dtlz.DTLZ4, "DTLZ5": dtlz.DTLZ5, "DTLZ6": dtlz.DTLZ6, "DTLZ7": dtlz.DTLZ7, } num_var = {"ZDT1": 30, "ZDT2": 30, "ZDT3": 30, "ZDT4": 10, "ZDT6": 10} if not (name in problems.keys()): msg = ( "Specified Problem not yet supported.\n The supported problems are:" + str(problems.keys())) raise ProblemError(msg) if "ZDT" in name: if n_of_variables is None: n_of_variables = num_var[name] if n_of_objectives is None: n_of_objectives = 2 if not (n_of_variables == num_var[name]): msg = (name + " problem has been limited to " + str(num_var[name]) + " variables. Number of variables recieved = " + str(n_of_variables)) raise ProblemError(msg) if not (n_of_objectives == 2): msg = ("ZDT problems can only have 2 objectives. " + "Number of objectives recieved = " + str(n_of_objectives)) raise ProblemError(msg) obj_func = problems[name]() elif "DTLZ" in name: if (n_of_variables is None) or (n_of_objectives is None): msg = ("Please provide both number of variables and objectives" + " for the DTLZ problems") raise ProblemError(msg) obj_func = problems[name](n_of_objectives, n_of_variables) else: msg = "How did you end up here?" raise ProblemError(msg) lower_limits = obj_func.min_bounds upper_limits = obj_func.max_bounds var_names = ["x" + str(i + 1) for i in range(n_of_variables)] obj_names = ["f" + str(i + 1) for i in range(n_of_objectives)] variables = variable_builder( names=var_names, initial_values=lower_limits, lower_bounds=lower_limits, upper_bounds=upper_limits, ) # Because optproblems can only handle one objective at a time def modified_obj_func(x): if isinstance(x, list): if len(x) == n_of_variables: return [obj_func(x)] elif len(x[0]) == n_of_variables: return list(map(obj_func, x)) else: if x.ndim == 1: return [obj_func(x)] elif x.ndim == 2: return list(map(obj_func, x)) raise TypeError("Unforseen problem, contact developer") objective = VectorObjective(name=obj_names, evaluator=modified_obj_func) problem = MOProblem([objective], variables, None) return problem
xs = np.atleast_2d(xs) return -8.21 + (0.71 / (1.09 - xs[:, 0]**2)) def f4(xs): xs = np.atleast_2d(xs) return -0.96 + (0.96 / (1.09 - xs[:, 1]**2)) def objectives(xs): return np.stack((f1(xs), f2(xs), f3(xs), f4(xs))).T obj1 = _ScalarObjective("obj1", f1) obj2 = _ScalarObjective("obj2", f2) obj3 = _ScalarObjective("obj3", f3) obj4 = _ScalarObjective("obj4", f4) objkaikki = VectorObjective("obj", objectives) # variables var_names = ["x1", "x2" ] # Make sure that the variable names are meaningful to you. initial_values = np.array([0.5, 0.5]) lower_bounds = [0.3, 0.3] upper_bounds = [1.0, 1.0] bounds = np.stack((lower_bounds, upper_bounds)) variables = variable_builder(var_names, initial_values, lower_bounds, upper_bounds) # problem prob = MOProblem(objectives=[obj1, obj2, obj3, obj4], variables=variables) # objectives "seperately"
) * self._max_multiplier) n_obj = 3 n_var = 10 benchmark = dtlz.DTLZ1(num_objectives=n_obj, num_variables=n_var) variables = variable_builder( names=[f"x{i+1}" for i in range(n_var)], initial_values=[0] * n_var, lower_bounds=[0] * n_var, upper_bounds=[1] * n_var, ) objectives = [ VectorObjective(name=[f"f{i+1}" for i in range(n_obj)], evaluator=lambda x: np.asarray(list(map(benchmark, x)))) ] utopian = np.asarray([0, 0, 0]) - 1e-6 nadir = np.asarray([1, 1, 1]) PIS = classificationPIS(scalarizers=[AUG_GUESS_GLIDE], utopian=utopian, nadir=nadir) first_preference = { "classifications": ["=", ">=", "<="], "current solution": np.asarray([0.5, 0.5, 0.5]), "levels": np.asarray([0.5, 0.8, 0.2]), } PIS.update_preference(first_preference)