Project-Platypus / Platypus

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

violating constraints #102

Closed harsha3080 closed 1 year ago

harsha3080 commented 5 years ago

The following is my code:

(In File 2): import math import random import operator import functools from platypus.core import Problem, Solution, EPSILON from platypus.types import Real, Binary from abc import ABCMeta from retaining_wall_2ga_20p import opt from check import intersection_constr

Depth_of_Excavation = 9 x1 = 5 x2= 20 x3 = -15 x4 = -40 x5 = 7 x6 = 10 x7 = 1 x8 = 10

count = 0

class wall(Problem):

def __init__(self):
    super(wall, self).__init__(6,2,4)
    self.types[:] = [Real(x1,x2), Real(x3,x4), Real(x5,x6), Real(x1,x2), Real(x3,x4), Real(x7,x8)]
    self.constraints[0] = ">=0"
    self.constraints[1] = "<=0"
    self.constraints[2] = "!=10"
    self.constraints[3] = "==0"

def evaluate(self, solution):
    global count
    count +=1
    print(count)
    l1 = solution.variables[0]
    a1 = solution.variables[1]
    y1 = solution.variables[2]
    l2 = solution.variables[3]
    a2 = solution.variables[4]
    y2 = solution.variables[5]
    intersec_value = intersection_constr(l1,a1,y1,l2,a2,y2)
    solution.constraints[0] = y1-y2-2.5
    solution.constraints[1] = y1-y2-4.5
    solution.constraints[3] = intersec_value
    parameters = [l1,a1,y1,l2,a2,y2]
    print(parameters)
    test = opt(l1,a1,y1,l2,a2,y2)
    c = (70*(l1 + l2))
    solution.constraints[2] = test
    solution.variables[:] = [l1,a1,y1,l2,a2,y2]
    solution.objectives[:] = [test,c]

I run the file 1 below....everything is working fine but the population mostly violating constrains.

(In File 1):
from newtask import NSGAII from newtaskk import wall

problem = wall() algorithm = NSGAII(problem) algorithm.run(1350) for solution in algorithm.result: print(solution.variables) print(solution.objectives)

PastelBelem8 commented 4 years ago

Unfortunately, I also reached the same problem. It turns out that the constraint optimization methods provided by Platypus, compute the value for every solution for a constrained problem, yielding a vector with the individual magnitude of violation for each solution and a total penalty. In the documentation they clearly state:

The final population could contain infeasible and dominated solutions if the number of function evaluations was insufficient (e.g. algorithm.Run(100)). In this case, we would need to filter out the infeasible solutions.

Personally, I'm not a fan of having this particular implementation! The user should be provided with the option to select the intended use of a constrained optimization method: either hard constraint or soft constraint.

samira-kulkarni26 commented 4 years ago

I was also facing similar issue.I am trying to solve a multi-objective constrained problem using NSGA-II algorithm available in platypus. Problem has two objectives one is to minimize while the other objective is to maximize the value, with one of the constraints is sum of variable so selected should be equal to one.

The problem is supposed to provide optimization results if the variables so selected satisfies mentioned constraints.It is observed that while evaluating the algorithm, constraints are being violated to met the objective mentioned.

Is there any way to assign weights to constraints to ensure that particular constraint is satisfied always for optimized results? Is there any way to ensure that constraints are never being violated for specified objective?

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.