Skip to content

Commit

Permalink
fix: Fixed seed and multiproc bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Agrover112 committed Sep 30, 2021
1 parent c53c92f commit 915fa05
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 34 deletions.
2 changes: 1 addition & 1 deletion fliscopt/__init__.py
@@ -1,3 +1,3 @@
from . import ga,hc,rs,sa,multiproc,chaining,base_algorithm,fitness,utils

__version__ = "0.1.4"
__version__ = "0.1.5"
6 changes: 3 additions & 3 deletions fliscopt/base_algorithm.py
Expand Up @@ -16,11 +16,11 @@ def __init__(self, domain, fitness_function,seed=random.randint(10, 100),seed_in
self.max_time=1000
if self.seed_init:
# Set the seed for initial population only
self.r_init = random.Random(seed)
self.r_init = random.Random(self.seed)
else:
# Same seeds for both init and other random generators
self.r_init = random.Random(seed)
random.seed(seed)
self.r_init = random.Random(self.seed)
random.seed(self.seed)

self.best_cost =0.0 # returned
@abstractmethod
Expand Down
24 changes: 9 additions & 15 deletions fliscopt/ga.py
Expand Up @@ -3,7 +3,7 @@
import time
sys.path.append(os.getcwd())
from .utils.util import plot_scores, print_schedule, read_file
from .base_algorithm import FlightAlgorithm
from .base_algorithm import FlightAlgorithm,random
from .rs import RandomSearch
from .utils.ga_utils import crossover, mutation
from .fitness import *
Expand Down Expand Up @@ -41,6 +41,7 @@ def run(self,domain,fitness_function,seed) -> tuple:




class GA(BaseGA):
def __init__(self, domain=domain['domain'], fitness_function=fitness_function, seed=random.randint(10, 100), seed_init=True, init=[],max_time=100,
population_size=100, step=1, probability_mutation=0.2, elitism=0.2,
Expand All @@ -49,9 +50,7 @@ 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.domain = domain
self.fitness_function = fitness_function
self.seed = seed
self.__init__(domain, fitness_function, seed, self.seed_init, self.init,self.max_time)
population = []
scores = []
nfe = 0
Expand Down Expand Up @@ -97,7 +96,7 @@ def run(self,domain,fitness_function,seed) -> tuple:
i1 = random.randint(0, number_elitism)
i2 = random.randint(0, number_elitism)
population.append(
crossover(domain, ordered_individuals[i1], ordered_individuals[i2]))
crossover(self.domain, ordered_individuals[i1], ordered_individuals[i2]))

if time.time()-self.start_time>self.max_time:
return costs[0][1], costs[0][0], scores, nfe, self.seed
Expand All @@ -113,9 +112,7 @@ 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.domain = domain
self.fitness_function = fitness_function
self.seed = seed
self.__init__(domain, fitness_function, seed, self.seed_init, self.init,self.max_time)
population = []
scores = []
nfe = 0
Expand Down Expand Up @@ -157,7 +154,7 @@ def run(self,domain,fitness_function,seed) -> tuple:
i1 = random.randint(0, number_elitism)
i2 = random.randint(0, number_elitism)
population.append(
crossover(domain, ordered_individuals[i1], ordered_individuals[i2]))
crossover(self.domain, ordered_individuals[i1], ordered_individuals[i2]))
else:
m = random.randint(0, number_elitism)
population.append(
Expand All @@ -169,7 +166,6 @@ def run(self,domain,fitness_function,seed) -> tuple:
return costs[0][1], costs[0][0], scores, nfe, self.seed



class GAReversals(BaseGA):
def __init__(self, domain=domain['domain'], fitness_function=fitness_function, seed=random.randint(10, 100), seed_init=True, init=[],max_time=100,
population_size=100, step=1, probability_mutation=0.2, elitism=0.2,
Expand All @@ -180,9 +176,7 @@ def __init__(self, domain=domain['domain'], fitness_function=fitness_function, s
self.step_length = step_length

def run(self,domain,fitness_function,seed) -> tuple:
self.domain = domain
self.fitness_function = fitness_function
self.seed = seed
self.__init__(domain, fitness_function, seed, self.seed_init, self.init,self.max_time)
population = []
scores = []
nfe = 0
Expand Down Expand Up @@ -261,8 +255,8 @@ def run(self,domain,fitness_function,seed) -> tuple:
return costs[0][1], costs[0][0], scores, nfe, self.seed

return costs[0][1], costs[0][0], scores, nfe, self.seed


if __name__ == '__main__':
read_file('flights.txt')
sga = ReverseGA(seed_init=False,search=True)
Expand Down
9 changes: 4 additions & 5 deletions fliscopt/hc.py
Expand Up @@ -4,9 +4,9 @@
import time
from abc import ABCMeta
from .utils.util import plot_scores, print_schedule, read_file
from .base_algorithm import FlightAlgorithm
from .base_algorithm import FlightAlgorithm,random

import random
#import random
from .fitness import *

class HillClimb(FlightAlgorithm,metaclass=ABCMeta):
Expand All @@ -22,12 +22,11 @@ def get_name(self) -> str:
return self.__class__.__name__

def run(self,domain,fitness_function,seed) -> tuple:
self.__init__(domain,fitness_function,seed,self.seed_init, self.init,self.max_time)
count = 0
scores = []
nfe = 0
self.domain=domain
self.fitness_function=fitness_function
self.seed=seed

if len(self.init) > 0:
solution = self.init
else:
Expand Down
5 changes: 3 additions & 2 deletions fliscopt/multiproc.py
@@ -1,6 +1,6 @@
import os
import sys
sys.path.append(os.getcwd())
sys.path.append("..")


import multiprocessing
Expand Down Expand Up @@ -69,5 +69,6 @@ def multiple_runs(algorithm, domain, fitness_function, init=[], record=False, n_

if __name__ == '__main__':
read_file('flights.txt')
multiple_runs(ReverseGA, domain, fitness_function, record=False, n=10)
multiple_runs(ReverseGA, domain['griewank']*12, griewank, record=False, n=10)


5 changes: 2 additions & 3 deletions fliscopt/rs.py
Expand Up @@ -27,9 +27,7 @@ def get_name(self) -> str:


def run(self,domain,fitness_function,seed):
self.domain=domain
self.fitness_function=fitness_function
self.seed=seed
self.__init__(domain,fitness_function,seed,self.seed_init, self.init,self.max_time)
scores = []
nfe = 0
if len(self.init) > 0:
Expand All @@ -55,6 +53,7 @@ def run(self,domain,fitness_function,seed):
if time.time()-self.start_time>self.max_time:
return self.best_solution, self.best_cost, scores, nfe, self.seed
return self.best_solution, self.best_cost, scores, nfe, self.seed


if __name__ == '__main__':
read_file('flights.txt')
Expand Down
8 changes: 3 additions & 5 deletions fliscopt/sa.py
Expand Up @@ -4,11 +4,11 @@
sys.path.append(os.getcwd())
from abc import ABCMeta
from .utils.util import plot_scores, print_schedule, read_file
from .base_algorithm import FlightAlgorithm
from .base_algorithm import FlightAlgorithm,random
import heapq
import math

import random
#import random
from .fitness import *

class SimulatedAnnealing(FlightAlgorithm,metaclass=ABCMeta):
Expand All @@ -28,9 +28,7 @@ def get_name(self) -> str:
return self.__class__.__name__

def run(self,domain,fitness_function,seed) -> tuple:
self.domain=domain
self.fitness_function=fitness_function
self.seed=seed
self.__init__(domain,fitness_function,seed,self.seed_init, self.init,self.max_time)
count = 0
nfe = 0
scores = []
Expand Down

0 comments on commit 915fa05

Please sign in to comment.