Non Linear Programming, baik constraint ataupun objective function memiliki persamaan nonlinear, seperti perkalian antara variable, atau penggunaan fungsi non linear seperti Sin(), Cos() dan lainnya.
Mari kita ubah persamaan matematika dari modul sebelumnya menjadi persamaan nonlinear
#objective function
max x + xy
#constraint
-x + 2xy <= 8
2x + y <= 14
2x - y <= 10
0 <= x <= 10
0 <= y <= 10
#import library yang diperlukan
import pyomo.environ as pyo
from pyomo.environ import *
from pyomo.opt import SolverFactory
#inisialisasi model
model = pyo.ConcreteModel()
#definisikan variable
model.x = pyo.Var(bounds=(0,10))
model.y = pyo.Var(bounds=(0,10))
x = model.x
y = model.y
#definsikan constraint, perhatikan C1 memiliki persamaan nonlinear
model.C1 = pyo.Constraint(expr= -x+2*y*x<=8)
model.C2 = pyo.Constraint(expr= 2*x+y<=14)
model.C3 = pyo.Constraint(expr= 2*x-y<=10)
#definisikan objective function, perhatikan persamaan adalah nonlinear
model.obj = pyo.Objective(expr= x+y*x, sense=maximize)
#gunakan solver ipopt
opt = SolverFactory('ipopt', executable='C:\\ipopt\\bin\\ipopt.exe')
opt.solve(model)
model.pprint()
x_value = pyo.value(x)
y_value = pyo.value(y)
print('---------------------------------')
print('x=',x_value)
print('y=',y_value)
F:\Project\optimizationpy> python nonlp_pyomo.py
2 Var Declarations
x : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : 0 : 5.6067151589768685 : 10 : False : False : Reals
y : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : 0 : 1.2134302215032389 : 10 : False : False : Reals
1 Objective Declarations
obj : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : maximize : x + y*x
3 Constraint Declarations
C1 : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : -Inf : - x + 2*y*x : 8.0 : True
C2 : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : -Inf : 2*x + y : 14.0 : True
C3 : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : -Inf : 2*x - y : 10.0 : True
6 Declarations: x y C1 C2 C3 obj
---------------------------------
x= 5.6067151589768685
y= 1.2134302215032389
Solver dapat download di https://drive.google.com/file/d/1dgFE5ItIVxKPKsDyFXv3QpnW1cSi-AwF/view?usp=sharing
Pembahasan Code
Silakan lihat komentar pada code diatas untuk melihat penjelasan program. Dapat Anda perhatikan, proses dan logika coding tidak ada yang berubah dengan linear programming sebelumnya.
Perbadaanya saat penggunaan solver
opt = SolverFactory('ipopt', executable='C:\\ipopt\\bin\\ipopt.exe')