Skip to content

Commit 4bf0d53

Browse files
committed
Add constrained example
1 parent 58104c7 commit 4bf0d53

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

examples/rosenbrock_constrained.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# DFO-LS example: minimize the Rosenbrock function with arbitrary convex constraints
2+
from __future__ import print_function
3+
import numpy as np
4+
import dfols
5+
6+
# Define the objective function
7+
def rosenbrock(x):
8+
return np.array([10.0 * (x[1] - x[0] ** 2), 1.0 - x[0]])
9+
10+
# Define the starting point
11+
x0 = np.array([-1.2, 1])
12+
13+
def pball(x):
14+
c = np.array([0.7,1.5]) # ball centre
15+
r = 0.4 # ball radius
16+
return c + (r/np.max([np.linalg.norm(x-c),r]))*(x-c)
17+
18+
def pbox(x):
19+
l = np.array([-2, 1.1]) # lower bound
20+
u = np.array([0.9, 3]) # upper bound
21+
return np.minimum(np.maximum(x,l), u)
22+
23+
# For optional extra output details
24+
import logging
25+
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
26+
27+
# Call DFO-LS
28+
soln = dfols.solve(rosenbrock, x0, projections=[pball,pbox])
29+
30+
# Display output
31+
print(soln)

0 commit comments

Comments
 (0)