-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulatedannealing.py
More file actions
54 lines (41 loc) · 1.71 KB
/
Copy pathsimulatedannealing.py
File metadata and controls
54 lines (41 loc) · 1.71 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
import random
import math
# Define the objective function
def objective_function(x):
return x**2
# Define the simulated annealing algorithm function
def simulated_annealing(initial_state, objective_function, max_iterations, max_temperature):
current_state = initial_state
current_energy = objective_function(current_state)
best_state = current_state
best_energy = current_energy
temperature = max_temperature
for i in range(max_iterations):
# Calculate the acceptance probability
temperature = temperature * 0.9
neighbor = current_state + random.uniform(-1, 1)
neighbor_energy = objective_function(neighbor)
delta_energy = neighbor_energy - current_energy
acceptance_probability = math.exp(-delta_energy / temperature)
# Determine whether to accept the new state
if delta_energy < 0:
current_state = neighbor
current_energy = neighbor_energy
if current_energy < best_energy:
best_state = current_state
best_energy = current_energy
elif random.uniform(0, 1) < acceptance_probability:
current_state = neighbor
current_energy = neighbor_energy
# Print the current state, energy, and temperature
print(
f"Iteration {i}: State = {current_state}, Energy = {current_energy}, Temperature = {temperature}")
return best_state, best_energy
# Test the algorithm
initial_state = 10
max_iterations = 100
max_temperature = 1000
best_state, best_energy = simulated_annealing(
initial_state, objective_function, max_iterations, max_temperature)
print(f"\nBest state found: {best_state}")
print(f"Best energy found: {best_energy}")