Project-Platypus / Platypus

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

Close to work perfectly, however... #162

Closed RafaHPSUnicamp closed 1 year ago

RafaHPSUnicamp commented 3 years ago

Hi.

My code is now working. However, is still going as a unfeasible problem, despite the solutions looks like feasible solution. Also, the errors is close to zero, according to this code:


#variaveis

S_preco = 1069.23
F_preco = 1071.09
OB_preco = 2006.66
OR_preco = 2669.21
B_preco = 2540.47
S_custo = 533.17
F_custo = 569.89
OB_custo = 1384.39
OR_custo = 1466.34
B_custo = 2389.89

S_total = 2329278

S_percentoleo = 0.2057
C_percentoleo = 0.0064

OBF_percentoleo = 0.22

OR_massamolar = 873*0.000001
M_massamolar = 32*0.000001
B_massamolar = 292*0.000001
G_massamolar = 92*0.000001

S_capacidade = 3600000
OR_capacidade = 367200
B_capacidade = 887760*(880/1000)

S_demanda = 80638
F_demanda = 398984
OB_demanda = 164700
OR_demanda = 164700
B_demanda = 77634

from platypus import NSGAII, Problem, Real, unique, nondominated

def belegundu(vars):
    S_comercio = vars[0]
    F_comercio = vars[1]
    OB_comercio = vars[2]
    OR_comercio = vars[3]
    B_total = vars[4]
    S_insumo = vars[5]

    objs = [S_comercio*S_preco - S_comercio*S_custo + F_comercio*F_preco - F_comercio*F_custo + OB_comercio*OB_preco - OB_comercio*OB_custo + OR_comercio*OR_preco - OR_comercio*OR_custo + B_total*B_preco - B_total*B_custo]
    constrs = [S_comercio + S_insumo - S_total,
        F_comercio + (OB_comercio + B_total + OR_comercio) - S_insumo,
        (OB_comercio + B_total + OR_comercio) - S_total*0.19,
        S_comercio - S_demanda,
        F_comercio - F_demanda,
        OB_comercio - OB_demanda,
        OR_comercio - OR_demanda,
        B_total - B_demanda]
    return objs, constrs

problem = Problem(6, 1, 8)
problem.types[:] = [Real(80638, 2329278), Real(398984, 2329278), Real(164700, 442562.82), Real(164700, 442562.82), Real(77634, 442562.82), Real(0, 2329278)]
problem.constraints[0] = "==0"
problem.constraints[1] = "==0"
problem.constraints[2] = "==0"
problem.constraints[3] = ">=0"
problem.constraints[4] = ">=0"
problem.constraints[5] = ">=0"
problem.constraints[6] = ">=0"
problem.constraints[7] = ">=0"
problem.function = belegundu
problem.directions[:] = Problem.MAXIMIZE

algorithm = NSGAII(problem)
algorithm.run(3000000)

for solution in algorithm.result:
    print(solution.objectives)

for solution in algorithm.result:
    print(solution.variables)

for solution in unique(nondominated(algorithm.result)):
    print(solution.objectives)

for solution in unique(nondominated(algorithm.result)):
    print(solution.variables)

for solution in unique(nondominated(algorithm.result)):
    print(solution.constraints)

for solution in unique(nondominated(algorithm.result)):
    print(solution.feasible)

for solution in algorithm.result:
    print(solution.constraint_violation)

So, what adjusts I needed to do to the FALSE become TRUE?? As can see, the errors in the solution.constraint_violation, is closing to zero. However, it still appoint as unfeasible solution. Also, I checked out that restrictions is now obeyed.

jetuk commented 3 years ago

One option would be to add a small tolerance to your constraint calculation. E.g.

abs(S_comercio + S_insumo - S_total)

...

problem.constraints[0] = "=<1e-6"
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.