Project-Platypus / Platypus

A Free and Open Source Python Library for Multiobjective Optimization
GNU General Public License v3.0
566 stars 153 forks source link

Not being able to store Non-Optimized Results #210

Closed farleynitro closed 1 year ago

farleynitro commented 1 year ago

When running an optimization for a Model class using the Problem Class, it is desired to also store the non-optimized model outcomes.

Why? In case one changes the Problem formulation, you can look what effect this has on model outcomes you are not optimizing over anymore.

Currently Platypus does not include a method to inform on such outcomes after every run/solution found. A solution would be to have a .INFO direction, or .NEUTRAL, instead of .MAXIMIZE or .MINIMIZE, this way the outcome is automatically part of the output.

Therefore, the length of non-optimized results should match the length of found solutions.

github-actions[bot] commented 1 year ago

This issue is stale and will be closed soon. If you feel this issue is still relevant, please comment to keep it active. Please also consider working on a fix and submitting a PR.

debpal commented 1 month ago

Try this way


# define class
class Schaffer(platypus.Problem):

    def __init__(self):
        super().__init__(1, 2)
        self.types[:] = platypus.Real(-3, 3)

    def objective_1(self, var):

        return var**2

    def objective_2(self, var):

        return (var-2)**2

    def evaluate(self, solution):
        x = solution.variables[:]
        solution.objectives[0] = self.objective_1(x[0])
        solution.objectives[1] = self.objective_2(x[0])

# define function
def schaffer_objectives_repository(
    select_algorithm, 
    nfe,
    **kwargs
):

    def schaffer_generation(
        algorithm
    ):

        objs_list = [i.objectives for i in algorithm.result]

        return objectives_history.append(objs_list)

    # optimization
    algorithm = getattr(platypus, select_algorithm)(
        problem=Schaffer(),
        **kwargs
    )

    # storing objectives
    objectives_history = []
    algorithm.run(nfe, callback=schaffer_generation)

    return objectives_history

# run the function
schaffer_objectives_repository(
    select_algorithm='NSGAII, 
    nfe=1000,
    population_size=20
)