Skip to content

Commit

Permalink
fix: Major bug fixes
Browse files Browse the repository at this point in the history
- Fixed RandomSearch no start_time problem.
- Fixed BUG where GA/GAReversals would not register correct number of reversals.
- Added an example for GAReversals.
- Removed print line from test_algorithms not required
  • Loading branch information
Agrover112 committed Oct 25, 2021
1 parent c8ed9c4 commit 1da4969
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
8 changes: 6 additions & 2 deletions examples/example1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from fliscopt.utils.util import print_schedule, read_file,plot_scores
from fliscopt.rs import RandomSearch
from fliscopt.ga import GA, ReverseGA
from fliscopt.ga import GA, ReverseGA, GAReversals
from fliscopt.hc import HillClimb
from fliscopt.chaining import IteratedChaining
from fliscopt.multiproc import multiple_runs
Expand Down Expand Up @@ -33,16 +33,20 @@
Iterated Chaining and GAReversals and ReverseGA are algos I haven;t really found being
implemented. So I implemented them.
"""


"""

read_file('flights.txt')
ic=IteratedChaining(rounds=5, n_obs=2, tol=90)
soln, cost, scores, nfe=ic.run('RandomSearch', 'HillClimb')
print_schedule(soln,'FCO')
#multiple_runs(ReverseGA, domain, fitness_function, record=False, n=2)

read_file('flights.txt')
sga=GAReversals(seed_init=False,search=False,n_k=250,number_generations=1000)
soln, cost, scores, nfe, seed = sga.run(domain=domain['domain'], fitness_function=fitness_function,seed=5)
plot_scores(scores, sga.get_base(),fname='flight_scheduling', save_fig=False)

hc=HillClimb(seed_init=False,max_time=0.0000001)
# All domains defined with a single-tuple/or without a multiplier have n-dimensional Input Domain
Expand Down
19 changes: 15 additions & 4 deletions fliscopt/ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import time
sys.path.append(os.getcwd())

from .utils.util import plot_scores, print_schedule, read_file
from .base_algorithm import FlightAlgorithm,random
from .rs import RandomSearch
Expand Down Expand Up @@ -105,7 +106,9 @@ def __init__(self, domain=domain['domain'], fitness_function=fitness_function, s
0, elitism, number_generations, search)

def run(self,domain,fitness_function,seed) -> tuple:
self.__init__(domain, fitness_function, seed, self.seed_init, self.init,self.max_time)
#self.__init__(domain, fitness_function, seed, self.seed_init, self.init,self.max_time)
super().__init__(domain, fitness_function, seed, self.seed_init, self.init,self.max_time, self.population_size, self.step, self.probability_mutation,
0, self.elitism,self.number_generations, self.search)
population = []
scores = []
nfe = 0
Expand Down Expand Up @@ -189,7 +192,10 @@ def __init__(self, domain=domain['domain'], fitness_function=fitness_function, s
probability_crossover, elitism, number_generations, search)

def run(self,domain,fitness_function,seed) -> tuple:
self.__init__(domain, fitness_function, seed, self.seed_init, self.init,self.max_time)
#self.__init__(domain, fitness_function, seed, self.seed_init, self.init,self.max_time)
super().__init__(domain, fitness_function, seed, self.seed_init, self.init,self.max_time, self.population_size, self.step, 0.0,
self.probability_crossover,self. elitism,self.number_generations, self.search)

population = []
scores = []
nfe = 0
Expand Down Expand Up @@ -278,10 +284,13 @@ def __init__(self, domain=domain['domain'], fitness_function=fitness_function, s
0.0, elitism, number_generations, search)
self.n_k = n_k
self.step_length = step_length
print(self.n_k,self.step_length,self.number_generations,self.max_time)


def run(self,domain,fitness_function,seed) -> tuple:
self.__init__(domain, fitness_function, seed, self.seed_init, self.init,self.max_time)
#self.__init__(domain, fitness_function, seed, self.seed_init, self.init,self.max_time)
super().__init__(domain, fitness_function, seed, self.seed_init, self.init,self.max_time, self.population_size, self.step, self.probability_mutation,
0.0, self.elitism, self.number_generations, self.search)
population = []
scores = []
nfe = 0
Expand Down Expand Up @@ -309,7 +318,7 @@ def run(self,domain,fitness_function,seed) -> tuple:
else:
costs = [(self.fitness_function(individual, 'FCO'), individual)
for individual in population]
nfe += 1
nfe += 1
if i % self.n_k == 0 and i != 0:
if self.step_length == 1:
costs.sort(reverse=True)
Expand Down Expand Up @@ -358,6 +367,7 @@ def run(self,domain,fitness_function,seed) -> tuple:
mutation(self.domain, self.step, ordered_individuals[m]))

if time.time()-self.start_time>self.max_time:
print("reached")
return costs[0][1], costs[0][0], scores, nfe, self.seed

return costs[0][1], costs[0][0], scores, nfe, self.seed
Expand All @@ -371,3 +381,4 @@ def run(self,domain,fitness_function,seed) -> tuple:
seed=5)
plot_scores(scores, sga.get_base(),fname='flight_scheduling', save_fig=False)
print_schedule(soln, 'FCO')

2 changes: 1 addition & 1 deletion fliscopt/rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def run(self,domain,fitness_function,seed):
solution = [self.r_init.randint(self.domain[i][0], self.domain[i][1])
for i in range(len(self.domain))]


self.start_time = time.time()
for i in range(self.epochs):
if i != 0:
solution = [random.randint(self.domain[i][0], self.domain[i][1])
Expand Down
1 change: 0 additions & 1 deletion tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class TestAlgorithms(unittest.TestCase):
def test_rs(self):
rs = RandomSearch(max_time=0.00001)
res=rs.run(domain=domain['griewank']*5,fitness_function=griewank,seed=5)
print(type(res[4]),type(res[3]),type(res[2]))
self.assertIsNotNone(res[0],msg="Best sol returned None, expected output of type List")
self.assertEqual(len(res[0]),5,msg="Best sol output length not matching length :{} of input soln. Refer fitness_fn soln length".format(5))
self.assertIsNotNone(res[1],msg="Best cost returned None, expected output of type float/int")
Expand Down

0 comments on commit 1da4969

Please sign in to comment.