-
Notifications
You must be signed in to change notification settings - Fork 0
/
DerivativeFreeMethods.py
155 lines (103 loc) · 3.71 KB
/
DerivativeFreeMethods.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
# coding: utf-8
# In[1]:
'''Implementing Derivative Free Methods'''
'''Dichotomous search Algorithm'''
# Default values epsilon=0.01, function = -2x^2 - 2x, initial interval of uncertainity = [-3,6]
# and final length of uncertainity interval(l=0.2)
# If we will not enter anything,the program will consider the above default values.
def DichotomousSearch(k=0, epsilon=0.01, l=0.2):
try:
x = []
for i in range(2):
z = float(input('Enter the first and second value in interval of uncertainity'))
x.append(z)
except:
print('No interval taken, default value is assigned for interval of uncertainity i.e. x ')
x = [-3,6]
try:
epsilon = float(input('Enter the value of epsilon'))
except:
print('No interval taken, default value is assigned for epsilon')
pass
def func(y):
f = -(y**2) - 2*y
return f
a_k=x[0]
b_k=x[1]
p = {'k':k, 'epsilon':epsilon, 'l':l, 'a_k':a_k, 'b_k':b_k }
while (p['b_k'] - p['a_k'])> p['l']:
lambd_k = (p['b_k'] + p['a_k'])*0.5 - p['epsilon']
mu_k = (p['b_k'] + p['a_k'])*0.5 + p['epsilon']
f1 = func(lambd_k)
f2 = func(mu_k)
if f1>=f2:
p['b_k'] = mu_k
else:
p['a_k'] = lambd_k
k = k +1
local_minima = (p['a_k'] + p['b_k'])/2
minimum_value = func(local_minima)
optimized_values = {'local_minima':local_minima, 'minimum_value':minimum_value, 'Number of iterations taken':k}
return optimized_values
local_minima = DichotomousSearch()
print('Dichotomous Search method: ',local_minima)
# In[ ]:
'''Implementing Derivative Free Methods'''
'''Fibbonacci Search Algorithm'''
# Default values epsilon=0.01, function = x^2 + 54/x, initial interval of uncertainity = [0,5]
# and final length of uncertainity interval(L=0.2)
# Function for nth Fibonacci number where fn = [1,1,2,3,5,8,13,....] i.e. zeroth fibonacci number taken is 1,
# 1st fibonacci number = 1, 2nd fibonacci number = 2, and so on.
# If we will not enter anything,the program will consider the above default values.
def FibonacciNumber(n):
if n<0:
print("Incorrect input")
elif n==0:
return 1
elif n==1:
return 1
elif n==2:
return 2
else:
return FibonacciNumber(n-1)+FibonacciNumber(n-2)
def FibonacciSearch(k=2, n=3, l=0.2):
try:
x = []
for i in range(2):
z = float(input('Enter the first and second value in interval of uncertainity'))
x.append(z)
except:
print('No interval taken, default value is assigned for interval of uncertainity')
x = [0,5]
def func(y):
f = y**2 + 54/y
return f
a_k=x[0]
b_k=x[1]
L = x[1] - x[0]
p = {'k':k, 'a_k':a_k, 'b_k':b_k }
while (k-1)!=n:
num = n-k +1
den = n+1
num = FibonacciNumber(num)
den = FibonacciNumber(den)
L_star = (num/den)*L
lambd_k = p['a_k'] + L_star
mu_k = p['b_k'] - L_star
f1 = func(lambd_k)
f2 = func(mu_k)
if f1>=f2:
p['a_k'] = lambd_k
else:
p['b_k'] = mu_k
k = k + 1
local_minima = []
local_minima.append(p['a_k'])
local_minima.append(p['b_k'])
optimized_values = {'local_minima lies between':local_minima, 'Number of iterations taken':k-2}
return optimized_values
local_minima = FibonacciSearch(k=2, n=3, l=0.2)
print('Fibonacci Search method: ',local_minima)
# In[ ]:
# In[ ]: