refactor as a package
This commit is contained in:
parent
dcf9b798dc
commit
650d93585b
12 changed files with 477 additions and 520 deletions
40
sho/algo.py
Normal file
40
sho/algo.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
|
||||
########################################################################
|
||||
# Algorithms
|
||||
########################################################################
|
||||
|
||||
def random(func, init, again):
|
||||
"""Iterative random search template."""
|
||||
best_sol = init()
|
||||
best_val = func(best_sol)
|
||||
val,sol = best_val,best_sol
|
||||
i = 0
|
||||
while again(i, val, sol):
|
||||
sol = init()
|
||||
val = func(sol)
|
||||
if val > best_val:
|
||||
best_val = val
|
||||
best_sol = sol
|
||||
i += 1
|
||||
return best_val, best_sol
|
||||
|
||||
|
||||
def greedy(func, init, neighb, again):
|
||||
"""Iterative randomized greedy heuristic template."""
|
||||
best_sol = init()
|
||||
best_val = func(best_sol)
|
||||
val,sol = best_val,best_sol
|
||||
i = 1
|
||||
while again(i, best_val, best_sol):
|
||||
sol = neighb(best_sol)
|
||||
val = func(sol)
|
||||
if val > best_val:
|
||||
best_val = val
|
||||
best_sol = sol
|
||||
i += 1
|
||||
return best_val, best_sol
|
||||
|
||||
# TODO add a simulated annealing solver.
|
||||
# TODO add a population-based stochastic heuristic template.
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue