-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
65 lines (55 loc) · 1.87 KB
/
Copy pathexamples.py
File metadata and controls
65 lines (55 loc) · 1.87 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
from algorithm import PulseExpansionAlgorithm
# Test Example 1: Minimizing a quadratic function (1D)
def objective_function_1d(x):
"""
A simple quadratic function for testing the optimization algorithm.
The minimum is at x = 3.
"""
return (x - 3)**2
# Define search space (1D)
search_space_1d = [-10, 10]
# Instantiate the algorithm for 1D
pea_1d = PulseExpansionAlgorithm(
obj_function=objective_function_1d,
search_space=search_space_1d,
num_pulses=5,
max_iterations=100
)
# Run the algorithm for 1D
best_position_1d, best_fitness_1d = pea_1d.run()
print("--- Pulse Expansion Algorithm Example (1D Quadratic) ---")
print(f"Search Space: {search_space_1d}")
print(f"Objective Function: (x - 3)^2")
print("-" * 50)
print(f"Found Best Position: {best_position_1d}")
print(f"Found Best Fitness: {best_fitness_1d}")
print("-" * 50)
# Test Example 2: Minimizing a 2D Rosenbrock function
def rosenbrock_function_2d(x):
"""
The Rosenbrock function is a non-convex function used as a performance test problem
for optimization algorithms. The global minimum is at (1, 1) with a value of 0.
"""
return (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2
# Define search space (2D)
search_space_2d = [[-2, 2], [-2, 2]]
# Instantiate the algorithm for 2D
pea_2d = PulseExpansionAlgorithm(
obj_function=rosenbrock_function_2d,
search_space=search_space_2d,
num_pulses=10,
max_iterations=500,
decay_factor=0.98,
pulse_overlap_threshold=0.1,
reset_threshold=30
)
# Run the algorithm for 2D
best_position_2d, best_fitness_2d = pea_2d.run()
print("\n--- Pulse Expansion Algorithm Example (2D Rosenbrock) ---")
print(f"Search Space: {search_space_2d}")
print(f"Objective Function: Rosenbrock (2D)")
print("-" * 50)
print(f"Found Best Position: {best_position_2d}")
print(f"Found Best Fitness: {best_fitness_2d}")
print("-" * 50)