-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgekko_example.py
41 lines (37 loc) · 1.41 KB
/
gekko_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Run this example to check if the GEKKO optimization package has been installed correctly
"""
from gekko import GEKKO
m = GEKKO(remote=False) # Initialize gekko
m.options.SOLVER = 1 # APOPT is an MINLP solver
# optional solver settings with APOPT
m.solver_options = ['minlp_maximum_iterations 500',
# minlp iterations with integer solution
'minlp_max_iter_with_int_sol 10',
# treat minlp as nlp
'minlp_as_nlp 0',
# nlp sub-problem max iterations
'nlp_maximum_iterations 50',
# 1 = depth first, 2 = breadth first
'minlp_branch_method 1',
# maximum deviation from whole number
'minlp_integer_tol 0.05',
# covergence tolerance
'minlp_gap_tol 0.01']
# Initialize variables
x1 = m.Var(value=1, lb=1, ub=5)
x2 = m.Var(value=5, lb=1, ub=5)
# Integer constraints for x3 and x4
x3 = m.Var(value=5, lb=1, ub=5, integer=True)
x4 = m.Var(value=1, lb=1, ub=5, integer=True)
# Equations
m.Equation(x1*x2*x3*x4 >= 25)
m.Equation(x1**2+x2**2+x3**2+x4**2 == 40)
m.Obj(x1*x4*(x1+x2+x3)+x3) # Objective
m.solve(disp=True) # Solve
print('Results')
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))
print('x4: ' + str(x4.value))
print('Objective: ' + str(m.options.objfcnval))