1

我终于设法获得与我的医生研究相关的最小化和最大化代码。代码在这里。

我正在使用 Collaboratory,并且无论何时执行该代码,每次执行代码时,图形(代码的最后部分)也会有所不同。这是运行此代码并获得固定结果的一种方式吗?换句话说,每次模拟都得到相同的结果?

评估我的问题的代码:

!pip install platypus-opt

!pip install numpy

import matplotlib.pyplot as plt
import numpy as np

#variaveis (em R$/t)

S_preco = 2300
F_preco = 2350
O_preco = 3300
B_preco = 3800

#Área (em ha)
#MA - 1
#TO - 2
#PI - 3
#BA - 4
#Nova terra MA - 5
#Nova terra TO - 6
#Nova terra PI - 7
#Nova terra BA - 8

AreaMAX = [600000, 650000, 500000, 1500000, 309853, 930190, 14197, 55525]

#produtividade das terras (em t/ha)

Produtividade = [3.206, 3.322, 3.377, 3.779, 3, 3.12, 3, 3.3]

#custos

#custo do plantio da soja (em R$/t)
S_custo = [983, 966.5, 1051.5, 1040.167, 983, 966.5, 1051.5, 1040.167]

#custo do uso da terra (em R$/t)

Custo_terra = [0, 0, 0, 0, 7500, 9000, 10000, 20500]

#S_custo = 533.17
F_custo = 36.72
O_custo = 814.5
B_custo = 1000

#S_total = 2329278

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

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

S_demanda = 5056921 + 2332769 - 14000000
F_demanda = 1510028
O_demanda = 378913
B_demanda = 244031.02

#Definindo os valores iniciais

AreaSoja = [0, 0, 0, 0, 0, 0, 0, 0]

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

def belegundu(vars):
    AreaSoja[0] = vars[0]
    AreaSoja[1] = vars[1]
    AreaSoja[2] = vars[2]
    AreaSoja[3] = vars[3]
    AreaSoja[4] = vars[4]
    AreaSoja[5] = vars[5]
    AreaSoja[6] = vars[6]
    AreaSoja[7] = vars[7]
    S_total = vars[8] 
    S_comercio = vars[9]
    F_comercio = vars[10]
    O_comercio = vars[11]
    B_total = vars[12]
    G_total = vars[13]
    S_insumo = vars[14]

    objs = [S_comercio*S_preco + F_comercio*F_preco + O_comercio*O_preco + B_total*B_preco - (AreaSoja[0]*(Produtividade[0]*S_custo[0] + Custo_terra[0]) + AreaSoja[1]*(Produtividade[1]*S_custo[1] + Custo_terra[1]) + AreaSoja[2]*(Produtividade[2]*S_custo[2] + Custo_terra[2]) + AreaSoja[3]*(Produtividade[3]*S_custo[3] + Custo_terra[3]) + AreaSoja[4]*(Produtividade[4]*S_custo[4] + Custo_terra[4]) + AreaSoja[5]*(Produtividade[5]*S_custo[5] + Custo_terra[5]) + AreaSoja[6]*(Produtividade[6]*S_custo[6] + Custo_terra[6]) + AreaSoja[7]*(Produtividade[7]*S_custo[7] + Custo_terra[7])) - S_insumo*(F_custo + O_custo) - (B_total + G_total)*B_custo,
            AreaSoja[0] + AreaSoja[1] + AreaSoja[2] + AreaSoja[3] + AreaSoja[4] + AreaSoja[5] + AreaSoja[6] + AreaSoja[7]]
    constrs = [AreaSoja[0] - AreaMAX[0],
               AreaSoja[1] - AreaMAX[1],
               AreaSoja[2] - AreaMAX[2],
               AreaSoja[3] - AreaMAX[3],
               AreaSoja[4] - AreaMAX[4],
               AreaSoja[0]*Produtividade[0] + AreaSoja[1]*Produtividade[1] + AreaSoja[2]*Produtividade[2] + AreaSoja[3]*Produtividade[3] + AreaSoja[4]*Produtividade[4] + AreaSoja[5]*Produtividade[5] + AreaSoja[6]*Produtividade[6] + AreaSoja[7]*Produtividade[7] - S_total,
               S_comercio + S_insumo - S_total,
               F_comercio + (O_comercio + (B_total)*(O_massamolar/3*B_massamolar) + (G_total)*(O_massamolar/G_massamolar)) + 0.09*S_insumo - S_insumo,
               (O_comercio + (B_total)*(O_massamolar/3*B_massamolar) + (G_total)*(O_massamolar/G_massamolar)) - S_total*0.19,
               S_comercio - S_demanda,
               F_comercio - F_demanda,
               O_comercio - O_demanda,
               B_total - B_demanda]
    return objs, constrs

problem = Problem(15, 2, 13)
problem.types[:] = [Real(0, AreaMAX[0]), Real(0, AreaMAX[1]), Real(0, AreaMAX[2]), Real(0, AreaMAX[3]), Real(0, AreaMAX[4]),  Real(0, 1000000000), Real(0, 1000000000), Real(0, 1000000000), Real(0, 1000000000), Real(0, 1000000000), Real(0, 1000000000), Real(0, 1000000000), Real(0, 1000000000), Real(0, 1000000000), Real(0, 1000000000)]
problem.constraints[0] = "<=0"
problem.constraints[1] = "<=0"
problem.constraints[2] = "<=0"
problem.constraints[3] = "<=0"
problem.constraints[4] = "<=0"
problem.constraints[5] = "<1e-6"
problem.constraints[6] = "<1e-6"
problem.constraints[7] = "<1e-6"
problem.constraints[8] = "<1e-6"
problem.constraints[9] = ">=0"
problem.constraints[10] = ">=0"
problem.constraints[11] = ">=0"
problem.constraints[12] = ">=0"
problem.function = belegundu
problem.directions[0] = Problem.MAXIMIZE
problem.directions[1] = Problem.MINIMIZE

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

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)

import matplotlib.pyplot as plt

plt.scatter([s.objectives[0] for s in algorithm.result],
            [s.objectives[1] for s in algorithm.result])
plt.xlim([4000000000000, 5000000000000])
plt.ylim([0, 300000])
plt.xlabel("$f_1(x)$")
plt.ylabel("$f_2(x)$")
plt.show()`
4

0 回答 0