-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab_2.py
More file actions
46 lines (34 loc) · 1.49 KB
/
Lab_2.py
File metadata and controls
46 lines (34 loc) · 1.49 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
42
43
44
45
46
# All imports for task
import numpy as np
# Define constants
N = 50
max_steps = 1000
def generate_vectors(N):
first_two_coordinates = np.random.exponential(scale=1, size=(N, 2))
third_coordinate = np.random.normal(size=N)
vectors = np.column_stack((first_two_coordinates, third_coordinate))
return vectors
def kozinec_algorithm(vectors, steps):
center_of_mass = np.mean(vectors, axis=0) # Find the center of mass of the set of points
initial_vector = vectors[0] # Choose an initial vector
direction = initial_vector - center_of_mass # Direction of search
direction /= np.linalg.norm(direction) # Normalize the direction
# Search for the separating vector
for step in range(steps):
projections = np.dot(vectors, direction) # Compute projections of vectors onto the direction
max_index = np.argmax(projections)
max_projection = projections[max_index]
extreme_point = vectors[max_index] # Find a point on the boundary of the convex hull
# Check if this is a separating vector
if max_projection > 0:
return direction
# Update the direction for the next step
direction = extreme_point - center_of_mass
direction /= np.linalg.norm(direction)
return direction
generated_vectors = generate_vectors(N)
separating_vector = kozinec_algorithm(generated_vectors, max_steps)
print('Generated vectors:')
print(generated_vectors)
print()
print('Separating vector: {}'.format(separating_vector))