pythonmathematical-optimizationpygmo

Multi-objective optimisation using PyGMO


I am using the PyGMO package for Python, for multi-objective optimisation. I am unable to fix the dimension of the fitness function in the constructor, and the documentation is not very descriptive either. I am wondering if anyone here has had experience with PyGMO in the past: this could be fairly simple.

I try to construct a minimum example below:

from PyGMO.problem import base
from PyGMO import algorithm, population
import numpy as np
import matplotlib.pyplot as plt


class my_problem(base):
    def __init__(self, fdim=2):
        NUM_PARAMS = 4
        super(my_problem, self).__init__(NUM_PARAMS)
        self.set_bounds(0.01, 100)

    def _objfun_impl(self, K):
        E1 = K[0] + K[2]
        E2 = K[1] + K[3]

        return (E1, E2, )


if __name__ == '__main__':
    prob = my_problem()  # Create the problem
    print (prob)
    algo = algorithm.sms_emoa(gen=100)
    pop = population(prob, 50)
    pop = algo.evolve(pop)

    F = np.array([ind.cur_f for ind in pop]).T
    plt.scatter(F[0], F[1])
    plt.xlabel("$E_1$")
    plt.ylabel("$E_2$")
    plt.show()

fdim=2 above is a failed attempt to set the fitness dimension. The code fails with the following error:

ValueError: ..\..\src\problem\base.cpp,584: fitness dimension was changed inside objfun_impl().

I'd be grateful if someone can help figure this out. Thanks!


Solution

  • Are you looking at the correct documentation?

    There is no fdim (which anyway does nothing in your example since it is only a local variable and is not used). But there is n_obj:

    n_obj: number of objectives. Defaults to 1

    So, I think you want something like (corrected thanks to @Distopia):

    #(...)
    def __init__(self, fdim=2):
        NUM_PARAMS = 4
        super(my_problem, self).__init__(NUM_PARAMS, 0, fdim)
        self.set_bounds(0.01, 100)
    #(...)