Skip to content

Commit

Permalink
Allow optimizing per-move energy changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared Deckard committed Aug 2, 2019
1 parent 293bc81 commit 44f65cf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ them into the constructor like so

The last line (calling init on the super class) is critical.

## Optimizations

For some problems the `energy` function is prohibitively expensive to calculate
after every move. It is often possible to compute the change in energy that a
move causes much more efficiently. A delta value can be returned from `move` to
update the energy value without calling `energy` multiple times.

## Implementation Details

The simulated annealing algorithm requires that we track state (current, previous, best) and thus means we need to copy the `self.state` frequently.
Expand Down
9 changes: 6 additions & 3 deletions simanneal/anneal.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,12 @@ def anneal(self):
while step < self.steps and not self.user_exit:
step += 1
T = self.Tmax * math.exp(Tfactor * step / self.steps)
self.move()
E = self.energy()
dE = E - prevEnergy
dE = self.move()
if dE is None:
E = self.energy()
dE = E - prevEnergy
else:
E += dE
trials += 1
if dE > 0.0 and math.exp(-dE / T) < random.random():
# Restore previous state
Expand Down

0 comments on commit 44f65cf

Please sign in to comment.