Basic Algorithms and Techniques
Basic Algorithms and Techniques
Suppose we want to make a perfect crystal • Physical annealing: get the material very hot
•Perfect = all atoms are lined up on crystal lattice sites; – Give the atoms energy to move around
1
Metropolis criterion
Annealing - basics
• The if-then in the algorithm is “the Metropolis
• Metropolis algorithm:
criterion”
– after perturbing an atom and computing Δenergy, it
Start with the system in a known configuration, tells if we keep this new configuration or not
at known energy E; – if Δenergy <0, it is a better state, keep it
Perturb the system slightly (eg.
(eg move an atom – If Δenergy > 00, it is a worse state
state, maybe keep it
it,
to a new location) maybe not – depends on temperature
Temp. T, ΔE >0 Compute e−ΔE/KT Generate r,
If (ΔE <0) = a number in [0,1] a random number in [0,1]
then do something
−ΔE/KT
else do something
Probability of accepting
this perturbation
Compare r and e
if (r is smaller)
keep this perturbation
else reject it
Simulated annealing
Simulated annealing
• Metropolis algorithm iteratively visits configurations Start with the system in a known configuration, at known energy E
with “reasonably” probable energies at the given fixed T= temperature =hot; frozen = false;
temperature While (!frozen) {
repeat{
• Simulated annealing adds an outer loop that starts Perturb system slightly (move a particle)
with a high temperature and slowly cools it Compute ΔE, change in energy due to perturbation
• Do enough perturbations at each temperature in the If (ΔE < 0)
then accept this perturbation, this is the new system configuration
sequence of cooling steps to get to thermal equilibrium else accept maybe, with probability e -ΔE/T
(ie, do the Metropolis procedure) } until (the system is in thermal equilibrium at this T)
• Do enough temperatures so that the problem actually If (E still decreasing over the last few temperatures)
then T=0.9T /* cool the temperature, do more perturbations*/
freezes into a low energy state, and further cooling else frozen = true
does not further lower the energy }
return (final configuration as low energy solution)
2
Why does annealing work? Greedy iterative improvement
• Balls and hills • Only take moves that improve the cost
– Simple representation of a combinatorial task • Physical analogy: like a quench, cool too fast, or you get poor
– Can model as a cost surface (landscape) crystal
– The configuration we are visiting now is the ball on the hill • Can get easily trapped in local minima
yes
Cost Cost
never
All possible configurations of system that is being optimized All possible configurations of system that is being optimized
(example for only one variable)
ECE 256A 13 ECE 256A 14
Definitely Maybe
Probably Probably
Maybe No way NOT
Cost Cost
All possible configurations of system that is being optimized All possible configurations of system that is being optimized
(example for only one variable) (example for only one variable)
ECE 256A 15 ECE 256A 16
Configurations Configurations
All possible configurations of system that is being optimized • As a function of temperature, how much of this cost surface is
reachable, if we start from where the ball is?
Uphill Probability we will accept this move • T hides obstacles when hot; adaptively smoothes or flattens
ΔC Hot T=1000 Warm T=100 Cold T=1 these obstacles, so we ignore them at the start
1 0.999 0.99 0.37 • Cooling restricts us to smaller good areas, obstacles reappear.
100 0.900 0.37 ~0
1000 0.37 ~0 ~0
ECE 256A 17 ECE 256A 18
3
Simulated annealing : a special case of a wider class
Landscape flattening of adaptive heuristics for combinational
optimization.
• T=Hot T=Warm
Entire cost surface reachable,
Adaptive : some parameters of the heuristic can be
Cost no hills or obstacles. Cost
Unreachable here, this hill too high at
this lower temperature. modified.
Modification of parameters : by the algorithm itself
using some learning mechanism or by the user.
O i i i problem
Optimization bl
Configurations Configurations
Minimize h ( )
• T=Cool T=Cold
Unreachable here, this hill too high Cost Unreachable here, this hill too high subject to constraints c ( )
Cost at this very cold temperature.
at this yet lower temperature. Solutions which satisfy c ( ) : feasible solutions ;
A feasible solution which minimizes h ( ) →
optimal solution.
Configurations Configurations
ECE 256A 19 ECE 256A 20
4
Simulated Annealing procedure Simulated Annealing;
1. Acceptance : S := S0 ; /*initial Solution*/
if (h (New S) <h (S)) or (random < e(h(S)-h(New S))/T) T := T0 ; /*initial temperature*/
then accept : = true;
iterations := i0; /*initial # of iterations, ≥ 1 */
else accept : = false;
T is a heuristic parameter called “temperature” repeat
and “random” is a uniformly generated pseudorandom repeat
number in [0,1]
[0 1] N S := perturb
NewS t b (S) ;
2. “Time to adapt parameters” : number of iterations of the if (h(NewS) <h(S))or (random < e(h(S)-h(New S))/T)
inner repeat loop that have been performed since the last
adaptation. then S := NewS;
3. Adapt Parameters : T is updated to α T, α ∈ (0,1); until inner loop has been repeated iteration times;
#iterations of the inner loop is changed to β * #iterations, T := α * T; iteration := β * iterations;
β > 1. until “out of time” ;
4. Terminating criterion : computer time. end /* of Simulated Annealing */
ECE 256A 25 ECE 256A 26
5
procedure Sequence Heuristic
S := S0 ; /*initial solution*/ Experimental studies have shown that:
L := L0 ; /* initial sequence length*/
* A well thought-out heuristic tailored to the
length := 0 ;/* current length of bad specific problem performs better than
perturbation sequence*/
adaptive heuristic.
repeat
repeat * Adaptive heuristics perform better when they
NewS := perturb(S) ;
start from a “good” S0.
if h(NewS)<h(S) then [S := NewS; length :=0;] * Performance of an adaptive heuristic is
else [length := length + 1] ; affected by the perturbation function that is
until length > L ; used.
Update Length ; * Sequence heuristic performs better than
Update S ; simulated annealing when time is limited.
until “termination criteria” ;
end ; /* of the Sequence */
ECE 256A 31 ECE 256A 32
First approach:
Plane Sweep
For all columns (in any order) count number of
We start with a very simple problem and a simple crossing nets if netNumber > max then max :=
minded solution which is improved step-by-step: netNumber; end
Given a routing channel with terminals at 2 borders: n terminals, N columns => O(n • N)
1 2 1 We spend O(n) time even for empty column
2 2
Observation
Second refinement: Density can be calculated incrementally from
Jumping from non-empty to non-empty column: changes at terminal positions:
Sort terminals according to x;
Cur_terminal := firstTerminal; Nets starting at cur_x : increase
Repeat: density
cur_x = cur_terminal ↑ . x;
process nets crossing cur_x; ending at cur_x : decrease
while cur_terminal ↑ . x = cur_x do density
cur_terminal := succ (cur_terminal)
until terminals exhausted; else : do not change
density
ECE 256A 35 ECE 256A 36
6
Third refinement:
Repeat
cur_x := cur_terminal ↑ . x;
Achievement:
while cur_terminal ↑ . x = cur_x do
begin
case terminal_type of 1) Speed O(n • N) → O(n log n)
first : count := count + 1;
note maximum ; 2) Generalization of the algorithm:
last : count := count - 1; terminals are not required to be an grid.
other : count unchanged ;
end;
cur_terminal := succ (cur_terminal)
until terminals exhausted;
ECE 256A 37 ECE 256A 38
Some applications:
Constraint graph generation
* Constraint graph generation for compaction;
* Rectangle intersection;
* Line segment intersection;
* Fracturing of polygons;
* Boolean mask operations;
*VVarious
i subtasks
bt k iin channel
h l routing:
ti
(definition of zones, jog insertion);
* Generation of slice structures;
* Channel definition;
* off-line generation of corner-stitching data
structure.
ECE 256A 41 ECE 256A 42
7
Set of generated edges should be small. Generating
an edge for each pair of horizontally overlapping
Input: set of horizontal segments (representing pair of segments would yield an O(n2) algorithm.
rectangle center lines) in the plane. Many constraints would be redundant.
redundant
8
Complexity of this algorithm is the same :
O(n log n)
Calculation of longest paths takes O(m) time for
m edges.
ECE 256A 49