jMetalPy icon indicating copy to clipboard operation
jMetalPy copied to clipboard

Handling of Constraints - Max/Mix Value Instead

Open milagoranova opened this issue 4 years ago • 6 comments

Hi,

Thank you for always being so prompt with responses in the past. I have a quick question about using constraints.

I can see in the examples that you can set a constraint to a specific number - for example: solution.constraints[0] = 1.0 - (x1 * x1 + x2 * x2) / 225.0

However, in my problem I have a maximum value (0.15) that my x1 can be as a constraint: solution.constraints[0] = x1 <= 0.15

I was not sure how to implement that and if it is possible. From what I read from the documentation for JMetalPy you are using the implementation proposed in 'A fast and elitist multiobjective genetic algorithm' for the handling constraints which supports such a scenario.

Could you please give me some guidance on how to achieve using a max value as a constraint if possible? Thanks in advance for your input.

Kind regards, Mila

milagoranova avatar Feb 18 '20 10:02 milagoranova

I have the same question and look forward to your reply.

MAOW0817 avatar Feb 25 '20 09:02 MAOW0817

Sorry @milagoranova for not responding before.

Every constraint must be expressed as an unequality of type expression >=0.0. This way, when expression < 0.0 then it is considered as a constraint violation, and the overall constraint violation degree of a solution can be computed with this function:

def overall_constraint_violation_degree(solution: Solution) -> float:
    """
    Returns the constraint violation degree of a solution, which is the sum of the constraint values that are not zero
    :param solution:
    :return:
    """
    return sum([value for value in solution.constraints if value < 0])

This function is used in the DominanceComparator class according to the scheme for constraint handling defined in Deb et al. paper (http://www.iitk.ac.in/kangal/Deb_NSGA-II.pdf), Section IV.A. You can see some examples of constrained problems in Table V and how they are implemented in jMetalPy (problem SRN and TNK in the table are named Srinivas and Tanaka in jMetalPy).

ajnebro avatar Feb 25 '20 09:02 ajnebro

Thank you, but I find that sometimes the final solution still violates the constraints. Why? Can we abandon the infeasible solution.

MAOW0817 avatar Feb 28 '20 09:02 MAOW0817

That happens when the metaheuristic is unable to find feasible solutions, and the reason may be due to the problem (the constraints make the feasible search space very difficult to find) or to the algorithm (it is not properly configured). If have found some cases where a minimum of 100000 function evaluations have been needed to find the first feasible solution.

When all the solutions in the population violates some constraint then the problem becomes single-objective, as the metaheuristic will try to find the solutions having the smallest overall constraint violation degree. For that reason the infeasible solutions cannot be removed.

Which algorithm are you using?

ajnebro avatar Mar 03 '20 11:03 ajnebro

Thanks for the detailed responses ajnebro. I implemented my constraints using NSGA-II with two objectives and dominance_comparator= DominanceComparator() and worked as expected.

I was wondering if there is an implementation for constraints handling for the single objective Evolutionary Strategy algorithm. I've successfully implemented an experiment with NSGA-II with a single objective, but I wanted to compare the solutions with ones from the Evolutionary Strategy algorithm.

milagoranova avatar Mar 03 '20 15:03 milagoranova

The issue is that the evolution strategy uses a sort in the replacement() method that does not take into account the constraints:

81        population_pool.sort(key=lambda s: (s.objectives[0]))

Just try to change the sentence by:

81        population_pool.sort(key=lambda s: (-overall_constraint_violation_degree(s), s.objectives[0]))

You should include this import to make it working:

from jmetal.util.constraint_handling import overall_constraint_violation_degree

Feel free to try to see whether it works. I haven't tested it because currently we don't have any constrained single objective problem in jMetalPy.

ajnebro avatar Mar 03 '20 16:03 ajnebro