Project-Platypus / Platypus

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

```python #107

Closed tianjingyao closed 5 years ago

tianjingyao commented 5 years ago
from platypus import NSGAII, Problem, Real, Archive

# We can subclass other kinds of Archive instead if needed.
class LoggingArchive(Archive):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, *kwargs)
        self.log = []

    def add(self, solution):
        super().add(solution)
        self.log.append(solution)

log_archive = LoggingArchive()

# copy the example from the platypus README
def schaffer(x):
    return [x[0]**2, (x[0]-2)**2]
problem = Problem(1, 2)
problem.types[:] = Real(-10, 10)
problem.function = schaffer

# here we tell the optimizer to use the archive we chose.
algorithm = NSGAII(problem, archive = log_archive)
algorithm.run(10)

print(log_archive.log[:5]) # print the first 5 solutions in the log

Originally posted by @Wbec in https://github.com/Project-Platypus/Platypus/issues/71#issuecomment-513524660