Skip to content

Commit

Permalink
Merge pull request #8 from cclauss/patch-2
Browse files Browse the repository at this point in the history
Run flake8 in warning only mode on Python 2 and 3
  • Loading branch information
100 authored Sep 1, 2017
2 parents 196f553 + 1e994cf commit f38ca49
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 53 deletions.
13 changes: 11 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
sudo: false
dist: trusty
language: python
python:
- "2.7.13"
- "3.6.1"
- "nightly" # currently points to 3.7-dev
before_install:
- pip install flake8
# exit-zero treates all errors as warnings. The GitHub editor is 127 chars wide
- flake8 . --count --exit-zero --max-line-length=127 --statistics
install:
- pip install numpy
- pip install .
script: py.test -s
script: py.test -s
14 changes: 7 additions & 7 deletions Solid/EvolutionaryAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ def __init__(self, crossover_rate, mutation_rate, max_steps, max_fitness=None):
:param max_fitness: fitness value to stop algorithm once reached
"""
if isinstance(crossover_rate, float):
if crossover_rate >= 0 and crossover_rate <= 1:
if 0 <= crossover_rate <= 1:
self.crossover_rate = crossover_rate
else:
raise ValueError('Crossover rate must be a float between 0 and 1')
else:
raise ValueError('Crossover rate must be a float between 0 and 1')

if isinstance(mutation_rate, float):
if mutation_rate >= 0 and mutation_rate <= 1:
if 0 <= mutation_rate <= 1:
self.mutation_rate = mutation_rate
else:
raise ValueError('Mutation rate must be a float between 0 and 1')
Expand Down Expand Up @@ -105,7 +105,7 @@ def _populate_fitness(self):
:return: None
"""
self.fitnesses = list([self._fitness(x) for x in self.population])
self.fitnesses = [self._fitness(x) for x in self.population]

def _most_fit(self):
"""
Expand Down Expand Up @@ -183,8 +183,8 @@ def run(self, verbose=True):
for i in range(self.max_steps):
self.cur_steps += 1

if ((i + 1) % 100 == 0) and verbose:
print self
if verbose and ((i + 1) % 100 == 0):
print(self)

self.population = self._select_n(num_copy)
self._populate_fitness()
Expand All @@ -202,7 +202,7 @@ def run(self, verbose=True):
self.best_member = deepcopy(best_member)

if self.max_fitness is not None and self.best_fitness >= self.max_fitness:
print "TERMINATING - REACHED MAXIMUM FITNESS"
print("TERMINATING - REACHED MAXIMUM FITNESS")
return self.best_member, self.best_fitness
print "TERMINATING - REACHED MAXIMUM STEPS"
print("TERMINATING - REACHED MAXIMUM STEPS")
return self.best_member, self.best_fitness
12 changes: 6 additions & 6 deletions Solid/GeneticAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ def __init__(self, crossover_rate, mutation_rate, max_steps, max_fitness=None):
:param max_fitness: fitness value to stop algorithm once reached
"""
if isinstance(crossover_rate, float):
if crossover_rate >= 0 and crossover_rate <= 1:
if 0 <= crossover_rate <= 1:
self.crossover_rate = crossover_rate
else:
raise ValueError('Crossover rate must be a float between 0 and 1')
else:
raise ValueError('Crossover rate must be a float between 0 and 1')

if isinstance(mutation_rate, float):
if mutation_rate >= 0 and mutation_rate <= 1:
if 0 <= mutation_rate <= 1:
self.mutation_rate = mutation_rate
else:
raise ValueError('Mutation rate must be a float between 0 and 1')
Expand Down Expand Up @@ -186,8 +186,8 @@ def run(self, verbose=True):
for i in range(self.max_steps):
self.cur_steps += 1

if ((i + 1) % 100 == 0) and verbose:
print self
if verbose and ((i + 1) % 100 == 0):
print(self)

self.population = self._select_n(num_copy)
self._populate_fitness()
Expand All @@ -205,7 +205,7 @@ def run(self, verbose=True):
self.best_member = deepcopy(best_member)

if self.max_fitness is not None and self.best_fitness >= self.max_fitness:
print "TERMINATING - REACHED MAXIMUM FITNESS"
print("TERMINATING - REACHED MAXIMUM FITNESS")
return self.best_member, self.best_fitness
print "TERMINATING - REACHED MAXIMUM STEPS"
print("TERMINATING - REACHED MAXIMUM STEPS")
return self.best_member, self.best_fitness
12 changes: 6 additions & 6 deletions Solid/HarmonySearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _score_all(self):
:return: None
"""
self.scores = list([self._score(x) for x in self.memory])
self.scores = [self._score(x) for x in self.memory]

def _worst_score(self):
"""
Expand Down Expand Up @@ -139,8 +139,8 @@ def run(self, verbose=True):
for i in range(self.max_steps):
self.cur_steps += 1

if ((i + 1) % 100 == 0) and verbose:
print self
if verbose and ((i + 1) % 100 == 0):
print(self)

self._score_all()

Expand All @@ -161,7 +161,7 @@ def run(self, verbose=True):
self.best = self.memory[self._best_score()]

if self.max_score is not None and self._score(self.best) > self.max_score:
print "TERMINATING - REACHED MAXIMUM SCORE"
print("TERMINATING - REACHED MAXIMUM SCORE")
return self.best, self._score(self.best)
print "TERMINATING - REACHED MAXIMUM STEPS"
return self.best, self._score(self.best)
print("TERMINATING - REACHED MAXIMUM STEPS")
return self.best, self._score(self.best)
18 changes: 10 additions & 8 deletions Solid/ParticleSwarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def __init__(self, swarm_size, member_size, lower_bound, upper_bound, c1, c2, c3

self.pos = uniform(self.lower_bound, self.upper_bound, size=(swarm_size, member_size))

self.vel = uniform(self.lower_bound - self.upper_bound, self.upper_bound - self.lower_bound, size=(swarm_size, member_size))
self.vel = uniform(self.lower_bound - self.upper_bound, self.upper_bound - self.lower_bound,
size=(swarm_size, member_size))

self.best = copy(self.pos)

Expand Down Expand Up @@ -104,7 +105,8 @@ def _clear(self):
:return: None
"""
self.pos = uniform(self.lower_bound, self.upper_bound, size=(self.swarm_size, self.member_size))
self.vel = uniform(self.lower_bound - self.upper_bound, self.upper_bound - self.lower_bound, size=(self.swarm_size, self.member_size))
self.vel = uniform(self.lower_bound - self.upper_bound, self.upper_bound - self.lower_bound,
size=(self.swarm_size, self.member_size))
self.scores = self._score(self.pos)
self.best = copy(self.pos)
self.cur_steps = 0
Expand Down Expand Up @@ -155,7 +157,7 @@ def _global_best(self):
:return: None
"""
if self.global_best is None or min(self.scores) < self._objective(self.global_best[0]):
self.global_best = array([self.pos[argmin(self.scores)],] * self.swarm_size)
self.global_best = array([self.pos[argmin(self.scores)]] * self.swarm_size)

def run(self, verbose=True):
"""
Expand All @@ -168,8 +170,8 @@ def run(self, verbose=True):
for i in range(self.max_steps):
self.cur_steps += 1

if ((i + 1) % 100 == 0) and verbose:
print self
if verbose and ((i + 1) % 100 == 0):
print(self)

u1 = zeros((self.swarm_size, self.swarm_size))
u1[diag_indices_from(u1)] = [random() for x in range(self.swarm_size)]
Expand All @@ -187,8 +189,8 @@ def run(self, verbose=True):
self.scores = self._score(self.pos)
self._global_best()

if self._objective(self.global_best[0]) < self.min_objective:
print "TERMINATING - REACHED MINIMUM OBJECTIVE"
if self._objective(self.global_best[0]) < (self.min_objective or 0):
print("TERMINATING - REACHED MINIMUM OBJECTIVE")
return self.global_best[0], self._objective(self.global_best[0])
print "TERMINATING - REACHED MAXIMUM STEPS"
print("TERMINATING - REACHED MAXIMUM STEPS")
return self.global_best[0], self._objective(self.global_best[0])
12 changes: 6 additions & 6 deletions Solid/SimulatedAnnealing.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ def run(self, verbose=True):
for i in range(self.max_steps):
self.cur_steps += 1

if ((i + 1) % 100 == 0) and verbose:
print self
if verbose and ((i + 1) % 100 == 0):
print(self)

neighbor = self._neighbor()

Expand All @@ -157,12 +157,12 @@ def run(self, verbose=True):
self.best_state = deepcopy(self.current_state)

if self.min_energy is not None and self.current_energy < self.min_energy:
print "TERMINATING - REACHED MINIMUM ENERGY"
print("TERMINATING - REACHED MINIMUM ENERGY")
return self.best_state, self.best_energy

self.adjust_temp()
if self.current_temp < 0.000001:
print "TERMINATING - REACHED TEMPERATURE OF 0"
print("TERMINATING - REACHED TEMPERATURE OF 0")
return self.best_state, self.best_energy
print "TERMINATING - REACHED MAXIMUM STEPS"
return self.best_state, self.best_energy
print("TERMINATING - REACHED MAXIMUM STEPS")
return self.best_state, self.best_energy
14 changes: 7 additions & 7 deletions Solid/StochasticHillClimb.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class StochasticHillClimb:

def __init__(self, initial_state, temp, max_steps, max_objective=None):
"""
:param initial_state: initial state of hill climbing
:param max_steps: maximum steps to run hill climbing for
:param temp: temperature in probabilistic acceptance of transition
Expand Down Expand Up @@ -114,19 +114,19 @@ def run(self, verbose=True):
self.cur_steps += 1

if ((i + 1) % 100 == 0) and verbose:
print self
print(self)

neighbor = self._neighbor()

if self._accept_neighbor(neighbor):
self.current_state = neighbor

if self._objective(self.current_state) > self.best_objective:
if self._objective(self.current_state) > (self.best_objective or 0):
self.best_objective = self._objective(self.current_state)
self.best_state = deepcopy(self.current_state)

if self.max_objective is not None and self.best_objective > self.max_objective:
print "TERMINATING - REACHED MAXIMUM OBJECTIVE"
if self.max_objective is not None and (self.best_objective or 0) > self.max_objective:
print("TERMINATING - REACHED MAXIMUM OBJECTIVE")
return self.best_state, self.best_objective
print "TERMINATING - REACHED MAXIMUM STEPS"
return self.best_state, self.best_objective
print("TERMINATING - REACHED MAXIMUM STEPS")
return self.best_state, self.best_objective
12 changes: 6 additions & 6 deletions Solid/TabuSearch.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from abc import ABCMeta, abstractmethod
from copy import deepcopy
from random import randint, random, shuffle
from collections import deque
from numpy import argmax


class TabuSearch:
"""
Conducts tabu search
Expand Down Expand Up @@ -109,14 +109,14 @@ def run(self, verbose=True):
self.cur_steps += 1

if ((i + 1) % 100 == 0) and verbose:
print self
print(self)

neighborhood = self._neighborhood()
neighborhood_best = self._best(neighborhood)

while True:
if all([x in self.tabu_list for x in neighborhood]):
print "TERMINATING - NO SUITABLE NEIGHBORS"
print("TERMINATING - NO SUITABLE NEIGHBORS")
return self.best, self._score(self.best)
if neighborhood_best in self.tabu_list:
if self._score(neighborhood_best) > self._score(self.best):
Expand All @@ -134,7 +134,7 @@ def run(self, verbose=True):
break

if self.max_score is not None and self._score(self.best) > self.max_score:
print "TERMINATING - REACHED MAXIMUM SCORE"
print("TERMINATING - REACHED MAXIMUM SCORE")
return self.best, self._score(self.best)
print "TERMINATING - REACHED MAXIMUM STEPS"
return self.best, self._score(self.best)
print("TERMINATING - REACHED MAXIMUM STEPS")
return self.best, self._score(self.best)
6 changes: 3 additions & 3 deletions tests/test_evolutionary_algorithm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from random import choice, randint, random
from string import lowercase
from string import ascii_lowercase
from Solid.EvolutionaryAlgorithm import EvolutionaryAlgorithm


Expand All @@ -8,7 +8,7 @@ class Algorithm(EvolutionaryAlgorithm):
Tries to get a randomly-generated string to match string "clout"
"""
def _initial_population(self):
return list(''.join([choice(lowercase) for _ in range(5)]) for _ in range(50))
return list(''.join([choice(ascii_lowercase) for _ in range(5)]) for _ in range(50))

def _fitness(self, member):
return float(sum(member[i] == "clout"[i] for i in range(5)))
Expand All @@ -20,7 +20,7 @@ def _crossover(self, parent1, parent2):
def _mutate(self, member):
if self.mutation_rate >= random():
member = list(member)
member[randint(0,4)] = choice(lowercase)
member[randint(0, 4)] = choice(ascii_lowercase)
member = ''.join(member)
return member

Expand Down
4 changes: 2 additions & 2 deletions tests/test_tabu_search.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from random import choice, randint, random
from string import lowercase
from string import ascii_lowercase
from Solid.TabuSearch import TabuSearch
from copy import deepcopy

Expand All @@ -13,7 +13,7 @@ def _neighborhood(self):
neighborhood = []
for _ in range(10):
neighbor = deepcopy(member)
neighbor[randint(0,4)] = choice(lowercase)
neighbor[randint(0, 4)] = choice(ascii_lowercase)
neighbor = ''.join(neighbor)
neighborhood.append(neighbor)
return neighborhood
Expand Down

0 comments on commit f38ca49

Please sign in to comment.