-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
138 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from library.ParticleSwarm import ParticleSwarm | ||
|
||
|
||
class Algorithm(ParticleSwarm): | ||
""" | ||
Tries to get a randomly-generated list to match [.1, .2, .3, .2, .1] | ||
""" | ||
def _objective(self, member): | ||
return sum(abs(member[i] - [.1, .2, .3, .2, .1][i]) if (member[i] > 0 or member[i] < 0) else 1 for i in range(5)) | ||
|
||
|
||
def test_algorithm(): | ||
algorithm = Algorithm(50, 5, [0.,0.,0.,0.,0.], [1.,1.,1.,1.,1.], 1., 2., 2., 500, min_objective=None) | ||
algorithm.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from random import uniform | ||
from library.SimulatedAnnealing import SimulatedAnnealing | ||
from numpy import array | ||
|
||
|
||
class Algorithm(SimulatedAnnealing): | ||
""" | ||
Tries to get a randomly-generated list to match [.1, .2, .3, .2, .1] | ||
""" | ||
def _neighbor(self): | ||
return list(array(self.current_state) + array([uniform(-.02, .02) for _ in range(5)])) | ||
|
||
def _energy(self, member): | ||
return sum(abs(member[i] - [.1, .2, .3, .2, .1][i]) if (member[i] > 0 or member[i] < 0) else 1 for i in range(5)) | ||
|
||
|
||
def test_algorithm(): | ||
algorithm = Algorithm(list([uniform(0, 1) for _ in range(5)]), 5, .99, 5000) | ||
algorithm.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from random import uniform | ||
from library.StochasticHillClimb import StochasticHillClimb | ||
from numpy import array | ||
|
||
|
||
class Algorithm(StochasticHillClimb): | ||
""" | ||
Tries to get a randomly-generated list to match [.1, .2, .3, .2, .1] | ||
""" | ||
def _neighbor(self): | ||
return list(array(self.current_state) + array([uniform(-.02, .02) for _ in range(5)])) | ||
|
||
def _objective(self, state): | ||
return 1. / sum(abs(state[i] - [.1, .2, .3, .2, .1][i]) if (state[i] > 0 or state[i] < 0) else 1 for i in range(5)) | ||
|
||
|
||
def test_algorithm(): | ||
algorithm = Algorithm(list([uniform(0, 1) for _ in range(5)]), .01, 1000) | ||
algorithm.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from random import choice, randint, random | ||
from string import lowercase | ||
from library.TabuSearch import TabuSearch | ||
from copy import deepcopy | ||
|
||
|
||
class Algorithm(TabuSearch): | ||
""" | ||
Tries to get a randomly-generated string to match string "clout" | ||
""" | ||
def _neighborhood(self): | ||
member = list(self.current) | ||
neighborhood = [] | ||
for _ in range(10): | ||
neighbor = deepcopy(member) | ||
neighbor[randint(0,4)] = choice(lowercase) | ||
neighbor = ''.join(neighbor) | ||
neighborhood.append(neighbor) | ||
return neighborhood | ||
|
||
def _score(self, state): | ||
return float(sum(state[i] == "clout"[i] for i in range(5))) | ||
|
||
|
||
def test_algorithm(): | ||
algorithm = Algorithm('abcde', 50, 500, max_score=None) | ||
algorithm.run() |