Two-dimensional placement using simulated annealing algorithm: How to minimize line length and optimize layout - Python implementation details

1 Introduction

Simulated Annealing (SA) is a probabilistic search algorithm inspired by the annealing process of solids. When a solid is heated to high temperatures, the molecules inside it become disordered. As the temperature drops, the molecules gradually settle down and form structures. The simulated annealing algorithm imitates this process and gradually reduces the size of the search space when solving the problem.

In this article, we will use a simulated annealing algorithm to place objects on a two-dimensional plane to minimize wire lengths. This problem occurs in many practical applications, such as circuit design, warehouse layout, etc.

2. Brief description of simulated annealing algorithm

The core idea of ​​the simulated annealing algorithm is to use a "temperature" parameter to control the randomness of the search process. At high temperatures, the algorithm is more likely to accept a new solution that is worse than the current solution, thereby avoiding falling into a local optimum. As the temperature decreases, this possibility gradually decreases until eventually, the algorithm will only accept new solutions that are better than the current solution.

3. Python code implementation

First, we define some basic data structures and functions, such as points, lines, calculating line length, etc.:

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def distance_to(self, other):
        return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5


def total_line_length(points):
    total = 0
    for i in range(len(points) - 1):
        total += points[i].distance_to(points[i + 1])
    return total

Next, we define the core parts of simulated annealing. The following is a simplified simulated annealing algorithm:

import random
import math

def simulated_annealing(points, initial_temperature, cooling_rate, num_iterations):
    current_solution = points[:]
    best_solution = points[:]
    current_length = total_line_length(current_solution)
    best_length = current_length
    
    temperature = initial_temperature
    
    for iteration in range(num_iterations):
        # 随机选择两个点并交换它们的位置
        i, j = random.sample(range(len(points)), 2)
        new_solution = current_solution[:]
        new_solution[i], new_solution[j] = new_solution[j], new_solution[i]
        
        new_length = total_line_length(new_solution)
        
        if new_length < best_length:
            best_solution = new_solution[:]
            best_length = new_length
        
        # 根据温度和解的质量决定是否接受新解
        if new_length < current_length or random.random() < math.exp((current_length - new_length) / temperature):
            current_solution = new_solution[:]
            current_length = new_length
        
        # 降低温度
        temperature *= cooling_rate

    return best_solution

Please download the complete project for the specific process.

4. Usage examples

To show how to use the code above, we will randomly generate some points on a 2D plane and use a simulated annealing algorithm to find a placement method that minimizes the length of the connection:

if __name__ == "__main__":
    random_points = [Point(random.randint(0, 100), random.randint(0, 100)) for _ in range(10)]
    optimized_points = simulated_annealing(random_points, 1000, 0.995, 10000)

    print("Initial line length:", total_line_length(random_points))
    print("Optimized line length:", total_line_length(optimized_points))

5. Result analysis and visualization

To better understand our results, we can use matplotlib for visualization. First, we need to install matplotlib:

pip install matplotlib

Next, we can use the following code to display the point placement before and after optimization:

import matplotlib.pyplot as plt

def plot_points(points, title):
    xs = [point.x for point in points]
    ys = [point.y for point in points]
    
    plt.figure(figsize=(10, 7))
    plt.scatter(xs, ys, c='blue')
    plt.plot(xs + [xs[0]], ys + [ys[0]], c='red')  # 连接起点和终点
    plt.title(title)
    plt.show()

if __name__ == "__main__":
    random_points = [Point(random.randint(0, 100), random.randint(0, 100)) for _ in range(10)]
    optimized_points = simulated_annealing(random_points, 1000, 0.995, 10000)

    plot_points(random_points, "Initial Placement")
    plot_points(optimized_points, "Optimized Placement using Simulated Annealing")

Through the above visualization, we can intuitively observe the optimization effect of the simulated annealing algorithm on the wire length.

6. Simulated annealing parameter adjustment and its impact

The performance of the simulated annealing algorithm is closely related to its parameter settings. Here are the three key parameters and their effects:

  • Initial Temperature : Determines the randomness at the beginning of the algorithm. Too high a temperature may cause the algorithm to be too random in the early stages, while too low a temperature may cause the algorithm to fall into a local optimum.

  • Cooling Rate : Determines the speed at which the temperature drops. Cooling too fast can cause the algorithm to under-search the entire solution space, while cooling too slow can cause the algorithm to converge too slowly.

  • Number of Iterations : Determines how long the algorithm takes to run. The number of iterations needs to be coordinated with the above two parameters to obtain the best search effect.

For best performance, it is recommended to try different parameter combinations several times to find the best settings for your particular problem.

7. Optimization and improvement

Although the simulated annealing algorithm performs well on many problems, it is still a heuristic algorithm and cannot guarantee to find the global optimal solution. Therefore, in some cases, we may need to consider other strategies or algorithm combinations, such as genetic algorithms, ant colony optimization, etc., to further improve the quality of the solution.

In addition, we can also customize and optimize the algorithm for specific problem structures and characteristics. For example, for some problems, we can design more effective neighborhood search strategies, or use structural information of the problem to guide the search.

8. Extension: Combining other algorithms with simulated annealing

In many practical applications, simulated annealing algorithms may need to be combined with other algorithms to improve efficiency and accuracy. For example, we can first use a greedy algorithm to find an initial solution to the problem, and then use simulated annealing to further optimize this solution. In addition, the stochastic search properties of simulated annealing also make it suitable for combination with deterministic search algorithms such as gradient descent.

9. Practical Applications to Two-Dimensional Placement Problems

This 2D placement problem that we solve using the simulated annealing algorithm has applications in many real-world scenarios. Among them, chip design in the electronics industry is the most typical example. In complex chip designs, how to effectively place thousands of components to minimize the length of interconnect lines is a huge challenge. By using advanced algorithms such as simulated annealing, engineers can design chips with better performance and lower power consumption.

10. Conclusion

The simulated annealing algorithm is a powerful and flexible optimization tool, especially suitable for complex problems that are difficult to handle with traditional optimization algorithms. By setting parameters appropriately and combining other strategies, we can find high-quality solutions for a wide variety of problems. Although in this article we have only explored a simple 2D placement problem, simulated annealing has applications far beyond this. Whether in industry, finance or other fields, it has a wide range of application prospects.

11. References

  1. Kirkpatrick, S., Gelatt, C. D., & Vecchi, M. P. (1983). Optimization by simulated annealing. Science, 220(4598), 671-680.
  2. Černý, V. (1985). Thermodynamical approach to the traveling salesman problem: An efficient simulation algorithm. Journal of optimization theory and applications, 45(1), 41-51.
  3. Ingber, L. (1989). Very fast simulated re-annealing. Mathematical and computer modelling, 12(8), 967-973.

Guess you like

Origin blog.csdn.net/qq_38334677/article/details/132593089