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

How to select the best result and the best variables from them #158

Closed RafaHPSUnicamp closed 1 year ago

RafaHPSUnicamp commented 3 years ago

Hi, I just wanted to know how to select the best results from the Playtpus code that I adapted from LINGO software. Because the Playtpus gives me a lot of good results. For example, I wanted ONLY ONE result, and the best result. How I can do that from 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

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]
    C_insumo = vars[6]
    OB_total = vars[7]
    OR_total = vars[8]
    OR_biodiesel = vars[9]
    MOL = vars[10]
    M_insumo = vars[11]
    G_comercio = vars[12]
    objs = [1*(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_total - S_comercio - S_insumo,
        S_insumo - C_insumo - F_comercio - OB_total,
        C_insumo - 0.04*S_insumo,
        OB_total - (F_comercio*OBF_percentoleo)/(1 - OBF_percentoleo),
        OB_total - OB_comercio - OR_total,
        OR_total - OR_comercio - OR_biodiesel,
        OR_biodiesel - MOL*OR_massamolar,
        M_insumo - 3*MOL*M_massamolar,
        B_total - 3*MOL*B_massamolar,
        G_comercio - MOL*G_massamolar,
        S_insumo - S_capacidade,
        OR_total - OR_capacidade,
        B_total - B_capacidade,
        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(13, 1, 18)
problem.types[:] = [Real(0, 2329278), Real(0, 2329278), Real(0, 2329278), Real(0, 2329278), Real(0, 2329278), Real(0, 2329278), Real(0, 2329278), Real(0, 2329278), Real(0, 2329278), Real(0, 2329278), Real(0, 2329278), Real(0, 2329278), 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.constraints[8] = "==0"
problem.constraints[9] = "==0"
problem.constraints[10] = "<=0"
problem.constraints[11] = "<=0"
problem.constraints[12] = "<=0"
problem.constraints[13] = ">=0"
problem.constraints[14] = ">=0"
problem.constraints[15] = ">=0"
problem.constraints[16] = ">=0"
problem.constraints[17] = ">=0"
problem.function = belegundu
problem.directions[:] = Problem.MAXIMIZE

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

feasible_solutions = [s for s in algorithm.result if s.feasible]

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

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

Remember that I adapted from this LINGO code:

MAX = L_soja + L_farelo + L_oleobruto + L_oleorefinado + L_biodiesel;

L_soja = S_comercio*S_preco - S_comercio*S_custo;
L_farelo = F_comercio*F_preco - F_comercio*F_custo;
L_oleobruto = OB_comercio*OB_preco - OB_comercio*OB_custo;
L_oleorefinado = OR_comercio*OR_preco - OR_comercio*OR_custo;
L_biodiesel = B_total*B_preco - B_total*B_custo;

!variaveis de preço e de custo (em R$/t);

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;

!quantidade inicial de soja;

S_total = 2329278; !t;

!produção dos subprodutos;

S_total = S_comercio + S_insumo;
S_insumo = C_insumo + F_comercio + OB_total; !t;
C_insumo = 0.04*S_insumo;

S_percentoleo = 0.2057;
C_percentoleo = 0.0064;

OBF_percentoleo = 0.22;
OB_total = (F_comercio*OBF_percentoleo)/(1 - OBF_percentoleo);

!balanceamento das massas do óleo;

OB_total = OB_comercio + OR_total;

!balanceamento das massas do óleo refinado;

OR_total = OR_comercio + OR_biodiesel;

!estequiometria da reação do biodiesel (massas molares em kg/mol);

OR_biodiesel = MOL*OR_massamolar;
M_insumo = 3*MOL*M_massamolar;
B_total = 3*MOL*B_massamolar;
G_comercio = MOL*G_massamolar;

SUM_REAGENTES = OR_biodiesel + M_insumo;
SUM_PRODUTOS = B_total + G_comercio;

!constantes da reação estequiometrica (convertido de g/mol para t/mol) ;

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

!restrições (capacidade maxima);

S_insumo <= S_capacidade;
OR_total <= OR_capacidade;
B_total <= B_capacidade;

!restrições (demanda mínima);

S_comercio >= S_demanda;
F_comercio >= F_demanda;
OB_comercio >= OB_demanda;
OR_comercio >= OR_demanda;
B_total >= B_demanda;

!constantes das restrições (em t);

S_capacidade = 3600000;
OR_capacidade = 367200;
B_capacidade = 887760*(880/1000); !Convertido de metros cubicos (m³) para kg, e consequentemente em toneladas;

S_demanda = 80638;
F_demanda = 398984;
OB_demanda = 164700;
OR_demanda = 164700;
B_demanda = 77634;
EveCharbie commented 2 years ago

@RafaHPSUnicamp did someone answer you?

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.

EveCharbie commented 1 year ago

Just commenting so that the issue is not closed since we did not get an answer.

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.