forked from Yuricst/pygmo-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pygmo_snopt_unconstrained.py
91 lines (70 loc) · 2.53 KB
/
test_pygmo_snopt_unconstrained.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Test pygmo & snopt
"""
import os
from sys import platform
import numpy as np
from numba import jit, float64
import pygmo as pg
import pygmo_plugins_nonfree as ppnf
# PATH TO SNOPT LIBRARY --------------------------------- #
if platform == "linux" or platform == "linux2":
# linux
# provide path to snopt7.so as environment variable 'SNOPT_SO'
path_to_snopt7 = os.getenv('SNOPT_SO')
elif platform == "darwin":
# OS X
print("Not yet worked on...")
elif platform == "win32":
# Windows...
# provide path to snopt7.dll as environment variable 'SNOPT_DLL'
path_to_snopt7 = r"C:\Users\yshimane3\Documents\snopt\libsnopt7\snopt7.dll" #os.getenv('SNOPT_DLL')
print(f"Using path to snopt7: {path_to_snopt7}")
# ------------------------------------------------------- #
class Rosenbrock:
def __init__(self,dim):
self.dim = dim
def fitness(self,x):
return Rosenbrock._fitness(x)
# jit-ted fitness-computation for faster computation
@jit(float64[:](float64[:]),nopython=True)
def _fitness(x):
retval = np.zeros((1,))
for i in range(len(x) - 1):
tmp1 = (x[i + 1]-x[i]*x[i])
tmp2 = (1.-x[i])
retval[0] += 100.*tmp1*tmp1+tmp2*tmp2
return retval
def get_bounds(self):
return (np.full((self.dim,),-5.),np.full((self.dim,),10.))
def gradient(self, x):
return pg.estimate_gradient_h(lambda x: self.fitness(x), x)
def main():
# define udp
udp = Rosenbrock(dim=100)
prob = pg.problem(udp)
# c_tol = 1E-6
# prob.c_tol = [c_tol]*(nec + nic) # number of nonlinear constraints
# set population
pop = pg.population(prob, size=1)
# use snopt
pygmoSnopt = ppnf.snopt7(screen_output=False, library=path_to_snopt7, minor_version=7)
pygmoSnopt.set_numeric_option('Major feasibility tolerance', 1e-6)
pygmoSnopt.set_numeric_option('Minor feasibility tolerance', 1e-6)
pygmoSnopt.set_numeric_option('Major optimality tolerance', 1e-6)
pygmoSnopt.set_numeric_option('Major step limit', 2)
pygmoSnopt.set_integer_option('Iterations limit', 2000) # shoul be 4e3
# # pure SNOPT
algo = pg.algorithm(pygmoSnopt)
algo.set_verbosity(1)
print(algo)
# solve
pop = algo.evolve(pop)
# print result
xbest = pop.get_x()[pop.best_idx()]
fbest = pop.get_f()[pop.best_idx()]
print(f"fbest: {fbest}")
print(f"xbest: {xbest}")
print("Done!")
if __name__=="__main__":
main()