Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Constraints are seen as non-linear in the multi-objective case #145

Open
wittn-git opened this issue Nov 2, 2023 · 1 comment
Open
Labels
bug Something isn't working

Comments

@wittn-git
Copy link

I want to implement a multi-objective problem with two objectives and one constraint. However, when I am executing it, I get an error what: Non linear constraints detected in <class '__main__.SimpleProblem'> instance., with which my solver cannot deal (NSGA-||); however my constraint is non-linear.

To reproduce, I created a small example:
import pygmo as pg

class SimpleProblem:

    # objective functions and constraints
    def fitness(self, x):

        fitness_vector = []
        # first objective
        fitness_vector.append(x[0])
        constants = [1,0.5,0.5]
        # second objective
        fitness_vector.append(-sum([x[i] * constants[i] for i in range(3)]))
        # constraint
        fitness_vector.append(sum([x[i] for i in range(3)]) -2)

        return fitness_vector

    # number of objectives
    def get_nobj(self):
        return 2
    
    # number of inequality constraints
    def get_nic(self):
        return 1

    # real dimension of the problem
    def get_ncx(self):
        return 1

    # integer dimension of the problem
    def get_nix(self):
        return 3
    
    # bounds of the decision variables
    def get_bounds(self):
        return ([0] + [0] * 3, [1e6] + [1] * 3)


if __name__ == "__main__":
    model = SimpleProblem()
    problem = pg.problem(model)
    algorithm = pg.algorithm(pg.nsga2(gen=1000))
    population = pg.population(problem, size=100)
    population = algorithm.evolve(population)

I read drom the documentation, that to add objectives and constraints, one shall return a vector first containing the objectives followed by the constraints in the fitness function. However I do not know if this differs with multi-objectives, as I did not find any example for multi-objective optimization with constraints other than the variable bounds in the documentation. Hence I do not know if this is a bug or if I am supposed to give the objectives/constraints differently.

I am using Version 2.19.5.

Any help would be appreciated

@wittn-git wittn-git added the bug Something isn't working label Nov 2, 2023
@bluescarni
Copy link
Member

Hi Dominic,

The error message is indeed a bit misleading, in the sense that as far as PyGMO is concerned all constraints are nonlinear because PyGMO deals with general non-linear optimization problems and it currently has no capability to exploit the linear structure of some optimization problems.

The optimization problem you are trying to solve is quite complicated and there are not many solvers which are able to deal with constrained multi-objective problems. You can take a look at the list of algorithms here:

https://esa.github.io/pygmo2/overview.html#list-of-algorithms

And indeed I think only "Improved Harmony Search" can solve your problem. You also have the option of turning your multi objective constrained problem into a single-objective and/or unconstrained problem with the help of the decompose and/or unconstrain meta problems:

https://esa.github.io/pygmo2/problems.html?highlight=decompose#pygmo.decompose

https://esa.github.io/pygmo2/problems.html?highlight=decompose#pygmo.unconstrain

This should allow you to use other solvers as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants