-
Notifications
You must be signed in to change notification settings - Fork 0
/
One_dimensional_optimization.py
129 lines (96 loc) · 3.32 KB
/
One_dimensional_optimization.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
import matplotlib as mlab
import pylab
import math
while True:
variant = input('Enter your variant: ')
if not variant.isdigit() or int(variant) < 0:
print("Enter correct variant")
continue
if variant or variant == "exit":
break
own_vers = int(variant)
def func_x(i):
a = -own_vers
b = own_vers*2
n = own_vers+10
return a + i*(b-a)/n
arr_i = [i for i in range(0,own_vers+11)]
arr_of_x = (list(map(func_x,arr_i)))
def f1(x):
n = own_vers
return (x-n)**2 + n*x
def f2(x):
n = own_vers
return (x-n)**2 + n*x**2
def f3(x):
n = own_vers
return (x-n)**2 + math.sin(n*x)
arr_of_result_f1 = list(map(f1,arr_of_x))
arr_of_result_f2 = list(map(f2,arr_of_x))
arr_of_result_f3 = list(map(f3,arr_of_x))
print('Min of F1 is: ',min(arr_of_result_f1))
print('Min of F2 is: ',min(arr_of_result_f2))
print('Min of F3 is: ',min(arr_of_result_f3))
print('-----------------------------------------------------------------')
print('|{:^15}|{:^15}|{:^15}|{:^15}|'.format('ai','F1','F2','F3'))
print('-----------------------------------------------------------------')
for nn in arr_of_x:
print('|{:^15.11}|{:^15.11}|{:^15.11}|{:^15.11}|'.format(nn,f1(nn),f2(nn),f3(nn)))
def extreme_points(arr_func,final_arr):
arr_of_x = []
arr_of_x.clear()
if arr_func == []:
return
min_func = min(arr_func)
min_right_elem = arr_func.index(min_func)+1
min_left_elem = arr_func.index(min_func)-1
arr_of_x.append(min_left_elem)
arr_of_x.append(min_right_elem)
final_arr.append(min_func)
return arr_of_x
def fill_new_arr(first_ai,last_ai,i):
a = first_ai
b = last_ai
n = own_vers + 10
return a + i * (b - a) / n
def sequential_search_func(arr_of_result_func,curent_func):
final_arr_seq_searc = []
for i in range(1, 27):
extr_point_arr = extreme_points(arr_of_result_func,final_arr_seq_searc)
if i == 1:
first_ai = func_x(extr_point_arr[0])
last_ai = func_x(extr_point_arr[1])
else:
first_ai = arr_x_method2[extr_point_arr[0]]
last_ai = arr_x_method2[extr_point_arr[1]]
arr_x_method2 = []
arr_x_method2.clear()
for step in arr_i:
arr_x_method2.append(fill_new_arr(first_ai, last_ai, step))
arr_of_result_func.clear()
arr_of_result_func = list(map(curent_func, arr_x_method2))
return final_arr_seq_searc
print(sequential_search_func(arr_of_result_f1,f1))
print(sequential_search_func(arr_of_result_f2,f2))
print(sequential_search_func(arr_of_result_f3,f3))
graf1 = sequential_search_func(arr_of_result_f1,f1)
arr_iter = [i for i in range(1,26)]
while True:
shed_numb = input('Enter number of schedule of function: ')
if not variant.isdigit() or int(shed_numb) < 1 or int(shed_numb) > 3:
print("Enter correct schedule of function")
continue
if shed_numb or shed_numb == "exit":
break
pict_f4 = pylab.figure(4)
#pylab.yticks(range(-10,125,1))
pylab.xticks(range(1,26,1))
pylab.plot(arr_iter, graf1)
if shed_numb == 1:
pylab.plot(arr_iter, sequential_search_func(arr_of_result_f1, f1))
elif shed_numb == 2:
pylab.plot(arr_iter, sequential_search_func(arr_of_result_f2, f2))
elif shed_numb == 3:
pylab.plot(arr_iter, sequential_search_func(arr_of_result_f3, f3))
pict_f4.show()
input()