-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrosen_brock_brute_opt.py
More file actions
41 lines (36 loc) · 1.8 KB
/
rosen_brock_brute_opt.py
File metadata and controls
41 lines (36 loc) · 1.8 KB
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
#!/usr/bin/env python3
# Computes the minimum of Rosenbrock function (also known as banana function). Popular function to test robustness of an optimization method.
# Before running the code, do module load python/3.4 and module load all-pkgs
# Program is executed with four optional arguments. program.py low_x1 high_x1 low_x2 high_x2
# If you skip the optional arguments, random values are assigned.
import sys
import numpy
from scipy import optimize
from random import uniform
def rosenbrock(coordinates): # The rosenbrock function
x = coordinates[0]
y = coordinates[1]
f = (1 - x)**2 + (y - x**2)**2
return f
if __name__ == "__main__":
# assign defualt values from uniform random numbers
x_low = uniform(-10,0)
x_high = uniform(0,10)
y_low = uniform(-10,0)
y_high = uniform(0,10)
bound_array = [x_low, x_high, y_low, y_high]
for i in range(1,len(sys.argv)):
if i < 5:
# Replace the random values with the supplied values
bound_array[i-1] = float(sys.argv[i])
# The range for brute function requires in tuples
brute_range = ((bound_array[0],bound_array[1]), (bound_array[2], bound_array[3]))
print('Search Boundary x_low= {0:3.3f} x_high= {1:3.3f} y_low= {2:3.3f} y_high= {3:3.3f}'.format(*bound_array))
# Here we are doing a brute force optimization. The function is evaluated in grids of points.
# brute_range is a tuple and defines the boundary for the grid points
# finish=None means no local search. To make the search efficient choose finish=optimize.fmin
result_from_brute = optimize.brute(rosenbrock, brute_range, full_output=True, finish=None)
function_min = result_from_brute[1]
coordinate_of_min = result_from_brute[0]
#print ('Initial Coordinates= ',brute_range)
print ('Search Result= ',function_min, coordinate_of_min)