Skip to content

Commit caf6895

Browse files
committed
feat: Added circular shift mutation & fixed .csv write issue
1 parent 5ba345e commit caf6895

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

fliscopt/ga.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .utils.util import plot_scores, print_schedule, read_file
77
from .base_algorithm import FlightAlgorithm,random
88
from .rs import RandomSearch
9-
from .utils.ga_utils import crossover, mutation
9+
from .utils.ga_utils import crossover, mutation, circular_mutation
1010
from .fitness import *
1111
import random
1212
import heapq

fliscopt/multiproc.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .utils.util import read_file
1010

1111
from .fitness import *
12-
from .ga import GA,ReverseGA, GAReversals
12+
from .ga import GA, GARSReversals,ReverseGA, GAReversals
1313

1414
import rich
1515

@@ -26,7 +26,7 @@ def multiple_runs(algorithm, domain, fitness_function, init=[], record=False, n_
2626

2727

2828
f = open(os.path.join(os.getcwd()+'/results/multi_proc/' + fitness_function.__name__ + '/' +
29-
algorithm.__name__ + "_results.csv"), 'a+')
29+
algorithm.__name__ + "_results.csv"), 'a+',newline='\n')
3030

3131
d = domain
3232
fn = fitness_function
@@ -58,6 +58,7 @@ def multiple_runs(algorithm, domain, fitness_function, init=[], record=False, n_
5858
r[4]) + "\n")
5959
f.close()
6060
else:
61+
f.close()
6162
rich.print("[bold red]Run_Number[/bold red]\t[bold green]Solution[bold green]\t [bold magenta]Cost[/bold magenta] [bold magenta]NFE[/bold magenta] SEED", )
6263
for i, r in enumerate(res):
6364
print(i, r[0], r[1], r[3], r[4])
@@ -69,6 +70,6 @@ def multiple_runs(algorithm, domain, fitness_function, init=[], record=False, n_
6970

7071
if __name__ == '__main__':
7172
read_file('flights.txt')
72-
multiple_runs(ReverseGA, domain['griewank']*12, griewank, record=False, n=10)
73+
multiple_runs(GA, domain['domain'], fitness_function, record=True, n=10)
7374

7475

fliscopt/utils/ga_utils.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ def mutation(domain, step, solution) -> list:
1616
mutant = solution[0:gene]+[solution[gene]+step]+solution[gene+1:]
1717
return mutant
1818

19+
def circular_mutation(domain, step,shift, solution) -> list:
20+
#Cirulcar shift by shift places & then mutate normally
21+
return mutation(domain, step, solution[shift:] + solution[:shift])
22+
1923
# No good results tbh( also doesn't work for benchmarks yet) #FIX-NEEDED
2024
def multi_mutation(domain, step, solution) -> list:
2125
li = [i for i in range(domain[0][0], domain[0][1]+1)]
@@ -41,4 +45,6 @@ def multi_mutation(domain, step, solution) -> list:
4145

4246
return mutant
4347

44-
48+
if __name__ == "__main__":
49+
#print(circular_mutation([[0, 10], [0, 10]], 1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
50+
print(circular_mutation([(1, 10)]*10, 1,1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))

0 commit comments

Comments
 (0)