Ejemplo n.º 1
0
    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"

    # solved in Nautilus.py
    ideal = np.array([-6.34, -3.44487179, -7.5, 0])
    nadir = np.array([-4.751, -2.86054116, -0.32111111, 9.70666666])

    # start solving
    method = ReferencePointMethod(problem=prob, ideal=ideal, nadir=nadir)

    # Pareto optimal solution: [-6.30, -3.26, -2.60, 3.63]

    print("Let's start solving\n")
counter = 1
total_count = len(num_gen_per_iter) * len(n_objs) * len(problem_names)
for gen in num_gen_per_iter:
    for n_obj, n_var in zip(n_objs, n_vars):
        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(
Ejemplo n.º 3
0
    def f_5(x):
        return np.max([np.abs(x[:, 0] - 0.65), np.abs(x[:, 1] - 0.65)], axis=0)

    def c_1(x, f=None):
        x = x.squeeze()
        return (x[0] + x[1]) - 0.5

    f1 = _ScalarObjective(name="f1", evaluator=f_1)
    f2 = _ScalarObjective(name="f2", evaluator=f_2)
    f3 = _ScalarObjective(name="f3", evaluator=f_3)
    f4 = _ScalarObjective(name="f4", evaluator=f_4)
    f5 = _ScalarObjective(name="f5", evaluator=f_5)
    varsl = variable_builder(
        ["x_1", "x_2"],
        initial_values=[0.5, 0.5],
        lower_bounds=[0.3, 0.3],
        upper_bounds=[1.0, 1.0],
    )
    c1 = ScalarConstraint("c1", 2, 5, evaluator=c_1)
    problem = MOProblem(variables=varsl,
                        objectives=[f1, f2, f3, f4, f5],
                        constraints=[c1])

    method = NIMBUS(problem, scalar_method="scipy_de")
    reqs = method.request_classification()[0]

    response = {}
    response["classifications"] = ["<", "<=", "=", ">=", "0"]
    response["levels"] = [-6, -3, -5, 8, 0.349]
    response["number_of_solutions"] = 3
    reqs.response = response
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
        return res

    def f_3(x):
        # max x_1 + x_2 - x_3
        res = -x[:, 0] - x[:, 1] - x[:, 2]
        return res

    f1 = _ScalarObjective(name="Price", evaluator=f_1, maximize=False)
    f2 = _ScalarObjective(name="Quality", evaluator=f_2, maximize=True)
    f3 = _ScalarObjective(name="Size", evaluator=f_3, maximize=True)

    objl = [f1, f2, f3]

    # define the variables, bounds -5 <= x_1 and x_2 <= 5
    varsl = variable_builder(["x_1", "x_2", "x_3"],
                             initial_values=[0.5, 0.5, 0.5],
                             lower_bounds=[-5, -5, -5],
                             upper_bounds=[5, 5, 5])

    # define constraints
    def c_1(x, f=None):
        x = x.squeeze()
        # x_1 < 2
        return x[0] + 2

    # name of constraints, num variables, num objectives, evaluator
    c1 = ScalarConstraint("c1", len(varsl), len(objl), evaluator=c_1)

    problem = MOProblem(variables=varsl, objectives=objl, constraints=[c1])

    max_bool = list(
        map(lambda x: True if x < 0 else False, problem._max_multiplier))
Ejemplo n.º 6
0
        self.ideal_fitness = np.amin(np.vstack((self.ideal_fitness, fitness)),
                                     axis=0)

        self.ideal = (np.amin(
            np.vstack((self.ideal, objective_vectors)) * self._max_multiplier,
            axis=0,
        ) * 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)
Ejemplo n.º 7
0
    return x[:, 1]


def c_1(x, y):
    a = 0.1
    b = 16
    return (-x[:, 0]**2 - x[:, 1]**2 + 1 +
            a * np.cos(b * np.arctan(x[:, 0] / x[:, 1])))


def c_2(x, y):
    return -0.5 + (x[:, 0] - 0.5)**2 + (x[:, 1] - 0.5)**2


list_vars = variable_builder(["x", "y"],
                             initial_values=[0, 0],
                             lower_bounds=[0, 0],
                             upper_bounds=[np.pi, np.pi])

f1 = _ScalarObjective(name="f1", evaluator=f_1)
f2 = _ScalarObjective(name="f2", evaluator=f_2)

c1 = ScalarConstraint("c1", 2, 2, c_1)
c2 = ScalarConstraint("c2", 2, 2, c_2)

problem = MOProblem(variables=list_vars,
                    objectives=[f1, f2],
                    constraints=[c1, c2])

evolver = RVEA(problem)
"""
figure = animate_init_(evolver.population.objectives, filename="MOPC4.html")